From d41a0ac2c2b8f4936d168ef003cbf8e1e71fb1f2 Mon Sep 17 00:00:00 2001 From: Raiyan Hasan Date: Wed, 20 Nov 2024 15:08:42 +0000 Subject: [PATCH] Cohere support added --- .env.example | 4 +++ README.md | 2 +- app/lib/.server/llm/api-key.ts | 2 ++ app/lib/.server/llm/constants.ts | 2 +- app/lib/.server/llm/model.ts | 14 ++++++++++ app/utils/constants.ts | 16 +++++++++++ package.json | 1 + pnpm-lock.yaml | 47 ++++++++++++++++++++++++++++++++ 8 files changed, 86 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 00a0fd94..473f05ac 100644 --- a/.env.example +++ b/.env.example @@ -49,6 +49,10 @@ OPENAI_LIKE_API_KEY= # You only need this environment variable set if you want to use Mistral models MISTRAL_API_KEY= +# Get the Cohere Api key by following these instructions - +# https://dashboard.cohere.com/api-keys +# You only need this environment variable set if you want to use Cohere models +COHERE_API_KEY= # Get LMStudio Base URL from LM Studio Developer Console # Make sure to enable CORS diff --git a/README.md b/README.md index 6bcacd4e..7a3957ac 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ https://thinktank.ottomator.ai - ⬜ Azure Open AI API Integration - ⬜ Perplexity Integration - ⬜ Vertex AI Integration -- ⬜ Cohere Integration +- ✅ Cohere Integration - ⬜ Deploy directly to Vercel/Netlify/other similar platforms - ⬜ Prompt caching - ⬜ Better prompt enhancing diff --git a/app/lib/.server/llm/api-key.ts b/app/lib/.server/llm/api-key.ts index f282aa80..7d8d2f93 100644 --- a/app/lib/.server/llm/api-key.ts +++ b/app/lib/.server/llm/api-key.ts @@ -35,6 +35,8 @@ export function getAPIKey(cloudflareEnv: Env, provider: string, userApiKeys?: Re return env.OPENAI_LIKE_API_KEY || cloudflareEnv.OPENAI_LIKE_API_KEY; case "xAI": return env.XAI_API_KEY || cloudflareEnv.XAI_API_KEY; + case "Cohere": + return env.COHERE_API_KEY; default: return ""; } diff --git a/app/lib/.server/llm/constants.ts b/app/lib/.server/llm/constants.ts index 7b3a0f24..1f993c98 100644 --- a/app/lib/.server/llm/constants.ts +++ b/app/lib/.server/llm/constants.ts @@ -1,5 +1,5 @@ // see https://docs.anthropic.com/en/docs/about-claude/models -export const MAX_TOKENS = 8000; +export const MAX_TOKENS = 4096; // limits the number of model responses that can be returned in a single request export const MAX_RESPONSE_SEGMENTS = 2; diff --git a/app/lib/.server/llm/model.ts b/app/lib/.server/llm/model.ts index e07f2bbc..2e7c5684 100644 --- a/app/lib/.server/llm/model.ts +++ b/app/lib/.server/llm/model.ts @@ -7,6 +7,7 @@ import { createGoogleGenerativeAI } from '@ai-sdk/google'; import { ollama } from 'ollama-ai-provider'; import { createOpenRouter } from "@openrouter/ai-sdk-provider"; import { createMistral } from '@ai-sdk/mistral'; +import { createCohere } from '@ai-sdk/cohere' export function getAnthropicModel(apiKey: string, model: string) { const anthropic = createAnthropic({ @@ -23,6 +24,15 @@ export function getOpenAILikeModel(baseURL:string,apiKey: string, model: string) return openai(model); } + +export function getCohereAIModel(apiKey:string, model: string){ + const cohere = createCohere({ + apiKey, + }); + + return cohere(model); +} + export function getOpenAIModel(apiKey: string, model: string) { const openai = createOpenAI({ apiKey, @@ -108,6 +118,8 @@ export function getXAIModel(apiKey: string, model: string) { return openai(model); } + + export function getModel(provider: string, model: string, env: Env, apiKeys?: Record) { const apiKey = getAPIKey(env, provider, apiKeys); const baseURL = getBaseURL(env, provider); @@ -135,6 +147,8 @@ export function getModel(provider: string, model: string, env: Env, apiKeys?: Re return getLMStudioModel(baseURL, model); case 'xAI': return getXAIModel(apiKey, model); + case 'Cohere': + return getCohereAIModel(apiKey, model); default: return getOllamaModel(baseURL, model); } diff --git a/app/utils/constants.ts b/app/utils/constants.ts index 672c7a81..fe769322 100644 --- a/app/utils/constants.ts +++ b/app/utils/constants.ts @@ -33,6 +33,22 @@ const PROVIDER_LIST: ProviderInfo[] = [ staticModels: [], getDynamicModels: getOpenAILikeModels }, + { + name: 'Cohere', + staticModels: [ + { name: 'command-r-plus-08-2024', label: 'Command R plus Latest', provider: 'Cohere' }, + { name: 'command-r-08-2024', label: 'Command R Latest', provider: 'Cohere' }, + { name: 'command-r-plus', label: 'Command R plus', provider: 'Cohere' }, + { name: 'command-r', label: 'Command R', provider: 'Cohere' }, + { name: 'command', label: 'Command', provider: 'Cohere' }, + { name: 'command-nightly', label: 'Command Nightly', provider: 'Cohere' }, + { name: 'command-light', label: 'Command Light', provider: 'Cohere' }, + { name: 'command-light-nightly', label: 'Command Light Nightly', provider: 'Cohere' }, + { name: 'c4ai-aya-expanse-8b', label: 'c4AI Aya Expanse 8b', provider: 'Cohere' }, + { name: 'c4ai-aya-expanse-32b', label: 'c4AI Aya Expanse 32b', provider: 'Cohere' }, + ], + getApiKeyLink: 'https://dashboard.cohere.com/api-keys' + }, { name: 'OpenRouter', staticModels: [ diff --git a/package.json b/package.json index 40ede0f7..cc1c256d 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ }, "dependencies": { "@ai-sdk/anthropic": "^0.0.39", + "@ai-sdk/cohere": "^1.0.1", "@ai-sdk/google": "^0.0.52", "@ai-sdk/mistral": "^0.0.43", "@ai-sdk/openai": "^0.0.66", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4158d19c..951d1a4f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@ai-sdk/anthropic': specifier: ^0.0.39 version: 0.0.39(zod@3.23.8) + '@ai-sdk/cohere': + specifier: ^1.0.1 + version: 1.0.1(zod@3.23.8) '@ai-sdk/google': specifier: ^0.0.52 version: 0.0.52(zod@3.23.8) @@ -279,6 +282,12 @@ packages: peerDependencies: zod: ^3.0.0 + '@ai-sdk/cohere@1.0.1': + resolution: {integrity: sha512-xLaSYl/hs9EqfpvT9PvqZrDWjJPQPZBd0iT32T6812vN6kwuEQ6sSgQvqHWczIqxeej2GNRgMQwDL6Lh0L5pZw==} + engines: {node: '>=18'} + peerDependencies: + zod: ^3.0.0 + '@ai-sdk/google@0.0.52': resolution: {integrity: sha512-bfsA/1Ae0SQ6NfLwWKs5SU4MBwlzJjVhK6bTVBicYFjUxg9liK/W76P1Tq/qK9OlrODACz3i1STOIWsFPpIOuQ==} engines: {node: '>=18'} @@ -324,6 +333,15 @@ packages: zod: optional: true + '@ai-sdk/provider-utils@2.0.1': + resolution: {integrity: sha512-TNg7rPhRtETB2Z9F0JpOvpGii9Fs8EWM8nYy1jEkvSXkrPJ6b/9zVnDdaJsmLFDyrMbOsPJlkblYtmYEQou36w==} + engines: {node: '>=18'} + peerDependencies: + zod: ^3.0.0 + peerDependenciesMeta: + zod: + optional: true + '@ai-sdk/provider@0.0.12': resolution: {integrity: sha512-oOwPQD8i2Ynpn22cur4sk26FW3mSy6t6/X/K1Ay2yGBKYiSpRyLfObhOrZEGsXDx+3euKy4nEZ193R36NM+tpQ==} engines: {node: '>=18'} @@ -336,6 +354,10 @@ packages: resolution: {integrity: sha512-XMsNGJdGO+L0cxhhegtqZ8+T6nn4EoShS819OvCgI2kLbYTIvk0GWFGD0AXJmxkxs3DrpsJxKAFukFR7bvTkgQ==} engines: {node: '>=18'} + '@ai-sdk/provider@1.0.0': + resolution: {integrity: sha512-Sj29AzooJ7SYvhPd+AAWt/E7j63E9+AzRnoMHUaJPRYzOd/WDrVNxxv85prF9gDcQ7XPVlSk9j6oAZV9/DXYpA==} + engines: {node: '>=18'} + '@ai-sdk/react@0.0.62': resolution: {integrity: sha512-1asDpxgmeHWL0/EZPCLENxfOHT+0jce0z/zasRhascodm2S6f6/KZn5doLG9jdmarcb+GjMjFmmwyOVXz3W1xg==} engines: {node: '>=18'} @@ -3033,6 +3055,10 @@ packages: resolution: {integrity: sha512-v0eOBUbiaFojBu2s2NPBfYUoRR9GjcDNvCXVaqEf5vVfpIAh9f8RCo4vXTP8c63QRKCFwoLpMpTdPwwhEKVgzA==} engines: {node: '>=14.18'} + eventsource-parser@3.0.0: + resolution: {integrity: sha512-T1C0XCUimhxVQzW4zFipdx0SficT651NnkR0ZSH3yQwh+mFMdLfgjABVi4YtMTtaL4s168593DaoaRLMqryavA==} + engines: {node: '>=18.0.0'} + evp_bytestokey@1.0.3: resolution: {integrity: sha512-/f2Go4TognH/KvCISP7OUsHn85hT9nUkxxA9BEWxFn+Oj9o8ZNLm/40hdlgSLyuOimsrTKLUMEorQexp/aPQeA==} @@ -5687,6 +5713,12 @@ snapshots: '@ai-sdk/provider-utils': 1.0.9(zod@3.23.8) zod: 3.23.8 + '@ai-sdk/cohere@1.0.1(zod@3.23.8)': + dependencies: + '@ai-sdk/provider': 1.0.0 + '@ai-sdk/provider-utils': 2.0.1(zod@3.23.8) + zod: 3.23.8 + '@ai-sdk/google@0.0.52(zod@3.23.8)': dependencies: '@ai-sdk/provider': 0.0.24 @@ -5733,6 +5765,15 @@ snapshots: optionalDependencies: zod: 3.23.8 + '@ai-sdk/provider-utils@2.0.1(zod@3.23.8)': + dependencies: + '@ai-sdk/provider': 1.0.0 + eventsource-parser: 3.0.0 + nanoid: 3.3.7 + secure-json-parse: 2.7.0 + optionalDependencies: + zod: 3.23.8 + '@ai-sdk/provider@0.0.12': dependencies: json-schema: 0.4.0 @@ -5745,6 +5786,10 @@ snapshots: dependencies: json-schema: 0.4.0 + '@ai-sdk/provider@1.0.0': + dependencies: + json-schema: 0.4.0 + '@ai-sdk/react@0.0.62(react@18.3.1)(zod@3.23.8)': dependencies: '@ai-sdk/provider-utils': 1.0.20(zod@3.23.8) @@ -8751,6 +8796,8 @@ snapshots: eventsource-parser@1.1.2: {} + eventsource-parser@3.0.0: {} + evp_bytestokey@1.0.3: dependencies: md5.js: 1.3.5