Skip to content

Add New AI Model Provider

cybercoder edited this page Jan 23, 2024 · 1 revision

1. Define the provider and its models

Open ./src/lib/ai-providers.ts and define a new list for the provider's models:

const GoogleModels = ['gemini'] as const;

Then add the new provider to the AIProviders Object:

export const AIProviders = {
  // ... previously defined providers
 google: {
    name: 'google' as const,
    models: GoogleModels,
  },
};

2. Add the provider's initial state for the chat settings

Open ./src/store/chat-settings.ts

const initialProviders: ChatSettingsState['providers'] = {
  //...
  google: { name: 'google', model: 'gemini', apiKey: '' },
};

3. Create a new schema validation for the newly added provider

Open ./src/lib/validators.ts

// Create the schema validation
export const GoogleValidator = createProviderValidator(AIProviders.google);

// Define the type
export type GoogleType = z.infer<typeof GoogleValidator>;

// Update `ProvidersValidator`
export const ProvidersValidator = z.object({
  //.. previously defined validators
  google: GoogleValidator.optional(),
});

4. Update the chat api helper functions to include the newly added provider

Open ./src/lib/intellinode.ts

// import the provider validator, type, and the model chat input
// you'll get some typescript errors here, will fix those later
import {
  // ...,
  GeminiInput,
  GoogleType,
  GoogleValidator,
} from './validators';

// Add the validator
const validateProvider = (name: string) => {
  switch (name) {
    // ...
    case 'google':
      return GoogleValidator;
    default:
      throw new Error('provider is not supported');
  }
};

// Update `getChatInput` add the chat model input
function getChatInput(provider: string, model: string, systemMessage: string) {
  switch (provider) {
    //...
    case 'google':
      return new GeminiInput(systemMessage, { model });
    default:
      throw new Error('provider is not supported');
  }
}

// Update `getChatProviderKey`
export function getChatProviderKey(provider: ChatProvider) {
  switch (provider) {
    //...
    case 'google':
      return process.env.GOOGLE_API_KEY;
    default:
      return null;
  }
}

5. Update the default route

This route checks if the user has any defined api keys as environment variables.

export async function GET() {
  // ...
  const GoogleKey = getChatProviderKey('google');

  return NextResponse.json({
    //...
    google: !!GoogleKey,
  });
}

6. Update intellinode's type declaration

Since Intellinode doesn't support typescript yet and our project does, we need to update our custom type definitions and define add the new Model Input Class. open ./intellinode.d.ts and add the following:

declare module 'intellinode' {
  type SupportedChatModels = 'openai' | 'replicate' | 'sagemaker' | 'azure' | 'cohere' | 'gemini'; // add this
  // ...
  class GeminiInput {
    constructor(message: string, options?: { model?: string });

    addUserMessage(message: string): void;
    addAssistantMessage(message: string): void;
  }
  // ...
}

Update ChatProvider Types in ./lib/types.ts

export type ChatProvider =
  // ...
  'google';

Now all of the previous typescript errors should be gone. Your new provider and model will appear on the list of providers in chat settings alongside the input field for the apiKey. You can add the apiKey to .env file and leave this field empty, if you prefer not to expose it on the client.

Clone this wiki locally