Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Gemini Ai Support #446

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"@changesets/cli": "^2.27.8",
"@srcbook/configs": "workspace:^",
"@types/node": "^20.14.2",
"eslint": "^8.57.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal but why change this order?

Copy link
Contributor Author

@aakash-a-dev aakash-a-dev Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, we can ignore this during the integration I was getting a type error which I thought could be resolved by having ai-sdk for Google installed in the root package when I later realized it has nothing to with it, I removed it from package.json for avoiding not so necessary installation in the package. And honestly I didn't realized that it changed the order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this particular commit as well btw! I don't know how it got committed because I remembered I removed the dependency, if it's possible to not align with the following package.json then it works because we have nothing to do with root's package.json file

"prettier": "^3.3.3",
"typescript": "5.6.2",
"eslint": "^8.57.0"
"typescript": "5.6.2"
},
"dependencies": {
"turbo": "^2.1.1"
Expand Down
8 changes: 8 additions & 0 deletions packages/api/ai/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createAnthropic } from '@ai-sdk/anthropic';
import { getConfig } from '../config.mjs';
import type { LanguageModel } from 'ai';
import { getDefaultModel, type AiProviderType } from '@srcbook/shared';
import { createGoogleGenerativeAI } from '@ai-sdk/google';

/**
* Get the correct client and model configuration.
Expand Down Expand Up @@ -30,6 +31,13 @@ export async function getModel(): Promise<LanguageModel> {
const anthropic = createAnthropic({ apiKey: config.anthropicKey });
return anthropic(model);

case 'Gemini':
if (!config.geminiKey) {
throw new Error('Gemini API key is not set');
}
const google = createGoogleGenerativeAI({ apiKey: config.geminiKey });
return google(model) as LanguageModel;

case 'Xai':
if (!config.xaiKey) {
throw new Error('Xai API key is not set');
Expand Down
1 change: 1 addition & 0 deletions packages/api/db/schema.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const configs = sqliteTable('config', {
openaiKey: text('openai_api_key'),
anthropicKey: text('anthropic_api_key'),
xaiKey: text('xai_api_key'),
geminiKey: text('gemini_api_key'),
// TODO: This is deprecated in favor of SRCBOOK_DISABLE_ANALYTICS env variable. Remove this.
enabledAnalytics: integer('enabled_analytics', { mode: 'boolean' }).notNull().default(true),
// Stable ID for posthog
Expand Down
1 change: 1 addition & 0 deletions packages/api/drizzle/0014_Gemini_Integration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `config` ADD `gemini_api_key` text;
276 changes: 276 additions & 0 deletions packages/api/drizzle/meta/0014_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
{
"version": "6",
"dialect": "sqlite",
"id": "c148b92f-4dbc-4a31-887d-dfaebd4db615",
"prevId": "0acbefdc-659a-48ad-a4c1-a44ebca56c08",
"tables": {
"apps": {
"name": "apps",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"external_id": {
"name": "external_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"history": {
"name": "history",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "'[]'"
},
"history_version": {
"name": "history_version",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 1
},
"created_at": {
"name": "created_at",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "(unixepoch())"
},
"updated_at": {
"name": "updated_at",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "(unixepoch())"
}
},
"indexes": {
"apps_external_id_unique": {
"name": "apps_external_id_unique",
"columns": [
"external_id"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"config": {
"name": "config",
"columns": {
"base_dir": {
"name": "base_dir",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"default_language": {
"name": "default_language",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "'typescript'"
},
"openai_api_key": {
"name": "openai_api_key",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"anthropic_api_key": {
"name": "anthropic_api_key",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"xai_api_key": {
"name": "xai_api_key",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"gemini_api_key": {
"name": "gemini_api_key",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"enabled_analytics": {
"name": "enabled_analytics",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": true
},
"srcbook_installation_id": {
"name": "srcbook_installation_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "'gid1al6p9ibob8tb6qmmuokg94'"
},
"ai_provider": {
"name": "ai_provider",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "'openai'"
},
"ai_model": {
"name": "ai_model",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "'gpt-4o'"
},
"ai_base_url": {
"name": "ai_base_url",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"subscription_email": {
"name": "subscription_email",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"secrets": {
"name": "secrets",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"value": {
"name": "value",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {
"secrets_name_unique": {
"name": "secrets_name_unique",
"columns": [
"name"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"secrets_to_sessions": {
"name": "secrets_to_sessions",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"session_id": {
"name": "session_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"secret_id": {
"name": "secret_id",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {
"secrets_to_sessions_session_id_secret_id_unique": {
"name": "secrets_to_sessions_session_id_secret_id_unique",
"columns": [
"session_id",
"secret_id"
],
"isUnique": true
}
},
"foreignKeys": {
"secrets_to_sessions_secret_id_secrets_id_fk": {
"name": "secrets_to_sessions_secret_id_secrets_id_fk",
"tableFrom": "secrets_to_sessions",
"tableTo": "secrets",
"columnsFrom": [
"secret_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"indexes": {}
}
}
7 changes: 7 additions & 0 deletions packages/api/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@
"when": 1731347691803,
"tag": "0013_add_x_ai",
"breakpoints": true
},
{
"idx": 14,
"version": "6",
"when": 1732197490638,
"tag": "0014_Gemini_Integration",
"breakpoints": true
}
]
}
4 changes: 3 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
},
"dependencies": {
"@ai-sdk/anthropic": "catalog:",
"@ai-sdk/google": "^1.0.3",
"@ai-sdk/openai": "catalog:",
"@ai-sdk/provider": "^1.0.1",
"@srcbook/shared": "workspace:^",
"ai": "^3.3.33",
"ai": "^3.4.33",
"archiver": "^7.0.1",
"better-sqlite3": "^11.3.0",
"cors": "^2.8.5",
Expand Down
1 change: 1 addition & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"@ai-sdk/google": "1.0.1",
"@scure/base": "^1.1.8",
"zod": "catalog:"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/ai.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const AiProvider = {
OpenAI: 'openai',
Anthropic: 'anthropic',
XAI: 'Xai',
Gemini: 'Gemini',
Custom: 'custom',
} as const;

Expand All @@ -12,6 +13,7 @@ export const defaultModels: Record<AiProviderType, string> = {
[AiProvider.Anthropic]: 'claude-3-5-sonnet-latest',
[AiProvider.Custom]: 'mistral-nemo',
[AiProvider.XAI]: 'grok-beta',
[AiProvider.Gemini]: 'gemini-1.5-pro-latest',
} as const;

export function isValidProvider(provider: string): provider is AiProviderType {
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/components/use-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function SettingsProvider({ config, children }: ProviderPropsType) {
(config.openaiKey && config.aiProvider === 'openai') ||
(config.anthropicKey && config.aiProvider === 'anthropic') ||
(config.xaiKey && config.aiProvider === 'Xai') ||
(config.geminiKey && config.aiProvider === 'Gemini') ||
(config.aiProvider === 'custom' && !!config.aiBaseUrl) ||
false;

Expand Down
Loading