Skip to content

Commit

Permalink
Add support for X AI grok (#438)
Browse files Browse the repository at this point in the history
* Add support for X AI grok

* Remove debug logs
  • Loading branch information
nichochar authored Nov 11, 2024
1 parent d03eadc commit 9897d9a
Show file tree
Hide file tree
Showing 10 changed files with 424 additions and 29 deletions.
11 changes: 11 additions & 0 deletions packages/api/ai/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export async function getModel(): Promise<LanguageModel> {
const anthropic = createAnthropic({ apiKey: config.anthropicKey });
return anthropic(model);

case 'Xai':
if (!config.xaiKey) {
throw new Error('Xai API key is not set');
}
const xai = createOpenAI({
compatibility: 'compatible',
baseURL: 'https://api.x.ai/v1',
apiKey: config.xaiKey,
});
return xai(model);

case 'custom':
if (typeof aiBaseUrl !== 'string') {
throw new Error('Local AI base URL 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 @@ -8,6 +8,7 @@ export const configs = sqliteTable('config', {
defaultLanguage: text('default_language').notNull().default('typescript'),
openaiKey: text('openai_api_key'),
anthropicKey: text('anthropic_api_key'),
xaiKey: text('xai_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/0013_add_x_ai.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `config` ADD `xai_api_key` text;
269 changes: 269 additions & 0 deletions packages/api/drizzle/meta/0013_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
{
"version": "6",
"dialect": "sqlite",
"id": "0acbefdc-659a-48ad-a4c1-a44ebca56c08",
"prevId": "0e479af1-dade-4a47-88c8-438284446e01",
"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
},
"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": "'4tgs28j4rn6q9ggpipqllmqvbo'"
},
"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 @@ -92,6 +92,13 @@
"when": 1729193497907,
"tag": "0012_add_app_history",
"breakpoints": true
},
{
"idx": 13,
"version": "6",
"when": 1731347691803,
"tag": "0013_add_x_ai",
"breakpoints": true
}
]
}
4 changes: 3 additions & 1 deletion packages/shared/src/ai.mts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
export const AiProvider = {
OpenAI: 'openai',
Anthropic: 'anthropic',
XAI: 'Xai',
Custom: 'custom',
} as const;

export type AiProviderType = (typeof AiProvider)[keyof typeof AiProvider];

export const defaultModels: Record<AiProviderType, string> = {
[AiProvider.OpenAI]: 'chatgpt-4o-latest',
[AiProvider.Anthropic]: 'claude-3-5-sonnet-20240620',
[AiProvider.Anthropic]: 'claude-3-5-sonnet-latest',
[AiProvider.Custom]: 'mistral-nemo',
[AiProvider.XAI]: 'grok-beta',
} 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 @@ -34,6 +34,7 @@ export function SettingsProvider({ config, children }: ProviderPropsType) {
const aiEnabled =
(config.openaiKey && config.aiProvider === 'openai') ||
(config.anthropicKey && config.aiProvider === 'anthropic') ||
(config.xaiKey && config.aiProvider === 'Xai') ||
(config.aiProvider === 'custom' && !!config.aiBaseUrl) ||
false;

Expand Down
Loading

0 comments on commit 9897d9a

Please sign in to comment.