@langchain/classic 1.0.8 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/agents/agent.cjs.map +1 -1
- package/dist/agents/agent.js.map +1 -1
- package/dist/agents/chat/outputParser.d.cts +2 -2
- package/dist/agents/chat/outputParser.d.cts.map +1 -1
- package/dist/agents/helpers.cjs.map +1 -1
- package/dist/agents/helpers.js.map +1 -1
- package/dist/agents/openai_functions/index.cjs.map +1 -1
- package/dist/agents/openai_functions/index.js.map +1 -1
- package/dist/agents/react/index.d.cts +2 -2
- package/dist/agents/react/index.d.cts.map +1 -1
- package/dist/agents/react/index.d.ts +2 -2
- package/dist/agents/react/index.d.ts.map +1 -1
- package/dist/agents/structured_chat/index.d.cts +2 -2
- package/dist/agents/structured_chat/index.d.cts.map +1 -1
- package/dist/agents/structured_chat/index.d.ts +2 -2
- package/dist/agents/structured_chat/index.d.ts.map +1 -1
- package/dist/agents/tool_calling/index.d.cts +2 -2
- package/dist/agents/tool_calling/index.d.cts.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.cts +1 -1
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.cts.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/tool.d.cts +1 -1
- package/dist/agents/toolkits/conversational_retrieval/tool.d.cts.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/tool.d.ts +1 -1
- package/dist/agents/toolkits/conversational_retrieval/tool.d.ts.map +1 -1
- package/dist/agents/types.cjs.map +1 -1
- package/dist/agents/types.js.map +1 -1
- package/dist/chains/base.cjs.map +1 -1
- package/dist/chains/base.js.map +1 -1
- package/dist/chains/combine_docs_chain.cjs.map +1 -1
- package/dist/chains/combine_docs_chain.js.map +1 -1
- package/dist/chains/llm_chain.cjs.map +1 -1
- package/dist/chains/llm_chain.js.map +1 -1
- package/dist/chains/openai_functions/base.cjs.map +1 -1
- package/dist/chains/openai_functions/base.js.map +1 -1
- package/dist/chains/openai_functions/openapi.cjs.map +1 -1
- package/dist/chains/openai_functions/openapi.js.map +1 -1
- package/dist/chains/query_constructor/index.cjs.map +1 -1
- package/dist/chains/query_constructor/index.js.map +1 -1
- package/dist/chains/query_constructor/parser.cjs.map +1 -1
- package/dist/chains/query_constructor/parser.js.map +1 -1
- package/dist/chains/router/multi_prompt.cjs.map +1 -1
- package/dist/chains/router/multi_prompt.js.map +1 -1
- package/dist/chains/transform.cjs.map +1 -1
- package/dist/chains/transform.js.map +1 -1
- package/dist/chat_models/universal.cjs.map +1 -1
- package/dist/chat_models/universal.js.map +1 -1
- package/dist/document_loaders/fs/json.cjs.map +1 -1
- package/dist/document_loaders/fs/json.js.map +1 -1
- package/dist/embeddings/cache_backed.cjs.map +1 -1
- package/dist/embeddings/cache_backed.js.map +1 -1
- package/dist/evaluation/base.cjs.map +1 -1
- package/dist/evaluation/base.js.map +1 -1
- package/dist/evaluation/comparison/pairwise.d.ts +3 -3
- package/dist/evaluation/comparison/pairwise.d.ts.map +1 -1
- package/dist/evaluation/criteria/criteria.d.ts +3 -3
- package/dist/evaluation/criteria/criteria.d.ts.map +1 -1
- package/dist/experimental/autogpt/prompt.d.cts +2 -2
- package/dist/experimental/autogpt/prompt.d.cts.map +1 -1
- package/dist/experimental/autogpt/prompt.d.ts +2 -2
- package/dist/experimental/autogpt/prompt.d.ts.map +1 -1
- package/dist/experimental/masking/regex_masking_transformer.cjs.map +1 -1
- package/dist/experimental/masking/regex_masking_transformer.js.map +1 -1
- package/dist/experimental/openai_assistant/index.cjs.map +1 -1
- package/dist/experimental/openai_assistant/index.js.map +1 -1
- package/dist/experimental/plan_and_execute/agent_executor.cjs.map +1 -1
- package/dist/experimental/plan_and_execute/agent_executor.js.map +1 -1
- package/dist/experimental/prompts/custom_format.cjs.map +1 -1
- package/dist/experimental/prompts/custom_format.js.map +1 -1
- package/dist/experimental/prompts/handlebars.cjs.map +1 -1
- package/dist/experimental/prompts/handlebars.js.map +1 -1
- package/dist/load/import_constants.cjs +24 -24
- package/dist/load/import_constants.cjs.map +1 -1
- package/dist/load/import_constants.js +24 -24
- package/dist/load/import_constants.js.map +1 -1
- package/dist/load/import_map.cjs +44 -44
- package/dist/load/import_map.cjs.map +1 -1
- package/dist/load/import_map.js +52 -52
- package/dist/load/import_map.js.map +1 -1
- package/dist/output_parsers/openai_functions.cjs.map +1 -1
- package/dist/output_parsers/openai_functions.js.map +1 -1
- package/dist/output_parsers/regex.cjs.map +1 -1
- package/dist/output_parsers/regex.js.map +1 -1
- package/dist/output_parsers/router.cjs.map +1 -1
- package/dist/output_parsers/router.js.map +1 -1
- package/dist/output_parsers/structured.cjs.map +1 -1
- package/dist/output_parsers/structured.js.map +1 -1
- package/dist/retrievers/document_compressors/index.cjs.map +1 -1
- package/dist/retrievers/document_compressors/index.js.map +1 -1
- package/dist/retrievers/hyde.cjs +1 -1
- package/dist/retrievers/hyde.cjs.map +1 -1
- package/dist/retrievers/hyde.js +1 -1
- package/dist/retrievers/hyde.js.map +1 -1
- package/dist/retrievers/matryoshka_retriever.cjs +1 -1
- package/dist/retrievers/matryoshka_retriever.cjs.map +1 -1
- package/dist/retrievers/matryoshka_retriever.js +1 -1
- package/dist/retrievers/matryoshka_retriever.js.map +1 -1
- package/dist/retrievers/score_threshold.cjs.map +1 -1
- package/dist/retrievers/score_threshold.js.map +1 -1
- package/dist/retrievers/self_query/index.cjs +1 -1
- package/dist/retrievers/self_query/index.d.ts +1 -1
- package/dist/retrievers/self_query/index.js +1 -1
- package/dist/smith/config.cjs.map +1 -1
- package/dist/smith/config.js.map +1 -1
- package/dist/smith/runner_utils.cjs.map +1 -1
- package/dist/smith/runner_utils.js.map +1 -1
- package/dist/tools/retriever.d.cts +1 -1
- package/dist/tools/retriever.d.cts.map +1 -1
- package/dist/tools/retriever.d.ts +1 -1
- package/dist/tools/retriever.d.ts.map +1 -1
- package/dist/util/sql_utils.cjs.map +1 -1
- package/dist/util/sql_utils.js.map +1 -1
- package/dist/vectorstores/memory.cjs +1 -1
- package/dist/vectorstores/memory.js +1 -1
- package/package.json +17 -18
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"universal.js","names":["className: string","config","e: unknown","model: string","modelProvider?: string","params: Record<string, any>","modelName: string","fields: ConfigurableModelFields","config?: RunnableConfig","messages: BaseMessage[]","options?: this[\"ParsedCallOptions\"]","runManager?: CallbackManagerForLLMRun","tools: BindToolsInput[]","params?: Record<string, any>","modelParams: Record<string, any>","str: string","prefix: string","mergedConfig: RunnableConfig","remainingConfig: RunnableConfig","input: RunInput","options?: CallOptions","inputs: RunInput[]","options?: Partial<CallOptions> | Partial<CallOptions>[]","batchOptions?: RunnableBatchOptions","generator: AsyncGenerator<RunInput>","options: CallOptions","options?: Partial<CallOptions>","streamOptions?: Omit<LogStreamCallbackHandlerInput, \"autoClose\">","options: Partial<CallOptions> & {\n version: \"v1\" | \"v2\";\n encoding?: \"text/event-stream\" | undefined;\n }","streamOptions?: Omit<EventStreamCallbackHandlerInput, \"autoClose\">","model?: string","fields?: Partial<Record<string, any>> & {\n modelProvider?: string;\n configurableFields?: string[] | \"any\";\n configPrefix?: string;\n }","paramsCopy: Record<string, any>"],"sources":["../../src/chat_models/universal.ts"],"sourcesContent":["import {\n BaseLanguageModelInput,\n ToolDefinition,\n} from \"@langchain/core/language_models/base\";\nimport {\n BaseChatModel,\n BaseChatModelParams,\n BindToolsInput,\n type BaseChatModelCallOptions,\n} from \"@langchain/core/language_models/chat_models\";\nimport {\n BaseMessage,\n type AIMessageChunk,\n MessageStructure,\n} from \"@langchain/core/messages\";\nimport {\n type RunnableBatchOptions,\n RunnableBinding,\n type RunnableConfig,\n type RunnableToolLike,\n ensureConfig,\n} from \"@langchain/core/runnables\";\nimport {\n AsyncGeneratorWithSetup,\n IterableReadableStream,\n} from \"@langchain/core/utils/stream\";\nimport {\n type LogStreamCallbackHandlerInput,\n type RunLogPatch,\n type StreamEvent,\n} from \"@langchain/core/tracers/log_stream\";\nimport { type StructuredToolInterface } from \"@langchain/core/tools\";\nimport { CallbackManagerForLLMRun } from \"@langchain/core/callbacks/manager\";\nimport { ChatResult } from \"@langchain/core/outputs\";\n\n// TODO: remove once `EventStreamCallbackHandlerInput` is exposed in core\ninterface EventStreamCallbackHandlerInput\n extends Omit<LogStreamCallbackHandlerInput, \"_schemaFormat\"> {}\n\nexport interface ConfigurableChatModelCallOptions\n extends BaseChatModelCallOptions {\n tools?: (\n | StructuredToolInterface\n | Record<string, unknown>\n | ToolDefinition\n | RunnableToolLike\n )[];\n}\n\n// Configuration map for model providers\nexport const MODEL_PROVIDER_CONFIG = {\n openai: {\n package: \"@langchain/openai\",\n className: \"ChatOpenAI\",\n },\n anthropic: {\n package: \"@langchain/anthropic\",\n className: \"ChatAnthropic\",\n },\n azure_openai: {\n package: \"@langchain/openai\",\n className: \"AzureChatOpenAI\",\n },\n cohere: {\n package: \"@langchain/cohere\",\n className: \"ChatCohere\",\n },\n \"google-vertexai\": {\n package: \"@langchain/google-vertexai\",\n className: \"ChatVertexAI\",\n },\n \"google-vertexai-web\": {\n package: \"@langchain/google-vertexai-web\",\n className: \"ChatVertexAI\",\n },\n \"google-genai\": {\n package: \"@langchain/google-genai\",\n className: \"ChatGoogleGenerativeAI\",\n },\n ollama: {\n package: \"@langchain/ollama\",\n className: \"ChatOllama\",\n },\n mistralai: {\n package: \"@langchain/mistralai\",\n className: \"ChatMistralAI\",\n },\n groq: {\n package: \"@langchain/groq\",\n className: \"ChatGroq\",\n },\n cerebras: {\n package: \"@langchain/cerebras\",\n className: \"ChatCerebras\",\n },\n bedrock: {\n package: \"@langchain/aws\",\n className: \"ChatBedrockConverse\",\n },\n deepseek: {\n package: \"@langchain/deepseek\",\n className: \"ChatDeepSeek\",\n },\n xai: {\n package: \"@langchain/xai\",\n className: \"ChatXAI\",\n },\n fireworks: {\n package: \"@langchain/community/chat_models/fireworks\",\n className: \"ChatFireworks\",\n hasCircularDependency: true,\n },\n together: {\n package: \"@langchain/community/chat_models/togetherai\",\n className: \"ChatTogetherAI\",\n hasCircularDependency: true,\n },\n perplexity: {\n package: \"@langchain/community/chat_models/perplexity\",\n className: \"ChatPerplexity\",\n hasCircularDependency: true,\n },\n} as const;\n\nconst SUPPORTED_PROVIDERS = Object.keys(\n MODEL_PROVIDER_CONFIG\n) as (keyof typeof MODEL_PROVIDER_CONFIG)[];\nexport type ChatModelProvider = keyof typeof MODEL_PROVIDER_CONFIG;\ntype ModelProviderConfig = {\n package: string;\n className: string;\n hasCircularDependency?: boolean;\n};\n\n/**\n * Helper function to get a chat model class by its class name\n * @param className The class name (e.g., \"ChatOpenAI\", \"ChatAnthropic\")\n * @returns The imported model class or undefined if not found\n */\nexport async function getChatModelByClassName(className: string) {\n // Find the provider config that matches the class name\n const providerEntry = Object.entries(MODEL_PROVIDER_CONFIG).find(\n ([, config]) => config.className === className\n );\n\n if (!providerEntry) {\n return undefined;\n }\n\n const [, config] = providerEntry;\n try {\n const module = await import(config.package);\n return module[config.className];\n } catch (e: unknown) {\n const err = e as Error;\n if (\n \"code\" in err &&\n err.code?.toString().includes(\"ERR_MODULE_NOT_FOUND\") &&\n \"message\" in err &&\n typeof err.message === \"string\"\n ) {\n const msg = err.message.startsWith(\"Error: \")\n ? err.message.slice(\"Error: \".length)\n : err.message;\n const attemptedPackage = msg\n .split(\"Cannot find package '\")[1]\n .split(\"'\")[0];\n throw new Error(\n `Unable to import ${attemptedPackage}. Please install with ` +\n `\\`npm install ${attemptedPackage}\\` or \\`pnpm install ${attemptedPackage}\\``\n );\n }\n throw e;\n }\n}\n\nasync function _initChatModelHelper(\n model: string,\n modelProvider?: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n params: Record<string, any> = {}\n): Promise<BaseChatModel> {\n const modelProviderCopy = modelProvider || _inferModelProvider(model);\n if (!modelProviderCopy) {\n throw new Error(\n `Unable to infer model provider for { model: ${model} }, please specify modelProvider directly.`\n );\n }\n\n const config = MODEL_PROVIDER_CONFIG[\n modelProviderCopy as keyof typeof MODEL_PROVIDER_CONFIG\n ] as ModelProviderConfig;\n if (!config) {\n const supported = SUPPORTED_PROVIDERS.join(\", \");\n throw new Error(\n `Unsupported { modelProvider: ${modelProviderCopy} }.\\n\\nSupported model providers are: ${supported}`\n );\n }\n\n const { modelProvider: _unused, ...passedParams } = params;\n const ProviderClass = await getChatModelByClassName(config.className);\n return new ProviderClass({ model, ...passedParams });\n}\n\n/**\n * Attempts to infer the model provider based on the given model name.\n *\n * @param {string} modelName - The name of the model to infer the provider for.\n * @returns {string | undefined} The inferred model provider name, or undefined if unable to infer.\n *\n * @example\n * _inferModelProvider(\"gpt-4\"); // returns \"openai\"\n * _inferModelProvider(\"claude-2\"); // returns \"anthropic\"\n * _inferModelProvider(\"unknown-model\"); // returns undefined\n */\nexport function _inferModelProvider(modelName: string): string | undefined {\n if (\n modelName.startsWith(\"gpt-3\") ||\n modelName.startsWith(\"gpt-4\") ||\n modelName.startsWith(\"gpt-5\") ||\n modelName.startsWith(\"o1\") ||\n modelName.startsWith(\"o3\") ||\n modelName.startsWith(\"o4\")\n ) {\n return \"openai\";\n } else if (modelName.startsWith(\"claude\")) {\n return \"anthropic\";\n } else if (modelName.startsWith(\"command\")) {\n return \"cohere\";\n } else if (modelName.startsWith(\"accounts/fireworks\")) {\n return \"fireworks\";\n } else if (modelName.startsWith(\"gemini\")) {\n return \"google-vertexai\";\n } else if (modelName.startsWith(\"amazon.\")) {\n return \"bedrock\";\n } else if (modelName.startsWith(\"mistral\")) {\n return \"mistralai\";\n } else if (modelName.startsWith(\"sonar\") || modelName.startsWith(\"pplx\")) {\n return \"perplexity\";\n } else {\n return undefined;\n }\n}\n\ninterface ConfigurableModelFields extends BaseChatModelParams {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n defaultConfig?: Record<string, any>;\n /**\n * @default \"any\"\n */\n configurableFields?: string[] | \"any\";\n /**\n * @default \"\"\n */\n configPrefix?: string;\n /**\n * Methods which should be called after the model is initialized.\n * The key will be the method name, and the value will be the arguments.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n queuedMethodOperations?: Record<string, any>;\n}\n\n/**\n * Internal class used to create chat models.\n *\n * @internal\n */\nexport class ConfigurableModel<\n RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,\n CallOptions extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions\n> extends BaseChatModel<CallOptions, AIMessageChunk> {\n _llmType(): string {\n return \"chat_model\";\n }\n\n lc_namespace = [\"langchain\", \"chat_models\"];\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _defaultConfig?: Record<string, any> = {};\n\n /**\n * @default \"any\"\n */\n _configurableFields: string[] | \"any\" = \"any\";\n\n /**\n * @default \"\"\n */\n _configPrefix: string;\n\n /**\n * Methods which should be called after the model is initialized.\n * The key will be the method name, and the value will be the arguments.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _queuedMethodOperations: Record<string, any> = {};\n\n constructor(fields: ConfigurableModelFields) {\n super(fields);\n this._defaultConfig = fields.defaultConfig ?? {};\n\n if (fields.configurableFields === \"any\") {\n this._configurableFields = \"any\";\n } else {\n this._configurableFields = fields.configurableFields ?? [\n \"model\",\n \"modelProvider\",\n ];\n }\n\n if (fields.configPrefix) {\n this._configPrefix = fields.configPrefix.endsWith(\"_\")\n ? fields.configPrefix\n : `${fields.configPrefix}_`;\n } else {\n this._configPrefix = \"\";\n }\n\n this._queuedMethodOperations =\n fields.queuedMethodOperations ?? this._queuedMethodOperations;\n }\n\n async _model(\n config?: RunnableConfig\n ): Promise<\n BaseChatModel<BaseChatModelCallOptions, AIMessageChunk<MessageStructure>>\n > {\n const params = { ...this._defaultConfig, ...this._modelParams(config) };\n let initializedModel = await _initChatModelHelper(\n params.model,\n params.modelProvider,\n params\n );\n\n // Apply queued method operations\n const queuedMethodOperationsEntries = Object.entries(\n this._queuedMethodOperations\n );\n if (queuedMethodOperationsEntries.length > 0) {\n for (const [method, args] of queuedMethodOperationsEntries) {\n if (\n method in initializedModel &&\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n typeof (initializedModel as any)[method] === \"function\"\n ) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n initializedModel = await (initializedModel as any)[method](...args);\n }\n }\n }\n\n return initializedModel;\n }\n\n async _generate(\n messages: BaseMessage[],\n options?: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n const model = await this._model(options);\n return model._generate(messages, options ?? {}, runManager);\n }\n\n override bindTools(\n tools: BindToolsInput[],\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n params?: Record<string, any>\n ): ConfigurableModel<RunInput, CallOptions> {\n const newQueuedOperations = { ...this._queuedMethodOperations };\n newQueuedOperations.bindTools = [tools, params];\n return new ConfigurableModel<RunInput, CallOptions>({\n defaultConfig: this._defaultConfig,\n configurableFields: this._configurableFields,\n configPrefix: this._configPrefix,\n queuedMethodOperations: newQueuedOperations,\n });\n }\n\n // Extract the input types from the `BaseModel` class.\n withStructuredOutput: BaseChatModel[\"withStructuredOutput\"] = (\n schema,\n ...args\n ): ReturnType<BaseChatModel[\"withStructuredOutput\"]> => {\n const newQueuedOperations = { ...this._queuedMethodOperations };\n newQueuedOperations.withStructuredOutput = [schema, ...args];\n return new ConfigurableModel<RunInput, CallOptions>({\n defaultConfig: this._defaultConfig,\n configurableFields: this._configurableFields,\n configPrefix: this._configPrefix,\n queuedMethodOperations: newQueuedOperations,\n }) as unknown as ReturnType<BaseChatModel[\"withStructuredOutput\"]>;\n };\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _modelParams(config?: RunnableConfig): Record<string, any> {\n const configurable = config?.configurable ?? {};\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let modelParams: Record<string, any> = {};\n\n for (const [key, value] of Object.entries(configurable)) {\n if (key.startsWith(this._configPrefix)) {\n const strippedKey = this._removePrefix(key, this._configPrefix);\n modelParams[strippedKey] = value;\n }\n }\n\n if (this._configurableFields !== \"any\") {\n modelParams = Object.fromEntries(\n Object.entries(modelParams).filter(([key]) =>\n this._configurableFields.includes(key)\n )\n );\n }\n\n return modelParams;\n }\n\n _removePrefix(str: string, prefix: string): string {\n return str.startsWith(prefix) ? str.slice(prefix.length) : str;\n }\n\n /**\n * Bind config to a Runnable, returning a new Runnable.\n * @param {RunnableConfig | undefined} [config] - The config to bind.\n * @returns {RunnableBinding<RunInput, RunOutput, CallOptions>} A new RunnableBinding with the bound config.\n */\n withConfig(\n config?: RunnableConfig\n ): RunnableBinding<RunInput, AIMessageChunk, CallOptions> {\n const mergedConfig: RunnableConfig = { ...(config || {}) };\n const modelParams = this._modelParams(mergedConfig);\n\n const remainingConfig: RunnableConfig = Object.fromEntries(\n Object.entries(mergedConfig).filter(([k]) => k !== \"configurable\")\n );\n\n remainingConfig.configurable = Object.fromEntries(\n Object.entries(mergedConfig.configurable || {}).filter(\n ([k]) =>\n this._configPrefix &&\n !Object.keys(modelParams).includes(\n this._removePrefix(k, this._configPrefix)\n )\n )\n );\n\n const newConfigurableModel = new ConfigurableModel<RunInput, CallOptions>({\n defaultConfig: { ...this._defaultConfig, ...modelParams },\n configurableFields: Array.isArray(this._configurableFields)\n ? [...this._configurableFields]\n : this._configurableFields,\n configPrefix: this._configPrefix,\n queuedMethodOperations: this._queuedMethodOperations,\n });\n\n return new RunnableBinding<RunInput, AIMessageChunk, CallOptions>({\n config: mergedConfig,\n bound: newConfigurableModel,\n });\n }\n\n async invoke(\n input: RunInput,\n options?: CallOptions\n ): Promise<AIMessageChunk> {\n const model = await this._model(options);\n const config = ensureConfig(options);\n return model.invoke(input, config);\n }\n\n async stream(\n input: RunInput,\n options?: CallOptions\n ): Promise<IterableReadableStream<AIMessageChunk>> {\n const model = await this._model(options);\n const wrappedGenerator = new AsyncGeneratorWithSetup({\n generator: await model.stream(input, options),\n config: options,\n });\n await wrappedGenerator.setup;\n return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);\n }\n\n async batch(\n inputs: RunInput[],\n options?: Partial<CallOptions> | Partial<CallOptions>[],\n batchOptions?: RunnableBatchOptions & { returnExceptions?: false }\n ): Promise<AIMessageChunk[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<CallOptions> | Partial<CallOptions>[],\n batchOptions?: RunnableBatchOptions & { returnExceptions: true }\n ): Promise<(AIMessageChunk | Error)[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<CallOptions> | Partial<CallOptions>[],\n batchOptions?: RunnableBatchOptions\n ): Promise<(AIMessageChunk | Error)[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<CallOptions> | Partial<CallOptions>[],\n batchOptions?: RunnableBatchOptions\n ): Promise<(AIMessageChunk | Error)[]> {\n // We can super this since the base runnable implementation of\n // `.batch` will call `.invoke` on each input.\n return super.batch(inputs, options, batchOptions);\n }\n\n async *transform(\n generator: AsyncGenerator<RunInput>,\n options: CallOptions\n ): AsyncGenerator<AIMessageChunk> {\n const model = await this._model(options);\n const config = ensureConfig(options);\n\n yield* model.transform(generator, config);\n }\n\n async *streamLog(\n input: RunInput,\n options?: Partial<CallOptions>,\n streamOptions?: Omit<LogStreamCallbackHandlerInput, \"autoClose\">\n ): AsyncGenerator<RunLogPatch> {\n const model = await this._model(options);\n const config = ensureConfig(options);\n\n yield* model.streamLog(input, config, {\n ...streamOptions,\n _schemaFormat: \"original\",\n includeNames: streamOptions?.includeNames,\n includeTypes: streamOptions?.includeTypes,\n includeTags: streamOptions?.includeTags,\n excludeNames: streamOptions?.excludeNames,\n excludeTypes: streamOptions?.excludeTypes,\n excludeTags: streamOptions?.excludeTags,\n });\n }\n\n streamEvents(\n input: RunInput,\n options: Partial<CallOptions> & { version: \"v1\" | \"v2\" },\n streamOptions?: Omit<EventStreamCallbackHandlerInput, \"autoClose\">\n ): IterableReadableStream<StreamEvent>;\n\n streamEvents(\n input: RunInput,\n options: Partial<CallOptions> & {\n version: \"v1\" | \"v2\";\n encoding: \"text/event-stream\";\n },\n streamOptions?: Omit<EventStreamCallbackHandlerInput, \"autoClose\">\n ): IterableReadableStream<Uint8Array>;\n\n streamEvents(\n input: RunInput,\n options: Partial<CallOptions> & {\n version: \"v1\" | \"v2\";\n encoding?: \"text/event-stream\" | undefined;\n },\n streamOptions?: Omit<EventStreamCallbackHandlerInput, \"autoClose\">\n ): IterableReadableStream<StreamEvent | Uint8Array> {\n const outerThis = this;\n async function* wrappedGenerator() {\n const model = await outerThis._model(options);\n const config = ensureConfig(options);\n const eventStream = model.streamEvents(input, config, streamOptions);\n\n for await (const chunk of eventStream) {\n yield chunk;\n }\n }\n return IterableReadableStream.fromAsyncGenerator(wrappedGenerator());\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface InitChatModelFields extends Partial<Record<string, any>> {\n modelProvider?: string;\n configurableFields?: string[] | \"any\";\n configPrefix?: string;\n}\n\nexport type ConfigurableFields = \"any\" | string[];\n\nexport async function initChatModel<\n RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,\n CallOptions extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions\n>(\n model: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n fields?: Partial<Record<string, any>> & {\n modelProvider?: string;\n configurableFields?: never;\n configPrefix?: string;\n }\n): Promise<ConfigurableModel<RunInput, CallOptions>>;\n\nexport async function initChatModel<\n RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,\n CallOptions extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions\n>(\n model: never,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n options?: Partial<Record<string, any>> & {\n modelProvider?: string;\n configurableFields?: never;\n configPrefix?: string;\n }\n): Promise<ConfigurableModel<RunInput, CallOptions>>;\n\nexport async function initChatModel<\n RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,\n CallOptions extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions\n>(\n model?: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n options?: Partial<Record<string, any>> & {\n modelProvider?: string;\n configurableFields?: ConfigurableFields;\n configPrefix?: string;\n }\n): Promise<ConfigurableModel<RunInput, CallOptions>>;\n\n// ################################# FOR CONTRIBUTORS #################################\n//\n// If adding support for a new provider, please append the provider\n// name to the supported list in the docstring below.\n//\n// ####################################################################################\n\n/**\n * Initialize a ChatModel from the model name and provider.\n * Must have the integration package corresponding to the model provider installed.\n *\n * @template {extends BaseLanguageModelInput = BaseLanguageModelInput} RunInput - The input type for the model.\n * @template {extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions} CallOptions - Call options for the model.\n *\n * @param {string | ChatModelProvider} [model] - The name of the model, e.g. \"gpt-4\", \"claude-3-opus-20240229\".\n * Can be prefixed with the model provider, e.g. \"openai:gpt-4\", \"anthropic:claude-3-opus-20240229\".\n * @param {Object} [fields] - Additional configuration options.\n * @param {string} [fields.modelProvider] - The model provider. Supported values include:\n * - openai (@langchain/openai)\n * - anthropic (@langchain/anthropic)\n * - azure_openai (@langchain/openai)\n * - google-vertexai (@langchain/google-vertexai)\n * - google-vertexai-web (@langchain/google-vertexai-web)\n * - google-genai (@langchain/google-genai)\n * - bedrock (@langchain/aws)\n * - cohere (@langchain/cohere)\n * - fireworks (@langchain/community/chat_models/fireworks)\n * - together (@langchain/community/chat_models/togetherai)\n * - mistralai (@langchain/mistralai)\n * - groq (@langchain/groq)\n * - ollama (@langchain/ollama)\n * - perplexity (@langchain/community/chat_models/perplexity)\n * - cerebras (@langchain/cerebras)\n * - deepseek (@langchain/deepseek)\n * - xai (@langchain/xai)\n * @param {string[] | \"any\"} [fields.configurableFields] - Which model parameters are configurable:\n * - undefined: No configurable fields.\n * - \"any\": All fields are configurable. (See Security Note in description)\n * - string[]: Specified fields are configurable.\n * @param {string} [fields.configPrefix] - Prefix for configurable fields at runtime.\n * @param {Record<string, any>} [fields.params] - Additional keyword args to pass to the ChatModel constructor.\n * @returns {Promise<ConfigurableModel<RunInput, CallOptions>>} A class which extends BaseChatModel.\n * @throws {Error} If modelProvider cannot be inferred or isn't supported.\n * @throws {Error} If the model provider integration package is not installed.\n *\n * @example Initialize non-configurable models\n * ```typescript\n * import { initChatModel } from \"langchain/chat_models/universal\";\n *\n * const gpt4 = await initChatModel(\"openai:gpt-4\", {\n * temperature: 0.25,\n * });\n * const gpt4Result = await gpt4.invoke(\"what's your name\");\n *\n * const claude = await initChatModel(\"anthropic:claude-3-opus-20240229\", {\n * temperature: 0.25,\n * });\n * const claudeResult = await claude.invoke(\"what's your name\");\n *\n * const gemini = await initChatModel(\"gemini-1.5-pro\", {\n * modelProvider: \"google-vertexai\",\n * temperature: 0.25,\n * });\n * const geminiResult = await gemini.invoke(\"what's your name\");\n * ```\n *\n * @example Create a partially configurable model with no default model\n * ```typescript\n * import { initChatModel } from \"langchain/chat_models/universal\";\n *\n * const configurableModel = await initChatModel(undefined, {\n * temperature: 0,\n * configurableFields: [\"model\", \"apiKey\"],\n * });\n *\n * const gpt4Result = await configurableModel.invoke(\"what's your name\", {\n * configurable: {\n * model: \"gpt-4\",\n * },\n * });\n *\n * const claudeResult = await configurableModel.invoke(\"what's your name\", {\n * configurable: {\n * model: \"claude-3-5-sonnet-20240620\",\n * },\n * });\n * ```\n *\n * @example Create a fully configurable model with a default model and a config prefix\n * ```typescript\n * import { initChatModel } from \"langchain/chat_models/universal\";\n *\n * const configurableModelWithDefault = await initChatModel(\"gpt-4\", {\n * modelProvider: \"openai\",\n * configurableFields: \"any\",\n * configPrefix: \"foo\",\n * temperature: 0,\n * });\n *\n * const openaiResult = await configurableModelWithDefault.invoke(\n * \"what's your name\",\n * {\n * configurable: {\n * foo_apiKey: process.env.OPENAI_API_KEY,\n * },\n * }\n * );\n *\n * const claudeResult = await configurableModelWithDefault.invoke(\n * \"what's your name\",\n * {\n * configurable: {\n * foo_model: \"claude-3-5-sonnet-20240620\",\n * foo_modelProvider: \"anthropic\",\n * foo_temperature: 0.6,\n * foo_apiKey: process.env.ANTHROPIC_API_KEY,\n * },\n * }\n * );\n * ```\n *\n * @example Bind tools to a configurable model:\n * ```typescript\n * import { initChatModel } from \"langchain/chat_models/universal\";\n * import { z } from \"zod/v3\";\n * import { tool } from \"@langchain/core/tools\";\n *\n * const getWeatherTool = tool(\n * (input) => {\n * // Do something with the input\n * return JSON.stringify(input);\n * },\n * {\n * schema: z\n * .object({\n * location: z\n * .string()\n * .describe(\"The city and state, e.g. San Francisco, CA\"),\n * })\n * .describe(\"Get the current weather in a given location\"),\n * name: \"GetWeather\",\n * description: \"Get the current weather in a given location\",\n * }\n * );\n *\n * const getPopulationTool = tool(\n * (input) => {\n * // Do something with the input\n * return JSON.stringify(input);\n * },\n * {\n * schema: z\n * .object({\n * location: z\n * .string()\n * .describe(\"The city and state, e.g. San Francisco, CA\"),\n * })\n * .describe(\"Get the current population in a given location\"),\n * name: \"GetPopulation\",\n * description: \"Get the current population in a given location\",\n * }\n * );\n *\n * const configurableModel = await initChatModel(\"gpt-4\", {\n * configurableFields: [\"model\", \"modelProvider\", \"apiKey\"],\n * temperature: 0,\n * });\n *\n * const configurableModelWithTools = configurableModel.bindTools([\n * getWeatherTool,\n * getPopulationTool,\n * ]);\n *\n * const configurableToolResult = await configurableModelWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\",\n * {\n * configurable: {\n * apiKey: process.env.OPENAI_API_KEY,\n * },\n * }\n * );\n *\n * const configurableToolResult2 = await configurableModelWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\",\n * {\n * configurable: {\n * model: \"claude-3-5-sonnet-20240620\",\n * apiKey: process.env.ANTHROPIC_API_KEY,\n * },\n * }\n * );\n * ```\n *\n * @description\n * This function initializes a ChatModel based on the provided model name and provider.\n * It supports various model providers and allows for runtime configuration of model parameters.\n *\n * Security Note: Setting `configurableFields` to \"any\" means fields like apiKey, baseUrl, etc.\n * can be altered at runtime, potentially redirecting model requests to a different service/user.\n * Make sure that if you're accepting untrusted configurations, you enumerate the\n * `configurableFields` explicitly.\n *\n * The function will attempt to infer the model provider from the model name if not specified.\n * Certain model name prefixes are associated with specific providers:\n * - gpt-3... or gpt-4... -> openai\n * - claude... -> anthropic\n * - amazon.... -> bedrock\n * - gemini... -> google-vertexai\n * - command... -> cohere\n * - accounts/fireworks... -> fireworks\n *\n * @since 0.2.11\n * @version 0.2.11\n */\nexport async function initChatModel<\n RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,\n CallOptions extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions\n>(\n model?: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n fields?: Partial<Record<string, any>> & {\n modelProvider?: string;\n configurableFields?: string[] | \"any\";\n configPrefix?: string;\n }\n): Promise<ConfigurableModel<RunInput, CallOptions>> {\n // eslint-disable-next-line prefer-const\n let { configurableFields, configPrefix, modelProvider, ...params } = {\n configPrefix: \"\",\n ...(fields ?? {}),\n };\n if (modelProvider === undefined && model?.includes(\":\")) {\n const modelComponents = model.split(\":\", 2);\n if (SUPPORTED_PROVIDERS.includes(modelComponents[0] as ChatModelProvider)) {\n // eslint-disable-next-line no-param-reassign\n [modelProvider, model] = modelComponents;\n }\n }\n let configurableFieldsCopy = Array.isArray(configurableFields)\n ? [...configurableFields]\n : configurableFields;\n\n if (!model && configurableFieldsCopy === undefined) {\n configurableFieldsCopy = [\"model\", \"modelProvider\"];\n }\n if (configPrefix && configurableFieldsCopy === undefined) {\n console.warn(\n `{ configPrefix: ${configPrefix} } has been set but no fields are configurable. Set ` +\n `{ configurableFields: [...] } to specify the model params that are ` +\n `configurable.`\n );\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const paramsCopy: Record<string, any> = { ...params };\n\n if (configurableFieldsCopy === undefined) {\n return new ConfigurableModel<RunInput, CallOptions>({\n defaultConfig: {\n ...paramsCopy,\n model,\n modelProvider,\n },\n configPrefix,\n });\n } else {\n if (model) {\n paramsCopy.model = model;\n }\n if (modelProvider) {\n paramsCopy.modelProvider = modelProvider;\n }\n return new ConfigurableModel<RunInput, CallOptions>({\n defaultConfig: paramsCopy,\n configPrefix,\n configurableFields: configurableFieldsCopy,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAkDA,MAAa,wBAAwB;CACnC,QAAQ;EACN,SAAS;EACT,WAAW;CACZ;CACD,WAAW;EACT,SAAS;EACT,WAAW;CACZ;CACD,cAAc;EACZ,SAAS;EACT,WAAW;CACZ;CACD,QAAQ;EACN,SAAS;EACT,WAAW;CACZ;CACD,mBAAmB;EACjB,SAAS;EACT,WAAW;CACZ;CACD,uBAAuB;EACrB,SAAS;EACT,WAAW;CACZ;CACD,gBAAgB;EACd,SAAS;EACT,WAAW;CACZ;CACD,QAAQ;EACN,SAAS;EACT,WAAW;CACZ;CACD,WAAW;EACT,SAAS;EACT,WAAW;CACZ;CACD,MAAM;EACJ,SAAS;EACT,WAAW;CACZ;CACD,UAAU;EACR,SAAS;EACT,WAAW;CACZ;CACD,SAAS;EACP,SAAS;EACT,WAAW;CACZ;CACD,UAAU;EACR,SAAS;EACT,WAAW;CACZ;CACD,KAAK;EACH,SAAS;EACT,WAAW;CACZ;CACD,WAAW;EACT,SAAS;EACT,WAAW;EACX,uBAAuB;CACxB;CACD,UAAU;EACR,SAAS;EACT,WAAW;EACX,uBAAuB;CACxB;CACD,YAAY;EACV,SAAS;EACT,WAAW;EACX,uBAAuB;CACxB;AACF;AAED,MAAM,sBAAsB,OAAO,KACjC,sBACD;;;;;;AAaD,eAAsB,wBAAwBA,WAAmB;CAE/D,MAAM,gBAAgB,OAAO,QAAQ,sBAAsB,CAAC,KAC1D,CAAC,GAAGC,SAAO,KAAKA,SAAO,cAAc,UACtC;AAED,KAAI,CAAC,cACH,QAAO;CAGT,MAAM,GAAG,OAAO,GAAG;AACnB,KAAI;EACF,MAAM,SAAS,MAAM,OAAO,O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AgB,2BAA2B,QAC7C,QAAQ,KACN,CAAC,gBAAgB,EAAE,aAAa,oIAAoD,CAEnE,CAClB;CAIH,MAAMC,aAAkC,EAAE,GAAG,OAAQ;AAErD,KAAI,2BAA2B,OAC7B,QAAO,IAAI,kBAAyC;EAClD,eAAe;GACb,GAAG;GACH;GACA;EACD;EACD;CACD;MACI;AACL,MAAI,OACF,WAAW,QAAQ;AAErB,MAAI,eACF,WAAW,gBAAgB;AAE7B,SAAO,IAAI,kBAAyC;GAClD,eAAe;GACf;GACA,oBAAoB;EACrB;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"universal.js","names":["className: string","config","e: unknown","model: string","modelProvider?: string","params: Record<string, any>","modelName: string","fields: ConfigurableModelFields","config?: RunnableConfig","messages: BaseMessage[]","options?: this[\"ParsedCallOptions\"]","runManager?: CallbackManagerForLLMRun","tools: BindToolsInput[]","params?: Record<string, any>","modelParams: Record<string, any>","str: string","prefix: string","mergedConfig: RunnableConfig","remainingConfig: RunnableConfig","input: RunInput","options?: CallOptions","inputs: RunInput[]","options?: Partial<CallOptions> | Partial<CallOptions>[]","batchOptions?: RunnableBatchOptions","generator: AsyncGenerator<RunInput>","options: CallOptions","options?: Partial<CallOptions>","streamOptions?: Omit<LogStreamCallbackHandlerInput, \"autoClose\">","options: Partial<CallOptions> & {\n version: \"v1\" | \"v2\";\n encoding?: \"text/event-stream\" | undefined;\n }","streamOptions?: Omit<EventStreamCallbackHandlerInput, \"autoClose\">","model?: string","fields?: Partial<Record<string, any>> & {\n modelProvider?: string;\n configurableFields?: string[] | \"any\";\n configPrefix?: string;\n }","paramsCopy: Record<string, any>"],"sources":["../../src/chat_models/universal.ts"],"sourcesContent":["import {\n BaseLanguageModelInput,\n ToolDefinition,\n} from \"@langchain/core/language_models/base\";\nimport {\n BaseChatModel,\n BaseChatModelParams,\n BindToolsInput,\n type BaseChatModelCallOptions,\n} from \"@langchain/core/language_models/chat_models\";\nimport {\n BaseMessage,\n type AIMessageChunk,\n MessageStructure,\n} from \"@langchain/core/messages\";\nimport {\n type RunnableBatchOptions,\n RunnableBinding,\n type RunnableConfig,\n type RunnableToolLike,\n ensureConfig,\n} from \"@langchain/core/runnables\";\nimport {\n AsyncGeneratorWithSetup,\n IterableReadableStream,\n} from \"@langchain/core/utils/stream\";\nimport {\n type LogStreamCallbackHandlerInput,\n type RunLogPatch,\n type StreamEvent,\n} from \"@langchain/core/tracers/log_stream\";\nimport { type StructuredToolInterface } from \"@langchain/core/tools\";\nimport { CallbackManagerForLLMRun } from \"@langchain/core/callbacks/manager\";\nimport { ChatResult } from \"@langchain/core/outputs\";\n\n// TODO: remove once `EventStreamCallbackHandlerInput` is exposed in core\ninterface EventStreamCallbackHandlerInput\n extends Omit<LogStreamCallbackHandlerInput, \"_schemaFormat\"> {}\n\nexport interface ConfigurableChatModelCallOptions\n extends BaseChatModelCallOptions {\n tools?: (\n | StructuredToolInterface\n | Record<string, unknown>\n | ToolDefinition\n | RunnableToolLike\n )[];\n}\n\n// Configuration map for model providers\nexport const MODEL_PROVIDER_CONFIG = {\n openai: {\n package: \"@langchain/openai\",\n className: \"ChatOpenAI\",\n },\n anthropic: {\n package: \"@langchain/anthropic\",\n className: \"ChatAnthropic\",\n },\n azure_openai: {\n package: \"@langchain/openai\",\n className: \"AzureChatOpenAI\",\n },\n cohere: {\n package: \"@langchain/cohere\",\n className: \"ChatCohere\",\n },\n \"google-vertexai\": {\n package: \"@langchain/google-vertexai\",\n className: \"ChatVertexAI\",\n },\n \"google-vertexai-web\": {\n package: \"@langchain/google-vertexai-web\",\n className: \"ChatVertexAI\",\n },\n \"google-genai\": {\n package: \"@langchain/google-genai\",\n className: \"ChatGoogleGenerativeAI\",\n },\n ollama: {\n package: \"@langchain/ollama\",\n className: \"ChatOllama\",\n },\n mistralai: {\n package: \"@langchain/mistralai\",\n className: \"ChatMistralAI\",\n },\n groq: {\n package: \"@langchain/groq\",\n className: \"ChatGroq\",\n },\n cerebras: {\n package: \"@langchain/cerebras\",\n className: \"ChatCerebras\",\n },\n bedrock: {\n package: \"@langchain/aws\",\n className: \"ChatBedrockConverse\",\n },\n deepseek: {\n package: \"@langchain/deepseek\",\n className: \"ChatDeepSeek\",\n },\n xai: {\n package: \"@langchain/xai\",\n className: \"ChatXAI\",\n },\n fireworks: {\n package: \"@langchain/community/chat_models/fireworks\",\n className: \"ChatFireworks\",\n hasCircularDependency: true,\n },\n together: {\n package: \"@langchain/community/chat_models/togetherai\",\n className: \"ChatTogetherAI\",\n hasCircularDependency: true,\n },\n perplexity: {\n package: \"@langchain/community/chat_models/perplexity\",\n className: \"ChatPerplexity\",\n hasCircularDependency: true,\n },\n} as const;\n\nconst SUPPORTED_PROVIDERS = Object.keys(\n MODEL_PROVIDER_CONFIG\n) as (keyof typeof MODEL_PROVIDER_CONFIG)[];\nexport type ChatModelProvider = keyof typeof MODEL_PROVIDER_CONFIG;\ntype ModelProviderConfig = {\n package: string;\n className: string;\n hasCircularDependency?: boolean;\n};\n\n/**\n * Helper function to get a chat model class by its class name\n * @param className The class name (e.g., \"ChatOpenAI\", \"ChatAnthropic\")\n * @returns The imported model class or undefined if not found\n */\nexport async function getChatModelByClassName(className: string) {\n // Find the provider config that matches the class name\n const providerEntry = Object.entries(MODEL_PROVIDER_CONFIG).find(\n ([, config]) => config.className === className\n );\n\n if (!providerEntry) {\n return undefined;\n }\n\n const [, config] = providerEntry;\n try {\n const module = await import(config.package);\n return module[config.className];\n } catch (e: unknown) {\n const err = e as Error;\n if (\n \"code\" in err &&\n err.code?.toString().includes(\"ERR_MODULE_NOT_FOUND\") &&\n \"message\" in err &&\n typeof err.message === \"string\"\n ) {\n const msg = err.message.startsWith(\"Error: \")\n ? err.message.slice(\"Error: \".length)\n : err.message;\n const attemptedPackage = msg\n .split(\"Cannot find package '\")[1]\n .split(\"'\")[0];\n throw new Error(\n `Unable to import ${attemptedPackage}. Please install with ` +\n `\\`npm install ${attemptedPackage}\\` or \\`pnpm install ${attemptedPackage}\\``\n );\n }\n throw e;\n }\n}\n\nasync function _initChatModelHelper(\n model: string,\n modelProvider?: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n params: Record<string, any> = {}\n): Promise<BaseChatModel> {\n const modelProviderCopy = modelProvider || _inferModelProvider(model);\n if (!modelProviderCopy) {\n throw new Error(\n `Unable to infer model provider for { model: ${model} }, please specify modelProvider directly.`\n );\n }\n\n const config = MODEL_PROVIDER_CONFIG[\n modelProviderCopy as keyof typeof MODEL_PROVIDER_CONFIG\n ] as ModelProviderConfig;\n if (!config) {\n const supported = SUPPORTED_PROVIDERS.join(\", \");\n throw new Error(\n `Unsupported { modelProvider: ${modelProviderCopy} }.\\n\\nSupported model providers are: ${supported}`\n );\n }\n\n const { modelProvider: _unused, ...passedParams } = params;\n const ProviderClass = await getChatModelByClassName(config.className);\n return new ProviderClass({ model, ...passedParams });\n}\n\n/**\n * Attempts to infer the model provider based on the given model name.\n *\n * @param {string} modelName - The name of the model to infer the provider for.\n * @returns {string | undefined} The inferred model provider name, or undefined if unable to infer.\n *\n * @example\n * _inferModelProvider(\"gpt-4\"); // returns \"openai\"\n * _inferModelProvider(\"claude-2\"); // returns \"anthropic\"\n * _inferModelProvider(\"unknown-model\"); // returns undefined\n */\nexport function _inferModelProvider(modelName: string): string | undefined {\n if (\n modelName.startsWith(\"gpt-3\") ||\n modelName.startsWith(\"gpt-4\") ||\n modelName.startsWith(\"gpt-5\") ||\n modelName.startsWith(\"o1\") ||\n modelName.startsWith(\"o3\") ||\n modelName.startsWith(\"o4\")\n ) {\n return \"openai\";\n } else if (modelName.startsWith(\"claude\")) {\n return \"anthropic\";\n } else if (modelName.startsWith(\"command\")) {\n return \"cohere\";\n } else if (modelName.startsWith(\"accounts/fireworks\")) {\n return \"fireworks\";\n } else if (modelName.startsWith(\"gemini\")) {\n return \"google-vertexai\";\n } else if (modelName.startsWith(\"amazon.\")) {\n return \"bedrock\";\n } else if (modelName.startsWith(\"mistral\")) {\n return \"mistralai\";\n } else if (modelName.startsWith(\"sonar\") || modelName.startsWith(\"pplx\")) {\n return \"perplexity\";\n } else {\n return undefined;\n }\n}\n\ninterface ConfigurableModelFields extends BaseChatModelParams {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n defaultConfig?: Record<string, any>;\n /**\n * @default \"any\"\n */\n configurableFields?: string[] | \"any\";\n /**\n * @default \"\"\n */\n configPrefix?: string;\n /**\n * Methods which should be called after the model is initialized.\n * The key will be the method name, and the value will be the arguments.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n queuedMethodOperations?: Record<string, any>;\n}\n\n/**\n * Internal class used to create chat models.\n *\n * @internal\n */\nexport class ConfigurableModel<\n RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,\n CallOptions extends\n ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions,\n> extends BaseChatModel<CallOptions, AIMessageChunk> {\n _llmType(): string {\n return \"chat_model\";\n }\n\n lc_namespace = [\"langchain\", \"chat_models\"];\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _defaultConfig?: Record<string, any> = {};\n\n /**\n * @default \"any\"\n */\n _configurableFields: string[] | \"any\" = \"any\";\n\n /**\n * @default \"\"\n */\n _configPrefix: string;\n\n /**\n * Methods which should be called after the model is initialized.\n * The key will be the method name, and the value will be the arguments.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _queuedMethodOperations: Record<string, any> = {};\n\n constructor(fields: ConfigurableModelFields) {\n super(fields);\n this._defaultConfig = fields.defaultConfig ?? {};\n\n if (fields.configurableFields === \"any\") {\n this._configurableFields = \"any\";\n } else {\n this._configurableFields = fields.configurableFields ?? [\n \"model\",\n \"modelProvider\",\n ];\n }\n\n if (fields.configPrefix) {\n this._configPrefix = fields.configPrefix.endsWith(\"_\")\n ? fields.configPrefix\n : `${fields.configPrefix}_`;\n } else {\n this._configPrefix = \"\";\n }\n\n this._queuedMethodOperations =\n fields.queuedMethodOperations ?? this._queuedMethodOperations;\n }\n\n async _model(\n config?: RunnableConfig\n ): Promise<\n BaseChatModel<BaseChatModelCallOptions, AIMessageChunk<MessageStructure>>\n > {\n const params = { ...this._defaultConfig, ...this._modelParams(config) };\n let initializedModel = await _initChatModelHelper(\n params.model,\n params.modelProvider,\n params\n );\n\n // Apply queued method operations\n const queuedMethodOperationsEntries = Object.entries(\n this._queuedMethodOperations\n );\n if (queuedMethodOperationsEntries.length > 0) {\n for (const [method, args] of queuedMethodOperationsEntries) {\n if (\n method in initializedModel &&\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n typeof (initializedModel as any)[method] === \"function\"\n ) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n initializedModel = await (initializedModel as any)[method](...args);\n }\n }\n }\n\n return initializedModel;\n }\n\n async _generate(\n messages: BaseMessage[],\n options?: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n const model = await this._model(options);\n return model._generate(messages, options ?? {}, runManager);\n }\n\n override bindTools(\n tools: BindToolsInput[],\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n params?: Record<string, any>\n ): ConfigurableModel<RunInput, CallOptions> {\n const newQueuedOperations = { ...this._queuedMethodOperations };\n newQueuedOperations.bindTools = [tools, params];\n return new ConfigurableModel<RunInput, CallOptions>({\n defaultConfig: this._defaultConfig,\n configurableFields: this._configurableFields,\n configPrefix: this._configPrefix,\n queuedMethodOperations: newQueuedOperations,\n });\n }\n\n // Extract the input types from the `BaseModel` class.\n withStructuredOutput: BaseChatModel[\"withStructuredOutput\"] = (\n schema,\n ...args\n ): ReturnType<BaseChatModel[\"withStructuredOutput\"]> => {\n const newQueuedOperations = { ...this._queuedMethodOperations };\n newQueuedOperations.withStructuredOutput = [schema, ...args];\n return new ConfigurableModel<RunInput, CallOptions>({\n defaultConfig: this._defaultConfig,\n configurableFields: this._configurableFields,\n configPrefix: this._configPrefix,\n queuedMethodOperations: newQueuedOperations,\n }) as unknown as ReturnType<BaseChatModel[\"withStructuredOutput\"]>;\n };\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _modelParams(config?: RunnableConfig): Record<string, any> {\n const configurable = config?.configurable ?? {};\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let modelParams: Record<string, any> = {};\n\n for (const [key, value] of Object.entries(configurable)) {\n if (key.startsWith(this._configPrefix)) {\n const strippedKey = this._removePrefix(key, this._configPrefix);\n modelParams[strippedKey] = value;\n }\n }\n\n if (this._configurableFields !== \"any\") {\n modelParams = Object.fromEntries(\n Object.entries(modelParams).filter(([key]) =>\n this._configurableFields.includes(key)\n )\n );\n }\n\n return modelParams;\n }\n\n _removePrefix(str: string, prefix: string): string {\n return str.startsWith(prefix) ? str.slice(prefix.length) : str;\n }\n\n /**\n * Bind config to a Runnable, returning a new Runnable.\n * @param {RunnableConfig | undefined} [config] - The config to bind.\n * @returns {RunnableBinding<RunInput, RunOutput, CallOptions>} A new RunnableBinding with the bound config.\n */\n withConfig(\n config?: RunnableConfig\n ): RunnableBinding<RunInput, AIMessageChunk, CallOptions> {\n const mergedConfig: RunnableConfig = { ...(config || {}) };\n const modelParams = this._modelParams(mergedConfig);\n\n const remainingConfig: RunnableConfig = Object.fromEntries(\n Object.entries(mergedConfig).filter(([k]) => k !== \"configurable\")\n );\n\n remainingConfig.configurable = Object.fromEntries(\n Object.entries(mergedConfig.configurable || {}).filter(\n ([k]) =>\n this._configPrefix &&\n !Object.keys(modelParams).includes(\n this._removePrefix(k, this._configPrefix)\n )\n )\n );\n\n const newConfigurableModel = new ConfigurableModel<RunInput, CallOptions>({\n defaultConfig: { ...this._defaultConfig, ...modelParams },\n configurableFields: Array.isArray(this._configurableFields)\n ? [...this._configurableFields]\n : this._configurableFields,\n configPrefix: this._configPrefix,\n queuedMethodOperations: this._queuedMethodOperations,\n });\n\n return new RunnableBinding<RunInput, AIMessageChunk, CallOptions>({\n config: mergedConfig,\n bound: newConfigurableModel,\n });\n }\n\n async invoke(\n input: RunInput,\n options?: CallOptions\n ): Promise<AIMessageChunk> {\n const model = await this._model(options);\n const config = ensureConfig(options);\n return model.invoke(input, config);\n }\n\n async stream(\n input: RunInput,\n options?: CallOptions\n ): Promise<IterableReadableStream<AIMessageChunk>> {\n const model = await this._model(options);\n const wrappedGenerator = new AsyncGeneratorWithSetup({\n generator: await model.stream(input, options),\n config: options,\n });\n await wrappedGenerator.setup;\n return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);\n }\n\n async batch(\n inputs: RunInput[],\n options?: Partial<CallOptions> | Partial<CallOptions>[],\n batchOptions?: RunnableBatchOptions & { returnExceptions?: false }\n ): Promise<AIMessageChunk[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<CallOptions> | Partial<CallOptions>[],\n batchOptions?: RunnableBatchOptions & { returnExceptions: true }\n ): Promise<(AIMessageChunk | Error)[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<CallOptions> | Partial<CallOptions>[],\n batchOptions?: RunnableBatchOptions\n ): Promise<(AIMessageChunk | Error)[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<CallOptions> | Partial<CallOptions>[],\n batchOptions?: RunnableBatchOptions\n ): Promise<(AIMessageChunk | Error)[]> {\n // We can super this since the base runnable implementation of\n // `.batch` will call `.invoke` on each input.\n return super.batch(inputs, options, batchOptions);\n }\n\n async *transform(\n generator: AsyncGenerator<RunInput>,\n options: CallOptions\n ): AsyncGenerator<AIMessageChunk> {\n const model = await this._model(options);\n const config = ensureConfig(options);\n\n yield* model.transform(generator, config);\n }\n\n async *streamLog(\n input: RunInput,\n options?: Partial<CallOptions>,\n streamOptions?: Omit<LogStreamCallbackHandlerInput, \"autoClose\">\n ): AsyncGenerator<RunLogPatch> {\n const model = await this._model(options);\n const config = ensureConfig(options);\n\n yield* model.streamLog(input, config, {\n ...streamOptions,\n _schemaFormat: \"original\",\n includeNames: streamOptions?.includeNames,\n includeTypes: streamOptions?.includeTypes,\n includeTags: streamOptions?.includeTags,\n excludeNames: streamOptions?.excludeNames,\n excludeTypes: streamOptions?.excludeTypes,\n excludeTags: streamOptions?.excludeTags,\n });\n }\n\n streamEvents(\n input: RunInput,\n options: Partial<CallOptions> & { version: \"v1\" | \"v2\" },\n streamOptions?: Omit<EventStreamCallbackHandlerInput, \"autoClose\">\n ): IterableReadableStream<StreamEvent>;\n\n streamEvents(\n input: RunInput,\n options: Partial<CallOptions> & {\n version: \"v1\" | \"v2\";\n encoding: \"text/event-stream\";\n },\n streamOptions?: Omit<EventStreamCallbackHandlerInput, \"autoClose\">\n ): IterableReadableStream<Uint8Array>;\n\n streamEvents(\n input: RunInput,\n options: Partial<CallOptions> & {\n version: \"v1\" | \"v2\";\n encoding?: \"text/event-stream\" | undefined;\n },\n streamOptions?: Omit<EventStreamCallbackHandlerInput, \"autoClose\">\n ): IterableReadableStream<StreamEvent | Uint8Array> {\n const outerThis = this;\n async function* wrappedGenerator() {\n const model = await outerThis._model(options);\n const config = ensureConfig(options);\n const eventStream = model.streamEvents(input, config, streamOptions);\n\n for await (const chunk of eventStream) {\n yield chunk;\n }\n }\n return IterableReadableStream.fromAsyncGenerator(wrappedGenerator());\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface InitChatModelFields extends Partial<Record<string, any>> {\n modelProvider?: string;\n configurableFields?: string[] | \"any\";\n configPrefix?: string;\n}\n\nexport type ConfigurableFields = \"any\" | string[];\n\nexport async function initChatModel<\n RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,\n CallOptions extends\n ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions,\n>(\n model: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n fields?: Partial<Record<string, any>> & {\n modelProvider?: string;\n configurableFields?: never;\n configPrefix?: string;\n }\n): Promise<ConfigurableModel<RunInput, CallOptions>>;\n\nexport async function initChatModel<\n RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,\n CallOptions extends\n ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions,\n>(\n model: never,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n options?: Partial<Record<string, any>> & {\n modelProvider?: string;\n configurableFields?: never;\n configPrefix?: string;\n }\n): Promise<ConfigurableModel<RunInput, CallOptions>>;\n\nexport async function initChatModel<\n RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,\n CallOptions extends\n ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions,\n>(\n model?: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n options?: Partial<Record<string, any>> & {\n modelProvider?: string;\n configurableFields?: ConfigurableFields;\n configPrefix?: string;\n }\n): Promise<ConfigurableModel<RunInput, CallOptions>>;\n\n// ################################# FOR CONTRIBUTORS #################################\n//\n// If adding support for a new provider, please append the provider\n// name to the supported list in the docstring below.\n//\n// ####################################################################################\n\n/**\n * Initialize a ChatModel from the model name and provider.\n * Must have the integration package corresponding to the model provider installed.\n *\n * @template {extends BaseLanguageModelInput = BaseLanguageModelInput} RunInput - The input type for the model.\n * @template {extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions} CallOptions - Call options for the model.\n *\n * @param {string | ChatModelProvider} [model] - The name of the model, e.g. \"gpt-4\", \"claude-3-opus-20240229\".\n * Can be prefixed with the model provider, e.g. \"openai:gpt-4\", \"anthropic:claude-3-opus-20240229\".\n * @param {Object} [fields] - Additional configuration options.\n * @param {string} [fields.modelProvider] - The model provider. Supported values include:\n * - openai (@langchain/openai)\n * - anthropic (@langchain/anthropic)\n * - azure_openai (@langchain/openai)\n * - google-vertexai (@langchain/google-vertexai)\n * - google-vertexai-web (@langchain/google-vertexai-web)\n * - google-genai (@langchain/google-genai)\n * - bedrock (@langchain/aws)\n * - cohere (@langchain/cohere)\n * - fireworks (@langchain/community/chat_models/fireworks)\n * - together (@langchain/community/chat_models/togetherai)\n * - mistralai (@langchain/mistralai)\n * - groq (@langchain/groq)\n * - ollama (@langchain/ollama)\n * - perplexity (@langchain/community/chat_models/perplexity)\n * - cerebras (@langchain/cerebras)\n * - deepseek (@langchain/deepseek)\n * - xai (@langchain/xai)\n * @param {string[] | \"any\"} [fields.configurableFields] - Which model parameters are configurable:\n * - undefined: No configurable fields.\n * - \"any\": All fields are configurable. (See Security Note in description)\n * - string[]: Specified fields are configurable.\n * @param {string} [fields.configPrefix] - Prefix for configurable fields at runtime.\n * @param {Record<string, any>} [fields.params] - Additional keyword args to pass to the ChatModel constructor.\n * @returns {Promise<ConfigurableModel<RunInput, CallOptions>>} A class which extends BaseChatModel.\n * @throws {Error} If modelProvider cannot be inferred or isn't supported.\n * @throws {Error} If the model provider integration package is not installed.\n *\n * @example Initialize non-configurable models\n * ```typescript\n * import { initChatModel } from \"langchain/chat_models/universal\";\n *\n * const gpt4 = await initChatModel(\"openai:gpt-4\", {\n * temperature: 0.25,\n * });\n * const gpt4Result = await gpt4.invoke(\"what's your name\");\n *\n * const claude = await initChatModel(\"anthropic:claude-3-opus-20240229\", {\n * temperature: 0.25,\n * });\n * const claudeResult = await claude.invoke(\"what's your name\");\n *\n * const gemini = await initChatModel(\"gemini-1.5-pro\", {\n * modelProvider: \"google-vertexai\",\n * temperature: 0.25,\n * });\n * const geminiResult = await gemini.invoke(\"what's your name\");\n * ```\n *\n * @example Create a partially configurable model with no default model\n * ```typescript\n * import { initChatModel } from \"langchain/chat_models/universal\";\n *\n * const configurableModel = await initChatModel(undefined, {\n * temperature: 0,\n * configurableFields: [\"model\", \"apiKey\"],\n * });\n *\n * const gpt4Result = await configurableModel.invoke(\"what's your name\", {\n * configurable: {\n * model: \"gpt-4\",\n * },\n * });\n *\n * const claudeResult = await configurableModel.invoke(\"what's your name\", {\n * configurable: {\n * model: \"claude-3-5-sonnet-20240620\",\n * },\n * });\n * ```\n *\n * @example Create a fully configurable model with a default model and a config prefix\n * ```typescript\n * import { initChatModel } from \"langchain/chat_models/universal\";\n *\n * const configurableModelWithDefault = await initChatModel(\"gpt-4\", {\n * modelProvider: \"openai\",\n * configurableFields: \"any\",\n * configPrefix: \"foo\",\n * temperature: 0,\n * });\n *\n * const openaiResult = await configurableModelWithDefault.invoke(\n * \"what's your name\",\n * {\n * configurable: {\n * foo_apiKey: process.env.OPENAI_API_KEY,\n * },\n * }\n * );\n *\n * const claudeResult = await configurableModelWithDefault.invoke(\n * \"what's your name\",\n * {\n * configurable: {\n * foo_model: \"claude-3-5-sonnet-20240620\",\n * foo_modelProvider: \"anthropic\",\n * foo_temperature: 0.6,\n * foo_apiKey: process.env.ANTHROPIC_API_KEY,\n * },\n * }\n * );\n * ```\n *\n * @example Bind tools to a configurable model:\n * ```typescript\n * import { initChatModel } from \"langchain/chat_models/universal\";\n * import { z } from \"zod/v3\";\n * import { tool } from \"@langchain/core/tools\";\n *\n * const getWeatherTool = tool(\n * (input) => {\n * // Do something with the input\n * return JSON.stringify(input);\n * },\n * {\n * schema: z\n * .object({\n * location: z\n * .string()\n * .describe(\"The city and state, e.g. San Francisco, CA\"),\n * })\n * .describe(\"Get the current weather in a given location\"),\n * name: \"GetWeather\",\n * description: \"Get the current weather in a given location\",\n * }\n * );\n *\n * const getPopulationTool = tool(\n * (input) => {\n * // Do something with the input\n * return JSON.stringify(input);\n * },\n * {\n * schema: z\n * .object({\n * location: z\n * .string()\n * .describe(\"The city and state, e.g. San Francisco, CA\"),\n * })\n * .describe(\"Get the current population in a given location\"),\n * name: \"GetPopulation\",\n * description: \"Get the current population in a given location\",\n * }\n * );\n *\n * const configurableModel = await initChatModel(\"gpt-4\", {\n * configurableFields: [\"model\", \"modelProvider\", \"apiKey\"],\n * temperature: 0,\n * });\n *\n * const configurableModelWithTools = configurableModel.bindTools([\n * getWeatherTool,\n * getPopulationTool,\n * ]);\n *\n * const configurableToolResult = await configurableModelWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\",\n * {\n * configurable: {\n * apiKey: process.env.OPENAI_API_KEY,\n * },\n * }\n * );\n *\n * const configurableToolResult2 = await configurableModelWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\",\n * {\n * configurable: {\n * model: \"claude-3-5-sonnet-20240620\",\n * apiKey: process.env.ANTHROPIC_API_KEY,\n * },\n * }\n * );\n * ```\n *\n * @description\n * This function initializes a ChatModel based on the provided model name and provider.\n * It supports various model providers and allows for runtime configuration of model parameters.\n *\n * Security Note: Setting `configurableFields` to \"any\" means fields like apiKey, baseUrl, etc.\n * can be altered at runtime, potentially redirecting model requests to a different service/user.\n * Make sure that if you're accepting untrusted configurations, you enumerate the\n * `configurableFields` explicitly.\n *\n * The function will attempt to infer the model provider from the model name if not specified.\n * Certain model name prefixes are associated with specific providers:\n * - gpt-3... or gpt-4... -> openai\n * - claude... -> anthropic\n * - amazon.... -> bedrock\n * - gemini... -> google-vertexai\n * - command... -> cohere\n * - accounts/fireworks... -> fireworks\n *\n * @since 0.2.11\n * @version 0.2.11\n */\nexport async function initChatModel<\n RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,\n CallOptions extends\n ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions,\n>(\n model?: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n fields?: Partial<Record<string, any>> & {\n modelProvider?: string;\n configurableFields?: string[] | \"any\";\n configPrefix?: string;\n }\n): Promise<ConfigurableModel<RunInput, CallOptions>> {\n // eslint-disable-next-line prefer-const\n let { configurableFields, configPrefix, modelProvider, ...params } = {\n configPrefix: \"\",\n ...(fields ?? {}),\n };\n if (modelProvider === undefined && model?.includes(\":\")) {\n const modelComponents = model.split(\":\", 2);\n if (SUPPORTED_PROVIDERS.includes(modelComponents[0] as ChatModelProvider)) {\n // eslint-disable-next-line no-param-reassign\n [modelProvider, model] = modelComponents;\n }\n }\n let configurableFieldsCopy = Array.isArray(configurableFields)\n ? [...configurableFields]\n : configurableFields;\n\n if (!model && configurableFieldsCopy === undefined) {\n configurableFieldsCopy = [\"model\", \"modelProvider\"];\n }\n if (configPrefix && configurableFieldsCopy === undefined) {\n console.warn(\n `{ configPrefix: ${configPrefix} } has been set but no fields are configurable. Set ` +\n `{ configurableFields: [...] } to specify the model params that are ` +\n `configurable.`\n );\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const paramsCopy: Record<string, any> = { ...params };\n\n if (configurableFieldsCopy === undefined) {\n return new ConfigurableModel<RunInput, CallOptions>({\n defaultConfig: {\n ...paramsCopy,\n model,\n modelProvider,\n },\n configPrefix,\n });\n } else {\n if (model) {\n paramsCopy.model = model;\n }\n if (modelProvider) {\n paramsCopy.modelProvider = modelProvider;\n }\n return new ConfigurableModel<RunInput, CallOptions>({\n defaultConfig: paramsCopy,\n configPrefix,\n configurableFields: configurableFieldsCopy,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAkDA,MAAa,wBAAwB;CACnC,QAAQ;EACN,SAAS;EACT,WAAW;CACZ;CACD,WAAW;EACT,SAAS;EACT,WAAW;CACZ;CACD,cAAc;EACZ,SAAS;EACT,WAAW;CACZ;CACD,QAAQ;EACN,SAAS;EACT,WAAW;CACZ;CACD,mBAAmB;EACjB,SAAS;EACT,WAAW;CACZ;CACD,uBAAuB;EACrB,SAAS;EACT,WAAW;CACZ;CACD,gBAAgB;EACd,SAAS;EACT,WAAW;CACZ;CACD,QAAQ;EACN,SAAS;EACT,WAAW;CACZ;CACD,WAAW;EACT,SAAS;EACT,WAAW;CACZ;CACD,MAAM;EACJ,SAAS;EACT,WAAW;CACZ;CACD,UAAU;EACR,SAAS;EACT,WAAW;CACZ;CACD,SAAS;EACP,SAAS;EACT,WAAW;CACZ;CACD,UAAU;EACR,SAAS;EACT,WAAW;CACZ;CACD,KAAK;EACH,SAAS;EACT,WAAW;CACZ;CACD,WAAW;EACT,SAAS;EACT,WAAW;EACX,uBAAuB;CACxB;CACD,UAAU;EACR,SAAS;EACT,WAAW;EACX,uBAAuB;CACxB;CACD,YAAY;EACV,SAAS;EACT,WAAW;EACX,uBAAuB;CACxB;AACF;AAED,MAAM,sBAAsB,OAAO,KACjC,sBACD;;;;;;AAaD,eAAsB,wBAAwBA,WAAmB;CAE/D,MAAM,gBAAgB,OAAO,QAAQ,sBA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E5B;CACD,IAAI,yBAAyB,MAAM,QAAQ,mBAAmB,GAC1D,CAAC,GAAG,kBAAmB,IACvB;AAEJ,KAAI,CAAC,SAAS,2BAA2B,QACvC,yBAAyB,CAAC,SAAS,eAAgB;AAErD,KAAI,gBAAgB,2BAA2B,QAC7C,QAAQ,KACN,CAAC,gBAAgB,EAAE,aAAa,oIAAoD,CAEnE,CAClB;CAIH,MAAMC,aAAkC,EAAE,GAAG,OAAQ;AAErD,KAAI,2BAA2B,OAC7B,QAAO,IAAI,kBAAyC;EAClD,eAAe;GACb,GAAG;GACH;GACA;EACD;EACD;CACD;MACI;AACL,MAAI,OACF,WAAW,QAAQ;AAErB,MAAI,eACF,WAAW,gBAAgB;AAE7B,SAAO,IAAI,kBAAyC;GAClD,eAAe;GACf;GACA,oBAAoB;EACrB;CACF;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.cjs","names":["TextLoader","filePathOrBlob: string | Blob","pointers: string | string[]","raw: string","json: any","pointers: jsonpointer[]","extractedString: string[]","entry: object","extractedStrings: string[]","json: object","pointer: string"],"sources":["../../../src/document_loaders/fs/json.ts"],"sourcesContent":["import jsonpointer from \"jsonpointer\";\nimport { TextLoader } from \"./text.js\";\n\n/**\n * Class that extends the `TextLoader` class. It represents a document\n * loader that loads documents from JSON files. It has a constructor that\n * takes a `filePathOrBlob` parameter representing the path to the JSON\n * file or a `Blob` object, and an optional `pointers` parameter that\n * specifies the JSON pointers to extract.\n */\nexport class JSONLoader extends TextLoader {\n public pointers: string[];\n\n constructor(filePathOrBlob: string | Blob, pointers: string | string[] = []) {\n super(filePathOrBlob);\n this.pointers = Array.isArray(pointers) ? pointers : [pointers];\n }\n\n /**\n * Method that takes a `raw` string as a parameter and returns a promise\n * that resolves to an array of strings. It parses the raw JSON string and\n * extracts the values based on the specified JSON pointers. If no JSON\n * pointers are specified, it extracts all the strings from the JSON\n * object.\n * @param raw The raw JSON string to parse.\n * @returns A promise that resolves to an array of strings.\n */\n protected async parse(raw: string): Promise<string[]> {\n const json = JSON.parse(raw.trim());\n // If there is no pointers specified we extract all strings we found\n const extractAllStrings = !(this.pointers.length > 0);\n const compiledPointers = this.pointers.map((pointer) =>\n jsonpointer.compile(pointer)\n );\n\n return this.extractArrayStringsFromObject(\n json,\n compiledPointers,\n extractAllStrings\n );\n }\n\n /**\n * If JSON pointers are specified, return all strings below any of them\n * and exclude all other nodes expect if they match a JSON pointer (to allow to extract strings from different levels)\n *\n * If no JSON pointer is specified then return all string in the object\n */\n private extractArrayStringsFromObject(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n json: any,\n pointers: jsonpointer[],\n extractAllStrings = false,\n keyHasBeenFound = false\n ): string[] {\n if (!json) {\n return [];\n }\n\n if (typeof json === \"string\" && extractAllStrings) {\n return [json];\n }\n\n if (Array.isArray(json) && extractAllStrings) {\n let extractedString: string[] = [];\n for (const element of json) {\n extractedString = extractedString.concat(\n this.extractArrayStringsFromObject(element, pointers, true)\n );\n }\n\n return extractedString;\n }\n\n if (typeof json === \"object\") {\n if (extractAllStrings) {\n return this.extractArrayStringsFromObject(\n Object.values(json),\n pointers,\n true\n );\n }\n\n const targetedEntries = this.getTargetedEntries(json, pointers);\n const thisLevelEntries = Object.values(json) as object[];\n const notTargetedEntries = thisLevelEntries.filter(\n (entry: object) => !targetedEntries.includes(entry)\n );\n\n let extractedStrings: string[] = [];\n // If we found a targeted entry, we extract all strings from it\n if (targetedEntries.length > 0) {\n for (const oneEntry of targetedEntries) {\n extractedStrings = extractedStrings.concat(\n this.extractArrayStringsFromObject(oneEntry, pointers, true, true)\n );\n }\n\n for (const oneEntry of notTargetedEntries) {\n extractedStrings = extractedStrings.concat(\n this.extractArrayStringsFromObject(oneEntry, pointers, false, true)\n );\n }\n } else if (extractAllStrings || !keyHasBeenFound) {\n for (const oneEntry of notTargetedEntries) {\n extractedStrings = extractedStrings.concat(\n this.extractArrayStringsFromObject(\n oneEntry,\n pointers,\n extractAllStrings\n )\n );\n }\n }\n\n return extractedStrings;\n }\n\n return [];\n }\n\n /**\n * Method that takes a `json` object and an array of `pointers` as\n * parameters and returns an array of targeted entries. It iterates over\n * the JSON pointers and uses the `jsonpointer.get()` function to get the\n * targeted entries from the JSON object.\n * @param json The JSON object to get targeted entries from.\n * @param pointers The JSON pointers to get targeted entries.\n * @returns An array of targeted entries.\n */\n private getTargetedEntries(json: object, pointers: jsonpointer[]): object[] {\n const targetEntries = [];\n for (const pointer of pointers) {\n const targetedEntry = pointer.get(json);\n if (targetedEntry) {\n targetEntries.push(targetedEntry);\n }\n }\n\n return targetEntries;\n }\n}\n\n/**\n * Class that extends the `TextLoader` class. It represents a document\n * loader that loads documents from JSON Lines files. It has a constructor\n * that takes a `filePathOrBlob` parameter representing the path to the\n * JSON Lines file or a `Blob` object, and a `pointer` parameter that\n * specifies the JSON pointer to extract.\n */\nexport class JSONLinesLoader extends TextLoader {\n constructor(filePathOrBlob: string | Blob
|
|
1
|
+
{"version":3,"file":"json.cjs","names":["TextLoader","filePathOrBlob: string | Blob","pointers: string | string[]","raw: string","json: any","pointers: jsonpointer[]","extractedString: string[]","entry: object","extractedStrings: string[]","json: object","pointer: string"],"sources":["../../../src/document_loaders/fs/json.ts"],"sourcesContent":["import jsonpointer from \"jsonpointer\";\nimport { TextLoader } from \"./text.js\";\n\n/**\n * Class that extends the `TextLoader` class. It represents a document\n * loader that loads documents from JSON files. It has a constructor that\n * takes a `filePathOrBlob` parameter representing the path to the JSON\n * file or a `Blob` object, and an optional `pointers` parameter that\n * specifies the JSON pointers to extract.\n */\nexport class JSONLoader extends TextLoader {\n public pointers: string[];\n\n constructor(filePathOrBlob: string | Blob, pointers: string | string[] = []) {\n super(filePathOrBlob);\n this.pointers = Array.isArray(pointers) ? pointers : [pointers];\n }\n\n /**\n * Method that takes a `raw` string as a parameter and returns a promise\n * that resolves to an array of strings. It parses the raw JSON string and\n * extracts the values based on the specified JSON pointers. If no JSON\n * pointers are specified, it extracts all the strings from the JSON\n * object.\n * @param raw The raw JSON string to parse.\n * @returns A promise that resolves to an array of strings.\n */\n protected async parse(raw: string): Promise<string[]> {\n const json = JSON.parse(raw.trim());\n // If there is no pointers specified we extract all strings we found\n const extractAllStrings = !(this.pointers.length > 0);\n const compiledPointers = this.pointers.map((pointer) =>\n jsonpointer.compile(pointer)\n );\n\n return this.extractArrayStringsFromObject(\n json,\n compiledPointers,\n extractAllStrings\n );\n }\n\n /**\n * If JSON pointers are specified, return all strings below any of them\n * and exclude all other nodes expect if they match a JSON pointer (to allow to extract strings from different levels)\n *\n * If no JSON pointer is specified then return all string in the object\n */\n private extractArrayStringsFromObject(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n json: any,\n pointers: jsonpointer[],\n extractAllStrings = false,\n keyHasBeenFound = false\n ): string[] {\n if (!json) {\n return [];\n }\n\n if (typeof json === \"string\" && extractAllStrings) {\n return [json];\n }\n\n if (Array.isArray(json) && extractAllStrings) {\n let extractedString: string[] = [];\n for (const element of json) {\n extractedString = extractedString.concat(\n this.extractArrayStringsFromObject(element, pointers, true)\n );\n }\n\n return extractedString;\n }\n\n if (typeof json === \"object\") {\n if (extractAllStrings) {\n return this.extractArrayStringsFromObject(\n Object.values(json),\n pointers,\n true\n );\n }\n\n const targetedEntries = this.getTargetedEntries(json, pointers);\n const thisLevelEntries = Object.values(json) as object[];\n const notTargetedEntries = thisLevelEntries.filter(\n (entry: object) => !targetedEntries.includes(entry)\n );\n\n let extractedStrings: string[] = [];\n // If we found a targeted entry, we extract all strings from it\n if (targetedEntries.length > 0) {\n for (const oneEntry of targetedEntries) {\n extractedStrings = extractedStrings.concat(\n this.extractArrayStringsFromObject(oneEntry, pointers, true, true)\n );\n }\n\n for (const oneEntry of notTargetedEntries) {\n extractedStrings = extractedStrings.concat(\n this.extractArrayStringsFromObject(oneEntry, pointers, false, true)\n );\n }\n } else if (extractAllStrings || !keyHasBeenFound) {\n for (const oneEntry of notTargetedEntries) {\n extractedStrings = extractedStrings.concat(\n this.extractArrayStringsFromObject(\n oneEntry,\n pointers,\n extractAllStrings\n )\n );\n }\n }\n\n return extractedStrings;\n }\n\n return [];\n }\n\n /**\n * Method that takes a `json` object and an array of `pointers` as\n * parameters and returns an array of targeted entries. It iterates over\n * the JSON pointers and uses the `jsonpointer.get()` function to get the\n * targeted entries from the JSON object.\n * @param json The JSON object to get targeted entries from.\n * @param pointers The JSON pointers to get targeted entries.\n * @returns An array of targeted entries.\n */\n private getTargetedEntries(json: object, pointers: jsonpointer[]): object[] {\n const targetEntries = [];\n for (const pointer of pointers) {\n const targetedEntry = pointer.get(json);\n if (targetedEntry) {\n targetEntries.push(targetedEntry);\n }\n }\n\n return targetEntries;\n }\n}\n\n/**\n * Class that extends the `TextLoader` class. It represents a document\n * loader that loads documents from JSON Lines files. It has a constructor\n * that takes a `filePathOrBlob` parameter representing the path to the\n * JSON Lines file or a `Blob` object, and a `pointer` parameter that\n * specifies the JSON pointer to extract.\n */\nexport class JSONLinesLoader extends TextLoader {\n constructor(\n filePathOrBlob: string | Blob,\n public pointer: string\n ) {\n super(filePathOrBlob);\n }\n\n /**\n * Method that takes a `raw` string as a parameter and returns a promise\n * that resolves to an array of strings. It parses the raw JSON Lines\n * string, splits it into lines, parses each line as JSON, and extracts\n * the values based on the specified JSON pointer.\n * @param raw The raw JSON Lines string to parse.\n * @returns A promise that resolves to an array of strings.\n */\n protected async parse(raw: string): Promise<string[]> {\n const lines = raw.split(\"\\n\");\n const jsons = lines\n .map((line) => line.trim())\n .filter(Boolean)\n .map((line) => JSON.parse(line));\n const pointer = jsonpointer.compile(this.pointer);\n return jsons.map((json) => {\n const data = pointer.get(json);\n if (typeof data === \"string\") {\n return data;\n }\n if (!data) {\n return \"\";\n }\n if (typeof data === \"object\") {\n return JSON.stringify(data);\n }\n return \"\";\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAUA,IAAa,aAAb,cAAgCA,4CAAW;CACzC,AAAO;CAEP,YAAYC,gBAA+BC,WAA8B,CAAE,GA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AAqB,CAAC,gBAC/B,MAAK,MAAM,YAAY,oBACrB,mBAAmB,iBAAiB,OAClC,KAAK,8BACH,UACA,UACA,kBACD,CACF;AAIL,UAAO;EACR;AAED,SAAO,CAAE;CACV;;;;;;;;;;CAWD,AAAQ,mBAAmBC,MAAcJ,UAAmC;EAC1E,MAAM,gBAAgB,CAAE;AACxB,OAAK,MAAM,WAAW,UAAU;GAC9B,MAAM,gBAAgB,QAAQ,IAAI,KAAK;AACvC,OAAI,eACF,cAAc,KAAK,cAAc;EAEpC;AAED,SAAO;CACR;AACF;;;;;;;;AASD,IAAa,kBAAb,cAAqCL,4CAAW;CAC9C,YACEC,gBACOS,SACP;EACA,MAAM,eAAe;EAFd;CAGR;;;;;;;;;CAUD,MAAgB,MAAMP,KAAgC;EACpD,MAAM,QAAQ,IAAI,MAAM,KAAK;EAC7B,MAAM,QAAQ,MACX,IAAI,CAAC,SAAS,KAAK,MAAM,CAAC,CAC1B,OAAO,QAAQ,CACf,IAAI,CAAC,SAAS,KAAK,MAAM,KAAK,CAAC;EAClC,MAAM,UAAU,oBAAY,QAAQ,KAAK,QAAQ;AACjD,SAAO,MAAM,IAAI,CAAC,SAAS;GACzB,MAAM,OAAO,QAAQ,IAAI,KAAK;AAC9B,OAAI,OAAO,SAAS,SAClB,QAAO;AAET,OAAI,CAAC,KACH,QAAO;AAET,OAAI,OAAO,SAAS,SAClB,QAAO,KAAK,UAAU,KAAK;AAE7B,UAAO;EACR,EAAC;CACH;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.js","names":["filePathOrBlob: string | Blob","pointers: string | string[]","raw: string","json: any","pointers: jsonpointer[]","extractedString: string[]","entry: object","extractedStrings: string[]","json: object","pointer: string"],"sources":["../../../src/document_loaders/fs/json.ts"],"sourcesContent":["import jsonpointer from \"jsonpointer\";\nimport { TextLoader } from \"./text.js\";\n\n/**\n * Class that extends the `TextLoader` class. It represents a document\n * loader that loads documents from JSON files. It has a constructor that\n * takes a `filePathOrBlob` parameter representing the path to the JSON\n * file or a `Blob` object, and an optional `pointers` parameter that\n * specifies the JSON pointers to extract.\n */\nexport class JSONLoader extends TextLoader {\n public pointers: string[];\n\n constructor(filePathOrBlob: string | Blob, pointers: string | string[] = []) {\n super(filePathOrBlob);\n this.pointers = Array.isArray(pointers) ? pointers : [pointers];\n }\n\n /**\n * Method that takes a `raw` string as a parameter and returns a promise\n * that resolves to an array of strings. It parses the raw JSON string and\n * extracts the values based on the specified JSON pointers. If no JSON\n * pointers are specified, it extracts all the strings from the JSON\n * object.\n * @param raw The raw JSON string to parse.\n * @returns A promise that resolves to an array of strings.\n */\n protected async parse(raw: string): Promise<string[]> {\n const json = JSON.parse(raw.trim());\n // If there is no pointers specified we extract all strings we found\n const extractAllStrings = !(this.pointers.length > 0);\n const compiledPointers = this.pointers.map((pointer) =>\n jsonpointer.compile(pointer)\n );\n\n return this.extractArrayStringsFromObject(\n json,\n compiledPointers,\n extractAllStrings\n );\n }\n\n /**\n * If JSON pointers are specified, return all strings below any of them\n * and exclude all other nodes expect if they match a JSON pointer (to allow to extract strings from different levels)\n *\n * If no JSON pointer is specified then return all string in the object\n */\n private extractArrayStringsFromObject(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n json: any,\n pointers: jsonpointer[],\n extractAllStrings = false,\n keyHasBeenFound = false\n ): string[] {\n if (!json) {\n return [];\n }\n\n if (typeof json === \"string\" && extractAllStrings) {\n return [json];\n }\n\n if (Array.isArray(json) && extractAllStrings) {\n let extractedString: string[] = [];\n for (const element of json) {\n extractedString = extractedString.concat(\n this.extractArrayStringsFromObject(element, pointers, true)\n );\n }\n\n return extractedString;\n }\n\n if (typeof json === \"object\") {\n if (extractAllStrings) {\n return this.extractArrayStringsFromObject(\n Object.values(json),\n pointers,\n true\n );\n }\n\n const targetedEntries = this.getTargetedEntries(json, pointers);\n const thisLevelEntries = Object.values(json) as object[];\n const notTargetedEntries = thisLevelEntries.filter(\n (entry: object) => !targetedEntries.includes(entry)\n );\n\n let extractedStrings: string[] = [];\n // If we found a targeted entry, we extract all strings from it\n if (targetedEntries.length > 0) {\n for (const oneEntry of targetedEntries) {\n extractedStrings = extractedStrings.concat(\n this.extractArrayStringsFromObject(oneEntry, pointers, true, true)\n );\n }\n\n for (const oneEntry of notTargetedEntries) {\n extractedStrings = extractedStrings.concat(\n this.extractArrayStringsFromObject(oneEntry, pointers, false, true)\n );\n }\n } else if (extractAllStrings || !keyHasBeenFound) {\n for (const oneEntry of notTargetedEntries) {\n extractedStrings = extractedStrings.concat(\n this.extractArrayStringsFromObject(\n oneEntry,\n pointers,\n extractAllStrings\n )\n );\n }\n }\n\n return extractedStrings;\n }\n\n return [];\n }\n\n /**\n * Method that takes a `json` object and an array of `pointers` as\n * parameters and returns an array of targeted entries. It iterates over\n * the JSON pointers and uses the `jsonpointer.get()` function to get the\n * targeted entries from the JSON object.\n * @param json The JSON object to get targeted entries from.\n * @param pointers The JSON pointers to get targeted entries.\n * @returns An array of targeted entries.\n */\n private getTargetedEntries(json: object, pointers: jsonpointer[]): object[] {\n const targetEntries = [];\n for (const pointer of pointers) {\n const targetedEntry = pointer.get(json);\n if (targetedEntry) {\n targetEntries.push(targetedEntry);\n }\n }\n\n return targetEntries;\n }\n}\n\n/**\n * Class that extends the `TextLoader` class. It represents a document\n * loader that loads documents from JSON Lines files. It has a constructor\n * that takes a `filePathOrBlob` parameter representing the path to the\n * JSON Lines file or a `Blob` object, and a `pointer` parameter that\n * specifies the JSON pointer to extract.\n */\nexport class JSONLinesLoader extends TextLoader {\n constructor(filePathOrBlob: string | Blob
|
|
1
|
+
{"version":3,"file":"json.js","names":["filePathOrBlob: string | Blob","pointers: string | string[]","raw: string","json: any","pointers: jsonpointer[]","extractedString: string[]","entry: object","extractedStrings: string[]","json: object","pointer: string"],"sources":["../../../src/document_loaders/fs/json.ts"],"sourcesContent":["import jsonpointer from \"jsonpointer\";\nimport { TextLoader } from \"./text.js\";\n\n/**\n * Class that extends the `TextLoader` class. It represents a document\n * loader that loads documents from JSON files. It has a constructor that\n * takes a `filePathOrBlob` parameter representing the path to the JSON\n * file or a `Blob` object, and an optional `pointers` parameter that\n * specifies the JSON pointers to extract.\n */\nexport class JSONLoader extends TextLoader {\n public pointers: string[];\n\n constructor(filePathOrBlob: string | Blob, pointers: string | string[] = []) {\n super(filePathOrBlob);\n this.pointers = Array.isArray(pointers) ? pointers : [pointers];\n }\n\n /**\n * Method that takes a `raw` string as a parameter and returns a promise\n * that resolves to an array of strings. It parses the raw JSON string and\n * extracts the values based on the specified JSON pointers. If no JSON\n * pointers are specified, it extracts all the strings from the JSON\n * object.\n * @param raw The raw JSON string to parse.\n * @returns A promise that resolves to an array of strings.\n */\n protected async parse(raw: string): Promise<string[]> {\n const json = JSON.parse(raw.trim());\n // If there is no pointers specified we extract all strings we found\n const extractAllStrings = !(this.pointers.length > 0);\n const compiledPointers = this.pointers.map((pointer) =>\n jsonpointer.compile(pointer)\n );\n\n return this.extractArrayStringsFromObject(\n json,\n compiledPointers,\n extractAllStrings\n );\n }\n\n /**\n * If JSON pointers are specified, return all strings below any of them\n * and exclude all other nodes expect if they match a JSON pointer (to allow to extract strings from different levels)\n *\n * If no JSON pointer is specified then return all string in the object\n */\n private extractArrayStringsFromObject(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n json: any,\n pointers: jsonpointer[],\n extractAllStrings = false,\n keyHasBeenFound = false\n ): string[] {\n if (!json) {\n return [];\n }\n\n if (typeof json === \"string\" && extractAllStrings) {\n return [json];\n }\n\n if (Array.isArray(json) && extractAllStrings) {\n let extractedString: string[] = [];\n for (const element of json) {\n extractedString = extractedString.concat(\n this.extractArrayStringsFromObject(element, pointers, true)\n );\n }\n\n return extractedString;\n }\n\n if (typeof json === \"object\") {\n if (extractAllStrings) {\n return this.extractArrayStringsFromObject(\n Object.values(json),\n pointers,\n true\n );\n }\n\n const targetedEntries = this.getTargetedEntries(json, pointers);\n const thisLevelEntries = Object.values(json) as object[];\n const notTargetedEntries = thisLevelEntries.filter(\n (entry: object) => !targetedEntries.includes(entry)\n );\n\n let extractedStrings: string[] = [];\n // If we found a targeted entry, we extract all strings from it\n if (targetedEntries.length > 0) {\n for (const oneEntry of targetedEntries) {\n extractedStrings = extractedStrings.concat(\n this.extractArrayStringsFromObject(oneEntry, pointers, true, true)\n );\n }\n\n for (const oneEntry of notTargetedEntries) {\n extractedStrings = extractedStrings.concat(\n this.extractArrayStringsFromObject(oneEntry, pointers, false, true)\n );\n }\n } else if (extractAllStrings || !keyHasBeenFound) {\n for (const oneEntry of notTargetedEntries) {\n extractedStrings = extractedStrings.concat(\n this.extractArrayStringsFromObject(\n oneEntry,\n pointers,\n extractAllStrings\n )\n );\n }\n }\n\n return extractedStrings;\n }\n\n return [];\n }\n\n /**\n * Method that takes a `json` object and an array of `pointers` as\n * parameters and returns an array of targeted entries. It iterates over\n * the JSON pointers and uses the `jsonpointer.get()` function to get the\n * targeted entries from the JSON object.\n * @param json The JSON object to get targeted entries from.\n * @param pointers The JSON pointers to get targeted entries.\n * @returns An array of targeted entries.\n */\n private getTargetedEntries(json: object, pointers: jsonpointer[]): object[] {\n const targetEntries = [];\n for (const pointer of pointers) {\n const targetedEntry = pointer.get(json);\n if (targetedEntry) {\n targetEntries.push(targetedEntry);\n }\n }\n\n return targetEntries;\n }\n}\n\n/**\n * Class that extends the `TextLoader` class. It represents a document\n * loader that loads documents from JSON Lines files. It has a constructor\n * that takes a `filePathOrBlob` parameter representing the path to the\n * JSON Lines file or a `Blob` object, and a `pointer` parameter that\n * specifies the JSON pointer to extract.\n */\nexport class JSONLinesLoader extends TextLoader {\n constructor(\n filePathOrBlob: string | Blob,\n public pointer: string\n ) {\n super(filePathOrBlob);\n }\n\n /**\n * Method that takes a `raw` string as a parameter and returns a promise\n * that resolves to an array of strings. It parses the raw JSON Lines\n * string, splits it into lines, parses each line as JSON, and extracts\n * the values based on the specified JSON pointer.\n * @param raw The raw JSON Lines string to parse.\n * @returns A promise that resolves to an array of strings.\n */\n protected async parse(raw: string): Promise<string[]> {\n const lines = raw.split(\"\\n\");\n const jsons = lines\n .map((line) => line.trim())\n .filter(Boolean)\n .map((line) => JSON.parse(line));\n const pointer = jsonpointer.compile(this.pointer);\n return jsons.map((json) => {\n const data = pointer.get(json);\n if (typeof data === \"string\") {\n return data;\n }\n if (!data) {\n return \"\";\n }\n if (typeof data === \"object\") {\n return JSON.stringify(data);\n }\n return \"\";\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAUA,IAAa,aAAb,cAAgC,WAAW;CACzC,AAAO;CAEP,YAAYA,gBAA+BC,WAA8B,CAAE,GAAE;EAC3E,MAAM,eAAe;EACrB,KAAK,WAAW,MAAM,QAAQ,SAAS,GAAG,WAAW,CAAC,QAAS;CAChE;;;;;;;;;;CAWD,MAAgB,MAAMC,KAAgC;EACpD,MAAM,OAAO,KAAK,MAAM,IAAI,MAAM,CAAC;EAEnC,MAAM,oBAAoB,EAAE,KAAK,SAAS,SAAS;EACnD,MAAM,mBAAmB,KAAK,SAAS,IAAI,CAAC,YAC1C,YAAY,QAAQ,QAAQ,CAC7B;AAED,SAAO,KAAK,8BACV,MACA,kBACA,kBACD;CACF;;;;;;;CAQD,AAAQ,8BAENC,MACAC,UACA,oBAAoB,OACpB,kBAAkB,OACR;AACV,MAAI,CAAC,KACH,QAAO,CAAE;AAGX,MAAI,OAAO,SAAS,YAAY,kBAC9B,QAAO,CAAC,IAAK;AAGf,MAAI,MAAM,QAAQ,KAAK,IAAI,mBAAmB;GAC5C,IAAIC,kBAA4B,CAAE;AAClC,QAAK,MAAM,WAAW,MACpB,kBAAkB,gBAAgB,OAChC,KAAK,8BAA8B,SAAS,UAAU,KAAK,CAC5D;AAGH,UAAO;EACR;AAED,MAAI,OAAO,SAAS,UAAU;AAC5B,OAAI,kBACF,QAAO,KAAK,8BACV,OAAO,OAAO,KAAK,EACnB,UACA,KACD;GAGH,MAAM,kBAAkB,KAAK,mBAAmB,MAAM,SAAS;GAC/D,MAAM,mB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AS,SAClB,QAAO,KAAK,UAAU,KAAK;AAE7B,UAAO;EACR,EAAC;CACH;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache_backed.cjs","names":["Embeddings","fields: CacheBackedEmbeddingsFields","document: string","documents: string[]","keyValuePairs: [string, number[]][]","underlyingEmbeddings: EmbeddingsInterface","documentEmbeddingStore: BaseStore<string, Uint8Array>","options?: {\n namespace?: string;\n }","EncoderBackedStore"],"sources":["../../src/embeddings/cache_backed.ts"],"sourcesContent":["import { sha256 } from \"@langchain/core/utils/hash\";\nimport {\n type EmbeddingsInterface,\n Embeddings,\n} from \"@langchain/core/embeddings\";\nimport { BaseStore } from \"@langchain/core/stores\";\n\nimport { AsyncCallerParams } from \"@langchain/core/utils/async_caller\";\nimport { EncoderBackedStore } from \"../storage/encoder_backed.js\";\n\n/**\n * Interface for the fields required to initialize an instance of the\n * CacheBackedEmbeddings class.\n */\nexport interface CacheBackedEmbeddingsFields extends AsyncCallerParams {\n underlyingEmbeddings: EmbeddingsInterface;\n documentEmbeddingStore: BaseStore<string, number[]>;\n}\n\n/**\n * Interface for caching results from embedding models.\n *\n * The interface allows works with any store that implements\n * the abstract store interface accepting keys of type str and values of list of\n * floats.\n *\n * If need be, the interface can be extended to accept other implementations\n * of the value serializer and deserializer, as well as the key encoder.\n * @example\n * ```typescript\n * const underlyingEmbeddings = new OpenAIEmbeddings();\n *\n * const cacheBackedEmbeddings = CacheBackedEmbeddings.fromBytesStore(\n * underlyingEmbeddings,\n * new ConvexKVStore({ ctx }),\n * {\n * namespace: underlyingEmbeddings.modelName,\n * },\n * );\n *\n * const loader = new TextLoader(\"./state_of_the_union.txt\");\n * const rawDocuments = await loader.load();\n * const splitter = new RecursiveCharacterTextSplitter({\n * chunkSize: 1000,\n * chunkOverlap: 0,\n * });\n * const documents = await splitter.splitDocuments(rawDocuments);\n *\n * let time = Date.now();\n * const vectorstore = await ConvexVectorStore.fromDocuments(\n * documents,\n * cacheBackedEmbeddings,\n * { ctx },\n * );\n * console.log(`Initial creation time: ${Date.now() - time}ms`);\n *\n * time = Date.now();\n * const vectorstore2 = await ConvexVectorStore.fromDocuments(\n * documents,\n * cacheBackedEmbeddings,\n * { ctx },\n * );\n * console.log(`Cached creation time: ${Date.now() - time}ms`);\n *\n * ```\n */\nexport class CacheBackedEmbeddings extends Embeddings {\n protected underlyingEmbeddings: EmbeddingsInterface;\n\n protected documentEmbeddingStore: BaseStore<string, number[]>;\n\n constructor(fields: CacheBackedEmbeddingsFields) {\n super(fields);\n this.underlyingEmbeddings = fields.underlyingEmbeddings;\n this.documentEmbeddingStore = fields.documentEmbeddingStore;\n }\n\n /**\n * Embed query text.\n *\n * This method does not support caching at the moment.\n *\n * Support for caching queries is easy to implement, but might make\n * sense to hold off to see the most common patterns.\n *\n * If the cache has an eviction policy, we may need to be a bit more careful\n * about sharing the cache between documents and queries. Generally,\n * one is OK evicting query caches, but document caches should be kept.\n *\n * @param document The text to embed.\n * @returns The embedding for the given text.\n */\n async embedQuery(document: string): Promise<number[]> {\n return this.underlyingEmbeddings.embedQuery(document);\n }\n\n /**\n * Embed a list of texts.\n *\n * The method first checks the cache for the embeddings.\n * If the embeddings are not found, the method uses the underlying embedder\n * to embed the documents and stores the results in the cache.\n *\n * @param documents\n * @returns A list of embeddings for the given texts.\n */\n async embedDocuments(documents: string[]): Promise<number[][]> {\n const vectors = await this.documentEmbeddingStore.mget(documents);\n const missingIndicies = [];\n const missingDocuments = [];\n for (let i = 0; i < vectors.length; i += 1) {\n if (vectors[i] === undefined) {\n missingIndicies.push(i);\n missingDocuments.push(documents[i]);\n }\n }\n if (missingDocuments.length) {\n const missingVectors
|
|
1
|
+
{"version":3,"file":"cache_backed.cjs","names":["Embeddings","fields: CacheBackedEmbeddingsFields","document: string","documents: string[]","keyValuePairs: [string, number[]][]","underlyingEmbeddings: EmbeddingsInterface","documentEmbeddingStore: BaseStore<string, Uint8Array>","options?: {\n namespace?: string;\n }","EncoderBackedStore"],"sources":["../../src/embeddings/cache_backed.ts"],"sourcesContent":["import { sha256 } from \"@langchain/core/utils/hash\";\nimport {\n type EmbeddingsInterface,\n Embeddings,\n} from \"@langchain/core/embeddings\";\nimport { BaseStore } from \"@langchain/core/stores\";\n\nimport { AsyncCallerParams } from \"@langchain/core/utils/async_caller\";\nimport { EncoderBackedStore } from \"../storage/encoder_backed.js\";\n\n/**\n * Interface for the fields required to initialize an instance of the\n * CacheBackedEmbeddings class.\n */\nexport interface CacheBackedEmbeddingsFields extends AsyncCallerParams {\n underlyingEmbeddings: EmbeddingsInterface;\n documentEmbeddingStore: BaseStore<string, number[]>;\n}\n\n/**\n * Interface for caching results from embedding models.\n *\n * The interface allows works with any store that implements\n * the abstract store interface accepting keys of type str and values of list of\n * floats.\n *\n * If need be, the interface can be extended to accept other implementations\n * of the value serializer and deserializer, as well as the key encoder.\n * @example\n * ```typescript\n * const underlyingEmbeddings = new OpenAIEmbeddings();\n *\n * const cacheBackedEmbeddings = CacheBackedEmbeddings.fromBytesStore(\n * underlyingEmbeddings,\n * new ConvexKVStore({ ctx }),\n * {\n * namespace: underlyingEmbeddings.modelName,\n * },\n * );\n *\n * const loader = new TextLoader(\"./state_of_the_union.txt\");\n * const rawDocuments = await loader.load();\n * const splitter = new RecursiveCharacterTextSplitter({\n * chunkSize: 1000,\n * chunkOverlap: 0,\n * });\n * const documents = await splitter.splitDocuments(rawDocuments);\n *\n * let time = Date.now();\n * const vectorstore = await ConvexVectorStore.fromDocuments(\n * documents,\n * cacheBackedEmbeddings,\n * { ctx },\n * );\n * console.log(`Initial creation time: ${Date.now() - time}ms`);\n *\n * time = Date.now();\n * const vectorstore2 = await ConvexVectorStore.fromDocuments(\n * documents,\n * cacheBackedEmbeddings,\n * { ctx },\n * );\n * console.log(`Cached creation time: ${Date.now() - time}ms`);\n *\n * ```\n */\nexport class CacheBackedEmbeddings extends Embeddings {\n protected underlyingEmbeddings: EmbeddingsInterface;\n\n protected documentEmbeddingStore: BaseStore<string, number[]>;\n\n constructor(fields: CacheBackedEmbeddingsFields) {\n super(fields);\n this.underlyingEmbeddings = fields.underlyingEmbeddings;\n this.documentEmbeddingStore = fields.documentEmbeddingStore;\n }\n\n /**\n * Embed query text.\n *\n * This method does not support caching at the moment.\n *\n * Support for caching queries is easy to implement, but might make\n * sense to hold off to see the most common patterns.\n *\n * If the cache has an eviction policy, we may need to be a bit more careful\n * about sharing the cache between documents and queries. Generally,\n * one is OK evicting query caches, but document caches should be kept.\n *\n * @param document The text to embed.\n * @returns The embedding for the given text.\n */\n async embedQuery(document: string): Promise<number[]> {\n return this.underlyingEmbeddings.embedQuery(document);\n }\n\n /**\n * Embed a list of texts.\n *\n * The method first checks the cache for the embeddings.\n * If the embeddings are not found, the method uses the underlying embedder\n * to embed the documents and stores the results in the cache.\n *\n * @param documents\n * @returns A list of embeddings for the given texts.\n */\n async embedDocuments(documents: string[]): Promise<number[][]> {\n const vectors = await this.documentEmbeddingStore.mget(documents);\n const missingIndicies = [];\n const missingDocuments = [];\n for (let i = 0; i < vectors.length; i += 1) {\n if (vectors[i] === undefined) {\n missingIndicies.push(i);\n missingDocuments.push(documents[i]);\n }\n }\n if (missingDocuments.length) {\n const missingVectors =\n await this.underlyingEmbeddings.embedDocuments(missingDocuments);\n const keyValuePairs: [string, number[]][] = missingDocuments.map(\n (document, i) => [document, missingVectors[i]]\n );\n await this.documentEmbeddingStore.mset(keyValuePairs);\n for (let i = 0; i < missingIndicies.length; i += 1) {\n vectors[missingIndicies[i]] = missingVectors[i];\n }\n }\n return vectors as number[][];\n }\n\n /**\n * Create a new CacheBackedEmbeddings instance from another embeddings instance\n * and a storage instance.\n * @param underlyingEmbeddings Embeddings used to populate the cache for new documents.\n * @param documentEmbeddingStore Stores raw document embedding values. Keys are hashes of the document content.\n * @param options.namespace Optional namespace for store keys.\n * @returns A new CacheBackedEmbeddings instance.\n */\n static fromBytesStore(\n underlyingEmbeddings: EmbeddingsInterface,\n documentEmbeddingStore: BaseStore<string, Uint8Array>,\n options?: {\n namespace?: string;\n }\n ) {\n const encoder = new TextEncoder();\n const decoder = new TextDecoder();\n const encoderBackedStore = new EncoderBackedStore<\n string,\n number[],\n Uint8Array\n >({\n store: documentEmbeddingStore,\n keyEncoder: (key) => (options?.namespace ?? \"\") + sha256(key),\n valueSerializer: (value) => encoder.encode(JSON.stringify(value)),\n valueDeserializer: (serializedValue) =>\n JSON.parse(decoder.decode(serializedValue)),\n });\n return new this({\n underlyingEmbeddings,\n documentEmbeddingStore: encoderBackedStore,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,IAAa,wBAAb,cAA2CA,uCAAW;CACpD,AAAU;CAEV,AAAU;CAEV,YAAYC,QAAqC;EAC/C,MAAM,OAAO;EACb,KAAK,uBAAuB,OAAO;EACnC,KAAK,yBAAyB,OAAO;CACtC;;;;;;;;;;;;;;;;CAiBD,MAAM,WAAWC,UAAqC;AACpD,SAAO,KAAK,qBAAqB,WAAW,SAAS;CACtD;;;;;;;;;;;CAYD,MAAM,eAAeC,WAA0C;EAC7D,MAAM,UAAU,MAAM,KAAK,uBAAuB,KAAK,UAAU;EACjE,MAAM,kBAAkB,CAAE;EAC1B,MAAM,mBAAmB,CAAE;AAC3B,OAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK,EACvC,KAAI,QAAQ,OAAO,QAAW;GAC5B,gBAAgB,KAAK,EAAE;GACvB,iBAAiB,KAAK,UAAU,GAAG;EACpC;AAEH,MAAI,iBAAiB,QAAQ;GAC3B,MAAM,iBACJ,MAAM,KAAK,qBAAqB,eAAe,iBAAiB;GAClE,MAAMC,gBAAsC,iBAAiB,IAC3D,CAAC,UAAU,MAAM,CAAC,UAAU,eAAe,EAAG,EAC/C;GACD,MAAM,KAAK,uBAAuB,KAAK,cAAc;AACrD,QAAK,IAAI,IAAI,GAAG,IAAI,gBAAgB,QAAQ,KAAK,GAC/C,QAAQ,gBAAgB,MAAM,eAAe;EAEhD;AACD,SAAO;CACR;;;;;;;;;CAUD,OAAO,eACLC,sBACAC,wBACAC,SAGA;EACA,MAAM,UAAU,IAAI;EACpB,MAAM,UAAU,IAAI;EACpB,MAAM,qBAAqB,IAAIC,kDAI7B;GACA,OAAO;GACP,YAAY,CAAC,SAAS,SAAS,aAAa,8CAAa,IAAI;GAC7D,iBAAiB,CAAC,UAAU,QAAQ,OAAO,KAAK,UAAU,MAAM,CAAC;GACjE,mBAAmB,CAAC,oBAClB,KAAK,MAAM,QAAQ,OAAO,gBAAgB,CAAC;EAC9C;AACD,SAAO,IAAI,KAAK;GACd;GACA,wBAAwB;EACzB;CACF;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache_backed.js","names":["fields: CacheBackedEmbeddingsFields","document: string","documents: string[]","keyValuePairs: [string, number[]][]","underlyingEmbeddings: EmbeddingsInterface","documentEmbeddingStore: BaseStore<string, Uint8Array>","options?: {\n namespace?: string;\n }"],"sources":["../../src/embeddings/cache_backed.ts"],"sourcesContent":["import { sha256 } from \"@langchain/core/utils/hash\";\nimport {\n type EmbeddingsInterface,\n Embeddings,\n} from \"@langchain/core/embeddings\";\nimport { BaseStore } from \"@langchain/core/stores\";\n\nimport { AsyncCallerParams } from \"@langchain/core/utils/async_caller\";\nimport { EncoderBackedStore } from \"../storage/encoder_backed.js\";\n\n/**\n * Interface for the fields required to initialize an instance of the\n * CacheBackedEmbeddings class.\n */\nexport interface CacheBackedEmbeddingsFields extends AsyncCallerParams {\n underlyingEmbeddings: EmbeddingsInterface;\n documentEmbeddingStore: BaseStore<string, number[]>;\n}\n\n/**\n * Interface for caching results from embedding models.\n *\n * The interface allows works with any store that implements\n * the abstract store interface accepting keys of type str and values of list of\n * floats.\n *\n * If need be, the interface can be extended to accept other implementations\n * of the value serializer and deserializer, as well as the key encoder.\n * @example\n * ```typescript\n * const underlyingEmbeddings = new OpenAIEmbeddings();\n *\n * const cacheBackedEmbeddings = CacheBackedEmbeddings.fromBytesStore(\n * underlyingEmbeddings,\n * new ConvexKVStore({ ctx }),\n * {\n * namespace: underlyingEmbeddings.modelName,\n * },\n * );\n *\n * const loader = new TextLoader(\"./state_of_the_union.txt\");\n * const rawDocuments = await loader.load();\n * const splitter = new RecursiveCharacterTextSplitter({\n * chunkSize: 1000,\n * chunkOverlap: 0,\n * });\n * const documents = await splitter.splitDocuments(rawDocuments);\n *\n * let time = Date.now();\n * const vectorstore = await ConvexVectorStore.fromDocuments(\n * documents,\n * cacheBackedEmbeddings,\n * { ctx },\n * );\n * console.log(`Initial creation time: ${Date.now() - time}ms`);\n *\n * time = Date.now();\n * const vectorstore2 = await ConvexVectorStore.fromDocuments(\n * documents,\n * cacheBackedEmbeddings,\n * { ctx },\n * );\n * console.log(`Cached creation time: ${Date.now() - time}ms`);\n *\n * ```\n */\nexport class CacheBackedEmbeddings extends Embeddings {\n protected underlyingEmbeddings: EmbeddingsInterface;\n\n protected documentEmbeddingStore: BaseStore<string, number[]>;\n\n constructor(fields: CacheBackedEmbeddingsFields) {\n super(fields);\n this.underlyingEmbeddings = fields.underlyingEmbeddings;\n this.documentEmbeddingStore = fields.documentEmbeddingStore;\n }\n\n /**\n * Embed query text.\n *\n * This method does not support caching at the moment.\n *\n * Support for caching queries is easy to implement, but might make\n * sense to hold off to see the most common patterns.\n *\n * If the cache has an eviction policy, we may need to be a bit more careful\n * about sharing the cache between documents and queries. Generally,\n * one is OK evicting query caches, but document caches should be kept.\n *\n * @param document The text to embed.\n * @returns The embedding for the given text.\n */\n async embedQuery(document: string): Promise<number[]> {\n return this.underlyingEmbeddings.embedQuery(document);\n }\n\n /**\n * Embed a list of texts.\n *\n * The method first checks the cache for the embeddings.\n * If the embeddings are not found, the method uses the underlying embedder\n * to embed the documents and stores the results in the cache.\n *\n * @param documents\n * @returns A list of embeddings for the given texts.\n */\n async embedDocuments(documents: string[]): Promise<number[][]> {\n const vectors = await this.documentEmbeddingStore.mget(documents);\n const missingIndicies = [];\n const missingDocuments = [];\n for (let i = 0; i < vectors.length; i += 1) {\n if (vectors[i] === undefined) {\n missingIndicies.push(i);\n missingDocuments.push(documents[i]);\n }\n }\n if (missingDocuments.length) {\n const missingVectors
|
|
1
|
+
{"version":3,"file":"cache_backed.js","names":["fields: CacheBackedEmbeddingsFields","document: string","documents: string[]","keyValuePairs: [string, number[]][]","underlyingEmbeddings: EmbeddingsInterface","documentEmbeddingStore: BaseStore<string, Uint8Array>","options?: {\n namespace?: string;\n }"],"sources":["../../src/embeddings/cache_backed.ts"],"sourcesContent":["import { sha256 } from \"@langchain/core/utils/hash\";\nimport {\n type EmbeddingsInterface,\n Embeddings,\n} from \"@langchain/core/embeddings\";\nimport { BaseStore } from \"@langchain/core/stores\";\n\nimport { AsyncCallerParams } from \"@langchain/core/utils/async_caller\";\nimport { EncoderBackedStore } from \"../storage/encoder_backed.js\";\n\n/**\n * Interface for the fields required to initialize an instance of the\n * CacheBackedEmbeddings class.\n */\nexport interface CacheBackedEmbeddingsFields extends AsyncCallerParams {\n underlyingEmbeddings: EmbeddingsInterface;\n documentEmbeddingStore: BaseStore<string, number[]>;\n}\n\n/**\n * Interface for caching results from embedding models.\n *\n * The interface allows works with any store that implements\n * the abstract store interface accepting keys of type str and values of list of\n * floats.\n *\n * If need be, the interface can be extended to accept other implementations\n * of the value serializer and deserializer, as well as the key encoder.\n * @example\n * ```typescript\n * const underlyingEmbeddings = new OpenAIEmbeddings();\n *\n * const cacheBackedEmbeddings = CacheBackedEmbeddings.fromBytesStore(\n * underlyingEmbeddings,\n * new ConvexKVStore({ ctx }),\n * {\n * namespace: underlyingEmbeddings.modelName,\n * },\n * );\n *\n * const loader = new TextLoader(\"./state_of_the_union.txt\");\n * const rawDocuments = await loader.load();\n * const splitter = new RecursiveCharacterTextSplitter({\n * chunkSize: 1000,\n * chunkOverlap: 0,\n * });\n * const documents = await splitter.splitDocuments(rawDocuments);\n *\n * let time = Date.now();\n * const vectorstore = await ConvexVectorStore.fromDocuments(\n * documents,\n * cacheBackedEmbeddings,\n * { ctx },\n * );\n * console.log(`Initial creation time: ${Date.now() - time}ms`);\n *\n * time = Date.now();\n * const vectorstore2 = await ConvexVectorStore.fromDocuments(\n * documents,\n * cacheBackedEmbeddings,\n * { ctx },\n * );\n * console.log(`Cached creation time: ${Date.now() - time}ms`);\n *\n * ```\n */\nexport class CacheBackedEmbeddings extends Embeddings {\n protected underlyingEmbeddings: EmbeddingsInterface;\n\n protected documentEmbeddingStore: BaseStore<string, number[]>;\n\n constructor(fields: CacheBackedEmbeddingsFields) {\n super(fields);\n this.underlyingEmbeddings = fields.underlyingEmbeddings;\n this.documentEmbeddingStore = fields.documentEmbeddingStore;\n }\n\n /**\n * Embed query text.\n *\n * This method does not support caching at the moment.\n *\n * Support for caching queries is easy to implement, but might make\n * sense to hold off to see the most common patterns.\n *\n * If the cache has an eviction policy, we may need to be a bit more careful\n * about sharing the cache between documents and queries. Generally,\n * one is OK evicting query caches, but document caches should be kept.\n *\n * @param document The text to embed.\n * @returns The embedding for the given text.\n */\n async embedQuery(document: string): Promise<number[]> {\n return this.underlyingEmbeddings.embedQuery(document);\n }\n\n /**\n * Embed a list of texts.\n *\n * The method first checks the cache for the embeddings.\n * If the embeddings are not found, the method uses the underlying embedder\n * to embed the documents and stores the results in the cache.\n *\n * @param documents\n * @returns A list of embeddings for the given texts.\n */\n async embedDocuments(documents: string[]): Promise<number[][]> {\n const vectors = await this.documentEmbeddingStore.mget(documents);\n const missingIndicies = [];\n const missingDocuments = [];\n for (let i = 0; i < vectors.length; i += 1) {\n if (vectors[i] === undefined) {\n missingIndicies.push(i);\n missingDocuments.push(documents[i]);\n }\n }\n if (missingDocuments.length) {\n const missingVectors =\n await this.underlyingEmbeddings.embedDocuments(missingDocuments);\n const keyValuePairs: [string, number[]][] = missingDocuments.map(\n (document, i) => [document, missingVectors[i]]\n );\n await this.documentEmbeddingStore.mset(keyValuePairs);\n for (let i = 0; i < missingIndicies.length; i += 1) {\n vectors[missingIndicies[i]] = missingVectors[i];\n }\n }\n return vectors as number[][];\n }\n\n /**\n * Create a new CacheBackedEmbeddings instance from another embeddings instance\n * and a storage instance.\n * @param underlyingEmbeddings Embeddings used to populate the cache for new documents.\n * @param documentEmbeddingStore Stores raw document embedding values. Keys are hashes of the document content.\n * @param options.namespace Optional namespace for store keys.\n * @returns A new CacheBackedEmbeddings instance.\n */\n static fromBytesStore(\n underlyingEmbeddings: EmbeddingsInterface,\n documentEmbeddingStore: BaseStore<string, Uint8Array>,\n options?: {\n namespace?: string;\n }\n ) {\n const encoder = new TextEncoder();\n const decoder = new TextDecoder();\n const encoderBackedStore = new EncoderBackedStore<\n string,\n number[],\n Uint8Array\n >({\n store: documentEmbeddingStore,\n keyEncoder: (key) => (options?.namespace ?? \"\") + sha256(key),\n valueSerializer: (value) => encoder.encode(JSON.stringify(value)),\n valueDeserializer: (serializedValue) =>\n JSON.parse(decoder.decode(serializedValue)),\n });\n return new this({\n underlyingEmbeddings,\n documentEmbeddingStore: encoderBackedStore,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,IAAa,wBAAb,cAA2C,WAAW;CACpD,AAAU;CAEV,AAAU;CAEV,YAAYA,QAAqC;EAC/C,MAAM,OAAO;EACb,KAAK,uBAAuB,OAAO;EACnC,KAAK,yBAAyB,OAAO;CACtC;;;;;;;;;;;;;;;;CAiBD,MAAM,WAAWC,UAAqC;AACpD,SAAO,KAAK,qBAAqB,WAAW,SAAS;CACtD;;;;;;;;;;;CAYD,MAAM,eAAeC,WAA0C;EAC7D,MAAM,UAAU,MAAM,KAAK,uBAAuB,KAAK,UAAU;EACjE,MAAM,kBAAkB,CAAE;EAC1B,MAAM,mBAAmB,CAAE;AAC3B,OAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK,EACvC,KAAI,QAAQ,OAAO,QAAW;GAC5B,gBAAgB,KAAK,EAAE;GACvB,iBAAiB,KAAK,UAAU,GAAG;EACpC;AAEH,MAAI,iBAAiB,QAAQ;GAC3B,MAAM,iBACJ,MAAM,KAAK,qBAAqB,eAAe,iBAAiB;GAClE,MAAMC,gBAAsC,iBAAiB,IAC3D,CAAC,UAAU,MAAM,CAAC,UAAU,eAAe,EAAG,EAC/C;GACD,MAAM,KAAK,uBAAuB,KAAK,cAAc;AACrD,QAAK,IAAI,IAAI,GAAG,IAAI,gBAAgB,QAAQ,KAAK,GAC/C,QAAQ,gBAAgB,MAAM,eAAe;EAEhD;AACD,SAAO;CACR;;;;;;;;;CAUD,OAAO,eACLC,sBACAC,wBACAC,SAGA;EACA,MAAM,UAAU,IAAI;EACpB,MAAM,UAAU,IAAI;EACpB,MAAM,qBAAqB,IAAI,mBAI7B;GACA,OAAO;GACP,YAAY,CAAC,SAAS,SAAS,aAAa,MAAM,OAAO,IAAI;GAC7D,iBAAiB,CAAC,UAAU,QAAQ,OAAO,KAAK,UAAU,MAAM,CAAC;GACjE,mBAAmB,CAAC,oBAClB,KAAK,MAAM,QAAQ,OAAO,gBAAgB,CAAC;EAC9C;AACD,SAAO,IAAI,KAAK;GACd;GACA,wBAAwB;EACzB;CACF;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.cjs","names":["xs: Set<string>","ys: Set<string>","LLMChain","reference?: string","input?: string","BaseChain","args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>","config?: Callbacks | BaseCallbackConfig","args: StringEvaluatorArgs","args: PairwiseStringEvaluatorArgs","args: LLMPairwiseStringEvaluatorArgs","callOptions?: ExtractLLMCallOptions<this[\"llm\"]>","args: LLMTrajectoryEvaluatorArgs"],"sources":["../../src/evaluation/base.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { AgentStep } from \"@langchain/core/agents\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport {\n BaseCallbackConfig,\n Callbacks,\n} from \"@langchain/core/callbacks/manager\";\nimport { BaseChain, LLMChain, LLMChainInput } from \"../chains/index.js\";\n\n/**\n * Base input for evaluators.\n */\nexport interface LLMEvalChainInput<\n T extends EvalOutputType = EvalOutputType,\n L extends BaseLanguageModelInterface = BaseLanguageModelInterface\n> extends LLMChainInput<T, L> {}\n\nexport type ExtractLLMCallOptions<LanguageModelInterface> =\n LanguageModelInterface extends BaseLanguageModelInterface<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any,\n infer CallOptions\n >\n ? CallOptions\n : never;\n\n/**\n * Compare two sets for equality\n *\n * @param xs\n * @param ys\n */\nexport const eqSet = (xs: Set<string>, ys: Set<string>) =>\n xs.size === ys.size && [...xs].every((x) => ys.has(x));\n\n/**\n * The type of the output of an evaluation evaluator.\n */\nexport type EvalOutputType = Record<string, string | number | boolean>;\n\n/**\n * Base llm chain class for evaluators.\n */\nexport abstract class LLMEvalChain<\n T extends EvalOutputType = EvalOutputType,\n L extends BaseLanguageModelInterface = BaseLanguageModelInterface\n> extends LLMChain<T, L> {\n requiresInput?: boolean = false;\n\n requiresReference?: boolean = false;\n\n skipInputWarning?: string = `Ignoring input in ${this.constructor.name}, as it is not expected.`;\n\n skipReferenceWarning?: string = `Ignoring reference in ${this.constructor.name}, as it is not expected.`;\n\n /**\n * Check if the evaluation arguments are valid.\n * @param reference The reference label.\n * @param input The input string.\n * @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.\n */\n checkEvaluationArgs(reference?: string, input?: string): void {\n if (this.requiresInput && input == null) {\n throw new Error(`${this.constructor.name} requires an input string.`);\n } else if (input != null && !this.requiresInput) {\n console.warn(this.skipInputWarning);\n }\n if (this.requiresReference && reference == null) {\n throw new Error(`${this.constructor.name} requires a reference string.`);\n } else if (reference != null && !this.requiresReference) {\n console.warn(this.skipReferenceWarning);\n }\n }\n}\n\n/**\n * Base chain class for evaluators.\n */\nexport abstract class EvalChain<\n RunInput extends ChainValues = ChainValues,\n RunOutput extends ChainValues = ChainValues\n> extends BaseChain<RunInput, RunOutput> {\n requiresInput?: boolean = false;\n\n requiresReference?: boolean = false;\n\n skipInputWarning?: string = `Ignoring input in ${this.constructor.name}, as it is not expected.`;\n\n skipReferenceWarning?: string = `Ignoring reference in ${this.constructor.name}, as it is not expected.`;\n\n /**\n * Check if the evaluation arguments are valid.\n * @param reference The reference label.\n * @param input The input string.\n * @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.\n */\n checkEvaluationArgs(reference?: string, input?: string): void {\n if (this.requiresInput && input == null) {\n throw new Error(`${this.constructor.name} requires an input string.`);\n } else if (input != null && !this.requiresInput) {\n console.warn(this.skipInputWarning);\n }\n if (this.requiresReference && reference == null) {\n throw new Error(`${this.constructor.name} requires a reference string.`);\n } else if (reference != null && !this.requiresReference) {\n console.warn(this.skipReferenceWarning);\n }\n }\n}\n\n/**\n * @field prediction The output string from the model.\n * @field reference The expected output / reference string.\n * @field input The input string.\n */\nexport interface StringEvaluatorArgs {\n prediction: string;\n reference?: string;\n input?: string;\n}\n\n/**\n * @field prediction The output string from the first model.\n * @field predictionB The output string from the second model.\n */\nexport interface PairwiseStringEvaluatorArgs {\n prediction: string;\n predictionB: string;\n}\n\n/**\n * @field The input string.\n * @field prediction The output string from the first model.\n * @field predictionB The output string from the second model.\n * @field reference The expected output / reference string.\n */\nexport interface LLMPairwiseStringEvaluatorArgs {\n input: string;\n prediction: string;\n predictionB: string;\n reference?: string;\n}\n\n/**\n * Args for AgentTrajectoryEvaluator\n * @field input The input to the agent.\n * @field prediction The final predicted response.\n * @field reference The reference answer.\n * @field agentTrajectory The intermediate steps forming the agent trajectory.\n */\nexport interface LLMTrajectoryEvaluatorArgs {\n input: string;\n prediction: string;\n reference?: string;\n agentTrajectory: AgentStep[];\n}\n\n/**\n * Grade, tag, or otherwise evaluate predictions relative to their inputs\n * and/or reference labels\n */\nexport abstract class LLMStringEvaluator<\n T extends EvalOutputType = EvalOutputType,\n L extends BaseLanguageModelInterface = BaseLanguageModelInterface\n> extends LLMEvalChain<T, L> {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param callOptions\n * @param config\n */\n abstract _evaluateStrings(\n args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param callOptions\n * @param config\n */\n evaluateStrings(\n args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateStrings(args, config);\n }\n}\n\n/**\n * Grade, tag, or otherwise evaluate predictions relative to their inputs\n * and/or reference labels\n */\nexport abstract class StringEvaluator extends EvalChain {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param config\n */\n abstract _evaluateStrings(\n args: StringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param config\n */\n evaluateStrings(\n args: StringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateStrings(args, config);\n }\n}\n\n/**\n * Compare the output of two models (or two outputs of the same model).\n */\nexport abstract class PairwiseStringEvaluator extends EvalChain {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n abstract _evaluateStringPairs(\n args: PairwiseStringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n evaluateStringPairs(\n args: PairwiseStringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n return this._evaluateStringPairs(args, config);\n }\n}\n\n/**\n * Compare the output of two models (or two outputs of the same model).\n */\nexport abstract class LLMPairwiseStringEvaluator extends LLMEvalChain {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param callOptions\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n abstract _evaluateStringPairs(\n args: LLMPairwiseStringEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param callOptions\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n evaluateStringPairs(\n args: LLMPairwiseStringEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateStringPairs(args, callOptions, config);\n }\n}\n\n/**\n * Interface for evaluating agent trajectories.\n */\nexport abstract class AgentTrajectoryEvaluator extends LLMEvalChain {\n requiresInput = true;\n\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate a trajectory.\n * @return The evaluation result.\n * @param args\n * @param callOptions\n * @param config\n */\n abstract _evaluateAgentTrajectory(\n args: LLMTrajectoryEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate a trajectory.\n * @return The evaluation result.\n * @param args\n * @param callOptions\n * @param config\n */\n evaluateAgentTrajectory(\n args: LLMTrajectoryEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateAgentTrajectory(args, callOptions, config);\n }\n}\n"],"mappings":";;;;;;;;;;;AAgCA,MAAa,QAAQ,CAACA,IAAiBC,OACrC,GAAG,SAAS,GAAG,QAAQ,CAAC,GAAG,EAAG,EAAC,MAAM,CAAC,MAAM,GAAG,IAAI,EAAE,CAAC;;;;AAUxD,IAAsB,eAAtB,cAGUC,2BAAe;CACvB,gBAA0B;CAE1B,oBAA8B;CAE9B,mBAA4B,CAAC,kBAAkB,EAAE,KAAK,YAAY,KAAK,wBAAwB,CAAC;CAEhG,uBAAgC,CAAC,sBAAsB,EAAE,KAAK,YAAY,KAAK,wBAAwB,CAAC;;;;;;;CAQxG,oBAAoBC,WAAoBC,OAAsB;AAC5D,MAAI,KAAK,iBAAiB,SAAS,KACjC,OAAM,IAAI,MAAM,GAAG,KAAK,YAAY,KAAK,0BAA0B,CAAC;WAC3D,SAAS,QAAQ,CAAC,KAAK,eAChC,QAAQ,KAAK,KAAK,iBAAiB;AAErC,MAAI,K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CAmB3C,oBACEE,MACAF,QACsB;AACtB,SAAO,KAAK,qBAAqB,MAAM,OAAO;CAC/C;AACF;;;;AAKD,IAAsB,6BAAtB,cAAyD,aAAa;;;;CAIpE,iBAA0B,KAAK,YAAY;;;;;;;;CAsB3C,oBACEG,MACAC,aACAJ,QACsB;EACtB,KAAK,oBAAoB,KAAK,WAAW,KAAK,MAAM;AACpD,SAAO,KAAK,qBAAqB,MAAM,aAAa,OAAO;CAC5D;AACF;;;;AAKD,IAAsB,2BAAtB,cAAuD,aAAa;CAClE,gBAAgB;;;;CAKhB,iBAA0B,KAAK,YAAY;;;;;;;;CAsB3C,wBACEK,MACAD,aACAJ,QACsB;EACtB,KAAK,oBAAoB,KAAK,WAAW,KAAK,MAAM;AACpD,SAAO,KAAK,yBAAyB,MAAM,aAAa,OAAO;CAChE;AACF"}
|
|
1
|
+
{"version":3,"file":"base.cjs","names":["xs: Set<string>","ys: Set<string>","LLMChain","reference?: string","input?: string","BaseChain","args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>","config?: Callbacks | BaseCallbackConfig","args: StringEvaluatorArgs","args: PairwiseStringEvaluatorArgs","args: LLMPairwiseStringEvaluatorArgs","callOptions?: ExtractLLMCallOptions<this[\"llm\"]>","args: LLMTrajectoryEvaluatorArgs"],"sources":["../../src/evaluation/base.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { AgentStep } from \"@langchain/core/agents\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport {\n BaseCallbackConfig,\n Callbacks,\n} from \"@langchain/core/callbacks/manager\";\nimport { BaseChain, LLMChain, LLMChainInput } from \"../chains/index.js\";\n\n/**\n * Base input for evaluators.\n */\nexport interface LLMEvalChainInput<\n T extends EvalOutputType = EvalOutputType,\n L extends BaseLanguageModelInterface = BaseLanguageModelInterface,\n> extends LLMChainInput<T, L> {}\n\nexport type ExtractLLMCallOptions<LanguageModelInterface> =\n LanguageModelInterface extends BaseLanguageModelInterface<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any,\n infer CallOptions\n >\n ? CallOptions\n : never;\n\n/**\n * Compare two sets for equality\n *\n * @param xs\n * @param ys\n */\nexport const eqSet = (xs: Set<string>, ys: Set<string>) =>\n xs.size === ys.size && [...xs].every((x) => ys.has(x));\n\n/**\n * The type of the output of an evaluation evaluator.\n */\nexport type EvalOutputType = Record<string, string | number | boolean>;\n\n/**\n * Base llm chain class for evaluators.\n */\nexport abstract class LLMEvalChain<\n T extends EvalOutputType = EvalOutputType,\n L extends BaseLanguageModelInterface = BaseLanguageModelInterface,\n> extends LLMChain<T, L> {\n requiresInput?: boolean = false;\n\n requiresReference?: boolean = false;\n\n skipInputWarning?: string = `Ignoring input in ${this.constructor.name}, as it is not expected.`;\n\n skipReferenceWarning?: string = `Ignoring reference in ${this.constructor.name}, as it is not expected.`;\n\n /**\n * Check if the evaluation arguments are valid.\n * @param reference The reference label.\n * @param input The input string.\n * @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.\n */\n checkEvaluationArgs(reference?: string, input?: string): void {\n if (this.requiresInput && input == null) {\n throw new Error(`${this.constructor.name} requires an input string.`);\n } else if (input != null && !this.requiresInput) {\n console.warn(this.skipInputWarning);\n }\n if (this.requiresReference && reference == null) {\n throw new Error(`${this.constructor.name} requires a reference string.`);\n } else if (reference != null && !this.requiresReference) {\n console.warn(this.skipReferenceWarning);\n }\n }\n}\n\n/**\n * Base chain class for evaluators.\n */\nexport abstract class EvalChain<\n RunInput extends ChainValues = ChainValues,\n RunOutput extends ChainValues = ChainValues,\n> extends BaseChain<RunInput, RunOutput> {\n requiresInput?: boolean = false;\n\n requiresReference?: boolean = false;\n\n skipInputWarning?: string = `Ignoring input in ${this.constructor.name}, as it is not expected.`;\n\n skipReferenceWarning?: string = `Ignoring reference in ${this.constructor.name}, as it is not expected.`;\n\n /**\n * Check if the evaluation arguments are valid.\n * @param reference The reference label.\n * @param input The input string.\n * @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.\n */\n checkEvaluationArgs(reference?: string, input?: string): void {\n if (this.requiresInput && input == null) {\n throw new Error(`${this.constructor.name} requires an input string.`);\n } else if (input != null && !this.requiresInput) {\n console.warn(this.skipInputWarning);\n }\n if (this.requiresReference && reference == null) {\n throw new Error(`${this.constructor.name} requires a reference string.`);\n } else if (reference != null && !this.requiresReference) {\n console.warn(this.skipReferenceWarning);\n }\n }\n}\n\n/**\n * @field prediction The output string from the model.\n * @field reference The expected output / reference string.\n * @field input The input string.\n */\nexport interface StringEvaluatorArgs {\n prediction: string;\n reference?: string;\n input?: string;\n}\n\n/**\n * @field prediction The output string from the first model.\n * @field predictionB The output string from the second model.\n */\nexport interface PairwiseStringEvaluatorArgs {\n prediction: string;\n predictionB: string;\n}\n\n/**\n * @field The input string.\n * @field prediction The output string from the first model.\n * @field predictionB The output string from the second model.\n * @field reference The expected output / reference string.\n */\nexport interface LLMPairwiseStringEvaluatorArgs {\n input: string;\n prediction: string;\n predictionB: string;\n reference?: string;\n}\n\n/**\n * Args for AgentTrajectoryEvaluator\n * @field input The input to the agent.\n * @field prediction The final predicted response.\n * @field reference The reference answer.\n * @field agentTrajectory The intermediate steps forming the agent trajectory.\n */\nexport interface LLMTrajectoryEvaluatorArgs {\n input: string;\n prediction: string;\n reference?: string;\n agentTrajectory: AgentStep[];\n}\n\n/**\n * Grade, tag, or otherwise evaluate predictions relative to their inputs\n * and/or reference labels\n */\nexport abstract class LLMStringEvaluator<\n T extends EvalOutputType = EvalOutputType,\n L extends BaseLanguageModelInterface = BaseLanguageModelInterface,\n> extends LLMEvalChain<T, L> {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param callOptions\n * @param config\n */\n abstract _evaluateStrings(\n args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param callOptions\n * @param config\n */\n evaluateStrings(\n args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateStrings(args, config);\n }\n}\n\n/**\n * Grade, tag, or otherwise evaluate predictions relative to their inputs\n * and/or reference labels\n */\nexport abstract class StringEvaluator extends EvalChain {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param config\n */\n abstract _evaluateStrings(\n args: StringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param config\n */\n evaluateStrings(\n args: StringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateStrings(args, config);\n }\n}\n\n/**\n * Compare the output of two models (or two outputs of the same model).\n */\nexport abstract class PairwiseStringEvaluator extends EvalChain {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n abstract _evaluateStringPairs(\n args: PairwiseStringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n evaluateStringPairs(\n args: PairwiseStringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n return this._evaluateStringPairs(args, config);\n }\n}\n\n/**\n * Compare the output of two models (or two outputs of the same model).\n */\nexport abstract class LLMPairwiseStringEvaluator extends LLMEvalChain {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param callOptions\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n abstract _evaluateStringPairs(\n args: LLMPairwiseStringEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param callOptions\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n evaluateStringPairs(\n args: LLMPairwiseStringEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateStringPairs(args, callOptions, config);\n }\n}\n\n/**\n * Interface for evaluating agent trajectories.\n */\nexport abstract class AgentTrajectoryEvaluator extends LLMEvalChain {\n requiresInput = true;\n\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate a trajectory.\n * @return The evaluation result.\n * @param args\n * @param callOptions\n * @param config\n */\n abstract _evaluateAgentTrajectory(\n args: LLMTrajectoryEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate a trajectory.\n * @return The evaluation result.\n * @param args\n * @param callOptions\n * @param config\n */\n evaluateAgentTrajectory(\n args: LLMTrajectoryEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateAgentTrajectory(args, callOptions, config);\n }\n}\n"],"mappings":";;;;;;;;;;;AAgCA,MAAa,QAAQ,CAACA,IAAiBC,OACrC,GAAG,SAAS,GAAG,QAAQ,CAAC,GAAG,EAAG,EAAC,MAAM,CAAC,MAAM,GAAG,IAAI,EAAE,CAAC;;;;AAUxD,IAAsB,eAAtB,cAGUC,2BAAe;CACvB,gBAA0B;CAE1B,oBAA8B;CAE9B,mBAA4B,CAAC,kBAAkB,EAAE,KAAK,YAAY,KAAK,wBAAwB,CAAC;CAEhG,uBAAgC,CAAC,sBAAsB,EAAE,KAAK,YAAY,KAAK,wBAAwB,CAAC;;;;;;;CAQxG,oBAAoBC,WAAoBC,OAAsB;AAC5D,MAAI,KAAK,iBAAiB,SAAS,KACjC,OAAM,IAAI,MAAM,GAAG,KAAK,YAAY,KAAK,0BAA0B,CAAC;WAC3D,SAAS,QAAQ,CAAC,KAAK,eAChC,QAAQ,KAAK,KAAK,iBAAiB;AAErC,MAAI,KAAK,qBAAqB,aAAa,KACzC,OAAM,IAAI,MAAM,GAAG,KAAK,YAAY,KAAK,6BAA6B,CAAC;WAC9D,aAAa,QAAQ,CAAC,KAAK,mBACpC,QAAQ,KAAK,KAAK,qBAAqB;CAE1C;AACF;;;;AAKD,IAAsB,YAAtB,cAGUC,uBAA+B;CACvC,gBAA0B;CAE1B,oBAA8B;CAE9B,mBAA4B,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,KAAK,qBAAqB,MAAM,aAAa,OAAO;CAC5D;AACF;;;;AAKD,IAAsB,2BAAtB,cAAuD,aAAa;CAClE,gBAAgB;;;;CAKhB,iBAA0B,KAAK,YAAY;;;;;;;;CAsB3C,wBACEK,MACAD,aACAJ,QACsB;EACtB,KAAK,oBAAoB,KAAK,WAAW,KAAK,MAAM;AACpD,SAAO,KAAK,yBAAyB,MAAM,aAAa,OAAO;CAChE;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","names":["xs: Set<string>","ys: Set<string>","reference?: string","input?: string","args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>","config?: Callbacks | BaseCallbackConfig","args: StringEvaluatorArgs","args: PairwiseStringEvaluatorArgs","args: LLMPairwiseStringEvaluatorArgs","callOptions?: ExtractLLMCallOptions<this[\"llm\"]>","args: LLMTrajectoryEvaluatorArgs"],"sources":["../../src/evaluation/base.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { AgentStep } from \"@langchain/core/agents\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport {\n BaseCallbackConfig,\n Callbacks,\n} from \"@langchain/core/callbacks/manager\";\nimport { BaseChain, LLMChain, LLMChainInput } from \"../chains/index.js\";\n\n/**\n * Base input for evaluators.\n */\nexport interface LLMEvalChainInput<\n T extends EvalOutputType = EvalOutputType,\n L extends BaseLanguageModelInterface = BaseLanguageModelInterface\n> extends LLMChainInput<T, L> {}\n\nexport type ExtractLLMCallOptions<LanguageModelInterface> =\n LanguageModelInterface extends BaseLanguageModelInterface<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any,\n infer CallOptions\n >\n ? CallOptions\n : never;\n\n/**\n * Compare two sets for equality\n *\n * @param xs\n * @param ys\n */\nexport const eqSet = (xs: Set<string>, ys: Set<string>) =>\n xs.size === ys.size && [...xs].every((x) => ys.has(x));\n\n/**\n * The type of the output of an evaluation evaluator.\n */\nexport type EvalOutputType = Record<string, string | number | boolean>;\n\n/**\n * Base llm chain class for evaluators.\n */\nexport abstract class LLMEvalChain<\n T extends EvalOutputType = EvalOutputType,\n L extends BaseLanguageModelInterface = BaseLanguageModelInterface\n> extends LLMChain<T, L> {\n requiresInput?: boolean = false;\n\n requiresReference?: boolean = false;\n\n skipInputWarning?: string = `Ignoring input in ${this.constructor.name}, as it is not expected.`;\n\n skipReferenceWarning?: string = `Ignoring reference in ${this.constructor.name}, as it is not expected.`;\n\n /**\n * Check if the evaluation arguments are valid.\n * @param reference The reference label.\n * @param input The input string.\n * @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.\n */\n checkEvaluationArgs(reference?: string, input?: string): void {\n if (this.requiresInput && input == null) {\n throw new Error(`${this.constructor.name} requires an input string.`);\n } else if (input != null && !this.requiresInput) {\n console.warn(this.skipInputWarning);\n }\n if (this.requiresReference && reference == null) {\n throw new Error(`${this.constructor.name} requires a reference string.`);\n } else if (reference != null && !this.requiresReference) {\n console.warn(this.skipReferenceWarning);\n }\n }\n}\n\n/**\n * Base chain class for evaluators.\n */\nexport abstract class EvalChain<\n RunInput extends ChainValues = ChainValues,\n RunOutput extends ChainValues = ChainValues\n> extends BaseChain<RunInput, RunOutput> {\n requiresInput?: boolean = false;\n\n requiresReference?: boolean = false;\n\n skipInputWarning?: string = `Ignoring input in ${this.constructor.name}, as it is not expected.`;\n\n skipReferenceWarning?: string = `Ignoring reference in ${this.constructor.name}, as it is not expected.`;\n\n /**\n * Check if the evaluation arguments are valid.\n * @param reference The reference label.\n * @param input The input string.\n * @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.\n */\n checkEvaluationArgs(reference?: string, input?: string): void {\n if (this.requiresInput && input == null) {\n throw new Error(`${this.constructor.name} requires an input string.`);\n } else if (input != null && !this.requiresInput) {\n console.warn(this.skipInputWarning);\n }\n if (this.requiresReference && reference == null) {\n throw new Error(`${this.constructor.name} requires a reference string.`);\n } else if (reference != null && !this.requiresReference) {\n console.warn(this.skipReferenceWarning);\n }\n }\n}\n\n/**\n * @field prediction The output string from the model.\n * @field reference The expected output / reference string.\n * @field input The input string.\n */\nexport interface StringEvaluatorArgs {\n prediction: string;\n reference?: string;\n input?: string;\n}\n\n/**\n * @field prediction The output string from the first model.\n * @field predictionB The output string from the second model.\n */\nexport interface PairwiseStringEvaluatorArgs {\n prediction: string;\n predictionB: string;\n}\n\n/**\n * @field The input string.\n * @field prediction The output string from the first model.\n * @field predictionB The output string from the second model.\n * @field reference The expected output / reference string.\n */\nexport interface LLMPairwiseStringEvaluatorArgs {\n input: string;\n prediction: string;\n predictionB: string;\n reference?: string;\n}\n\n/**\n * Args for AgentTrajectoryEvaluator\n * @field input The input to the agent.\n * @field prediction The final predicted response.\n * @field reference The reference answer.\n * @field agentTrajectory The intermediate steps forming the agent trajectory.\n */\nexport interface LLMTrajectoryEvaluatorArgs {\n input: string;\n prediction: string;\n reference?: string;\n agentTrajectory: AgentStep[];\n}\n\n/**\n * Grade, tag, or otherwise evaluate predictions relative to their inputs\n * and/or reference labels\n */\nexport abstract class LLMStringEvaluator<\n T extends EvalOutputType = EvalOutputType,\n L extends BaseLanguageModelInterface = BaseLanguageModelInterface\n> extends LLMEvalChain<T, L> {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param callOptions\n * @param config\n */\n abstract _evaluateStrings(\n args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param callOptions\n * @param config\n */\n evaluateStrings(\n args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateStrings(args, config);\n }\n}\n\n/**\n * Grade, tag, or otherwise evaluate predictions relative to their inputs\n * and/or reference labels\n */\nexport abstract class StringEvaluator extends EvalChain {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param config\n */\n abstract _evaluateStrings(\n args: StringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param config\n */\n evaluateStrings(\n args: StringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateStrings(args, config);\n }\n}\n\n/**\n * Compare the output of two models (or two outputs of the same model).\n */\nexport abstract class PairwiseStringEvaluator extends EvalChain {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n abstract _evaluateStringPairs(\n args: PairwiseStringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n evaluateStringPairs(\n args: PairwiseStringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n return this._evaluateStringPairs(args, config);\n }\n}\n\n/**\n * Compare the output of two models (or two outputs of the same model).\n */\nexport abstract class LLMPairwiseStringEvaluator extends LLMEvalChain {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param callOptions\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n abstract _evaluateStringPairs(\n args: LLMPairwiseStringEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param callOptions\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n evaluateStringPairs(\n args: LLMPairwiseStringEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateStringPairs(args, callOptions, config);\n }\n}\n\n/**\n * Interface for evaluating agent trajectories.\n */\nexport abstract class AgentTrajectoryEvaluator extends LLMEvalChain {\n requiresInput = true;\n\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate a trajectory.\n * @return The evaluation result.\n * @param args\n * @param callOptions\n * @param config\n */\n abstract _evaluateAgentTrajectory(\n args: LLMTrajectoryEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate a trajectory.\n * @return The evaluation result.\n * @param args\n * @param callOptions\n * @param config\n */\n evaluateAgentTrajectory(\n args: LLMTrajectoryEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateAgentTrajectory(args, callOptions, config);\n }\n}\n"],"mappings":";;;;;;;;;;;AAgCA,MAAa,QAAQ,CAACA,IAAiBC,OACrC,GAAG,SAAS,GAAG,QAAQ,CAAC,GAAG,EAAG,EAAC,MAAM,CAAC,MAAM,GAAG,IAAI,EAAE,CAAC;;;;AAUxD,IAAsB,eAAtB,cAGU,SAAe;CACvB,gBAA0B;CAE1B,oBAA8B;CAE9B,mBAA4B,CAAC,kBAAkB,EAAE,KAAK,YAAY,KAAK,wBAAwB,CAAC;CAEhG,uBAAgC,CAAC,sBAAsB,EAAE,KAAK,YAAY,KAAK,wBAAwB,CAAC;;;;;;;CAQxG,oBAAoBC,WAAoBC,OAAsB;AAC5D,MAAI,KAAK,iBAAiB,SAAS,KACjC,OAAM,IAAI,MAAM,GAAG,KAAK,YAAY,KAAK,0BAA0B,CAAC;WAC3D,SAAS,QAAQ,CAAC,KAAK,eAChC,QAAQ,KAAK,KAAK,iBAAiB;AAErC,MAAI,KAAK,qBAAqB,aAAa,KACzC,OAAM,IAAI,MAAM,GAAG,KAAK,YAAY,KAAK,6BAA6B,CAAC;WAC9D,aAAa,QAAQ,CAAC,KAAK,mBACpC,QAAQ,KAAK,KAAK,qBAAqB;CAE1C;AACF;;;;AAKD,IAAsB,YAAtB,cAGU,UAA+B;CACvC,gBAA0B;CAE1B,oBAA8B;CAE9B,mBAA4B,CAAC,kBAAkB,EAAE,KAAK,YAAY,KAAK,wBAAwB,CAAC;CAEhG,uBAAgC,CAAC,sBAAsB,EAAE,KAAK,YAAY,KAAK,wBAAwB,CAAC;;;;;;;CAQxG,oBAAoBD,WAAoBC,OAAsB;AAC5D,MAAI,KAAK,iBAAiB,SAAS,KACjC,OAAM,IAAI,MAAM,GAAG,KAAK,YAAY,KAAK,0BAA0B,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AAa,OAAO;CAChE;AACF"}
|
|
1
|
+
{"version":3,"file":"base.js","names":["xs: Set<string>","ys: Set<string>","reference?: string","input?: string","args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>","config?: Callbacks | BaseCallbackConfig","args: StringEvaluatorArgs","args: PairwiseStringEvaluatorArgs","args: LLMPairwiseStringEvaluatorArgs","callOptions?: ExtractLLMCallOptions<this[\"llm\"]>","args: LLMTrajectoryEvaluatorArgs"],"sources":["../../src/evaluation/base.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { AgentStep } from \"@langchain/core/agents\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport {\n BaseCallbackConfig,\n Callbacks,\n} from \"@langchain/core/callbacks/manager\";\nimport { BaseChain, LLMChain, LLMChainInput } from \"../chains/index.js\";\n\n/**\n * Base input for evaluators.\n */\nexport interface LLMEvalChainInput<\n T extends EvalOutputType = EvalOutputType,\n L extends BaseLanguageModelInterface = BaseLanguageModelInterface,\n> extends LLMChainInput<T, L> {}\n\nexport type ExtractLLMCallOptions<LanguageModelInterface> =\n LanguageModelInterface extends BaseLanguageModelInterface<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any,\n infer CallOptions\n >\n ? CallOptions\n : never;\n\n/**\n * Compare two sets for equality\n *\n * @param xs\n * @param ys\n */\nexport const eqSet = (xs: Set<string>, ys: Set<string>) =>\n xs.size === ys.size && [...xs].every((x) => ys.has(x));\n\n/**\n * The type of the output of an evaluation evaluator.\n */\nexport type EvalOutputType = Record<string, string | number | boolean>;\n\n/**\n * Base llm chain class for evaluators.\n */\nexport abstract class LLMEvalChain<\n T extends EvalOutputType = EvalOutputType,\n L extends BaseLanguageModelInterface = BaseLanguageModelInterface,\n> extends LLMChain<T, L> {\n requiresInput?: boolean = false;\n\n requiresReference?: boolean = false;\n\n skipInputWarning?: string = `Ignoring input in ${this.constructor.name}, as it is not expected.`;\n\n skipReferenceWarning?: string = `Ignoring reference in ${this.constructor.name}, as it is not expected.`;\n\n /**\n * Check if the evaluation arguments are valid.\n * @param reference The reference label.\n * @param input The input string.\n * @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.\n */\n checkEvaluationArgs(reference?: string, input?: string): void {\n if (this.requiresInput && input == null) {\n throw new Error(`${this.constructor.name} requires an input string.`);\n } else if (input != null && !this.requiresInput) {\n console.warn(this.skipInputWarning);\n }\n if (this.requiresReference && reference == null) {\n throw new Error(`${this.constructor.name} requires a reference string.`);\n } else if (reference != null && !this.requiresReference) {\n console.warn(this.skipReferenceWarning);\n }\n }\n}\n\n/**\n * Base chain class for evaluators.\n */\nexport abstract class EvalChain<\n RunInput extends ChainValues = ChainValues,\n RunOutput extends ChainValues = ChainValues,\n> extends BaseChain<RunInput, RunOutput> {\n requiresInput?: boolean = false;\n\n requiresReference?: boolean = false;\n\n skipInputWarning?: string = `Ignoring input in ${this.constructor.name}, as it is not expected.`;\n\n skipReferenceWarning?: string = `Ignoring reference in ${this.constructor.name}, as it is not expected.`;\n\n /**\n * Check if the evaluation arguments are valid.\n * @param reference The reference label.\n * @param input The input string.\n * @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.\n */\n checkEvaluationArgs(reference?: string, input?: string): void {\n if (this.requiresInput && input == null) {\n throw new Error(`${this.constructor.name} requires an input string.`);\n } else if (input != null && !this.requiresInput) {\n console.warn(this.skipInputWarning);\n }\n if (this.requiresReference && reference == null) {\n throw new Error(`${this.constructor.name} requires a reference string.`);\n } else if (reference != null && !this.requiresReference) {\n console.warn(this.skipReferenceWarning);\n }\n }\n}\n\n/**\n * @field prediction The output string from the model.\n * @field reference The expected output / reference string.\n * @field input The input string.\n */\nexport interface StringEvaluatorArgs {\n prediction: string;\n reference?: string;\n input?: string;\n}\n\n/**\n * @field prediction The output string from the first model.\n * @field predictionB The output string from the second model.\n */\nexport interface PairwiseStringEvaluatorArgs {\n prediction: string;\n predictionB: string;\n}\n\n/**\n * @field The input string.\n * @field prediction The output string from the first model.\n * @field predictionB The output string from the second model.\n * @field reference The expected output / reference string.\n */\nexport interface LLMPairwiseStringEvaluatorArgs {\n input: string;\n prediction: string;\n predictionB: string;\n reference?: string;\n}\n\n/**\n * Args for AgentTrajectoryEvaluator\n * @field input The input to the agent.\n * @field prediction The final predicted response.\n * @field reference The reference answer.\n * @field agentTrajectory The intermediate steps forming the agent trajectory.\n */\nexport interface LLMTrajectoryEvaluatorArgs {\n input: string;\n prediction: string;\n reference?: string;\n agentTrajectory: AgentStep[];\n}\n\n/**\n * Grade, tag, or otherwise evaluate predictions relative to their inputs\n * and/or reference labels\n */\nexport abstract class LLMStringEvaluator<\n T extends EvalOutputType = EvalOutputType,\n L extends BaseLanguageModelInterface = BaseLanguageModelInterface,\n> extends LLMEvalChain<T, L> {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param callOptions\n * @param config\n */\n abstract _evaluateStrings(\n args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param callOptions\n * @param config\n */\n evaluateStrings(\n args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateStrings(args, config);\n }\n}\n\n/**\n * Grade, tag, or otherwise evaluate predictions relative to their inputs\n * and/or reference labels\n */\nexport abstract class StringEvaluator extends EvalChain {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param config\n */\n abstract _evaluateStrings(\n args: StringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate Chain or LLM output, based on optional input and label.\n * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:\n * - score: the score of the evaluation, if applicable.\n * - value: the string value of the evaluation, if applicable.\n * - reasoning: the reasoning for the evaluation, if applicable.\n * @param args\n * @param config\n */\n evaluateStrings(\n args: StringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateStrings(args, config);\n }\n}\n\n/**\n * Compare the output of two models (or two outputs of the same model).\n */\nexport abstract class PairwiseStringEvaluator extends EvalChain {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n abstract _evaluateStringPairs(\n args: PairwiseStringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n evaluateStringPairs(\n args: PairwiseStringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n return this._evaluateStringPairs(args, config);\n }\n}\n\n/**\n * Compare the output of two models (or two outputs of the same model).\n */\nexport abstract class LLMPairwiseStringEvaluator extends LLMEvalChain {\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param callOptions\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n abstract _evaluateStringPairs(\n args: LLMPairwiseStringEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate the output string pairs.\n * @param args\n * @param callOptions\n * @param config\n * @return A dictionary containing the preference, scores, and/or other information.\n */\n evaluateStringPairs(\n args: LLMPairwiseStringEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateStringPairs(args, callOptions, config);\n }\n}\n\n/**\n * Interface for evaluating agent trajectories.\n */\nexport abstract class AgentTrajectoryEvaluator extends LLMEvalChain {\n requiresInput = true;\n\n /**\n * The name of the evaluation.\n */\n evaluationName?: string = this.constructor.name;\n\n /**\n * Evaluate a trajectory.\n * @return The evaluation result.\n * @param args\n * @param callOptions\n * @param config\n */\n abstract _evaluateAgentTrajectory(\n args: LLMTrajectoryEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues>;\n\n /**\n * Evaluate a trajectory.\n * @return The evaluation result.\n * @param args\n * @param callOptions\n * @param config\n */\n evaluateAgentTrajectory(\n args: LLMTrajectoryEvaluatorArgs,\n callOptions?: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n this.checkEvaluationArgs(args.reference, args.input);\n return this._evaluateAgentTrajectory(args, callOptions, config);\n }\n}\n"],"mappings":";;;;;;;;;;;AAgCA,MAAa,QAAQ,CAACA,IAAiBC,OACrC,GAAG,SAAS,GAAG,QAAQ,CAAC,GAAG,EAAG,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AAtB,cAGU,aAAmB;;;;CAI3B,iBAA0B,KAAK,YAAY;;;;;;;;;;;CA2B3C,gBACEC,MACAC,QACsB;EACtB,KAAK,oBAAoB,KAAK,WAAW,KAAK,MAAM;AACpD,SAAO,KAAK,iBAAiB,MAAM,OAAO;CAC3C;AACF;;;;;AAMD,IAAsB,kBAAtB,cAA8C,UAAU;;;;CAItD,iBAA0B,KAAK,YAAY;;;;;;;;;;CAyB3C,gBACEC,MACAD,QACsB;EACtB,KAAK,oBAAoB,KAAK,WAAW,KAAK,MAAM;AACpD,SAAO,KAAK,iBAAiB,MAAM,OAAO;CAC3C;AACF;;;;AAKD,IAAsB,0BAAtB,cAAsD,UAAU;;;;CAI9D,iBAA0B,KAAK,YAAY;;;;;;;CAmB3C,oBACEE,MACAF,QACsB;AACtB,SAAO,KAAK,qBAAqB,MAAM,OAAO;CAC/C;AACF;;;;AAKD,IAAsB,6BAAtB,cAAyD,aAAa;;;;CAIpE,iBAA0B,KAAK,YAAY;;;;;;;;CAsB3C,oBACEG,MACAC,aACAJ,QACsB;EACtB,KAAK,oBAAoB,KAAK,WAAW,KAAK,MAAM;AACpD,SAAO,KAAK,qBAAqB,MAAM,aAAa,OAAO;CAC5D;AACF;;;;AAKD,IAAsB,2BAAtB,cAAuD,aAAa;CAClE,gBAAgB;;;;CAKhB,iBAA0B,KAAK,YAAY;;;;;;;;CAsB3C,wBACEK,MACAD,aACAJ,QACsB;EACtB,KAAK,oBAAoB,KAAK,WAAW,KAAK,MAAM;AACpD,SAAO,KAAK,yBAAyB,MAAM,aAAa,OAAO;CAChE;AACF"}
|
|
@@ -6,7 +6,7 @@ import { ChatGeneration, Generation } from "@langchain/core/outputs";
|
|
|
6
6
|
import { BaseCallbackConfig, Callbacks } from "@langchain/core/callbacks/manager";
|
|
7
7
|
import { BaseLLMOutputParser } from "@langchain/core/output_parsers";
|
|
8
8
|
import { ChainValues } from "@langchain/core/utils/types";
|
|
9
|
-
import * as
|
|
9
|
+
import * as _langchain_core_prompt_values4 from "@langchain/core/prompt_values";
|
|
10
10
|
|
|
11
11
|
//#region src/evaluation/comparison/pairwise.d.ts
|
|
12
12
|
/**
|
|
@@ -30,7 +30,7 @@ declare class PairwiseStringEvalChain extends LLMPairwiseStringEvaluator {
|
|
|
30
30
|
skipReferenceWarning: string;
|
|
31
31
|
outputParser: PairwiseStringResultOutputParser;
|
|
32
32
|
static resolvePairwiseCriteria(criteria?: CriteriaLike): Record<string, string>;
|
|
33
|
-
static resolvePairwisePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any,
|
|
33
|
+
static resolvePairwisePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, _langchain_core_prompt_values4.BasePromptValueInterface, any>;
|
|
34
34
|
/**
|
|
35
35
|
* Create a new instance of the PairwiseStringEvalChain.
|
|
36
36
|
* @param llm
|
|
@@ -49,7 +49,7 @@ declare class PairwiseStringEvalChain extends LLMPairwiseStringEvaluator {
|
|
|
49
49
|
declare class LabeledPairwiseStringEvalChain extends PairwiseStringEvalChain {
|
|
50
50
|
static lc_name(): string;
|
|
51
51
|
requiresReference: boolean;
|
|
52
|
-
static resolvePairwisePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any,
|
|
52
|
+
static resolvePairwisePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, _langchain_core_prompt_values4.BasePromptValueInterface, any>;
|
|
53
53
|
}
|
|
54
54
|
//#endregion
|
|
55
55
|
export { LabeledPairwiseStringEvalChain, PairwiseStringEvalChain, PairwiseStringResultOutputParser };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pairwise.d.ts","names":["BaseLanguageModelInterface","BaseLLMOutputParser","ChainValues","ChatGeneration","Generation","BasePromptTemplate","Callbacks","BaseCallbackConfig","EvalOutputType","LLMEvalChainInput","LLMPairwiseStringEvaluator","LLMPairwiseStringEvaluatorArgs","ExtractLLMCallOptions","CriteriaLike","PairwiseStringResultOutputParser","Promise","PairwiseStringEvalChain","Record","
|
|
1
|
+
{"version":3,"file":"pairwise.d.ts","names":["BaseLanguageModelInterface","BaseLLMOutputParser","ChainValues","ChatGeneration","Generation","BasePromptTemplate","Callbacks","BaseCallbackConfig","EvalOutputType","LLMEvalChainInput","LLMPairwiseStringEvaluator","LLMPairwiseStringEvaluatorArgs","ExtractLLMCallOptions","CriteriaLike","PairwiseStringResultOutputParser","Promise","PairwiseStringEvalChain","Record","_langchain_core_prompt_values4","BasePromptValueInterface","Omit","Partial","LabeledPairwiseStringEvalChain"],"sources":["../../../src/evaluation/comparison/pairwise.d.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { BaseLLMOutputParser } from \"@langchain/core/output_parsers\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { ChatGeneration, Generation } from \"@langchain/core/outputs\";\nimport { BasePromptTemplate } from \"@langchain/core/prompts\";\nimport { Callbacks, BaseCallbackConfig } from \"@langchain/core/callbacks/manager\";\nimport { EvalOutputType, LLMEvalChainInput, LLMPairwiseStringEvaluator, LLMPairwiseStringEvaluatorArgs, type ExtractLLMCallOptions } from \"../base.js\";\nimport { CriteriaLike } from \"../criteria/criteria.js\";\n/**\n * A parser for the output of the PairwiseStringEvalChain.\n */\nexport declare class PairwiseStringResultOutputParser extends BaseLLMOutputParser<EvalOutputType> {\n static lc_name(): string;\n lc_namespace: string[];\n parseResult(generations: Generation[] | ChatGeneration[], _callbacks: Callbacks | undefined): Promise<EvalOutputType>;\n}\n/**\n * A chain for comparing two outputs, such as the outputs\n * of two models, prompts, or outputs of a single model on similar inputs.\n */\nexport declare class PairwiseStringEvalChain extends LLMPairwiseStringEvaluator {\n static lc_name(): string;\n criterionName?: string;\n evaluationName?: string;\n requiresInput: boolean;\n requiresReference: boolean;\n skipReferenceWarning: string;\n outputParser: PairwiseStringResultOutputParser;\n static resolvePairwiseCriteria(criteria?: CriteriaLike): Record<string, string>;\n static resolvePairwisePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, import(\"@langchain/core/prompt_values\").BasePromptValueInterface, any>;\n /**\n * Create a new instance of the PairwiseStringEvalChain.\n * @param llm\n * @param criteria The criteria to use for evaluation.\n * @param chainOptions Options to pass to the chain.\n */\n static fromLLM(llm: BaseLanguageModelInterface, criteria?: CriteriaLike, chainOptions?: Partial<Omit<LLMEvalChainInput, \"llm\">>): Promise<PairwiseStringEvalChain>;\n _prepareOutput(result: ChainValues): any;\n _evaluateStringPairs(args: LLMPairwiseStringEvaluatorArgs, callOptions: ExtractLLMCallOptions<this[\"llm\"]>, config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;\n}\n/**\n * A chain for comparing two outputs, such as the outputs\n * of two models, prompts, or outputs of a single model on similar inputs,\n * with labeled preferences.\n */\nexport declare class LabeledPairwiseStringEvalChain extends PairwiseStringEvalChain {\n static lc_name(): string;\n requiresReference: boolean;\n static resolvePairwisePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, import(\"@langchain/core/prompt_values\").BasePromptValueInterface, any>;\n}\n//# sourceMappingURL=pairwise.d.ts.map"],"mappings":";;;;;;;;;;;;;;AAWqBc,cAAAA,gCAAAA,SAAyCb,mBAAT,CAA6BO,cAA7B,CAAA,CAAA;EAA6BA,OAAAA,OAAAA,CAAAA,CAAAA,EAAAA,MAAAA;EAGrDJ,YAAAA,EAAAA,MAAAA,EAAAA;EAAeD,WAAAA,CAAAA,WAAAA,EAAfC,UAAeD,EAAAA,GAAAA,cAAAA,EAAAA,EAAAA,UAAAA,EAA8BG,SAA9BH,GAAAA,SAAAA,CAAAA,EAAsDY,OAAtDZ,CAA8DK,cAA9DL,CAAAA;;;;;AAHqC;AAS5Da,cAAAA,uBAAAA,SAAgCN,0BAAAA,CAAT;EAO1BI,OAAAA,OAAAA,CAAAA,CAAAA,EAAAA,MAAAA;EAC4BD,aAAAA,CAAAA,EAAAA,MAAAA;EAAeI,cAAAA,CAAAA,EAAAA,MAAAA;EACnBZ,aAAAA,EAAAA,OAAAA;EAAkBa,iB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