Merge pull request #2623 from Lash-L/vertexai_embedding

Add vertexai embedding option
This commit is contained in:
Nate Sesti 2024-10-22 11:36:54 -07:00 • committed by GitHub
commit a92a242551
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 256 additions and 17 deletions

18
core/index.d.ts vendored
View file

@ -48,9 +48,9 @@ export interface IndexingProgressUpdate {
export type PromptTemplate =
| string
| ((
history: ChatMessage[],
otherData: Record<string, string>,
) => string | ChatMessage[]);
history: ChatMessage[],
otherData: Record<string, string>,
) => 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<TabAutocompleteOptions>;
/** UI styles customization */

View file

@ -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<EmbedOptions> | 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<Response>(() =>
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;

View file

@ -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
};

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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.

View file

@ -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"]
}
}
]
},

View file

@ -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"]
}
}
]
},

View file

@ -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"]
}
}
]
},

View file

@ -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"
]
}
}
]
},