✨ add Cohere as Embeddings Provider
This commit is contained in:
parent
bb71479614
commit
c2f8712393
8 changed files with 148 additions and 3 deletions
1
core/index.d.ts
vendored
1
core/index.d.ts
vendored
|
|
@ -631,6 +631,7 @@ export type EmbeddingsProviderName =
|
|||
| "transformers.js"
|
||||
| "ollama"
|
||||
| "openai"
|
||||
| "cohere"
|
||||
| "free-trial";
|
||||
|
||||
export interface EmbedOptions {
|
||||
|
|
|
|||
67
core/indexing/embeddings/CohereEmbeddingsProvider.ts
Normal file
67
core/indexing/embeddings/CohereEmbeddingsProvider.ts
Normal file
|
|
@ -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<EmbedOptions> | 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<Response>(() =>
|
||||
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;
|
||||
|
|
@ -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,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
15
docs/static/schemas/config.json
vendored
15
docs/static/schemas/config.json
vendored
|
|
@ -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"]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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"]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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"]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue