diff --git a/core/index.d.ts b/core/index.d.ts index d5963ea75..d4edf3546 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -495,6 +495,7 @@ type ModelProvider = | "openai" | "free-trial" | "anthropic" + | "cohere" | "together" | "ollama" | "huggingface-tgi" @@ -548,6 +549,9 @@ export type ModelName = | "claude-3-sonnet-20240229" | "claude-3-haiku-20240307" | "claude-2.1" + // Cohere + |"command-r" + |"command-r-plus" // Gemini | "gemini-pro" | "gemini-1.5-pro-latest" diff --git a/core/llm/autodetect.ts b/core/llm/autodetect.ts index 1f42213a0..c32fcded0 100644 --- a/core/llm/autodetect.ts +++ b/core/llm/autodetect.ts @@ -110,6 +110,7 @@ function autodetectTemplateType(model: string): TemplateType | undefined { if ( lower.includes("gpt") || + lower.includes("command") || lower.includes("chat-bison") || lower.includes("pplx") || lower.includes("gemini") diff --git a/core/llm/llms/Cohere.ts b/core/llm/llms/Cohere.ts new file mode 100644 index 000000000..d5781f645 --- /dev/null +++ b/core/llm/llms/Cohere.ts @@ -0,0 +1,92 @@ +import { BaseLLM } from ".."; +import { + ChatMessage, + CompletionOptions, + LLMOptions, + ModelProvider, +} from "../.."; +import { stripImages } from "../countTokens"; +import { streamJSON } from "../stream"; + +class Cohere extends BaseLLM { + static providerName: ModelProvider = "cohere"; + static defaultOptions: Partial = { + apiBase: "https://api.cohere.ai/v1", + }; + static maxStopSequences = 5; + + private _convertMessages(msgs: ChatMessage[]): any[] { + const messages = []; + for (const m of msgs) { + if (m.role === "system" || !m.content) { + continue; + } + messages.push({ + role: m.role === "assistant" ? "chatbot" : m.role, + message: m.content, + }); + } + return messages; + } + + private _convertArgs(options: CompletionOptions) { + return { + model: options.model, + stream: options.stream ?? true, + temperature: options.temperature, + max_tokens: options.maxTokens, + k: options.topK, + p: options.topP, + stop_sequences: options.stop?.slice(0, Cohere.maxStopSequences), + frequency_penalty: options.frequencyPenalty, + presence_penalty: options.presencePenalty, + raw_prompting: options.raw, + }; + } + + protected async *_streamComplete( + prompt: string, + options: CompletionOptions, + ): AsyncGenerator { + const messages = [{ role: "user" as const, content: prompt }]; + for await (const update of this._streamChat(messages, options)) { + yield stripImages(update.content); + } + } + + protected async *_streamChat( + messages: ChatMessage[], + options: CompletionOptions, + ): AsyncGenerator { + const headers = { + "Content-Type": "application/json", + Authorization: `Bearer ${this.apiKey}`, + ...this.requestOptions?.headers, + }; + + const resp = await this.fetch(new URL("chat", this.apiBase), { + method: "POST", + headers, + body: JSON.stringify({ + ...this._convertArgs(options), + message: messages.pop()?.content, + chat_history: this._convertMessages(messages), + preamble: this.systemMessage, + }), + }); + + if (options.stream === false) { + const data = await resp.json(); + yield { role: "assistant", content: data.text }; + return; + } + + for await (const value of streamJSON(resp)) { + if (value.event_type === "text-generation") { + yield { role: "assistant", content: value.text }; + } + } + } +} + +export default Cohere; diff --git a/core/llm/llms/index.ts b/core/llm/llms/index.ts index 9b6c027d4..d1d28253b 100644 --- a/core/llm/llms/index.ts +++ b/core/llm/llms/index.ts @@ -8,6 +8,7 @@ import { } from "../.."; import { DEFAULT_MAX_TOKENS } from "../constants"; import Anthropic from "./Anthropic"; +import Cohere from "./Cohere"; import Bedrock from "./Bedrock"; import DeepInfra from "./DeepInfra"; import Flowise from "./Flowise"; @@ -76,6 +77,7 @@ export async function renderTemplatedString( const LLMs = [ Anthropic, + Cohere, FreeTrial, Gemini, Llamafile, diff --git a/core/llm/stream.ts b/core/llm/stream.ts index 302ee82ed..02a35c365 100644 --- a/core/llm/stream.ts +++ b/core/llm/stream.ts @@ -73,3 +73,18 @@ export async function* streamSse(response: Response): AsyncGenerator { } } } + +export async function* streamJSON(response: Response): AsyncGenerator { + let buffer = ""; + for await (const value of streamResponse(response)) { + buffer += value; + + let position; + while ((position = buffer.indexOf("\n")) >= 0) { + const line = buffer.slice(0, position); + const data = JSON.parse(line); + yield data; + buffer = buffer.slice(position + 1); + } + } +} diff --git a/docs/docs/reference/Model Providers/cohere.md b/docs/docs/reference/Model Providers/cohere.md new file mode 100644 index 000000000..06906470c --- /dev/null +++ b/docs/docs/reference/Model Providers/cohere.md @@ -0,0 +1,20 @@ +# Cohere + +To setup Cohere, add the following to your `config.json` file: + +```json title="~/.continue/config.json" +{ + "models": [ + { + "title": "Cohere", + "provider": "cohere", + "model": "command-r-plus", + "apiKey": "YOUR_API_KEY" + } + ] +} +``` + +Visit the [Cohere dashboard](https://dashboard.cohere.com/api-keys) to create an API key. + +[View the source](https://github.com/continuedev/continue/blob/main/core/llm/llms/Cohere.ts) diff --git a/docs/static/schemas/config.json b/docs/static/schemas/config.json index 591403628..df0c2b496 100644 --- a/docs/static/schemas/config.json +++ b/docs/static/schemas/config.json @@ -134,6 +134,7 @@ "openai", "free-trial", "anthropic", + "cohere", "bedrock", "together", "ollama", @@ -153,6 +154,7 @@ "### OpenAI\nUse gpt-4, gpt-3.5-turbo, or any other OpenAI model. See [here](https://openai.com/product#made-for-developers) to obtain an API key.\n\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/openai)", "### Free Trial\nNew users can try out Continue for free using a proxy server that securely makes calls to OpenAI using our API key. If you are ready to use your own API key or have used all 250 free uses, you can enter your API key in config.py where it says `apiKey=\"\"` or select another model provider.\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/freetrial)", "### Anthropic\nTo get started with Anthropic models, you first need to sign up for the open beta [here](https://claude.ai/login) to obtain an API key.\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/anthropicllm)", + "### Cohere\nTo use Cohere, visit the [Cohere dashboard](https://dashboard.cohere.com/api-keys) to create an API key.\n\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/cohere)", "### Bedrock\nTo get started with Bedrock you need to sign up on AWS [here](https://aws.amazon.com/bedrock/claude/)", "### Together\nTogether is a hosted service that provides extremely fast streaming of open-source language models. To get started with Together:\n1. Obtain an API key from [here](https://together.ai)\n2. Paste below\n3. Select a model preset\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/togetherllm)", "### Ollama\nTo get started with Ollama, follow these steps:\n1. Download from [ollama.ai](https://ollama.ai/) and open the application\n2. Open a terminal and run `ollama run `. Example model names are `codellama:7b-instruct` or `llama2:7b-text`. You can find the full list [here](https://ollama.ai/library).\n3. Make sure that the model name used in step 2 is the same as the one in config.py (e.g. `model=\"codellama:7b-instruct\"`)\n4. Once the model has finished downloading, you can start asking questions through Continue.\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/ollama)", @@ -175,7 +177,7 @@ }, "apiKey": { "title": "Api Key", - "description": "OpenAI, Anthropic, Together, or other API key", + "description": "OpenAI, Anthropic, Cohere, Together, or other API key", "type": "string" }, "apiBase": { @@ -303,6 +305,7 @@ "enum": [ "openai", "anthropic", + "cohere", "gemini", "huggingface-inference-api", "replicate", @@ -540,6 +543,23 @@ } } }, + { + "if": { + "properties": { + "provider": { + "enum": ["cohere"] + } + }, + "required": ["provider"] + }, + "then": { + "properties": { + "model": { + "enum": ["command-r", "command-r-plus"] + } + } + } + }, { "if": { "properties": { @@ -930,6 +950,8 @@ "claude-3-sonnet-20240229", "claude-3-haiku-20240307", "claude-2.1", + "command-r", + "command-r-plus", "chat-bison-001", "gemini-pro", "gemini-1.5-pro-latest", diff --git a/extensions/intellij/src/main/resources/config_schema.json b/extensions/intellij/src/main/resources/config_schema.json index 591403628..df0c2b496 100644 --- a/extensions/intellij/src/main/resources/config_schema.json +++ b/extensions/intellij/src/main/resources/config_schema.json @@ -134,6 +134,7 @@ "openai", "free-trial", "anthropic", + "cohere", "bedrock", "together", "ollama", @@ -153,6 +154,7 @@ "### OpenAI\nUse gpt-4, gpt-3.5-turbo, or any other OpenAI model. See [here](https://openai.com/product#made-for-developers) to obtain an API key.\n\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/openai)", "### Free Trial\nNew users can try out Continue for free using a proxy server that securely makes calls to OpenAI using our API key. If you are ready to use your own API key or have used all 250 free uses, you can enter your API key in config.py where it says `apiKey=\"\"` or select another model provider.\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/freetrial)", "### Anthropic\nTo get started with Anthropic models, you first need to sign up for the open beta [here](https://claude.ai/login) to obtain an API key.\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/anthropicllm)", + "### Cohere\nTo use Cohere, visit the [Cohere dashboard](https://dashboard.cohere.com/api-keys) to create an API key.\n\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/cohere)", "### Bedrock\nTo get started with Bedrock you need to sign up on AWS [here](https://aws.amazon.com/bedrock/claude/)", "### Together\nTogether is a hosted service that provides extremely fast streaming of open-source language models. To get started with Together:\n1. Obtain an API key from [here](https://together.ai)\n2. Paste below\n3. Select a model preset\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/togetherllm)", "### Ollama\nTo get started with Ollama, follow these steps:\n1. Download from [ollama.ai](https://ollama.ai/) and open the application\n2. Open a terminal and run `ollama run `. Example model names are `codellama:7b-instruct` or `llama2:7b-text`. You can find the full list [here](https://ollama.ai/library).\n3. Make sure that the model name used in step 2 is the same as the one in config.py (e.g. `model=\"codellama:7b-instruct\"`)\n4. Once the model has finished downloading, you can start asking questions through Continue.\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/ollama)", @@ -175,7 +177,7 @@ }, "apiKey": { "title": "Api Key", - "description": "OpenAI, Anthropic, Together, or other API key", + "description": "OpenAI, Anthropic, Cohere, Together, or other API key", "type": "string" }, "apiBase": { @@ -303,6 +305,7 @@ "enum": [ "openai", "anthropic", + "cohere", "gemini", "huggingface-inference-api", "replicate", @@ -540,6 +543,23 @@ } } }, + { + "if": { + "properties": { + "provider": { + "enum": ["cohere"] + } + }, + "required": ["provider"] + }, + "then": { + "properties": { + "model": { + "enum": ["command-r", "command-r-plus"] + } + } + } + }, { "if": { "properties": { @@ -930,6 +950,8 @@ "claude-3-sonnet-20240229", "claude-3-haiku-20240307", "claude-2.1", + "command-r", + "command-r-plus", "chat-bison-001", "gemini-pro", "gemini-1.5-pro-latest", diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index 591403628..df0c2b496 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -134,6 +134,7 @@ "openai", "free-trial", "anthropic", + "cohere", "bedrock", "together", "ollama", @@ -153,6 +154,7 @@ "### OpenAI\nUse gpt-4, gpt-3.5-turbo, or any other OpenAI model. See [here](https://openai.com/product#made-for-developers) to obtain an API key.\n\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/openai)", "### Free Trial\nNew users can try out Continue for free using a proxy server that securely makes calls to OpenAI using our API key. If you are ready to use your own API key or have used all 250 free uses, you can enter your API key in config.py where it says `apiKey=\"\"` or select another model provider.\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/freetrial)", "### Anthropic\nTo get started with Anthropic models, you first need to sign up for the open beta [here](https://claude.ai/login) to obtain an API key.\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/anthropicllm)", + "### Cohere\nTo use Cohere, visit the [Cohere dashboard](https://dashboard.cohere.com/api-keys) to create an API key.\n\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/cohere)", "### Bedrock\nTo get started with Bedrock you need to sign up on AWS [here](https://aws.amazon.com/bedrock/claude/)", "### Together\nTogether is a hosted service that provides extremely fast streaming of open-source language models. To get started with Together:\n1. Obtain an API key from [here](https://together.ai)\n2. Paste below\n3. Select a model preset\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/togetherllm)", "### Ollama\nTo get started with Ollama, follow these steps:\n1. Download from [ollama.ai](https://ollama.ai/) and open the application\n2. Open a terminal and run `ollama run `. Example model names are `codellama:7b-instruct` or `llama2:7b-text`. You can find the full list [here](https://ollama.ai/library).\n3. Make sure that the model name used in step 2 is the same as the one in config.py (e.g. `model=\"codellama:7b-instruct\"`)\n4. Once the model has finished downloading, you can start asking questions through Continue.\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/ollama)", @@ -175,7 +177,7 @@ }, "apiKey": { "title": "Api Key", - "description": "OpenAI, Anthropic, Together, or other API key", + "description": "OpenAI, Anthropic, Cohere, Together, or other API key", "type": "string" }, "apiBase": { @@ -303,6 +305,7 @@ "enum": [ "openai", "anthropic", + "cohere", "gemini", "huggingface-inference-api", "replicate", @@ -540,6 +543,23 @@ } } }, + { + "if": { + "properties": { + "provider": { + "enum": ["cohere"] + } + }, + "required": ["provider"] + }, + "then": { + "properties": { + "model": { + "enum": ["command-r", "command-r-plus"] + } + } + } + }, { "if": { "properties": { @@ -930,6 +950,8 @@ "claude-3-sonnet-20240229", "claude-3-haiku-20240307", "claude-2.1", + "command-r", + "command-r-plus", "chat-bison-001", "gemini-pro", "gemini-1.5-pro-latest", diff --git a/extensions/vscode/continue_rc_schema.json b/extensions/vscode/continue_rc_schema.json index d963b8552..011dce046 100644 --- a/extensions/vscode/continue_rc_schema.json +++ b/extensions/vscode/continue_rc_schema.json @@ -134,6 +134,7 @@ "openai", "free-trial", "anthropic", + "cohere", "bedrock", "together", "ollama", @@ -153,6 +154,7 @@ "### OpenAI\nUse gpt-4, gpt-3.5-turbo, or any other OpenAI model. See [here](https://openai.com/product#made-for-developers) to obtain an API key.\n\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/openai)", "### Free Trial\nNew users can try out Continue for free using a proxy server that securely makes calls to OpenAI using our API key. If you are ready to use your own API key or have used all 250 free uses, you can enter your API key in config.py where it says `apiKey=\"\"` or select another model provider.\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/freetrial)", "### Anthropic\nTo get started with Anthropic models, you first need to sign up for the open beta [here](https://claude.ai/login) to obtain an API key.\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/anthropicllm)", + "### Cohere\nTo use Cohere, visit the [Cohere dashboard](https://dashboard.cohere.com/api-keys) to create an API key.\n\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/cohere)", "### Bedrock\nTo get started with Bedrock you need to sign up on AWS [here](https://aws.amazon.com/bedrock/claude/)", "### Together\nTogether is a hosted service that provides extremely fast streaming of open-source language models. To get started with Together:\n1. Obtain an API key from [here](https://together.ai)\n2. Paste below\n3. Select a model preset\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/togetherllm)", "### Ollama\nTo get started with Ollama, follow these steps:\n1. Download from [ollama.ai](https://ollama.ai/) and open the application\n2. Open a terminal and run `ollama run `. Example model names are `codellama:7b-instruct` or `llama2:7b-text`. You can find the full list [here](https://ollama.ai/library).\n3. Make sure that the model name used in step 2 is the same as the one in config.py (e.g. `model=\"codellama:7b-instruct\"`)\n4. Once the model has finished downloading, you can start asking questions through Continue.\n> [Reference](https://continue.dev/docs/reference/Model%20Providers/ollama)", @@ -175,7 +177,7 @@ }, "apiKey": { "title": "Api Key", - "description": "OpenAI, Anthropic, Together, or other API key", + "description": "OpenAI, Anthropic, Cohere, Together, or other API key", "type": "string" }, "apiBase": { @@ -312,6 +314,7 @@ "enum": [ "openai", "anthropic", + "cohere", "gemini", "huggingface-inference-api", "replicate", @@ -584,6 +587,30 @@ } } }, + { + "if": { + "properties": { + "provider": { + "enum": [ + "cohere" + ] + } + }, + "required": [ + "provider" + ] + }, + "then": { + "properties": { + "model": { + "enum": [ + "command-r", + "command-r-plus" + ] + } + } + } + }, { "if": { "properties": { @@ -1038,6 +1065,8 @@ "claude-3-sonnet-20240229", "claude-3-haiku-20240307", "claude-2.1", + "command-r", + "command-r-plus", "chat-bison-001", "gemini-pro", "gemini-1.5-pro-latest", diff --git a/gui/public/logos/cohere.png b/gui/public/logos/cohere.png new file mode 100644 index 000000000..3da0b8373 Binary files /dev/null and b/gui/public/logos/cohere.png differ diff --git a/gui/src/util/modelData.ts b/gui/src/util/modelData.ts index 706f25d15..17bfe50bd 100644 --- a/gui/src/util/modelData.ts +++ b/gui/src/util/modelData.ts @@ -502,6 +502,34 @@ const neuralChat: ModelPackage = { providerOptions: ["ollama", "lmstudio", "llamacpp", "replicate"], }; +const commandR: ModelPackage = { + title: "Command R", + description: + "Command R is a scalable generative model targeting RAG and Tool Use to enable production-scale AI for enterprise.", + params: { + model: "command-r", + contextLength: 128_000, + title: "Command R", + apiKey: "", + }, + providerOptions: ["cohere"], + icon: "cohere.png", +}; + +const commandRPlus: ModelPackage = { + title: "Command R+", + description: + "Command R+ is a state-of-the-art RAG-optimized model designed to tackle enterprise-grade workloads", + params: { + model: "command-r-plus", + contextLength: 128_000, + title: "Command R+", + apiKey: "", + }, + providerOptions: ["cohere"], + icon: "cohere.png", +}; + const osModels = [ deepseek, wizardCoder, @@ -623,6 +651,8 @@ export const MODEL_INFO: ModelPackage[] = [ claude3Opus, claude3Sonnet, claude3Haiku, + commandR, + commandRPlus, gemini15Pro, geminiPro, claude2, @@ -695,6 +725,28 @@ export const PROVIDER_INFO: { [key: string]: ModelInfo } = { ], packages: [claude3Opus, claude3Sonnet, claude3Haiku, claude2], }, + cohere: { + title: "Cohere", + provider: "cohere", + refPage: "cohere", + description: + "Optimized for enterprise generative AI, search and discovery, and advanced retrieval.", + icon: "cohere.png", + tags: [ModelProviderTag["Requires API Key"]], + longDescription: + "To use Cohere, visit the [Cohere dashboard](https://dashboard.cohere.com/api-keys) to create an API key.", + collectInputFor: [ + { + inputType: CollectInputType.text, + key: "apiKey", + label: "API Key", + placeholder: "Enter your Cohere API key", + required: true, + }, + ...completionParamsInputs, + ], + packages: [commandR, commandRPlus], + }, ollama: { title: "Ollama", provider: "ollama", diff --git a/install-dependencies.sh b/install-dependencies.sh index 3400e40b6..b32f75774 100755 --- a/install-dependencies.sh +++ b/install-dependencies.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # This is used in a task in .vscode/tasks.json # Start developing with: # - Run Task -> Install Dependencies