diff --git a/core/index.d.ts b/core/index.d.ts index 7c47ff5e5..be55efaa6 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -801,6 +801,7 @@ export interface ModelDescription { } export type EmbeddingsProviderName = + | "sagemaker" | "bedrock" | "huggingface-tei" | "transformers.js" diff --git a/core/indexing/embeddings/SageMakerEmbeddingsProvider.ts b/core/indexing/embeddings/SageMakerEmbeddingsProvider.ts new file mode 100644 index 000000000..9b860f1ae --- /dev/null +++ b/core/indexing/embeddings/SageMakerEmbeddingsProvider.ts @@ -0,0 +1,114 @@ +import { + InvokeEndpointCommand, + SageMakerRuntimeClient, + } from "@aws-sdk/client-sagemaker-runtime"; + import { fromIni } from "@aws-sdk/credential-providers"; + + import { + EmbeddingsProviderName, + EmbedOptions, + FetchFunction, + } from "../../index.js"; + import BaseEmbeddingsProvider from "./BaseEmbeddingsProvider.js"; + + + class SageMakerEmbeddingsProvider extends BaseEmbeddingsProvider { + static providerName: EmbeddingsProviderName = "sagemaker"; + + static defaultOptions: Partial | undefined = { + region: "us-west-2", + maxBatchSize: 1, + }; + profile?: string | undefined; + + constructor(options: EmbedOptions, fetch: FetchFunction) { + super(options, fetch); + if (!options.apiBase) { + options.apiBase = `https://runtime.sagemaker.${options.region}.amazonaws.com`; + } + + if (options.profile) { + this.profile = options.profile; + } else { + this.profile = "sagemaker"; + } + } + + async embed(chunks: string[]) { + const credentials = await this._getCredentials(); + const client = new SageMakerRuntimeClient({ + region: this.options.region, + credentials: { + accessKeyId: credentials.accessKeyId, + secretAccessKey: credentials.secretAccessKey, + sessionToken: credentials.sessionToken || "", + }, + }); + + const batchedChunks = this.getBatchedChunks(chunks); + return ( + await Promise.all( + batchedChunks.map(async (batch) => { + const input = this._generateInvokeModelCommandInput( + batch, + this.options, + ); + const command = new InvokeEndpointCommand(input); + const response = await client.send(command); + + if (response.Body) { + const responseBody = JSON.parse( + new TextDecoder().decode(response.Body), + ); + // If the body contains a key called "embedding" or "embeddings", return the value, otherwise return the whole body + if (responseBody.embedding) { + return responseBody.embedding; + } else if (responseBody.embeddings) { + return responseBody.embeddings; + } else { + return responseBody; + } + } + }), + ) + ).flat(); + } + private _generateInvokeModelCommandInput( + prompts: string | string[], + options: EmbedOptions, + ): any { + const payload = { + inputs: prompts, + normalize: true, + // ...(options.requestOptions?.extraBodyProperties || {}), + }; + + if (options.requestOptions?.extraBodyProperties) { + Object.assign(payload, options.requestOptions.extraBodyProperties); + } + + return { + EndpointName: this.options.model, + Body: JSON.stringify(payload), + ContentType: "application/json", + CustomAttributes: "accept_eula=false", + }; + } + + private async _getCredentials() { + try { + return await fromIni({ + profile: this.profile, + ignoreCache: true, + })(); + } catch (e) { + console.warn( + `AWS profile with name ${this.profile} not found in ~/.aws/credentials, using default profile`, + ); + return await fromIni()(); + } + } + } + + export default SageMakerEmbeddingsProvider; + \ No newline at end of file diff --git a/core/indexing/embeddings/index.ts b/core/indexing/embeddings/index.ts index f22c8af42..0eb2ae11f 100644 --- a/core/indexing/embeddings/index.ts +++ b/core/indexing/embeddings/index.ts @@ -1,5 +1,6 @@ import { EmbeddingsProviderName } from "../../index.js"; import BaseEmbeddingsProvider from "./BaseEmbeddingsProvider.js"; +import SageMakerEmbeddingsProvider from "./SageMakerEmbeddingsProvider.js"; import BedrockEmbeddingsProvider from "./BedrockEmbeddingsProvider.js"; import CohereEmbeddingsProvider from "./CohereEmbeddingsProvider.js"; import ContinueProxyEmbeddingsProvider from "./ContinueProxyEmbeddingsProvider.js"; @@ -22,6 +23,7 @@ export const allEmbeddingsProviders: Record< EmbeddingsProviderName, EmbeddingsProviderConstructor > = { + sagemaker: SageMakerEmbeddingsProvider, bedrock: BedrockEmbeddingsProvider, ollama: OllamaEmbeddingsProvider, "transformers.js": TransformersJsEmbeddingsProvider, diff --git a/docs/docs/customize/model-providers/more/sagemaker.md b/docs/docs/customize/model-providers/more/sagemaker.md index fc3bb9b4a..0c9eba812 100644 --- a/docs/docs/customize/model-providers/more/sagemaker.md +++ b/docs/docs/customize/model-providers/more/sagemaker.md @@ -1,8 +1,8 @@ # AWS SageMaker -SageMaker provider support SageMaker endpoint deployed with [LMI](https://docs.djl.ai/docs/serving/serving/docs/lmi/index.html) +SageMaker can be used for both chat and embedding models. Chat models are supported for endpoints deployed with [LMI](https://docs.djl.ai/docs/serving/serving/docs/lmi/index.html), and embedding models are supported for endpoints deployed with [HuggingFace TEI](https://huggingface.co/blog/sagemaker-huggingface-embedding) -To setup SageMaker, add the following to your `config.json` file: +To setup SageMaker as a chat model provider, add the following to your `config.json` file: ```json title="config.json" { @@ -13,7 +13,11 @@ To setup SageMaker, add the following to your `config.json` file: "model": "lmi-model-deepseek-coder-xxxxxxx", "region": "us-west-2" } - ] + ], + "embeddingsProvider": { + "provider": "sagemaker", + "model": "mxbai-embed-large-v1-endpoint" + }, } ``` diff --git a/docs/static/schemas/config.json b/docs/static/schemas/config.json index 827330b49..c1b80c901 100644 --- a/docs/static/schemas/config.json +++ b/docs/static/schemas/config.json @@ -246,7 +246,24 @@ "region": { "title": "Region", "description": "The region where the model is hosted", - "type": "string" + "anyOf":[ + { + "enum": [ + "us-east-1", + "us-east-2", + "us-west-1", + "us-west-2", + "eu-west-1", + "eu-central-1", + "ap-southeast-1", + "ap-northeast-1", + "ap-south-1" + ] + }, + { + "type": "string" + } + ] }, "profile": { "title": "Profile", @@ -2150,7 +2167,8 @@ "gemini", "voyage", "nvidia", - "bedrock" + "bedrock", + "sagemaker" ] }, "model": { @@ -2184,7 +2202,24 @@ "region": { "title": "Region", "description": "The region where the model is hosted", - "type": "string" + "anyOf":[ + { + "enum": [ + "us-east-1", + "us-east-2", + "us-west-1", + "us-west-2", + "eu-west-1", + "eu-central-1", + "ap-southeast-1", + "ap-northeast-1", + "ap-south-1" + ] + }, + { + "type": "string" + } + ] }, "profile": { "title": "Profile", @@ -2206,6 +2241,24 @@ "then": { "required": ["apiKey"] } + }, + { + "if": { + "properties": { + "provider": { + "enum": ["sagemaker"] + } + }, + "required": ["provider"] + }, + "then": { + "properties": { + "model": { + "markdownDescription": "SageMaker endpoint name" + } + }, + "required": ["model"] + } } ] }, diff --git a/extensions/intellij/src/main/resources/config_schema.json b/extensions/intellij/src/main/resources/config_schema.json index 827330b49..16321df00 100644 --- a/extensions/intellij/src/main/resources/config_schema.json +++ b/extensions/intellij/src/main/resources/config_schema.json @@ -246,7 +246,25 @@ "region": { "title": "Region", "description": "The region where the model is hosted", - "type": "string" + "anyOf":[ + { + "enum": [ + "us-east-1", + "us-east-2", + "us-west-1", + "us-west-2", + "eu-west-1", + "eu-central-1", + "ap-southeast-1", + "ap-northeast-1", + "ap-south-1" + ], + "type" : "string" + }, + { + "type": "string" + } + ] }, "profile": { "title": "Profile", @@ -2150,7 +2168,8 @@ "gemini", "voyage", "nvidia", - "bedrock" + "bedrock", + "sagemaker" ] }, "model": { @@ -2184,7 +2203,7 @@ "region": { "title": "Region", "description": "The region where the model is hosted", - "type": "string" + "$ref": "#/definitions/ModelDescription/properties/region" }, "profile": { "title": "Profile", @@ -2206,6 +2225,24 @@ "then": { "required": ["apiKey"] } + }, + { + "if": { + "properties": { + "provider": { + "enum": ["sagemaker"] + } + }, + "required": ["provider"] + }, + "then": { + "properties": { + "model": { + "markdownDescription": "SageMaker endpoint name" + } + }, + "required": ["model"] + } } ] }, diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index 827330b49..16321df00 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -246,7 +246,25 @@ "region": { "title": "Region", "description": "The region where the model is hosted", - "type": "string" + "anyOf":[ + { + "enum": [ + "us-east-1", + "us-east-2", + "us-west-1", + "us-west-2", + "eu-west-1", + "eu-central-1", + "ap-southeast-1", + "ap-northeast-1", + "ap-south-1" + ], + "type" : "string" + }, + { + "type": "string" + } + ] }, "profile": { "title": "Profile", @@ -2150,7 +2168,8 @@ "gemini", "voyage", "nvidia", - "bedrock" + "bedrock", + "sagemaker" ] }, "model": { @@ -2184,7 +2203,7 @@ "region": { "title": "Region", "description": "The region where the model is hosted", - "type": "string" + "$ref": "#/definitions/ModelDescription/properties/region" }, "profile": { "title": "Profile", @@ -2206,6 +2225,24 @@ "then": { "required": ["apiKey"] } + }, + { + "if": { + "properties": { + "provider": { + "enum": ["sagemaker"] + } + }, + "required": ["provider"] + }, + "then": { + "properties": { + "model": { + "markdownDescription": "SageMaker endpoint name" + } + }, + "required": ["model"] + } } ] }, diff --git a/extensions/vscode/continue_rc_schema.json b/extensions/vscode/continue_rc_schema.json index 6099744e4..7dba9328e 100644 --- a/extensions/vscode/continue_rc_schema.json +++ b/extensions/vscode/continue_rc_schema.json @@ -249,7 +249,24 @@ "region": { "title": "Region", "description": "The region where the model is hosted", - "type": "string" + "anyOf": [ + { + "enum": [ + "us-east-1", + "us-east-2", + "us-west-1", + "us-west-2", + "eu-west-1", + "eu-central-1", + "ap-southeast-1", + "ap-northeast-1", + "ap-south-1" + ] + }, + { + "type": "string" + } + ] }, "profile": { "title": "Profile", @@ -2396,7 +2413,8 @@ "gemini", "voyage", "nvidia", - "bedrock" + "bedrock", + "sagemaker" ] }, "model": { @@ -2430,7 +2448,24 @@ "region": { "title": "Region", "description": "The region where the model is hosted", - "type": "string" + "anyOf": [ + { + "enum": [ + "us-east-1", + "us-east-2", + "us-west-1", + "us-west-2", + "eu-west-1", + "eu-central-1", + "ap-southeast-1", + "ap-northeast-1", + "ap-south-1" + ] + }, + { + "type": "string" + } + ] }, "profile": { "title": "Profile", @@ -2463,6 +2498,30 @@ "apiKey" ] } + }, + { + "if": { + "properties": { + "provider": { + "enum": [ + "sagemaker" + ] + } + }, + "required": [ + "provider" + ] + }, + "then": { + "properties": { + "model": { + "markdownDescription": "SageMaker endpoint name" + } + }, + "required": [ + "model" + ] + } } ] },