diff --git a/core/index.d.ts b/core/index.d.ts index e7c22db2f..183d5e427 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -48,9 +48,9 @@ export interface IndexingProgressUpdate { export type PromptTemplate = | string | (( - history: ChatMessage[], - otherData: Record, - ) => string | ChatMessage[]); + history: ChatMessage[], + otherData: Record, + ) => string | ChatMessage[]); export interface ILLM extends LLMOptions { get providerName(): ModelProvider; @@ -822,7 +822,8 @@ export type EmbeddingsProviderName = | "deepinfra" | "nvidia" | "voyage" - | "mistral"; + | "mistral" + | "vertex"; export interface EmbedOptions { apiBase?: string; @@ -839,6 +840,9 @@ export interface EmbedOptions { // AWS and GCP Options region?: string; + + // GCP Options + projectId?: string; } export interface EmbeddingsProviderDescription extends EmbedOptions { @@ -1041,9 +1045,9 @@ export interface Config { embeddingsProvider?: EmbeddingsProviderDescription | EmbeddingsProvider; /** The model that Continue will use for tab autocompletions. */ tabAutocompleteModel?: - | CustomLLM - | ModelDescription - | (CustomLLM | ModelDescription)[]; + | CustomLLM + | ModelDescription + | (CustomLLM | ModelDescription)[]; /** Options for tab autocomplete */ tabAutocompleteOptions?: Partial; /** UI styles customization */ diff --git a/core/indexing/embeddings/VertexEmbeddingsProvider.ts b/core/indexing/embeddings/VertexEmbeddingsProvider.ts new file mode 100644 index 000000000..06a8929cc --- /dev/null +++ b/core/indexing/embeddings/VertexEmbeddingsProvider.ts @@ -0,0 +1,89 @@ +import { GoogleAuth } from "google-auth-library"; +import { Response } from "node-fetch"; +import { EmbeddingsProviderName, EmbedOptions, FetchFunction } from "../../index.js"; +import { withExponentialBackoff } from "../../util/withExponentialBackoff.js"; +import BaseEmbeddingsProvider from "./BaseEmbeddingsProvider.js"; + +/** + * [View the Vertex Text Embedding docs.](https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/get-text-embeddings) + */ +class VertexEmbeddingsProvider extends BaseEmbeddingsProvider { + static providerName: EmbeddingsProviderName = "vertex"; + declare apiBase: string; + + + static defaultOptions: Partial | undefined = { + model: "text-embedding-004", + maxBatchSize: 5, + region: "us-central1" + }; + + private clientPromise = new GoogleAuth({ + scopes: "https://www.googleapis.com/auth/cloud-platform", + }).getClient(); + + private static getDefaultApiBaseFrom(options: EmbedOptions) { + const { region, projectId } = options; + if (!region || !projectId) { + throw new Error( + "region and projectId must be defined if apiBase is not provided", + ); + } + + return `https://${region}-aiplatform.googleapis.com/v1/projects/${projectId}/locations/${region}`; + } + + + + constructor(options: EmbedOptions, fetch: FetchFunction) { + super(options, fetch); + this.apiBase ??= VertexEmbeddingsProvider.getDefaultApiBaseFrom(options); + + } + + + + get urlPath(): string { + return `/publishers/google/models/${this.options.model}:predict`; + } + + async embed(chunks: string[]) { + const client = await this.clientPromise; + const { token } = await client.getAccessToken(); + if (!token) { + throw new Error( + "Could not get an access token. Set up your Google Application Default Credentials.", + ); + } + const batchedChunks = this.getBatchedChunks(chunks); + + return ( + await Promise.all( + batchedChunks.map(async (batch) => { + const fetchWithBackoff = () => + withExponentialBackoff(() => + this.fetch(new URL(this.apiBase + this.urlPath), { + method: "POST", + body: JSON.stringify({ + instances: batch.map((chunk) => ({ content: chunk })), + }), + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + }), + ); + const resp = await fetchWithBackoff(); + + if (!resp.ok) { + throw new Error(await resp.text()); + } + + const data = (await resp.json()) as any; + return data.predictions.map((prediction: any) => prediction.embeddings.values); + }), + ) + ).flat(); + } +} +export default VertexEmbeddingsProvider; diff --git a/core/indexing/embeddings/index.ts b/core/indexing/embeddings/index.ts index 0eb2ae11f..fe9309f73 100644 --- a/core/indexing/embeddings/index.ts +++ b/core/indexing/embeddings/index.ts @@ -14,6 +14,7 @@ import OpenAIEmbeddingsProvider from "./OpenAIEmbeddingsProvider.js"; import TransformersJsEmbeddingsProvider from "./TransformersJsEmbeddingsProvider.js"; import NvidiaEmbeddingsProvider from "./NvidiaEmbeddingsProvider.js"; import VoyageEmbeddingsProvider from "./VoyageEmbeddingsProvider.js"; +import VertexEmbeddingsProvider from "./VertexEmbeddingsProvider.js"; type EmbeddingsProviderConstructor = new ( ...args: any[] @@ -37,4 +38,5 @@ export const allEmbeddingsProviders: Record< nvidia: NvidiaEmbeddingsProvider, voyage: VoyageEmbeddingsProvider, mistral: MistralEmbeddingsProvider, + vertex: VertexEmbeddingsProvider }; diff --git a/docs/docs/customize/model-providers/more/anthropic-vertexai.md b/docs/docs/customize/model-providers/more/anthropic-vertexai.md index 3f00c17f6..3f5280598 100644 --- a/docs/docs/customize/model-providers/more/anthropic-vertexai.md +++ b/docs/docs/customize/model-providers/more/anthropic-vertexai.md @@ -35,9 +35,18 @@ Anthropic Vertex AI currently does not offer any autocomplete models. ## Embeddings model -Anthropic Vertex AI currently does not offer any embeddings models. +We recommend configuring **text-embedding-004** as your embeddings model. -[Click here](../../model-types/embeddings.md) to see a list of embeddings model providers. +```json title="config.json" +{ + "embeddingsProvider": { + "provider": "vertex", + "model": "text-embedding-004", + "projectId": "[PROJECT_ID]", + "region": "us-central1" + } +} +``` ## Reranking model diff --git a/docs/docs/customize/model-providers/more/gemini-vertexai.md b/docs/docs/customize/model-providers/more/gemini-vertexai.md index fb92e60ff..074278c1f 100644 --- a/docs/docs/customize/model-providers/more/gemini-vertexai.md +++ b/docs/docs/customize/model-providers/more/gemini-vertexai.md @@ -35,9 +35,19 @@ Gemini Vertex AI currently does not offer any autocomplete models. ## Embeddings model -Gemini Vertex AI currently does not offer any embeddings models. +We recommend configuring **text-embedding-004** as your embeddings model. + +```json title="config.json" +{ + "embeddingsProvider": { + "provider": "vertex", + "model": "text-embedding-004", + "projectId": "[PROJECT_ID]", + "region": "us-central1" + } +} +``` -[Click here](../../model-types/embeddings.md) to see a list of embeddings model providers. ## Reranking model diff --git a/docs/docs/customize/model-providers/more/mistral-vertexai.md b/docs/docs/customize/model-providers/more/mistral-vertexai.md index c4590e41b..8d64c7ffe 100644 --- a/docs/docs/customize/model-providers/more/mistral-vertexai.md +++ b/docs/docs/customize/model-providers/more/mistral-vertexai.md @@ -45,9 +45,18 @@ We recommend configuring **Codestral** as your autocomplete model. ## Embeddings model -Mistral Vertex AI currently does not offer any embeddings models. +We recommend configuring **text-embedding-004** as your embeddings model. -[Click here](../../model-types/embeddings.md) to see a list of embeddings model providers. +```json title="config.json" +{ + "embeddingsProvider": { + "provider": "vertex", + "model": "text-embedding-004", + "projectId": "[PROJECT_ID]", + "region": "us-central1" + } +} +``` ## Reranking model diff --git a/docs/docs/customize/model-types/embeddings.md b/docs/docs/customize/model-types/embeddings.md index c32468967..a84346fa0 100644 --- a/docs/docs/customize/model-types/embeddings.md +++ b/docs/docs/customize/model-types/embeddings.md @@ -69,3 +69,7 @@ See [here](../model-providers/more/cohere.md#embeddings-model) for instructions ### Gemini See [here](../model-providers/top-level/gemini.md#embeddings-model) for instructions on how to use Gemini for embeddings. + +### Vertex + +See [here](../model-providers/more/vertex.md#embeddings-model) for instructions on how to use Vertex for embeddings. \ No newline at end of file diff --git a/docs/static/schemas/config.json b/docs/static/schemas/config.json index ce4e8e1a4..6da984ead 100644 --- a/docs/static/schemas/config.json +++ b/docs/static/schemas/config.json @@ -2427,7 +2427,8 @@ "voyage", "nvidia", "bedrock", - "sagemaker" + "sagemaker", + "vertex" ] }, "model": { @@ -2501,6 +2502,31 @@ }, "required": ["model"] } + }, + { + "if": { + "properties": { + "provider": { + "enum": ["vertex"] + } + }, + "required": ["provider"] + }, + "then": { + "properties": { + "projectId": { + "markdownDescription": "The name of your vertexAI project" + }, + "region": { + "markdownDescription": "The region your vertexAI model is hosted in - typically central1", + "default": "central1" + }, + "model": { + "default": "text-embedding-004" + } + }, + "required": ["projectId", "model", "region"] + } } ] }, diff --git a/extensions/intellij/src/main/resources/config_schema.json b/extensions/intellij/src/main/resources/config_schema.json index ce4e8e1a4..6da984ead 100644 --- a/extensions/intellij/src/main/resources/config_schema.json +++ b/extensions/intellij/src/main/resources/config_schema.json @@ -2427,7 +2427,8 @@ "voyage", "nvidia", "bedrock", - "sagemaker" + "sagemaker", + "vertex" ] }, "model": { @@ -2501,6 +2502,31 @@ }, "required": ["model"] } + }, + { + "if": { + "properties": { + "provider": { + "enum": ["vertex"] + } + }, + "required": ["provider"] + }, + "then": { + "properties": { + "projectId": { + "markdownDescription": "The name of your vertexAI project" + }, + "region": { + "markdownDescription": "The region your vertexAI model is hosted in - typically central1", + "default": "central1" + }, + "model": { + "default": "text-embedding-004" + } + }, + "required": ["projectId", "model", "region"] + } } ] }, diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index ce4e8e1a4..6da984ead 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -2427,7 +2427,8 @@ "voyage", "nvidia", "bedrock", - "sagemaker" + "sagemaker", + "vertex" ] }, "model": { @@ -2501,6 +2502,31 @@ }, "required": ["model"] } + }, + { + "if": { + "properties": { + "provider": { + "enum": ["vertex"] + } + }, + "required": ["provider"] + }, + "then": { + "properties": { + "projectId": { + "markdownDescription": "The name of your vertexAI project" + }, + "region": { + "markdownDescription": "The region your vertexAI model is hosted in - typically central1", + "default": "central1" + }, + "model": { + "default": "text-embedding-004" + } + }, + "required": ["projectId", "model", "region"] + } } ] }, diff --git a/extensions/vscode/continue_rc_schema.json b/extensions/vscode/continue_rc_schema.json index b2b7dbb78..86304fa6e 100644 --- a/extensions/vscode/continue_rc_schema.json +++ b/extensions/vscode/continue_rc_schema.json @@ -2724,7 +2724,8 @@ "voyage", "nvidia", "bedrock", - "sagemaker" + "sagemaker", + "vertex" ] }, "model": { @@ -2815,6 +2816,39 @@ "model" ] } + }, + { + "if": { + "properties": { + "provider": { + "enum": [ + "vertex" + ] + } + }, + "required": [ + "provider" + ] + }, + "then": { + "properties": { + "projectId": { + "markdownDescription": "The name of your vertexAI project" + }, + "region": { + "markdownDescription": "The region your vertexAI model is hosted in - typically central1", + "default": "central1" + }, + "model": { + "default": "text-embedding-004" + } + }, + "required": [ + "projectId", + "model", + "region" + ] + } } ] },