-
Notifications
You must be signed in to change notification settings - Fork 15
Add New AI Model Provider
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,
},
};
Open ./src/store/chat-settings.ts
const initialProviders: ChatSettingsState['providers'] = {
//...
google: { name: 'google', model: 'gemini', apiKey: '' },
};
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(),
});
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;
}
}
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,
});
}
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.