From c2f871239311144ba8af4a6bd396fc18d50dc3ef Mon Sep 17 00:00:00 2001 From: Maxime Brunet Date: Sat, 20 Apr 2024 15:45:33 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20Cohere=20as=20Embeddings=20Pr?= =?UTF-8?q?ovider?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/index.d.ts | 1 + .../embeddings/CohereEmbeddingsProvider.ts | 67 +++++++++++++++++++ core/indexing/embeddings/index.ts | 2 + docs/docs/walkthroughs/codebase-embeddings.md | 16 +++++ docs/static/schemas/config.json | 15 ++++- .../src/main/resources/config_schema.json | 15 ++++- extensions/vscode/config_schema.json | 15 ++++- extensions/vscode/continue_rc_schema.json | 20 ++++++ 8 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 core/indexing/embeddings/CohereEmbeddingsProvider.ts diff --git a/core/index.d.ts b/core/index.d.ts index fb7108a3e..da00408a5 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -631,6 +631,7 @@ export type EmbeddingsProviderName = | "transformers.js" | "ollama" | "openai" + | "cohere" | "free-trial"; export interface EmbedOptions { diff --git a/core/indexing/embeddings/CohereEmbeddingsProvider.ts b/core/indexing/embeddings/CohereEmbeddingsProvider.ts new file mode 100644 index 000000000..05e248792 --- /dev/null +++ b/core/indexing/embeddings/CohereEmbeddingsProvider.ts @@ -0,0 +1,67 @@ +import fetch, { Response } from "node-fetch"; +import { EmbedOptions } from "../.."; +import { withExponentialBackoff } from "../../util/withExponentialBackoff"; +import BaseEmbeddingsProvider from "./BaseEmbeddingsProvider"; + +class CohereEmbeddingsProvider extends BaseEmbeddingsProvider { + static maxBatchSize = 96; + + static defaultOptions: Partial | undefined = { + apiBase: "https://api.cohere.ai/v1/", + model: "embed-english-v3.0", + }; + + get id(): string { + return this.options.model ?? "cohere"; + } + + async embed(chunks: string[]) { + if (!this.options.apiBase?.endsWith("/")) { + this.options.apiBase += "/"; + } + + const batchedChunks = []; + for ( + let i = 0; + i < chunks.length; + i += CohereEmbeddingsProvider.maxBatchSize + ) { + batchedChunks.push( + chunks.slice(i, i + CohereEmbeddingsProvider.maxBatchSize), + ); + } + return ( + await Promise.all( + batchedChunks.map(async (batch) => { + const fetchWithBackoff = () => + withExponentialBackoff(() => + fetch(new URL("embed", this.options.apiBase), { + method: "POST", + body: JSON.stringify({ + texts: batch, + model: this.options.model, + input_type: "search_document", + embedding_types: ["float"], + truncate: "END", + }), + headers: { + Authorization: `Bearer ${this.options.apiKey}`, + "Content-Type": "application/json", + }, + }), + ); + const resp = await fetchWithBackoff(); + + if (!resp.ok) { + throw new Error(await resp.text()); + } + + const data = (await resp.json()) as any; + return data.embeddings.float; + }), + ) + ).flat(); + } +} + +export default CohereEmbeddingsProvider; diff --git a/core/indexing/embeddings/index.ts b/core/indexing/embeddings/index.ts index 7430a652d..73a030ad7 100644 --- a/core/indexing/embeddings/index.ts +++ b/core/indexing/embeddings/index.ts @@ -1,4 +1,5 @@ import { EmbeddingsProviderName } from "../.."; +import CohereEmbeddingsProvider from "./CohereEmbeddingsProvider"; import FreeTrialEmbeddingsProvider from "./FreeTrialEmbeddingsProvider"; import OllamaEmbeddingsProvider from "./OllamaEmbeddingsProvider"; import OpenAIEmbeddingsProvider from "./OpenAIEmbeddingsProvider"; @@ -10,5 +11,6 @@ export const AllEmbeddingsProviders: { ollama: OllamaEmbeddingsProvider, "transformers.js": TransformersJsEmbeddingsProvider, openai: OpenAIEmbeddingsProvider, + cohere: CohereEmbeddingsProvider, "free-trial": FreeTrialEmbeddingsProvider, }; diff --git a/docs/docs/walkthroughs/codebase-embeddings.md b/docs/docs/walkthroughs/codebase-embeddings.md index 701100a4d..5bb87615a 100644 --- a/docs/docs/walkthroughs/codebase-embeddings.md +++ b/docs/docs/walkthroughs/codebase-embeddings.md @@ -137,6 +137,22 @@ For certain scenarios, you may still find the text-embedding-ada-002 model relev } ``` +### Cohere + +Configuration for the `embed-english-v3.0` model. This is the default. + +```json title="~/.continue/config.json" +{ + "embeddingsProvider": { + "provider": "cohere", + "model": "embed-english-v3.0", + "apiKey": "YOUR_API_KEY" + } +} +``` + +See Cohere's [embeddings](https://docs.cohere.com/docs/embed-2) for available models. Only embedding models v3 and higher are supported. + ### Writing a custom `EmbeddingsProvider` If you have your own API capable of generating embeddings, Continue makes it easy to write a custom `EmbeddingsProvider`. All you have to do is write a function that converts strings to arrays of numbers, and add this to your config in `config.ts`. Here's an example: diff --git a/docs/static/schemas/config.json b/docs/static/schemas/config.json index 47f1df213..16e37b28f 100644 --- a/docs/static/schemas/config.json +++ b/docs/static/schemas/config.json @@ -1664,7 +1664,7 @@ "type": "object", "properties": { "provider": { - "enum": ["transformers.js", "ollama", "openai", "free-trial"] + "enum": ["transformers.js", "ollama", "openai", "cohere", "free-trial"] }, "model": { "type": "string" @@ -1690,6 +1690,19 @@ "then": { "required": ["model"] } + }, + { + "if": { + "properties": { + "provider": { + "enum": ["cohere"] + } + }, + "required": ["provider"] + }, + "then": { + "required": ["apiKey"] + } } ] }, diff --git a/extensions/intellij/src/main/resources/config_schema.json b/extensions/intellij/src/main/resources/config_schema.json index 47f1df213..16e37b28f 100644 --- a/extensions/intellij/src/main/resources/config_schema.json +++ b/extensions/intellij/src/main/resources/config_schema.json @@ -1664,7 +1664,7 @@ "type": "object", "properties": { "provider": { - "enum": ["transformers.js", "ollama", "openai", "free-trial"] + "enum": ["transformers.js", "ollama", "openai", "cohere", "free-trial"] }, "model": { "type": "string" @@ -1690,6 +1690,19 @@ "then": { "required": ["model"] } + }, + { + "if": { + "properties": { + "provider": { + "enum": ["cohere"] + } + }, + "required": ["provider"] + }, + "then": { + "required": ["apiKey"] + } } ] }, diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index 47f1df213..16e37b28f 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -1664,7 +1664,7 @@ "type": "object", "properties": { "provider": { - "enum": ["transformers.js", "ollama", "openai", "free-trial"] + "enum": ["transformers.js", "ollama", "openai", "cohere", "free-trial"] }, "model": { "type": "string" @@ -1690,6 +1690,19 @@ "then": { "required": ["model"] } + }, + { + "if": { + "properties": { + "provider": { + "enum": ["cohere"] + } + }, + "required": ["provider"] + }, + "then": { + "required": ["apiKey"] + } } ] }, diff --git a/extensions/vscode/continue_rc_schema.json b/extensions/vscode/continue_rc_schema.json index 737e09ead..15108d11b 100644 --- a/extensions/vscode/continue_rc_schema.json +++ b/extensions/vscode/continue_rc_schema.json @@ -1865,6 +1865,7 @@ "transformers.js", "ollama", "openai", + "cohere", "free-trial" ] }, @@ -1900,6 +1901,25 @@ "model" ] } + }, + { + "if": { + "properties": { + "provider": { + "enum": [ + "cohere" + ] + } + }, + "required": [ + "provider" + ] + }, + "then": { + "required": [ + "apiKey" + ] + } } ] },