UNPKG

@langchain/azure-openai

Version:

Azure SDK for OpenAI integrations for LangChain.js

156 lines (155 loc) 6.28 kB
import { Embeddings } from "@langchain/core/embeddings"; import { OpenAIClient as AzureOpenAIClient, AzureKeyCredential, OpenAIKeyCredential, } from "@azure/openai"; import { isTokenCredential, } from "@azure/core-auth"; import { getEnvironmentVariable } from "@langchain/core/utils/env"; import { chunkArray } from "@langchain/core/utils/chunk_array"; import { USER_AGENT_PREFIX } from "./constants.js"; /** @deprecated Import from "@langchain/openai" instead. */ export class AzureOpenAIEmbeddings extends Embeddings { constructor(fields) { const fieldsWithDefaults = { maxConcurrency: 2, ...fields }; super(fieldsWithDefaults); Object.defineProperty(this, "modelName", { enumerable: true, configurable: true, writable: true, value: "text-embedding-ada-002" }); Object.defineProperty(this, "model", { enumerable: true, configurable: true, writable: true, value: "text-embedding-ada-002" }); Object.defineProperty(this, "batchSize", { enumerable: true, configurable: true, writable: true, value: 512 }); Object.defineProperty(this, "stripNewLines", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(this, "timeout", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "user", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "azureOpenAIApiKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "apiKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "azureOpenAIEndpoint", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "azureOpenAIApiDeploymentName", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "client", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.azureOpenAIApiDeploymentName = (fieldsWithDefaults?.azureOpenAIEmbeddingsApiDeploymentName || fieldsWithDefaults?.azureOpenAIApiDeploymentName) ?? (getEnvironmentVariable("AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME") || getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME")); this.azureOpenAIEndpoint = fields?.azureOpenAIEndpoint ?? getEnvironmentVariable("AZURE_OPENAI_API_ENDPOINT"); const openAiApiKey = fields?.apiKey ?? fields?.openAIApiKey ?? getEnvironmentVariable("OPENAI_API_KEY"); this.azureOpenAIApiKey = fields?.apiKey ?? fields?.azureOpenAIApiKey ?? getEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? openAiApiKey; this.apiKey = this.azureOpenAIApiKey; const azureCredential = fields?.credentials ?? (this.apiKey === openAiApiKey ? new OpenAIKeyCredential(this.apiKey ?? "") : new AzureKeyCredential(this.apiKey ?? "")); // eslint-disable-next-line no-instanceof/no-instanceof const isOpenAIApiKey = azureCredential instanceof OpenAIKeyCredential; if (!this.apiKey && !fields?.credentials) { throw new Error("Azure OpenAI API key not found"); } if (!this.azureOpenAIEndpoint && !isOpenAIApiKey) { throw new Error("Azure OpenAI Endpoint not found"); } if (!this.azureOpenAIApiDeploymentName && !isOpenAIApiKey) { throw new Error("Azure OpenAI Deployment name not found"); } this.modelName = fieldsWithDefaults?.model ?? fieldsWithDefaults?.modelName ?? this.model; this.model = this.modelName; this.batchSize = fieldsWithDefaults?.batchSize ?? (this.apiKey ? 1 : this.batchSize); this.stripNewLines = fieldsWithDefaults?.stripNewLines ?? this.stripNewLines; this.timeout = fieldsWithDefaults?.timeout; const options = { userAgentOptions: { userAgentPrefix: USER_AGENT_PREFIX }, }; if (isOpenAIApiKey) { this.client = new AzureOpenAIClient(azureCredential); } else if (isTokenCredential(azureCredential)) { this.client = new AzureOpenAIClient(this.azureOpenAIEndpoint ?? "", azureCredential, options); } else { this.client = new AzureOpenAIClient(this.azureOpenAIEndpoint ?? "", azureCredential, options); } } async embedDocuments(texts) { const batches = chunkArray(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize); const batchRequests = batches.map((batch) => this.getEmbeddings(batch)); const embeddings = await Promise.all(batchRequests); return embeddings.flat(); } async embedQuery(document) { const input = [ this.stripNewLines ? document.replace(/\n/g, " ") : document, ]; const embeddings = await this.getEmbeddings(input); return embeddings.flat(); } async getEmbeddings(input) { const deploymentName = this.azureOpenAIApiDeploymentName || this.model; const res = await this.caller.call(() => this.client.getEmbeddings(deploymentName, input, { user: this.user, model: this.model, requestOptions: { timeout: this.timeout, }, })); return res.data.map((data) => data.embedding); } }