From 0f018ec18dcf2089d83c16fae5fb686b0757adfe Mon Sep 17 00:00:00 2001 From: kbreese-x Date: Fri, 4 Oct 2024 13:14:20 -0700 Subject: [PATCH 1/3] Add support for sagemaker as an embeddings model provider --- core/index.d.ts | 1 + .../embeddings/SageMakerEmbeddingsProvider.ts | 114 ++++++++++++++++++ core/indexing/embeddings/index.ts | 2 + .../model-providers/more/sagemaker.md | 10 +- docs/docs/customize/model-types/embeddings.md | 4 + .../src/main/resources/config_schema.json | 59 ++++++++- extensions/vscode/config_schema.json | 59 ++++++++- 7 files changed, 240 insertions(+), 9 deletions(-) create mode 100644 core/indexing/embeddings/SageMakerEmbeddingsProvider.ts diff --git a/core/index.d.ts b/core/index.d.ts index c1c8ddc04..4ac6ce58b 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/docs/customize/model-types/embeddings.md b/docs/docs/customize/model-types/embeddings.md index c32468967..d4d062854 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. + +### Amazon + +You can use either [Bedrock](../model-providers/top-level/bedrock.md#embeddings-model) or [SageMaker](../model-providers/more/sagemaker.md) for generating embeddings. \ No newline at end of file diff --git a/extensions/intellij/src/main/resources/config_schema.json b/extensions/intellij/src/main/resources/config_schema.json index 3f340f6ce..4bb201b3e 100644 --- a/extensions/intellij/src/main/resources/config_schema.json +++ b/extensions/intellij/src/main/resources/config_schema.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", @@ -2145,7 +2162,8 @@ "gemini", "voyage", "nvidia", - "bedrock" + "bedrock", + "sagemaker" ] }, "model": { @@ -2179,7 +2197,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", @@ -2201,6 +2236,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 3f340f6ce..4bb201b3e 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.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", @@ -2145,7 +2162,8 @@ "gemini", "voyage", "nvidia", - "bedrock" + "bedrock", + "sagemaker" ] }, "model": { @@ -2179,7 +2197,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", @@ -2201,6 +2236,24 @@ "then": { "required": ["apiKey"] } + }, + { + "if": { + "properties": { + "provider": { + "enum": ["sagemaker"] + } + }, + "required": ["provider"] + }, + "then": { + "properties": { + "model": { + "markdownDescription": "SageMaker endpoint name" + } + }, + "required": ["model"] + } } ] }, From 27013d47d7eef4f41220c2bed2b1b89fd0493d5d Mon Sep 17 00:00:00 2001 From: kbreese-x Date: Fri, 4 Oct 2024 19:05:18 -0700 Subject: [PATCH 2/3] update docs and schema --- docs/docs/customize/model-types/embeddings.md | 4 ---- .../src/main/resources/config_schema.json | 22 +++---------------- extensions/vscode/config_schema.json | 22 +++---------------- 3 files changed, 6 insertions(+), 42 deletions(-) diff --git a/docs/docs/customize/model-types/embeddings.md b/docs/docs/customize/model-types/embeddings.md index d4d062854..c32468967 100644 --- a/docs/docs/customize/model-types/embeddings.md +++ b/docs/docs/customize/model-types/embeddings.md @@ -69,7 +69,3 @@ 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. - -### Amazon - -You can use either [Bedrock](../model-providers/top-level/bedrock.md#embeddings-model) or [SageMaker](../model-providers/more/sagemaker.md) for generating embeddings. \ No newline at end of file diff --git a/extensions/intellij/src/main/resources/config_schema.json b/extensions/intellij/src/main/resources/config_schema.json index 4bb201b3e..b942e38bb 100644 --- a/extensions/intellij/src/main/resources/config_schema.json +++ b/extensions/intellij/src/main/resources/config_schema.json @@ -258,7 +258,8 @@ "ap-southeast-1", "ap-northeast-1", "ap-south-1" - ] + ], + "type" : "string" }, { "type": "string" @@ -2197,24 +2198,7 @@ "region": { "title": "Region", "description": "The region where the model is hosted", - "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" - } - ] + "$ref": "#/definitions/ModelDescription/properties/region" }, "profile": { "title": "Profile", diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index 4bb201b3e..b942e38bb 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -258,7 +258,8 @@ "ap-southeast-1", "ap-northeast-1", "ap-south-1" - ] + ], + "type" : "string" }, { "type": "string" @@ -2197,24 +2198,7 @@ "region": { "title": "Region", "description": "The region where the model is hosted", - "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" - } - ] + "$ref": "#/definitions/ModelDescription/properties/region" }, "profile": { "title": "Profile", From d2d2c1e058059648f5c7050bbbfee942e98e91f4 Mon Sep 17 00:00:00 2001 From: kbreese-x Date: Tue, 8 Oct 2024 11:37:58 -0700 Subject: [PATCH 3/3] config and build artifacts --- binary/package-lock.json | 1 + docs/static/schemas/config.json | 59 ++++++++++++++++++-- extensions/vscode/continue_rc_schema.json | 65 +++++++++++++++++++++-- extensions/vscode/package-lock.json | 4 +- extensions/vscode/package.json | 2 +- 5 files changed, 122 insertions(+), 9 deletions(-) diff --git a/binary/package-lock.json b/binary/package-lock.json index 5f1410c1a..010492e82 100644 --- a/binary/package-lock.json +++ b/binary/package-lock.json @@ -74,6 +74,7 @@ "jsdom": "^24.0.0", "launchdarkly-node-client-sdk": "^3.2.0", "llm-code-highlighter": "^0.0.14", + "lru-cache": "^10.2.2", "mac-ca": "^3.1.0", "node-fetch": "^3.3.2", "node-html-markdown": "^1.3.0", diff --git a/docs/static/schemas/config.json b/docs/static/schemas/config.json index 3f340f6ce..4bb201b3e 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", @@ -2145,7 +2162,8 @@ "gemini", "voyage", "nvidia", - "bedrock" + "bedrock", + "sagemaker" ] }, "model": { @@ -2179,7 +2197,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", @@ -2201,6 +2236,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 26877d8a3..cc747a337 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", @@ -2391,7 +2408,8 @@ "gemini", "voyage", "nvidia", - "bedrock" + "bedrock", + "sagemaker" ] }, "model": { @@ -2425,7 +2443,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", @@ -2458,6 +2493,30 @@ "apiKey" ] } + }, + { + "if": { + "properties": { + "provider": { + "enum": [ + "sagemaker" + ] + } + }, + "required": [ + "provider" + ] + }, + "then": { + "properties": { + "model": { + "markdownDescription": "SageMaker endpoint name" + } + }, + "required": [ + "model" + ] + } } ] }, diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 5bfd16377..bf3982055 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -1,12 +1,12 @@ { "name": "continue", - "version": "0.9.214", + "version": "0.9.215", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "continue", - "version": "0.9.214", + "version": "0.9.215", "license": "Apache-2.0", "dependencies": { "@electron/rebuild": "^3.2.10", diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index 2269b08dc..197340faa 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -2,7 +2,7 @@ "name": "continue", "icon": "media/icon.png", "author": "Continue Dev, Inc", - "version": "0.9.214", + "version": "0.9.215", "repository": { "type": "git", "url": "https://github.com/continuedev/continue"