Merge pull request #2233 from adarshdotexe/nvidia-embeddings-provider

This commit is contained in:
Nate Sesti 2024-09-11 10:51:07 -07:00 • committed by GitHub
commit 7a6092ff2a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 108 additions and 62 deletions

1
core/index.d.ts vendored
View file

@ -798,6 +798,7 @@ export type EmbeddingsProviderName =
| "gemini"
| "continue-proxy"
| "deepinfra"
| "nvidia"
| "voyage";
export interface EmbedOptions {

View file

@ -0,0 +1,80 @@
import { Response } from "node-fetch";
import { EmbeddingsProviderName, EmbedOptions } from "../../index.js";
import { withExponentialBackoff } from "../../util/withExponentialBackoff.js";
import BaseEmbeddingsProvider from "./BaseEmbeddingsProvider.js";
class NvidiaEmbeddingsProvider extends BaseEmbeddingsProvider {
static maxBatchSize = 96;
static providerName: EmbeddingsProviderName = "nvidia";
static defaultOptions: Partial<EmbedOptions> | undefined = {
apiBase: "https://integrate.api.nvidia.com/v1/",
model: "nvidia/nv-embedqa-mistral-7b-v2",
};
async embed(chunks: string[]) {
if (!this.options.apiBase?.endsWith("/")) {
this.options.apiBase += "/";
}
const batchedChunks = [];
for (
let i = 0;
i < chunks.length;
i += NvidiaEmbeddingsProvider.maxBatchSize
) {
batchedChunks.push(
chunks.slice(i, i + NvidiaEmbeddingsProvider.maxBatchSize),
);
}
return (
await Promise.all(
batchedChunks.map(async (batch) => {
if (batch.length === 0) {
return [];
}
// Input list must be non empty and all elements must be non empty, therefore, for all empty elements replace it with a token
const emptyToken = '[EMPTY]';
for (let i = 0; i < batch.length; i++) {
if (batch[i].trim() === '') {
batch[i] = emptyToken;
}
}
const fetchWithBackoff = () =>
withExponentialBackoff<Response>(() =>
this.fetch(new URL("embeddings", this.options.apiBase), {
method: "POST",
body: JSON.stringify({
input: batch,
model: this.options.model,
input_type: 'passage',
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.data.map(
(result: { embedding: number[] }) => result.embedding,
);
}),
)
).flat();
}
}
export default NvidiaEmbeddingsProvider;

View file

@ -10,6 +10,7 @@ import HuggingFaceTEIEmbeddingsProvider from "./HuggingFaceTEIEmbeddingsProvider
import OllamaEmbeddingsProvider from "./OllamaEmbeddingsProvider.js";
import OpenAIEmbeddingsProvider from "./OpenAIEmbeddingsProvider.js";
import TransformersJsEmbeddingsProvider from "./TransformersJsEmbeddingsProvider.js";
import NvidiaEmbeddingsProvider from "./NvidiaEmbeddingsProvider.js";
import VoyageEmbeddingsProvider from "./VoyageEmbeddingsProvider.js";
type EmbeddingsProviderConstructor = new (
@ -30,5 +31,6 @@ export const allEmbeddingsProviders: Record<
gemini: GeminiEmbeddingsProvider,
"continue-proxy": ContinueProxyEmbeddingsProvider,
deepinfra: DeepInfraEmbeddingsProvider,
nvidia: NvidiaEmbeddingsProvider,
voyage: VoyageEmbeddingsProvider,
};

View file

@ -68,6 +68,21 @@ Whether to use re-ranking, which will allow initial selection of `nRetrieve` res
We also support other methods of generating embeddings, which can be configured with the `"embeddingsProvider"` property in `config.json`. We currently have built-in support for the following providers:
### NVIDIA
NVIDIA provides text embedding models as NIMs (NVIDIA Inference Models) that generate semantically meaningful vector representations of text.
Sample configuration for the `nvidia/nv-embedqa-mistral-7b-v2` model. This is the default model for the nvidia provider.
```json title="~/.continue/config.json"
{
"embeddingsProvider": {
"provider": "nvidia",
"model": "nvidia/nv-embedqa-mistral-7b-v2",
"apiKey": "YOUR_API_KEY"
}
}
```
Some of the other key NVIDIA embedding NIMs are: `nvidia/nv-embedqa-e5-v5` and `baai/bge-m3`. See Nvidia's [catalog](https://build.nvidia.com/search?term=embeddings) for more available models.
### Transformers.js (currently VS Code only)
[Transformers.js](https://huggingface.co/docs/transformers.js/index) is a JavaScript port of the popular [Transformers](https://huggingface.co/transformers/) library. It allows embeddings to be calculated locally in the browser (or in this case inside of the sidebar of your IDE). The model used is `all-MiniLM-L6-v2`, which is shipped alongside the Continue extension and generates embeddings of size 384.

View file

@ -2094,6 +2094,7 @@
"cohere",
"free-trial",
"gemini",
"nvidia",
"voyage",
"bedrock"
]
@ -2136,20 +2137,7 @@
"if": {
"properties": {
"provider": {
"enum": ["cohere", "voyage"]
}
},
"required": ["provider"]
},
"then": {
"required": ["apiKey"]
}
},
{
"if": {
"properties": {
"provider": {
"enum": ["gemini"]
"enum": ["cohere", "voyage", "nvidia", "gemini"]
}
},
"required": ["provider"]

View file

@ -2095,6 +2095,7 @@
"free-trial",
"gemini",
"voyage",
"nvidia",
"bedrock"
]
},
@ -2136,20 +2137,7 @@
"if": {
"properties": {
"provider": {
"enum": ["cohere", "voyage"]
}
},
"required": ["provider"]
},
"then": {
"required": ["apiKey"]
}
},
{
"if": {
"properties": {
"provider": {
"enum": ["gemini"]
"enum": ["cohere", "voyage", "gemini", "nvidia"]
}
},
"required": ["provider"]

View file

@ -2095,6 +2095,7 @@
"free-trial",
"gemini",
"voyage",
"nvidia",
"bedrock"
]
},
@ -2136,20 +2137,7 @@
"if": {
"properties": {
"provider": {
"enum": ["cohere", "voyage"]
}
},
"required": ["provider"]
},
"then": {
"required": ["apiKey"]
}
},
{
"if": {
"properties": {
"provider": {
"enum": ["gemini"]
"enum": ["cohere", "voyage", "nvidia", "gemini"]
}
},
"required": ["provider"]

View file

@ -2330,6 +2330,7 @@
"free-trial",
"gemini",
"voyage",
"nvidia",
"bedrock"
]
},
@ -2375,26 +2376,9 @@
"provider": {
"enum": [
"cohere",
"voyage"
]
}
},
"required": [
"provider"
]
},
"then": {
"required": [
"apiKey"
]
}
},
{
"if": {
"properties": {
"provider": {
"enum": [
"gemini"
"voyage",
"gemini",
"nvidia"
]
}
},