From d48eb2969f6a06e7d47e11ed5be3d57b9e94693b Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Mon, 13 May 2024 14:21:21 +0300 Subject: [PATCH 01/11] Init --- examples/src/embeddings/deepinfra.ts | 14 ++ .../src/embeddings/deepinfra.ts | 191 ++++++++++++++++++ .../embeddings/tests/deepinfra.int.test.ts | 34 ++++ 3 files changed, 239 insertions(+) create mode 100644 examples/src/embeddings/deepinfra.ts create mode 100644 libs/langchain-community/src/embeddings/deepinfra.ts create mode 100644 libs/langchain-community/src/embeddings/tests/deepinfra.int.test.ts diff --git a/examples/src/embeddings/deepinfra.ts b/examples/src/embeddings/deepinfra.ts new file mode 100644 index 000000000000..49b75993b308 --- /dev/null +++ b/examples/src/embeddings/deepinfra.ts @@ -0,0 +1,14 @@ +import { DeepInfraEmbeddings } from "@langchain/community/embeddings/deepinfra"; + + +const model = new DeepInfraEmbeddings({ + apiToken: process.env.DEEPINFRA_API_TOKEN!, + batchSize: 1024, // Default value + modelName: "sentence-transformers/all-mpnet-base-v2", // Default value +}); + +const {embeddings} = await model.embedQuery( + "Tell me a story about a dragon and a princess." +); +console.log({ embeddings }); + diff --git a/libs/langchain-community/src/embeddings/deepinfra.ts b/libs/langchain-community/src/embeddings/deepinfra.ts new file mode 100644 index 000000000000..26906c806f51 --- /dev/null +++ b/libs/langchain-community/src/embeddings/deepinfra.ts @@ -0,0 +1,191 @@ +import axios, {AxiosInstance} from "axios"; + +import { getEnvironmentVariable } from "@langchain/core/utils/env"; +import { Embeddings, EmbeddingsParams } from "@langchain/core/embeddings"; +import { chunkArray } from "@langchain/core/utils/chunk_array"; + +/** + * The default model name to use for generating embeddings. + */ +const DEFAULT_MODEL_NAME = "sentence-transformers/clip-ViT-B-32"; + +/** + * The default batch size to use for generating embeddings. + * This is limited by the DeepInfra API to a maximum of 1024. + */ +const DEFAULT_BATCH_SIZE = 1024; + +/** + * Environment variable name for the DeepInfra API token. + */ +const API_TOKEN_ENV_VAR = "DEEPINFRA_API_TOKEN"; + + +export interface DeepInfraEmbeddingsRequest { + inputs: string[]; + normalize?: boolean; + image?: string; + webhook?: string; +} + + +/** + * Input parameters for the DeepInfra embeddings + */ +export interface DeepInfraEmbeddingsParams extends EmbeddingsParams { + + /** + * The API token to use for authentication. + * If not provided, it will be read from the `DEEPINFRA_API_TOKEN` environment variable. + */ + apiToken?: string; + + /** + * The model ID to use for generating completions. + * Default: `sentence-transformers/clip-ViT-B-32` + */ + modelName?: string; + + /** + * The maximum number of texts to embed in a single request. This is + * limited by the DeepInfra API to a maximum of 1024. + */ + batchSize?: number; +} + +/** + * A class for generating embeddings using the Cohere API. + * @example + * ```typescript + * // Embed a query using the CohereEmbeddings class + * const model = new ChatOpenAI(); + * const res = await model.embedQuery( + * "What would be a good company name for a company that makes colorful socks?", + * ); + * console.log({ res }); + * ``` + */ +export class DeepInfraEmbeddings + extends Embeddings + implements DeepInfraEmbeddingsParams +{ + + private client: AxiosInstance; + + private readonly apiToken: string; + + private readonly batchSize: number; + + private readonly modelName: string; + + + /** + * Constructor for the CohereEmbeddings class. + * @param fields - An optional object with properties to configure the instance. + */ + constructor( + fields?: Partial & { + verbose?: boolean; + } + ) { + const fieldsWithDefaults = { + modelName: DEFAULT_MODEL_NAME, + batchSize: DEFAULT_BATCH_SIZE, + ...fields }; + + super(fieldsWithDefaults); + + const apiKey = + fieldsWithDefaults?.apiToken || getEnvironmentVariable(API_TOKEN_ENV_VAR); + + if (!apiKey) { + throw new Error("DeepInfra API token not found"); + } + + this.modelName = fieldsWithDefaults?.modelName ?? this.modelName; + this.batchSize = fieldsWithDefaults?.batchSize ?? this.batchSize; + this.apiToken = apiKey; + + } + + /** + * Generates embeddings for an array of texts. + * @param inputs - An array of strings to generate embeddings for. + * @returns A Promise that resolves to an array of embeddings. + */ + async embedDocuments(inputs: string[]): Promise { + await this.maybeInitClient(); + + const batches = chunkArray(inputs, this.batchSize) as string[][]; + + const batchRequests = batches.map((batch : string[]) => + this.embeddingWithRetry({ + inputs: batch, + }) + ); + + const batchResponses = await Promise.all(batchRequests); + + const embeddings: number[][] = []; + + for (let i = 0; i < batchResponses.length; i += 1) { + const batch = batches[i]; + const { body: batchResponse } = batchResponses[i]; + for (let j = 0; j < batch.length; j += 1) { + embeddings.push(batchResponse.embeddings[j]); + } + } + + return embeddings; + } + + /** + * Generates an embedding for a single text. + * @param text - A string to generate an embedding for. + * @returns A Promise that resolves to an array of numbers representing the embedding. + */ + async embedQuery(text: string): Promise { + await this.maybeInitClient(); + + const { body } = await this.embeddingWithRetry({ + inputs: [text], + }); + return body.embeddings[0]; + } + + /** + * Generates embeddings with retry capabilities. + * @param request - An object containing the request parameters for generating embeddings. + * @returns A Promise that resolves to the API response. + */ + private async embeddingWithRetry( + request: DeepInfraEmbeddingsRequest + ) { + this.maybeInitClient(); + return this.caller.call(this.client.post.bind(this.client,""), request); + } + + /** + * Initializes the DeepInfra client if it hasn't been initialized already. + */ + private maybeInitClient() { + if (!this.client) { + + this.client = axios.create({ + baseURL: `https://api.deepinfra.com/v1/inference/${this.modelName}`, + headers: { + Authorization: `Bearer ${this.apiToken}`, + ContentType: "application/json", + }, + }); + } + } + + /** @ignore */ + static async imports(): Promise<{ + }> { + // Axios has already been defined as dependency in the package.json + // so we can use it here without importing it. + return {}; + } +} diff --git a/libs/langchain-community/src/embeddings/tests/deepinfra.int.test.ts b/libs/langchain-community/src/embeddings/tests/deepinfra.int.test.ts new file mode 100644 index 000000000000..840f33a6170b --- /dev/null +++ b/libs/langchain-community/src/embeddings/tests/deepinfra.int.test.ts @@ -0,0 +1,34 @@ +import { test, expect } from "@jest/globals"; +import { DeepInfraEmbeddings } from "../deepinfra.js"; + +test("Test DeepInfraEmbeddings.embedQuery", async () => { + const embeddings = new DeepInfraEmbeddings(); + const res = await embeddings.embedQuery("Hello world"); + expect(typeof res[0]).toBe("number"); +}); + +test("Test DeepInfraEmbeddings.embedDocuments", async () => { + const embeddings = new DeepInfraEmbeddings(); + const res = await embeddings.embedDocuments(["Hello world", "Bye bye"]); + expect(res).toHaveLength(2); + expect(typeof res[0][0]).toBe("number"); + expect(typeof res[1][0]).toBe("number"); +}); + +test("Test DeepInfraEmbeddings concurrency", async () => { + const embeddings = new DeepInfraEmbeddings({ + batchSize: 1, + }); + const res = await embeddings.embedDocuments([ + "Hello world", + "Bye bye", + "Hello world", + "Bye bye", + "Hello world", + "Bye bye", + ]); + expect(res).toHaveLength(6); + expect(res.find((embedding) => typeof embedding[0] !== "number")).toBe( + undefined + ); +}); From f361789467997c3d0f45f16cebe722dc9af13ce4 Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Mon, 13 May 2024 15:52:54 +0300 Subject: [PATCH 02/11] fix(type errors) --- .../src/embeddings/deepinfra.ts | 49 +++++++++++++------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/libs/langchain-community/src/embeddings/deepinfra.ts b/libs/langchain-community/src/embeddings/deepinfra.ts index 26906c806f51..886797f2ab83 100644 --- a/libs/langchain-community/src/embeddings/deepinfra.ts +++ b/libs/langchain-community/src/embeddings/deepinfra.ts @@ -1,4 +1,4 @@ -import axios, {AxiosInstance} from "axios"; +import axios, {AxiosInstance, AxiosResponse} from "axios"; import { getEnvironmentVariable } from "@langchain/core/utils/env"; import { Embeddings, EmbeddingsParams } from "@langchain/core/embeddings"; @@ -53,6 +53,25 @@ export interface DeepInfraEmbeddingsParams extends EmbeddingsParams { batchSize?: number; } +/** + * Response from the DeepInfra embeddings API. + */ +export interface DeepInfraEmbeddingsResponse { + /** + * The embeddings generated for the input texts. + */ + embeddings: number[][]; + /** + * The number of tokens in the input texts. + */ + input_tokens: number; + /** + * The status of the inference. + */ + request_id?: string; +} + + /** * A class for generating embeddings using the Cohere API. * @example @@ -72,11 +91,11 @@ export class DeepInfraEmbeddings private client: AxiosInstance; - private readonly apiToken: string; + apiToken: string; - private readonly batchSize: number; + batchSize: number; - private readonly modelName: string; + modelName: string; /** @@ -126,17 +145,17 @@ export class DeepInfraEmbeddings const batchResponses = await Promise.all(batchRequests); - const embeddings: number[][] = []; + const out: number[][] = []; for (let i = 0; i < batchResponses.length; i += 1) { const batch = batches[i]; - const { body: batchResponse } = batchResponses[i]; + const { embeddings } = batchResponses[i]; for (let j = 0; j < batch.length; j += 1) { - embeddings.push(batchResponse.embeddings[j]); + out.push(embeddings[j]); } } - return embeddings; + return out; } /** @@ -147,10 +166,10 @@ export class DeepInfraEmbeddings async embedQuery(text: string): Promise { await this.maybeInitClient(); - const { body } = await this.embeddingWithRetry({ + const {embeddings} = await this.embeddingWithRetry({ inputs: [text], }); - return body.embeddings[0]; + return embeddings[0]; } /** @@ -160,9 +179,10 @@ export class DeepInfraEmbeddings */ private async embeddingWithRetry( request: DeepInfraEmbeddingsRequest - ) { + ): Promise { this.maybeInitClient(); - return this.caller.call(this.client.post.bind(this.client,""), request); + const response = await this.caller.call(this.client.post.bind(this.client,""), request); + return (response as AxiosResponse).data; } /** @@ -171,7 +191,7 @@ export class DeepInfraEmbeddings private maybeInitClient() { if (!this.client) { - this.client = axios.create({ + this.client = axios.default.create({ baseURL: `https://api.deepinfra.com/v1/inference/${this.modelName}`, headers: { Authorization: `Bearer ${this.apiToken}`, @@ -182,8 +202,7 @@ export class DeepInfraEmbeddings } /** @ignore */ - static async imports(): Promise<{ - }> { + static async imports(): Promise<{}> { // Axios has already been defined as dependency in the package.json // so we can use it here without importing it. return {}; From 09b9fee04066aac0a8ef1eafad5f9f2392fe46d1 Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Tue, 14 May 2024 18:26:57 +0300 Subject: [PATCH 03/11] feat(deepinfra embeddings) --- libs/langchain-community/.gitignore | 4 ++++ libs/langchain-community/langchain.config.js | 1 + libs/langchain-community/package.json | 13 +++++++++++++ 3 files changed, 18 insertions(+) diff --git a/libs/langchain-community/.gitignore b/libs/langchain-community/.gitignore index 15955d6874cd..1b63fb99048e 100644 --- a/libs/langchain-community/.gitignore +++ b/libs/langchain-community/.gitignore @@ -138,6 +138,10 @@ embeddings/cohere.cjs embeddings/cohere.js embeddings/cohere.d.ts embeddings/cohere.d.cts +embeddings/deepinfra.cjs +embeddings/deepinfra.js +embeddings/deepinfra.d.ts +embeddings/deepinfra.d.cts embeddings/fireworks.cjs embeddings/fireworks.js embeddings/fireworks.d.ts diff --git a/libs/langchain-community/langchain.config.js b/libs/langchain-community/langchain.config.js index 710b2bdb1eb1..21c2a703b7f8 100644 --- a/libs/langchain-community/langchain.config.js +++ b/libs/langchain-community/langchain.config.js @@ -64,6 +64,7 @@ export const config = { "embeddings/bedrock": "embeddings/bedrock", "embeddings/cloudflare_workersai": "embeddings/cloudflare_workersai", "embeddings/cohere": "embeddings/cohere", + "embeddings/deepinfra": "embeddings/deepinfra", "embeddings/fireworks": "embeddings/fireworks", "embeddings/googlepalm": "embeddings/googlepalm", "embeddings/googlevertexai": "embeddings/googlevertexai", diff --git a/libs/langchain-community/package.json b/libs/langchain-community/package.json index c340929a2b2e..7dad394b1639 100644 --- a/libs/langchain-community/package.json +++ b/libs/langchain-community/package.json @@ -873,6 +873,15 @@ "import": "./embeddings/cohere.js", "require": "./embeddings/cohere.cjs" }, + "./embeddings/deepinfra": { + "types": { + "import": "./embeddings/deepinfra.d.ts", + "require": "./embeddings/deepinfra.d.cts", + "default": "./embeddings/deepinfra.d.ts" + }, + "import": "./embeddings/deepinfra.js", + "require": "./embeddings/deepinfra.cjs" + }, "./embeddings/fireworks": { "types": { "import": "./embeddings/fireworks.d.ts", @@ -2448,6 +2457,10 @@ "embeddings/cohere.js", "embeddings/cohere.d.ts", "embeddings/cohere.d.cts", + "embeddings/deepinfra.cjs", + "embeddings/deepinfra.js", + "embeddings/deepinfra.d.ts", + "embeddings/deepinfra.d.cts", "embeddings/fireworks.cjs", "embeddings/fireworks.js", "embeddings/fireworks.d.ts", From c778987451c43ecead5c154e62a3abc0daa78bb5 Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Wed, 15 May 2024 13:36:46 +0300 Subject: [PATCH 04/11] fix(default model) --- examples/src/embeddings/deepinfra.ts | 6 +++--- .../src/embeddings/tests/deepinfra.int.test.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/src/embeddings/deepinfra.ts b/examples/src/embeddings/deepinfra.ts index 49b75993b308..c28838f3b1c1 100644 --- a/examples/src/embeddings/deepinfra.ts +++ b/examples/src/embeddings/deepinfra.ts @@ -4,11 +4,11 @@ import { DeepInfraEmbeddings } from "@langchain/community/embeddings/deepinfra"; const model = new DeepInfraEmbeddings({ apiToken: process.env.DEEPINFRA_API_TOKEN!, batchSize: 1024, // Default value - modelName: "sentence-transformers/all-mpnet-base-v2", // Default value + modelName: "sentence-transformers/clip-ViT-B-32", // Default value }); -const {embeddings} = await model.embedQuery( +const embeddings = await model.embedQuery( "Tell me a story about a dragon and a princess." ); -console.log({ embeddings }); +console.log(embeddings); diff --git a/libs/langchain-community/src/embeddings/tests/deepinfra.int.test.ts b/libs/langchain-community/src/embeddings/tests/deepinfra.int.test.ts index 840f33a6170b..d7e5f7159cc3 100644 --- a/libs/langchain-community/src/embeddings/tests/deepinfra.int.test.ts +++ b/libs/langchain-community/src/embeddings/tests/deepinfra.int.test.ts @@ -22,10 +22,10 @@ test("Test DeepInfraEmbeddings concurrency", async () => { const res = await embeddings.embedDocuments([ "Hello world", "Bye bye", - "Hello world", - "Bye bye", - "Hello world", - "Bye bye", + "we need", + "at least", + "six documents", + "to test concurrency" ]); expect(res).toHaveLength(6); expect(res.find((embedding) => typeof embedding[0] !== "number")).toBe( From 945f7b9dba33791c993a672a28ead5d6279a7d38 Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Wed, 22 May 2024 14:06:08 +0300 Subject: [PATCH 05/11] fix(deepinfra): axios is removed --- examples/src/embeddings/deepinfra.ts | 2 +- .../src/embeddings/deepinfra.ts | 61 +++++-------------- 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/examples/src/embeddings/deepinfra.ts b/examples/src/embeddings/deepinfra.ts index c28838f3b1c1..e8db9b9694bf 100644 --- a/examples/src/embeddings/deepinfra.ts +++ b/examples/src/embeddings/deepinfra.ts @@ -2,7 +2,7 @@ import { DeepInfraEmbeddings } from "@langchain/community/embeddings/deepinfra"; const model = new DeepInfraEmbeddings({ - apiToken: process.env.DEEPINFRA_API_TOKEN!, + apiToken: process.env.DEEPINFRA_API_TOKEN, batchSize: 1024, // Default value modelName: "sentence-transformers/clip-ViT-B-32", // Default value }); diff --git a/libs/langchain-community/src/embeddings/deepinfra.ts b/libs/langchain-community/src/embeddings/deepinfra.ts index 886797f2ab83..1e20d9cc8c21 100644 --- a/libs/langchain-community/src/embeddings/deepinfra.ts +++ b/libs/langchain-community/src/embeddings/deepinfra.ts @@ -1,5 +1,3 @@ -import axios, {AxiosInstance, AxiosResponse} from "axios"; - import { getEnvironmentVariable } from "@langchain/core/utils/env"; import { Embeddings, EmbeddingsParams } from "@langchain/core/embeddings"; import { chunkArray } from "@langchain/core/utils/chunk_array"; @@ -20,7 +18,6 @@ const DEFAULT_BATCH_SIZE = 1024; */ const API_TOKEN_ENV_VAR = "DEEPINFRA_API_TOKEN"; - export interface DeepInfraEmbeddingsRequest { inputs: string[]; normalize?: boolean; @@ -28,12 +25,10 @@ export interface DeepInfraEmbeddingsRequest { webhook?: string; } - /** * Input parameters for the DeepInfra embeddings */ export interface DeepInfraEmbeddingsParams extends EmbeddingsParams { - /** * The API token to use for authentication. * If not provided, it will be read from the `DEEPINFRA_API_TOKEN` environment variable. @@ -71,13 +66,12 @@ export interface DeepInfraEmbeddingsResponse { request_id?: string; } - /** - * A class for generating embeddings using the Cohere API. + * A class for generating embeddings using the DeepInfra API. * @example * ```typescript - * // Embed a query using the CohereEmbeddings class - * const model = new ChatOpenAI(); + * // Embed a query using the DeepInfraEmbeddings class + * const model = new DeepInfraEmbeddings(); * const res = await model.embedQuery( * "What would be a good company name for a company that makes colorful socks?", * ); @@ -88,18 +82,14 @@ export class DeepInfraEmbeddings extends Embeddings implements DeepInfraEmbeddingsParams { - - private client: AxiosInstance; - apiToken: string; batchSize: number; modelName: string; - /** - * Constructor for the CohereEmbeddings class. + * Constructor for the DeepInfraEmbeddings class. * @param fields - An optional object with properties to configure the instance. */ constructor( @@ -110,7 +100,8 @@ export class DeepInfraEmbeddings const fieldsWithDefaults = { modelName: DEFAULT_MODEL_NAME, batchSize: DEFAULT_BATCH_SIZE, - ...fields }; + ...fields, + }; super(fieldsWithDefaults); @@ -124,7 +115,6 @@ export class DeepInfraEmbeddings this.modelName = fieldsWithDefaults?.modelName ?? this.modelName; this.batchSize = fieldsWithDefaults?.batchSize ?? this.batchSize; this.apiToken = apiKey; - } /** @@ -133,11 +123,9 @@ export class DeepInfraEmbeddings * @returns A Promise that resolves to an array of embeddings. */ async embedDocuments(inputs: string[]): Promise { - await this.maybeInitClient(); - const batches = chunkArray(inputs, this.batchSize) as string[][]; - const batchRequests = batches.map((batch : string[]) => + const batchRequests = batches.map((batch: string[]) => this.embeddingWithRetry({ inputs: batch, }) @@ -164,9 +152,7 @@ export class DeepInfraEmbeddings * @returns A Promise that resolves to an array of numbers representing the embedding. */ async embedQuery(text: string): Promise { - await this.maybeInitClient(); - - const {embeddings} = await this.embeddingWithRetry({ + const { embeddings } = await this.embeddingWithRetry({ inputs: [text], }); return embeddings[0]; @@ -180,31 +166,16 @@ export class DeepInfraEmbeddings private async embeddingWithRetry( request: DeepInfraEmbeddingsRequest ): Promise { - this.maybeInitClient(); - const response = await this.caller.call(this.client.post.bind(this.client,""), request); - return (response as AxiosResponse).data; - } - - /** - * Initializes the DeepInfra client if it hasn't been initialized already. - */ - private maybeInitClient() { - if (!this.client) { - - this.client = axios.default.create({ - baseURL: `https://api.deepinfra.com/v1/inference/${this.modelName}`, + const response = await this.caller.call(() => + fetch(`https://api.deepinfra.com/v1/inference/${this.modelName}`, { + method: "POST", headers: { Authorization: `Bearer ${this.apiToken}`, - ContentType: "application/json", + "Content-Type": "application/json", }, - }); - } - } - - /** @ignore */ - static async imports(): Promise<{}> { - // Axios has already been defined as dependency in the package.json - // so we can use it here without importing it. - return {}; + body: JSON.stringify(request), + }).then((res) => res.json()) + ); + return response as DeepInfraEmbeddingsResponse; } } From 87e5977de62b37d61dbef314ba9ec9b82d76f053 Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Wed, 22 May 2024 14:08:12 +0300 Subject: [PATCH 06/11] ref(deepinfra): remove redundant cast --- libs/langchain-community/src/embeddings/deepinfra.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/langchain-community/src/embeddings/deepinfra.ts b/libs/langchain-community/src/embeddings/deepinfra.ts index 1e20d9cc8c21..6963902351a9 100644 --- a/libs/langchain-community/src/embeddings/deepinfra.ts +++ b/libs/langchain-community/src/embeddings/deepinfra.ts @@ -123,7 +123,7 @@ export class DeepInfraEmbeddings * @returns A Promise that resolves to an array of embeddings. */ async embedDocuments(inputs: string[]): Promise { - const batches = chunkArray(inputs, this.batchSize) as string[][]; + const batches = chunkArray(inputs, this.batchSize); const batchRequests = batches.map((batch: string[]) => this.embeddingWithRetry({ From d50c600bc1ec728945681366f0dc7d9a8b9fdb26 Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Thu, 23 May 2024 16:41:01 +0300 Subject: [PATCH 07/11] format(deepinfra) --- examples/src/embeddings/deepinfra.ts | 2 -- .../src/embeddings/tests/deepinfra.int.test.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/src/embeddings/deepinfra.ts b/examples/src/embeddings/deepinfra.ts index e8db9b9694bf..ed48eaf6a3b8 100644 --- a/examples/src/embeddings/deepinfra.ts +++ b/examples/src/embeddings/deepinfra.ts @@ -1,6 +1,5 @@ import { DeepInfraEmbeddings } from "@langchain/community/embeddings/deepinfra"; - const model = new DeepInfraEmbeddings({ apiToken: process.env.DEEPINFRA_API_TOKEN, batchSize: 1024, // Default value @@ -11,4 +10,3 @@ const embeddings = await model.embedQuery( "Tell me a story about a dragon and a princess." ); console.log(embeddings); - diff --git a/libs/langchain-community/src/embeddings/tests/deepinfra.int.test.ts b/libs/langchain-community/src/embeddings/tests/deepinfra.int.test.ts index d7e5f7159cc3..748bc1dc6dc9 100644 --- a/libs/langchain-community/src/embeddings/tests/deepinfra.int.test.ts +++ b/libs/langchain-community/src/embeddings/tests/deepinfra.int.test.ts @@ -25,7 +25,7 @@ test("Test DeepInfraEmbeddings concurrency", async () => { "we need", "at least", "six documents", - "to test concurrency" + "to test concurrency", ]); expect(res).toHaveLength(6); expect(res.find((embedding) => typeof embedding[0] !== "number")).toBe( From 76242e86d97e6f3bdd3cff0f8286e00dd0745b2f Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Fri, 24 May 2024 14:12:41 +0300 Subject: [PATCH 08/11] doc(deepinfra) --- examples/src/models/embeddings/deepinfra.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 examples/src/models/embeddings/deepinfra.ts diff --git a/examples/src/models/embeddings/deepinfra.ts b/examples/src/models/embeddings/deepinfra.ts new file mode 100644 index 000000000000..ed48eaf6a3b8 --- /dev/null +++ b/examples/src/models/embeddings/deepinfra.ts @@ -0,0 +1,12 @@ +import { DeepInfraEmbeddings } from "@langchain/community/embeddings/deepinfra"; + +const model = new DeepInfraEmbeddings({ + apiToken: process.env.DEEPINFRA_API_TOKEN, + batchSize: 1024, // Default value + modelName: "sentence-transformers/clip-ViT-B-32", // Default value +}); + +const embeddings = await model.embedQuery( + "Tell me a story about a dragon and a princess." +); +console.log(embeddings); From 29707e7e26ee9b2881ec6e6f0cd97f4a067700dc Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Tue, 28 May 2024 14:26:47 +0300 Subject: [PATCH 09/11] doc(deepinfra) --- .../integrations/text_embedding/deepinfra.mdx | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 docs/core_docs/docs/integrations/text_embedding/deepinfra.mdx diff --git a/docs/core_docs/docs/integrations/text_embedding/deepinfra.mdx b/docs/core_docs/docs/integrations/text_embedding/deepinfra.mdx new file mode 100644 index 000000000000..ddcd0e5c5ef7 --- /dev/null +++ b/docs/core_docs/docs/integrations/text_embedding/deepinfra.mdx @@ -0,0 +1,128 @@ +--- +sidebar_label: DeepInfra +--- + + +# DeepInfra Embeddings + +The `DeepInfraEmbeddings` class utilizes the DeepInfra API to generate embeddings for given text inputs. This guide will walk you through the setup and usage of the `DeepInfraEmbeddings` class, helping you integrate it into your project seamlessly. + +## Installation + +Install the `@langchain/community` package as shown below: + +import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx"; + + + +## Initialization + +With this integration, you can use the DeepInfra embeddings model to get embeddings for your text data. Here is the [link](https://deepinfra.com/models/embeddings) to the embeddings models. + +First, you need to sign up on the DeepInfra website and get the API token from [here](https://deepinfra.com/dash/api_keys). You can copy names from the model cards and start using them in your code. + +```bash npm2yarn + +To use the `DeepInfraEmbeddings` class, you need an API token from DeepInfra. You can pass this token directly to the constructor or set it as an environment variable (`DEEPINFRA_API_TOKEN`). + + + +### Basic Usage + +Here’s how to create an instance of `DeepInfraEmbeddings`: + +```typescript +import { DeepInfraEmbeddings } from "@langchain/community/embeddings/deepinfra"; + +const embeddings = new DeepInfraEmbeddings({ + apiToken: "YOUR_API_TOKEN", + modelName: "sentence-transformers/clip-ViT-B-32", // Optional, defaults to "sentence-transformers/clip-ViT-B-32" + batchSize: 1024, // Optional, defaults to 1024 +}); +``` + +If the `apiToken` is not provided, it will be read from the `DEEPINFRA_API_TOKEN` environment variable. + +## Generating Embeddings + +### Embedding a Single Query + +To generate embeddings for a single text query, use the `embedQuery` method: + +```typescript +const embedding = await embeddings.embedQuery("What would be a good company name for a company that makes colorful socks?"); +console.log(embedding); +``` + +### Embedding Multiple Documents + +To generate embeddings for multiple documents, use the `embedDocuments` method. This method will handle batching automatically based on the `batchSize` parameter: + +```typescript +const documents = [ + "Document 1 text...", + "Document 2 text...", + "Document 3 text...", +]; + +const embeddingsArray = await embeddings.embedDocuments(documents); +console.log(embeddingsArray); +``` + +## Customizing Requests + +You can customize the base URL the SDK sends requests to by passing a `configuration` parameter: + +```typescript +const customEmbeddings = new DeepInfraEmbeddings({ + apiToken: "YOUR_API_TOKEN", + configuration: { + baseURL: "https://your_custom_url.com", + }, +}); +``` + +This allows you to route requests through a custom endpoint if needed. + +## Error Handling + +If the API token is not provided and cannot be found in the environment variables, an error will be thrown: + +```typescript +try { + const embeddings = new DeepInfraEmbeddings(); +} catch (error) { + console.error("DeepInfra API token not found"); +} +``` + +## Example + +Here’s a complete example of how to set up and use the `DeepInfraEmbeddings` class: + +```typescript +import { DeepInfraEmbeddings } from "@langchain/community/embeddings/deepinfra"; + +const embeddings = new DeepInfraEmbeddings({ + apiToken: "YOUR_API_TOKEN", + modelName: "sentence-transformers/clip-ViT-B-32", + batchSize: 512, +}); + +async function runExample() { + const queryEmbedding = await embeddings.embedQuery("Example query text."); + console.log("Query Embedding:", queryEmbedding); + + const documents = ["Text 1", "Text 2", "Text 3"]; + const documentEmbeddings = await embeddings.embedDocuments(documents); + console.log("Document Embeddings:", documentEmbeddings); +} + +runExample(); +``` + +## Feedback and Support + +For feedback or questions, please contact [feedback@deepinfra.com](mailto:feedback@deepinfra.com). + + From e495ed1e4b5afaf8aece8f80a25ee5425a29b8fe Mon Sep 17 00:00:00 2001 From: Jacob Lee Date: Fri, 31 May 2024 14:25:36 -0700 Subject: [PATCH 10/11] Update deepinfra.mdx --- .../docs/integrations/text_embedding/deepinfra.mdx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/core_docs/docs/integrations/text_embedding/deepinfra.mdx b/docs/core_docs/docs/integrations/text_embedding/deepinfra.mdx index ddcd0e5c5ef7..ebcc7d88db31 100644 --- a/docs/core_docs/docs/integrations/text_embedding/deepinfra.mdx +++ b/docs/core_docs/docs/integrations/text_embedding/deepinfra.mdx @@ -15,14 +15,16 @@ import IntegrationInstallTooltip from "@mdx_components/integration_install_toolt +```bash npm2yarn +npm i @langchain/community +``` + ## Initialization With this integration, you can use the DeepInfra embeddings model to get embeddings for your text data. Here is the [link](https://deepinfra.com/models/embeddings) to the embeddings models. First, you need to sign up on the DeepInfra website and get the API token from [here](https://deepinfra.com/dash/api_keys). You can copy names from the model cards and start using them in your code. -```bash npm2yarn - To use the `DeepInfraEmbeddings` class, you need an API token from DeepInfra. You can pass this token directly to the constructor or set it as an environment variable (`DEEPINFRA_API_TOKEN`). From ce10418b6e5d3995715f88029b675399d811dfba Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Fri, 31 May 2024 14:37:05 -0700 Subject: [PATCH 11/11] Format --- .../docs/integrations/text_embedding/deepinfra.mdx | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/docs/core_docs/docs/integrations/text_embedding/deepinfra.mdx b/docs/core_docs/docs/integrations/text_embedding/deepinfra.mdx index ebcc7d88db31..34b6b942bd7a 100644 --- a/docs/core_docs/docs/integrations/text_embedding/deepinfra.mdx +++ b/docs/core_docs/docs/integrations/text_embedding/deepinfra.mdx @@ -2,7 +2,6 @@ sidebar_label: DeepInfra --- - # DeepInfra Embeddings The `DeepInfraEmbeddings` class utilizes the DeepInfra API to generate embeddings for given text inputs. This guide will walk you through the setup and usage of the `DeepInfraEmbeddings` class, helping you integrate it into your project seamlessly. @@ -27,8 +26,6 @@ First, you need to sign up on the DeepInfra website and get the API token from [ To use the `DeepInfraEmbeddings` class, you need an API token from DeepInfra. You can pass this token directly to the constructor or set it as an environment variable (`DEEPINFRA_API_TOKEN`). - - ### Basic Usage Here’s how to create an instance of `DeepInfraEmbeddings`: @@ -52,7 +49,9 @@ If the `apiToken` is not provided, it will be read from the `DEEPINFRA_API_TOKEN To generate embeddings for a single text query, use the `embedQuery` method: ```typescript -const embedding = await embeddings.embedQuery("What would be a good company name for a company that makes colorful socks?"); +const embedding = await embeddings.embedQuery( + "What would be a good company name for a company that makes colorful socks?" +); console.log(embedding); ``` @@ -126,5 +125,3 @@ runExample(); ## Feedback and Support For feedback or questions, please contact [feedback@deepinfra.com](mailto:feedback@deepinfra.com). - -