UNPKG

@langchain/community

Version:
186 lines (185 loc) 5.6 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_runtime = require("../_virtual/_rolldown/runtime.cjs"); const require_ibm = require("../utils/ibm.cjs"); let _langchain_core_utils_async_caller = require("@langchain/core/utils/async_caller"); let _langchain_core_embeddings = require("@langchain/core/embeddings"); //#region src/embeddings/ibm.ts var ibm_exports = /* @__PURE__ */ require_runtime.__exportAll({ WatsonxEmbeddings: () => WatsonxEmbeddings }); var WatsonxEmbeddings = class extends _langchain_core_embeddings.Embeddings { model; serviceUrl; version; spaceId; projectId; truncateInputTokens; returnOptions; maxRetries; maxConcurrency = 1; modelGatewayKwargs; modelGateway = false; service; gateway; checkValidProperties(fields, includeCommonProps = true) { const alwaysAllowedProps = [ "headers", "signal", "promptIndex" ]; const authProps = [ "serviceUrl", "watsonxAIApikey", "watsonxAIBearerToken", "watsonxAIUsername", "watsonxAIPassword", "watsonxAIUrl", "watsonxAIAuthType", "disableSSL" ]; const sharedProps = [ "maxRetries", "watsonxCallbacks", "authenticator", "serviceUrl", "version", "streaming", "callbackManager", "callbacks", "maxConcurrency", "cache", "metadata", "concurrency", "onFailedAttempt", "concurrency", "verbose", "tags", "headers", "signal", "disableStreaming" ]; const projectOrSpaceProps = [ "truncateInputTokens", "returnOptions", "model", "projectId", "spaceId" ]; const gatewayProps = [ "model", "modelGatewayKwargs", "modelGateway" ]; const validProps = [...alwaysAllowedProps]; if (includeCommonProps) validProps.push(...authProps, ...sharedProps); if (this.modelGateway) validProps.push(...gatewayProps); else if (this.spaceId || this.projectId) validProps.push(...projectOrSpaceProps); require_ibm.checkValidProps(fields, validProps); } constructor(fields) { super(fields); require_ibm.expectOneOf(fields, [ "projectId", "spaceId", "modelGateway" ], true); this.projectId = fields?.projectId; this.spaceId = fields?.spaceId; this.modelGateway = fields.modelGateway ?? this.modelGateway; this.checkValidProperties(fields); this.model = fields.model; this.version = fields.version; this.serviceUrl = fields.serviceUrl; this.truncateInputTokens = fields.truncateInputTokens; this.returnOptions = fields.returnOptions; this.maxConcurrency = fields.maxConcurrency ?? this.maxConcurrency; this.maxRetries = fields.maxRetries ?? 0; this.serviceUrl = fields?.serviceUrl; this.modelGatewayKwargs = fields.modelGatewayKwargs; const { watsonxAIApikey, watsonxAIAuthType, watsonxAIBearerToken, watsonxAIUsername, watsonxAIPassword, watsonxAIUrl, disableSSL, version, serviceUrl } = fields; const authData = { watsonxAIApikey, watsonxAIAuthType, watsonxAIBearerToken, watsonxAIUsername, watsonxAIPassword, watsonxAIUrl, disableSSL, version, serviceUrl }; if (this.modelGateway) { const auth = require_ibm.authenticateAndSetGatewayInstance(authData); if (auth) this.gateway = auth; else throw new Error("You have not provided one type of authentication"); } else { const auth = require_ibm.authenticateAndSetInstance(authData); if (auth) this.service = auth; else throw new Error("You have not provided one type of authentication"); } } scopeId() { if (this.projectId) return { projectId: this.projectId, modelId: this.model }; else if (this.spaceId) return { spaceId: this.spaceId, modelId: this.model }; else return { model: this.model }; } invocationParams() { return { truncate_input_tokens: this.truncateInputTokens, return_options: this.returnOptions }; } async listModels() { if (this.service) { const { service } = this; const listModelParams = { filters: "function_embedding" }; return (await new _langchain_core_utils_async_caller.AsyncCaller({ maxConcurrency: this.maxConcurrency, maxRetries: this.maxRetries }).call(() => service.listFoundationModelSpecs(listModelParams))).result.resources?.map((item) => item.model_id); } else throw new Error("This method is not supported in model gateway"); } async embedSingleText(inputs) { const scopeId = this.scopeId(); if ("modelId" in scopeId && this.service) { const { service } = this; return (await new _langchain_core_utils_async_caller.AsyncCaller({ maxConcurrency: this.maxConcurrency, maxRetries: this.maxRetries }).call(() => service.embedText({ inputs, ...scopeId, parameters: this.invocationParams() }))).result.results.map((item) => item.embedding); } else if (this.gateway && "model" in scopeId) { const { gateway } = this; return (await new _langchain_core_utils_async_caller.AsyncCaller({ maxConcurrency: this.maxConcurrency, maxRetries: this.maxRetries }).call(() => gateway.embeddings.completion.create({ input: inputs, ...scopeId }))).result.data.map((item) => item.embedding); } throw new Error("Invalid parameters provided. Please check passed properties to class instance"); } async embedDocuments(documents) { return await this.embedSingleText(documents); } async embedQuery(document) { return (await this.embedSingleText([document]))[0]; } }; //#endregion exports.WatsonxEmbeddings = WatsonxEmbeddings; Object.defineProperty(exports, "ibm_exports", { enumerable: true, get: function() { return ibm_exports; } }); //# sourceMappingURL=ibm.cjs.map