From d48eb2969f6a06e7d47e11ed5be3d57b9e94693b Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Mon, 13 May 2024 14:21:21 +0300 Subject: [PATCH 01/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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). - - From fafea9b590a62540284b937c20c8f03cf2f73879 Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Wed, 5 Jun 2024 13:23:54 +0300 Subject: [PATCH 12/20] feat(deepinfra): implement llm and chat. --- examples/src/llms/deepinfra.ts | 21 ++ .../src/models/chat/integration_deepinfra.ts | 18 ++ examples/src/models/llm/deepinfra.ts | 15 ++ libs/langchain-community/.gitignore | 8 + libs/langchain-community/langchain.config.js | 2 + libs/langchain-community/package.json | 26 +++ .../src/chat_models/deepinfra.ts | 202 ++++++++++++++++++ .../tests/chatdeepinfra.int.test.ts | 19 ++ .../langchain-community/src/llms/deepinfra.ts | 76 +++++++ .../src/llms/tests/deepinfra.int.test.ts | 8 + 10 files changed, 395 insertions(+) create mode 100644 examples/src/llms/deepinfra.ts create mode 100644 examples/src/models/chat/integration_deepinfra.ts create mode 100644 examples/src/models/llm/deepinfra.ts create mode 100644 libs/langchain-community/src/chat_models/deepinfra.ts create mode 100644 libs/langchain-community/src/chat_models/tests/chatdeepinfra.int.test.ts create mode 100644 libs/langchain-community/src/llms/deepinfra.ts create mode 100644 libs/langchain-community/src/llms/tests/deepinfra.int.test.ts diff --git a/examples/src/llms/deepinfra.ts b/examples/src/llms/deepinfra.ts new file mode 100644 index 000000000000..43c0e6cd843b --- /dev/null +++ b/examples/src/llms/deepinfra.ts @@ -0,0 +1,21 @@ +import { DeepInfraLLM } from "@langchain/community/llms/deepinfra"; + +const apiKey = process.env.DEEPINFRA_API_TOKEN; +const model = "meta-llama/Meta-Llama-3-70B-Instruct"; + + +export const run = async () => { + + + const llm = new DeepInfraLLM({ + temperature: 0.7, + maxTokens: 20, + model, + apiKey, + maxRetries: 5, + }); + const res = await llm.invoke( + "Question: What is the next step in the process of making a good game?\nAnswer:" + ); + console.log({ res }); +}; diff --git a/examples/src/models/chat/integration_deepinfra.ts b/examples/src/models/chat/integration_deepinfra.ts new file mode 100644 index 000000000000..c13ed5a6b550 --- /dev/null +++ b/examples/src/models/chat/integration_deepinfra.ts @@ -0,0 +1,18 @@ +import { ChatDeepInfra } from "@langchain/community/chat_models/deepinfra"; +import { HumanMessage } from "@langchain/core/messages"; + +const apiKey = process.env.DEEPINFRA_API_TOKEN; + +const model = "gpt-3.5-turbo"; + +const chat = new ChatDeepInfra({ + model, + apiKey +}); + +const messages = [new HumanMessage("Hello")]; + + +chat.invoke(messages).then((response: any) => { + console.log(response); +}); diff --git a/examples/src/models/llm/deepinfra.ts b/examples/src/models/llm/deepinfra.ts new file mode 100644 index 000000000000..249905abbac4 --- /dev/null +++ b/examples/src/models/llm/deepinfra.ts @@ -0,0 +1,15 @@ +import { DeepInfraLLM } from "@langchain/community/llms/deepinfra"; + + +const apiKey = process.env.DEEPINFRA_API_TOKEN; +const model = "meta-llama/Meta-Llama-3-70B-Instruct"; + +const llm = new DeepInfraLLM({ + maxTokens: 20, + model, + apiKey, +}); +const res = await llm.invoke( + "What is the next step in the process of making a good game?" +); +console.log({ res }); diff --git a/libs/langchain-community/.gitignore b/libs/langchain-community/.gitignore index bad275d50cf4..585ec0c0f9df 100644 --- a/libs/langchain-community/.gitignore +++ b/libs/langchain-community/.gitignore @@ -234,6 +234,10 @@ llms/cohere.cjs llms/cohere.js llms/cohere.d.ts llms/cohere.d.cts +llms/deepinfra.cjs +llms/deepinfra.js +llms/deepinfra.d.ts +llms/deepinfra.d.cts llms/fireworks.cjs llms/fireworks.js llms/fireworks.d.ts @@ -510,6 +514,10 @@ chat_models/cloudflare_workersai.cjs chat_models/cloudflare_workersai.js chat_models/cloudflare_workersai.d.ts chat_models/cloudflare_workersai.d.cts +chat_models/deepinfra.cjs +chat_models/deepinfra.js +chat_models/deepinfra.d.ts +chat_models/deepinfra.d.cts chat_models/fireworks.cjs chat_models/fireworks.js chat_models/fireworks.d.ts diff --git a/libs/langchain-community/langchain.config.js b/libs/langchain-community/langchain.config.js index d5c4c6b15eb3..53b32cfb4dd3 100644 --- a/libs/langchain-community/langchain.config.js +++ b/libs/langchain-community/langchain.config.js @@ -93,6 +93,7 @@ export const config = { "llms/bedrock/web": "llms/bedrock/web", "llms/cloudflare_workersai": "llms/cloudflare_workersai", "llms/cohere": "llms/cohere", + "llms/deepinfra": "llms/deepinfra", "llms/fireworks": "llms/fireworks", "llms/friendli": "llms/friendli", "llms/googlepalm": "llms/googlepalm", @@ -164,6 +165,7 @@ export const config = { "chat_models/bedrock": "chat_models/bedrock/index", "chat_models/bedrock/web": "chat_models/bedrock/web", "chat_models/cloudflare_workersai": "chat_models/cloudflare_workersai", + "chat_models/deepinfra": "chat_models/deepinfra", "chat_models/fireworks": "chat_models/fireworks", "chat_models/friendli": "chat_models/friendli", "chat_models/googlevertexai": "chat_models/googlevertexai/index", diff --git a/libs/langchain-community/package.json b/libs/langchain-community/package.json index ee6b54930c36..3b06fd302b5e 100644 --- a/libs/langchain-community/package.json +++ b/libs/langchain-community/package.json @@ -1235,6 +1235,15 @@ "import": "./llms/cohere.js", "require": "./llms/cohere.cjs" }, + "./llms/deepinfra": { + "types": { + "import": "./llms/deepinfra.d.ts", + "require": "./llms/deepinfra.d.cts", + "default": "./llms/deepinfra.d.ts" + }, + "import": "./llms/deepinfra.js", + "require": "./llms/deepinfra.cjs" + }, "./llms/fireworks": { "types": { "import": "./llms/fireworks.d.ts", @@ -1856,6 +1865,15 @@ "import": "./chat_models/cloudflare_workersai.js", "require": "./chat_models/cloudflare_workersai.cjs" }, + "./chat_models/deepinfra": { + "types": { + "import": "./chat_models/deepinfra.d.ts", + "require": "./chat_models/deepinfra.d.cts", + "default": "./chat_models/deepinfra.d.ts" + }, + "import": "./chat_models/deepinfra.js", + "require": "./chat_models/deepinfra.cjs" + }, "./chat_models/fireworks": { "types": { "import": "./chat_models/fireworks.d.ts", @@ -3239,6 +3257,10 @@ "llms/cohere.js", "llms/cohere.d.ts", "llms/cohere.d.cts", + "llms/deepinfra.cjs", + "llms/deepinfra.js", + "llms/deepinfra.d.ts", + "llms/deepinfra.d.cts", "llms/fireworks.cjs", "llms/fireworks.js", "llms/fireworks.d.ts", @@ -3515,6 +3537,10 @@ "chat_models/cloudflare_workersai.js", "chat_models/cloudflare_workersai.d.ts", "chat_models/cloudflare_workersai.d.cts", + "chat_models/deepinfra.cjs", + "chat_models/deepinfra.js", + "chat_models/deepinfra.d.ts", + "chat_models/deepinfra.d.cts", "chat_models/fireworks.cjs", "chat_models/fireworks.js", "chat_models/fireworks.d.ts", diff --git a/libs/langchain-community/src/chat_models/deepinfra.ts b/libs/langchain-community/src/chat_models/deepinfra.ts new file mode 100644 index 000000000000..aaddb1dcfb19 --- /dev/null +++ b/libs/langchain-community/src/chat_models/deepinfra.ts @@ -0,0 +1,202 @@ +import { + BaseChatModel, + type BaseChatModelParams, +} from "@langchain/core/language_models/chat_models"; +import { + AIMessage, + type BaseMessage, +} from "@langchain/core/messages"; +import { type ChatResult } from "@langchain/core/outputs"; +import { getEnvironmentVariable } from "@langchain/core/utils/env"; + +export const DEFAULT_MODEL = "meta-llama/Meta-Llama-3-70B-Instruct"; + +export type DeepInfraMessageRole = "system" | "assistant" | "user"; + +export const API_BASE_URL = "https://api.deepinfra.com/v1/openai/chat/completions"; + +export const ENV_VARIABLE_API_KEY = "DEEPINFRA_API_TOKEN"; + +interface DeepInfraMessage { + role: DeepInfraMessageRole; + content: string; +} + +interface ChatCompletionRequest { + model: string; + messages?: DeepInfraMessage[]; + stream?: boolean; + max_tokens?: number | null; + temperature?: number | null; +} + +interface BaseResponse { + code?: string; + message?: string; +} + +interface ChoiceMessage { + role: string; + content: string; +} + +interface ResponseChoice { + index: number; + finish_reason: "stop" | "length" | "null" | null; + delta: ChoiceMessage; + message: ChoiceMessage; +} + +interface ChatCompletionResponse extends BaseResponse { + choices: ResponseChoice[]; + usage: { + completion_tokens: number; + prompt_tokens: number; + total_tokens: number; + }; + output: { + text: string; + finish_reason: "stop" | "length" | "null" | null; + }; +} + +export interface ChatDeepInfraParams { + model: string; + apiKey?: string; + temperature?: number; + maxTokens?: number; +} + +function messageToRole(message: BaseMessage): DeepInfraMessageRole { + const type = message._getType(); + switch (type) { + case "ai": + return "assistant"; + case "human": + return "user"; + case "system": + return "system"; + default: + throw new Error(`Unknown message type: ${type}`); + } +} + +export class ChatDeepInfra extends BaseChatModel implements ChatDeepInfraParams { + static lc_name() { + return "ChatDeepInfra"; + } + + get callKeys() { + return ["stop", "signal", "options"]; + } + + apiKey?: string; + model: string; + apiUrl: string; + maxTokens?: number; + temperature?: number; + + constructor(fields: Partial & BaseChatModelParams = {}) { + super(fields); + + this.apiKey = fields?.apiKey ?? getEnvironmentVariable(ENV_VARIABLE_API_KEY); + if (!this.apiKey) { + throw new Error("API key is required, set `DEEPINFRA_API_TOKEN` environment variable or pass it as a parameter"); + } + + this.apiUrl = API_BASE_URL; + this.model = fields.model ?? DEFAULT_MODEL; + this.temperature = fields.temperature ?? 0; + this.maxTokens = fields.maxTokens; + } + + invocationParams(): Omit { + return { + model: this.model, + stream: false, + temperature: this.temperature, + max_tokens: this.maxTokens, + }; + } + + identifyingParams(): Omit { + return this.invocationParams(); + } + + async _generate( + messages: BaseMessage[], + options?: this["ParsedCallOptions"], + ): Promise { + const parameters = this.invocationParams(); + + const messagesMapped: DeepInfraMessage[] = messages.map((message) => ({ + role: messageToRole(message), + content: message.content as string, + })); + + const data = await this.completionWithRetry( + { ...parameters, messages: messagesMapped }, + false, + options?.signal + ).then((data) => { + console.log(data); + + if (data?.code) { + throw new Error(data?.message); + } + const { finish_reason, message } = data.choices[0]; + const text = message.content; + return { + ...data, + output: { text, finish_reason }, + }; + }); + + const { + prompt_tokens = 0, + completion_tokens = 0, + total_tokens = 0, + } = data.usage ?? {}; + + const { text } = data.output; + + return { + generations: [{ text, message: new AIMessage(text) }], + llmOutput: { + tokenUsage: { + promptTokens: prompt_tokens, + completionTokens: completion_tokens, + totalTokens: total_tokens, + }, + }, + }; + } + + async completionWithRetry( + request: ChatCompletionRequest, + stream: boolean, + signal?: AbortSignal, + ) { + const makeCompletionRequest = async () => { + const response = await fetch(this.apiUrl, { + method: "POST", + headers: { + Authorization: `Bearer ${this.apiKey}`, + "Content-Type": "application/json", + }, + body: JSON.stringify(request), + signal, + }); + + if (!stream) { + return response.json(); + } + }; + + return this.caller.call(makeCompletionRequest); + } + + _llmType(): string { + return "DeepInfra"; + } +} diff --git a/libs/langchain-community/src/chat_models/tests/chatdeepinfra.int.test.ts b/libs/langchain-community/src/chat_models/tests/chatdeepinfra.int.test.ts new file mode 100644 index 000000000000..e8b8cfbc97af --- /dev/null +++ b/libs/langchain-community/src/chat_models/tests/chatdeepinfra.int.test.ts @@ -0,0 +1,19 @@ +import { test } from "@jest/globals"; +import { HumanMessage } from "@langchain/core/messages"; +import { ChatDeepInfra } from "../deepinfra.js"; + +describe("ChatDeepInfra", () => { + test("call", async () => { + const deepInfraChat = new ChatDeepInfra({ maxTokens: 20 }); + const message = new HumanMessage("1 + 1 = "); + const res = await deepInfraChat.invoke([message]); + console.log({ res }); + }); + + test("generate", async () => { + const deepInfraChat = new ChatDeepInfra({ maxTokens: 20 }); + const message = new HumanMessage("1 + 1 = "); + const res = await deepInfraChat.generate([[message]]); + console.log(JSON.stringify(res, null, 2)); + }); +}); diff --git a/libs/langchain-community/src/llms/deepinfra.ts b/libs/langchain-community/src/llms/deepinfra.ts new file mode 100644 index 000000000000..897a5e145937 --- /dev/null +++ b/libs/langchain-community/src/llms/deepinfra.ts @@ -0,0 +1,76 @@ +import { LLM, type BaseLLMParams } from "@langchain/core/language_models/llms"; +import { getEnvironmentVariable } from "@langchain/core/utils/env"; + +export const DEEPINFRA_API_BASE = "https://api.deepinfra.com/v1/openai/completions"; + +export const DEFAULT_MODEL_NAME = "mistralai/Mixtral-8x22B-Instruct-v0.1" + +export const ENV_VARIABLE = "DEEPINFRA_API_TOKEN" + + +export interface DeepInfraLLMParams extends BaseLLMParams { + apiKey?: string; + model?: string; + maxTokens?: number; + temperature?: number; +} + +export class DeepInfraLLM extends LLM implements BaseLLMParams { + static lc_name() { + return "DeepInfraLLM"; + } + + + + lc_serializable = true; + + apiToken?: string; + + model?: string; + + maxTokens?: number; + + temperature?: number; + + constructor(fields: Partial = {}) { + super(fields); + + this.apiToken = fields.apiKey ?? getEnvironmentVariable(ENV_VARIABLE); + this.model = fields.model ?? DEFAULT_MODEL_NAME; + this.maxTokens = fields.maxTokens; + this.temperature = fields.temperature; + } + + + _llmType(): string { + return "DeepInfra"; + } + + async _call( + prompt: string, + options: this["ParsedCallOptions"] + ): Promise { + + const body = { + temperature: this.temperature, + max_tokens: this.maxTokens, + ...options, + prompt, + model: this.model, + } + const response = await this.caller.call(() => + fetch(DEEPINFRA_API_BASE, { + method: "POST", + headers: { + Authorization: `Bearer ${this.apiToken}`, + "Content-Type": "application/json", + }, + body: JSON.stringify(body), + }).then((res) => res.json()) + ); + console.log(response); + return response as string; + + } + +} diff --git a/libs/langchain-community/src/llms/tests/deepinfra.int.test.ts b/libs/langchain-community/src/llms/tests/deepinfra.int.test.ts new file mode 100644 index 000000000000..1c8853d2782a --- /dev/null +++ b/libs/langchain-community/src/llms/tests/deepinfra.int.test.ts @@ -0,0 +1,8 @@ +import { test } from "@jest/globals"; +import { DeepInfraLLM } from "../deepinfra.js"; + +test("Test DeepInfra", async () => { + const model = new DeepInfraLLM({ maxTokens: 20 }); + const res = await model.invoke("1 + 1 ="); + console.log(res); +}, 50000); From 08929fe6e61c5378c7c306483e736203e09a8227 Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Wed, 5 Jun 2024 13:30:53 +0300 Subject: [PATCH 13/20] ref(deepinfra): lint and prettier --- examples/src/llms/deepinfra.ts | 3 -- .../src/models/chat/integration_deepinfra.ts | 3 +- examples/src/models/llm/deepinfra.ts | 1 - libs/langchain-community/package.json | 14 +++++----- .../src/chat_models/deepinfra.ts | 28 ++++++++++++------- .../langchain-community/src/llms/deepinfra.ts | 16 ++++------- 6 files changed, 31 insertions(+), 34 deletions(-) diff --git a/examples/src/llms/deepinfra.ts b/examples/src/llms/deepinfra.ts index 43c0e6cd843b..0c31511dc97f 100644 --- a/examples/src/llms/deepinfra.ts +++ b/examples/src/llms/deepinfra.ts @@ -3,10 +3,7 @@ import { DeepInfraLLM } from "@langchain/community/llms/deepinfra"; const apiKey = process.env.DEEPINFRA_API_TOKEN; const model = "meta-llama/Meta-Llama-3-70B-Instruct"; - export const run = async () => { - - const llm = new DeepInfraLLM({ temperature: 0.7, maxTokens: 20, diff --git a/examples/src/models/chat/integration_deepinfra.ts b/examples/src/models/chat/integration_deepinfra.ts index c13ed5a6b550..92f33ff78048 100644 --- a/examples/src/models/chat/integration_deepinfra.ts +++ b/examples/src/models/chat/integration_deepinfra.ts @@ -7,12 +7,11 @@ const model = "gpt-3.5-turbo"; const chat = new ChatDeepInfra({ model, - apiKey + apiKey, }); const messages = [new HumanMessage("Hello")]; - chat.invoke(messages).then((response: any) => { console.log(response); }); diff --git a/examples/src/models/llm/deepinfra.ts b/examples/src/models/llm/deepinfra.ts index 249905abbac4..28a969424c11 100644 --- a/examples/src/models/llm/deepinfra.ts +++ b/examples/src/models/llm/deepinfra.ts @@ -1,6 +1,5 @@ import { DeepInfraLLM } from "@langchain/community/llms/deepinfra"; - const apiKey = process.env.DEEPINFRA_API_TOKEN; const model = "meta-llama/Meta-Llama-3-70B-Instruct"; diff --git a/libs/langchain-community/package.json b/libs/langchain-community/package.json index 3b06fd302b5e..255f7d463702 100644 --- a/libs/langchain-community/package.json +++ b/libs/langchain-community/package.json @@ -1866,13 +1866,13 @@ "require": "./chat_models/cloudflare_workersai.cjs" }, "./chat_models/deepinfra": { - "types": { - "import": "./chat_models/deepinfra.d.ts", - "require": "./chat_models/deepinfra.d.cts", - "default": "./chat_models/deepinfra.d.ts" - }, - "import": "./chat_models/deepinfra.js", - "require": "./chat_models/deepinfra.cjs" + "types": { + "import": "./chat_models/deepinfra.d.ts", + "require": "./chat_models/deepinfra.d.cts", + "default": "./chat_models/deepinfra.d.ts" + }, + "import": "./chat_models/deepinfra.js", + "require": "./chat_models/deepinfra.cjs" }, "./chat_models/fireworks": { "types": { diff --git a/libs/langchain-community/src/chat_models/deepinfra.ts b/libs/langchain-community/src/chat_models/deepinfra.ts index aaddb1dcfb19..b28deaa41e6a 100644 --- a/libs/langchain-community/src/chat_models/deepinfra.ts +++ b/libs/langchain-community/src/chat_models/deepinfra.ts @@ -2,10 +2,7 @@ import { BaseChatModel, type BaseChatModelParams, } from "@langchain/core/language_models/chat_models"; -import { - AIMessage, - type BaseMessage, -} from "@langchain/core/messages"; +import { AIMessage, type BaseMessage } from "@langchain/core/messages"; import { type ChatResult } from "@langchain/core/outputs"; import { getEnvironmentVariable } from "@langchain/core/utils/env"; @@ -13,7 +10,8 @@ export const DEFAULT_MODEL = "meta-llama/Meta-Llama-3-70B-Instruct"; export type DeepInfraMessageRole = "system" | "assistant" | "user"; -export const API_BASE_URL = "https://api.deepinfra.com/v1/openai/chat/completions"; +export const API_BASE_URL = + "https://api.deepinfra.com/v1/openai/chat/completions"; export const ENV_VARIABLE_API_KEY = "DEEPINFRA_API_TOKEN"; @@ -81,7 +79,10 @@ function messageToRole(message: BaseMessage): DeepInfraMessageRole { } } -export class ChatDeepInfra extends BaseChatModel implements ChatDeepInfraParams { +export class ChatDeepInfra + extends BaseChatModel + implements ChatDeepInfraParams +{ static lc_name() { return "ChatDeepInfra"; } @@ -91,17 +92,24 @@ export class ChatDeepInfra extends BaseChatModel implements ChatDeepInfraParams } apiKey?: string; + model: string; + apiUrl: string; + maxTokens?: number; + temperature?: number; constructor(fields: Partial & BaseChatModelParams = {}) { super(fields); - this.apiKey = fields?.apiKey ?? getEnvironmentVariable(ENV_VARIABLE_API_KEY); + this.apiKey = + fields?.apiKey ?? getEnvironmentVariable(ENV_VARIABLE_API_KEY); if (!this.apiKey) { - throw new Error("API key is required, set `DEEPINFRA_API_TOKEN` environment variable or pass it as a parameter"); + throw new Error( + "API key is required, set `DEEPINFRA_API_TOKEN` environment variable or pass it as a parameter" + ); } this.apiUrl = API_BASE_URL; @@ -125,7 +133,7 @@ export class ChatDeepInfra extends BaseChatModel implements ChatDeepInfraParams async _generate( messages: BaseMessage[], - options?: this["ParsedCallOptions"], + options?: this["ParsedCallOptions"] ): Promise { const parameters = this.invocationParams(); @@ -175,7 +183,7 @@ export class ChatDeepInfra extends BaseChatModel implements ChatDeepInfraParams async completionWithRetry( request: ChatCompletionRequest, stream: boolean, - signal?: AbortSignal, + signal?: AbortSignal ) { const makeCompletionRequest = async () => { const response = await fetch(this.apiUrl, { diff --git a/libs/langchain-community/src/llms/deepinfra.ts b/libs/langchain-community/src/llms/deepinfra.ts index 897a5e145937..468dd7f8eb62 100644 --- a/libs/langchain-community/src/llms/deepinfra.ts +++ b/libs/langchain-community/src/llms/deepinfra.ts @@ -1,12 +1,12 @@ import { LLM, type BaseLLMParams } from "@langchain/core/language_models/llms"; import { getEnvironmentVariable } from "@langchain/core/utils/env"; -export const DEEPINFRA_API_BASE = "https://api.deepinfra.com/v1/openai/completions"; +export const DEEPINFRA_API_BASE = + "https://api.deepinfra.com/v1/openai/completions"; -export const DEFAULT_MODEL_NAME = "mistralai/Mixtral-8x22B-Instruct-v0.1" - -export const ENV_VARIABLE = "DEEPINFRA_API_TOKEN" +export const DEFAULT_MODEL_NAME = "mistralai/Mixtral-8x22B-Instruct-v0.1"; +export const ENV_VARIABLE = "DEEPINFRA_API_TOKEN"; export interface DeepInfraLLMParams extends BaseLLMParams { apiKey?: string; @@ -20,8 +20,6 @@ export class DeepInfraLLM extends LLM implements BaseLLMParams { return "DeepInfraLLM"; } - - lc_serializable = true; apiToken?: string; @@ -41,7 +39,6 @@ export class DeepInfraLLM extends LLM implements BaseLLMParams { this.temperature = fields.temperature; } - _llmType(): string { return "DeepInfra"; } @@ -50,14 +47,13 @@ export class DeepInfraLLM extends LLM implements BaseLLMParams { prompt: string, options: this["ParsedCallOptions"] ): Promise { - const body = { temperature: this.temperature, max_tokens: this.maxTokens, ...options, prompt, model: this.model, - } + }; const response = await this.caller.call(() => fetch(DEEPINFRA_API_BASE, { method: "POST", @@ -70,7 +66,5 @@ export class DeepInfraLLM extends LLM implements BaseLLMParams { ); console.log(response); return response as string; - } - } From 936bdd4ccddcf567880b075d9482932761e39fb1 Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Wed, 5 Jun 2024 13:34:40 +0300 Subject: [PATCH 14/20] ref(deepinfra): remove console.log --- libs/langchain-community/src/chat_models/deepinfra.ts | 1 - libs/langchain-community/src/llms/deepinfra.ts | 9 ++++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/libs/langchain-community/src/chat_models/deepinfra.ts b/libs/langchain-community/src/chat_models/deepinfra.ts index b28deaa41e6a..e5466fe9264f 100644 --- a/libs/langchain-community/src/chat_models/deepinfra.ts +++ b/libs/langchain-community/src/chat_models/deepinfra.ts @@ -147,7 +147,6 @@ export class ChatDeepInfra false, options?.signal ).then((data) => { - console.log(data); if (data?.code) { throw new Error(data?.message); diff --git a/libs/langchain-community/src/llms/deepinfra.ts b/libs/langchain-community/src/llms/deepinfra.ts index 468dd7f8eb62..e55c37719aff 100644 --- a/libs/langchain-community/src/llms/deepinfra.ts +++ b/libs/langchain-community/src/llms/deepinfra.ts @@ -15,14 +15,14 @@ export interface DeepInfraLLMParams extends BaseLLMParams { temperature?: number; } -export class DeepInfraLLM extends LLM implements BaseLLMParams { +export class DeepInfraLLM extends LLM implements DeepInfraLLMParams { static lc_name() { return "DeepInfraLLM"; } lc_serializable = true; - apiToken?: string; + apiKey?: string; model?: string; @@ -33,7 +33,7 @@ export class DeepInfraLLM extends LLM implements BaseLLMParams { constructor(fields: Partial = {}) { super(fields); - this.apiToken = fields.apiKey ?? getEnvironmentVariable(ENV_VARIABLE); + this.apiKey = fields.apiKey ?? getEnvironmentVariable(ENV_VARIABLE); this.model = fields.model ?? DEFAULT_MODEL_NAME; this.maxTokens = fields.maxTokens; this.temperature = fields.temperature; @@ -58,13 +58,12 @@ export class DeepInfraLLM extends LLM implements BaseLLMParams { fetch(DEEPINFRA_API_BASE, { method: "POST", headers: { - Authorization: `Bearer ${this.apiToken}`, + Authorization: `Bearer ${this.apiKey}`, "Content-Type": "application/json", }, body: JSON.stringify(body), }).then((res) => res.json()) ); - console.log(response); return response as string; } } From 69ef80a57d468ba73104d313b17c837e10e233cf Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Wed, 5 Jun 2024 14:04:18 +0300 Subject: [PATCH 15/20] fix(chatdeepinfra): body --- examples/src/models/chat/integration_deepinfra.ts | 2 +- libs/langchain-community/src/chat_models/deepinfra.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/src/models/chat/integration_deepinfra.ts b/examples/src/models/chat/integration_deepinfra.ts index 92f33ff78048..6c3cf67a727f 100644 --- a/examples/src/models/chat/integration_deepinfra.ts +++ b/examples/src/models/chat/integration_deepinfra.ts @@ -3,7 +3,7 @@ import { HumanMessage } from "@langchain/core/messages"; const apiKey = process.env.DEEPINFRA_API_TOKEN; -const model = "gpt-3.5-turbo"; +const model = "meta-llama/Meta-Llama-3-70B-Instruct"; const chat = new ChatDeepInfra({ model, diff --git a/libs/langchain-community/src/chat_models/deepinfra.ts b/libs/langchain-community/src/chat_models/deepinfra.ts index e5466fe9264f..e9b75f9f752a 100644 --- a/libs/langchain-community/src/chat_models/deepinfra.ts +++ b/libs/langchain-community/src/chat_models/deepinfra.ts @@ -184,6 +184,14 @@ export class ChatDeepInfra stream: boolean, signal?: AbortSignal ) { + + const body = { + temperature: this.temperature, + max_tokens: this.maxTokens, + ...request, + model: this.model, + }; + const makeCompletionRequest = async () => { const response = await fetch(this.apiUrl, { method: "POST", @@ -191,7 +199,7 @@ export class ChatDeepInfra Authorization: `Bearer ${this.apiKey}`, "Content-Type": "application/json", }, - body: JSON.stringify(request), + body: JSON.stringify(body), signal, }); From 49ac1a2f7d42246f52a04d10db3653b4ffc04a1b Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Wed, 5 Jun 2024 16:13:44 +0300 Subject: [PATCH 16/20] fix(import map): deepinfra --- libs/langchain-community/src/load/import_map.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libs/langchain-community/src/load/import_map.ts b/libs/langchain-community/src/load/import_map.ts index bc4d5b33bd77..b8b0a18e0564 100644 --- a/libs/langchain-community/src/load/import_map.ts +++ b/libs/langchain-community/src/load/import_map.ts @@ -34,6 +34,7 @@ export * as embeddings__voyage from "../embeddings/voyage.js"; export * as llms__ai21 from "../llms/ai21.js"; export * as llms__aleph_alpha from "../llms/aleph_alpha.js"; export * as llms__cloudflare_workersai from "../llms/cloudflare_workersai.js"; +export * as llms__deepinfra from "../llms/deepinfra.js"; export * as llms__fireworks from "../llms/fireworks.js"; export * as llms__friendli from "../llms/friendli.js"; export * as llms__ollama from "../llms/ollama.js"; @@ -45,6 +46,7 @@ export * as vectorstores__vectara from "../vectorstores/vectara.js"; export * as chat_models__alibaba_tongyi from "../chat_models/alibaba_tongyi.js"; export * as chat_models__baiduwenxin from "../chat_models/baiduwenxin.js"; export * as chat_models__cloudflare_workersai from "../chat_models/cloudflare_workersai.js"; +export * as chat_models__deepinfra from "../chat_models/deepinfra.js"; export * as chat_models__fireworks from "../chat_models/fireworks.js"; export * as chat_models__friendli from "../chat_models/friendli.js"; export * as chat_models__minimax from "../chat_models/minimax.js"; From 699ec9ea9a547affac78fb63341844967017e431 Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Wed, 5 Jun 2024 16:19:22 +0300 Subject: [PATCH 17/20] fix(gitignore) --- docs/core_docs/.gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/core_docs/.gitignore b/docs/core_docs/.gitignore index 2ca6b86875a1..b41549b5ad7c 100644 --- a/docs/core_docs/.gitignore +++ b/docs/core_docs/.gitignore @@ -55,6 +55,8 @@ docs/how_to/tools_prompting.md docs/how_to/tools_prompting.mdx docs/how_to/tools_builtin.md docs/how_to/tools_builtin.mdx +docs/how_to/tool_calls_multimodal.md +docs/how_to/tool_calls_multimodal.mdx docs/how_to/tool_calling.md docs/how_to/tool_calling.mdx docs/how_to/structured_output.md From 98f34c5194fb4a99abcbb83e39e2c69c707194e2 Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Wed, 5 Jun 2024 16:26:55 +0300 Subject: [PATCH 18/20] revert(.gitignore) --- docs/core_docs/.gitignore | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/core_docs/.gitignore b/docs/core_docs/.gitignore index b41549b5ad7c..3e08a9a53f08 100644 --- a/docs/core_docs/.gitignore +++ b/docs/core_docs/.gitignore @@ -55,8 +55,6 @@ docs/how_to/tools_prompting.md docs/how_to/tools_prompting.mdx docs/how_to/tools_builtin.md docs/how_to/tools_builtin.mdx -docs/how_to/tool_calls_multimodal.md -docs/how_to/tool_calls_multimodal.mdx docs/how_to/tool_calling.md docs/how_to/tool_calling.mdx docs/how_to/structured_output.md @@ -174,4 +172,4 @@ docs/how_to/assign.mdx docs/how_to/agent_executor.md docs/how_to/agent_executor.mdx docs/integrations/llms/mistral.md -docs/integrations/llms/mistral.mdx \ No newline at end of file +docs/integrations/llms/mistral.mdx From 1ce74c4128069337892e5905f096e2653c898b7e Mon Sep 17 00:00:00 2001 From: Oguz Vuruskaner Date: Wed, 5 Jun 2024 16:32:23 +0300 Subject: [PATCH 19/20] revert(.gitignore) --- docs/core_docs/.gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/core_docs/.gitignore b/docs/core_docs/.gitignore index 3e08a9a53f08..cb9e3e07925d 100644 --- a/docs/core_docs/.gitignore +++ b/docs/core_docs/.gitignore @@ -55,6 +55,8 @@ docs/how_to/tools_prompting.md docs/how_to/tools_prompting.mdx docs/how_to/tools_builtin.md docs/how_to/tools_builtin.mdx +docs/how_to/tool_calls_multimodal.md +docs/how_to/tool_calls_multimodal.mdx docs/how_to/tool_calling.md docs/how_to/tool_calling.mdx docs/how_to/structured_output.md From f5059dce2afdcedcbd537dc65aeb85761271cb50 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Tue, 11 Jun 2024 12:56:46 -0700 Subject: [PATCH 20/20] Adds docs --- .../docs/integrations/chat/deep_infra.mdx | 25 +++++++++++++++++++ .../docs/integrations/llms/deep_infra.mdx | 25 +++++++++++++++++++ examples/src/llms/deepinfra.ts | 18 ------------- .../src/models/chat/integration_deepinfra.ts | 6 ++--- examples/src/models/llm/deepinfra.ts | 4 +++ .../src/chat_models/deepinfra.ts | 2 -- 6 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 docs/core_docs/docs/integrations/chat/deep_infra.mdx create mode 100644 docs/core_docs/docs/integrations/llms/deep_infra.mdx delete mode 100644 examples/src/llms/deepinfra.ts diff --git a/docs/core_docs/docs/integrations/chat/deep_infra.mdx b/docs/core_docs/docs/integrations/chat/deep_infra.mdx new file mode 100644 index 000000000000..5e5805c84bf9 --- /dev/null +++ b/docs/core_docs/docs/integrations/chat/deep_infra.mdx @@ -0,0 +1,25 @@ +--- +sidebar_label: Deep Infra +--- + +import CodeBlock from "@theme/CodeBlock"; + +# ChatDeepInfra + +LangChain supports chat models hosted by [Deep Infra](https://deepinfra.com/) through the `ChatDeepInfra` wrapper. +First, you'll need to install the `@langchain/community` package: + +import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx"; + + + +```bash npm2yarn +npm install @langchain/community +``` + +You'll need to obtain an API key and set it as an environment variable named `DEEPINFRA_API_TOKEN` +(or pass it into the constructor), then call the model as shown below: + +import Example from "@examples/models/chat/integration_deepinfra.ts"; + +{Example} diff --git a/docs/core_docs/docs/integrations/llms/deep_infra.mdx b/docs/core_docs/docs/integrations/llms/deep_infra.mdx new file mode 100644 index 000000000000..76e75db0e134 --- /dev/null +++ b/docs/core_docs/docs/integrations/llms/deep_infra.mdx @@ -0,0 +1,25 @@ +--- +sidebar_label: Deep Infra +--- + +import CodeBlock from "@theme/CodeBlock"; + +# DeepInfra + +LangChain supports LLMs hosted by [Deep Infra](https://deepinfra.com/) through the `DeepInfra` wrapper. +First, you'll need to install the `@langchain/community` package: + +import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx"; + + + +```bash npm2yarn +npm install @langchain/community +``` + +You'll need to obtain an API key and set it as an environment variable named `DEEPINFRA_API_TOKEN` +(or pass it into the constructor), then call the model as shown below: + +import Example from "@examples/models/llm/deepinfra.ts"; + +{Example} diff --git a/examples/src/llms/deepinfra.ts b/examples/src/llms/deepinfra.ts deleted file mode 100644 index 0c31511dc97f..000000000000 --- a/examples/src/llms/deepinfra.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { DeepInfraLLM } from "@langchain/community/llms/deepinfra"; - -const apiKey = process.env.DEEPINFRA_API_TOKEN; -const model = "meta-llama/Meta-Llama-3-70B-Instruct"; - -export const run = async () => { - const llm = new DeepInfraLLM({ - temperature: 0.7, - maxTokens: 20, - model, - apiKey, - maxRetries: 5, - }); - const res = await llm.invoke( - "Question: What is the next step in the process of making a good game?\nAnswer:" - ); - console.log({ res }); -}; diff --git a/examples/src/models/chat/integration_deepinfra.ts b/examples/src/models/chat/integration_deepinfra.ts index 6c3cf67a727f..def635fd676b 100644 --- a/examples/src/models/chat/integration_deepinfra.ts +++ b/examples/src/models/chat/integration_deepinfra.ts @@ -12,6 +12,6 @@ const chat = new ChatDeepInfra({ const messages = [new HumanMessage("Hello")]; -chat.invoke(messages).then((response: any) => { - console.log(response); -}); +const res = await chat.invoke(messages); + +console.log(res); diff --git a/examples/src/models/llm/deepinfra.ts b/examples/src/models/llm/deepinfra.ts index 28a969424c11..28571d07cb04 100644 --- a/examples/src/models/llm/deepinfra.ts +++ b/examples/src/models/llm/deepinfra.ts @@ -4,11 +4,15 @@ const apiKey = process.env.DEEPINFRA_API_TOKEN; const model = "meta-llama/Meta-Llama-3-70B-Instruct"; const llm = new DeepInfraLLM({ + temperature: 0.7, maxTokens: 20, model, apiKey, + maxRetries: 5, }); + const res = await llm.invoke( "What is the next step in the process of making a good game?" ); + console.log({ res }); diff --git a/libs/langchain-community/src/chat_models/deepinfra.ts b/libs/langchain-community/src/chat_models/deepinfra.ts index e9b75f9f752a..82626ecc0a9f 100644 --- a/libs/langchain-community/src/chat_models/deepinfra.ts +++ b/libs/langchain-community/src/chat_models/deepinfra.ts @@ -147,7 +147,6 @@ export class ChatDeepInfra false, options?.signal ).then((data) => { - if (data?.code) { throw new Error(data?.message); } @@ -184,7 +183,6 @@ export class ChatDeepInfra stream: boolean, signal?: AbortSignal ) { - const body = { temperature: this.temperature, max_tokens: this.maxTokens,