UNPKG

@langchain/core

Version:
1 lines 18.2 kB
{"version":3,"file":"base.d.ts","names":["TiktokenModel","ZodType","ZodTypeV3","$ZodType","ZodTypeV4","BaseCache","BasePromptValueInterface","BaseMessage","BaseMessageLike","MessageContent","LLMResult","CallbackManager","Callbacks","AsyncCaller","AsyncCallerParams","Runnable","RunnableInterface","RunnableConfig","JSONSchema","InferInteropZodOutput","InteropZodObject","InteropZodType","ModelProfile","getModelNameForTiktoken","getEmbeddingContextSize","getModelContextSize","isOpenAITool","ToolDefinition","CalculateMaxTokenProps","calculateMaxTokens","prompt","modelName","Promise","SerializedLLM","Record","BaseLangChainParams","BaseLangChain","RunInput","RunOutput","CallOptions","BaseLanguageModelParams","BaseLanguageModelTracingCallOptions","BaseLanguageModelCallOptions","FunctionDefinition","FunctionCallOption","BaseFunctionCallOptions","BaseLanguageModelInput","StructuredOutputType","StructuredOutputMethodOptions","IncludeRaw","StructuredOutputMethodParams","BaseLanguageModelInterface","LanguageModelOutput","LanguageModelLike","BaseLanguageModel","callbacks","callbackManager","config","TokenUsage"],"sources":["../../src/language_models/base.d.ts"],"sourcesContent":["import type { TiktokenModel } from \"js-tiktoken/lite\";\nimport type { ZodType as ZodTypeV3 } from \"zod/v3\";\nimport type { $ZodType as ZodTypeV4 } from \"zod/v4/core\";\nimport { type BaseCache } from \"../caches/index.js\";\nimport { type BasePromptValueInterface } from \"../prompt_values.js\";\nimport { type BaseMessage, type BaseMessageLike, type MessageContent } from \"../messages/base.js\";\nimport { type LLMResult } from \"../outputs.js\";\nimport { CallbackManager, Callbacks } from \"../callbacks/manager.js\";\nimport { AsyncCaller, AsyncCallerParams } from \"../utils/async_caller.js\";\nimport { Runnable, type RunnableInterface } from \"../runnables/base.js\";\nimport { RunnableConfig } from \"../runnables/config.js\";\nimport { JSONSchema } from \"../utils/json_schema.js\";\nimport { InferInteropZodOutput, InteropZodObject, InteropZodType } from \"../utils/types/zod.js\";\nimport { ModelProfile } from \"./profile.js\";\nexport declare const getModelNameForTiktoken: (modelName: string) => TiktokenModel;\nexport declare const getEmbeddingContextSize: (modelName?: string | undefined) => number;\n/**\n * Get the context window size (max input tokens) for a given model.\n *\n * Context window sizes are sourced from official model documentation:\n * - OpenAI: https://platform.openai.com/docs/models\n * - Anthropic: https://docs.anthropic.com/claude/docs/models-overview\n * - Google: https://ai.google.dev/gemini/docs/models/gemini\n *\n * @param modelName - The name of the model\n * @returns The context window size in tokens\n */\nexport declare const getModelContextSize: (modelName: string) => number;\n/**\n * Whether or not the input matches the OpenAI tool definition.\n * @param {unknown} tool The input to check.\n * @returns {boolean} Whether the input is an OpenAI tool definition.\n */\nexport declare function isOpenAITool(tool: unknown): tool is ToolDefinition;\ninterface CalculateMaxTokenProps {\n prompt: string;\n modelName: TiktokenModel;\n}\nexport declare const calculateMaxTokens: ({ prompt, modelName, }: CalculateMaxTokenProps) => Promise<number>;\nexport type SerializedLLM = {\n _model: string;\n _type: string;\n} & Record<string, any>;\nexport interface BaseLangChainParams {\n verbose?: boolean;\n callbacks?: Callbacks;\n tags?: string[];\n metadata?: Record<string, unknown>;\n}\n/**\n * Base class for language models, chains, tools.\n */\nexport declare abstract class BaseLangChain<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> extends Runnable<RunInput, RunOutput, CallOptions> implements BaseLangChainParams {\n /**\n * Whether to print out response text.\n */\n verbose: boolean;\n callbacks?: Callbacks;\n tags?: string[];\n metadata?: Record<string, unknown>;\n get lc_attributes(): {\n [key: string]: undefined;\n } | undefined;\n constructor(params: BaseLangChainParams);\n}\n/**\n * Base interface for language model parameters.\n * A subclass of {@link BaseLanguageModel} should have a constructor that\n * takes in a parameter that extends this interface.\n */\nexport interface BaseLanguageModelParams extends AsyncCallerParams, BaseLangChainParams {\n /**\n * @deprecated Use `callbacks` instead\n */\n callbackManager?: CallbackManager;\n cache?: BaseCache | boolean;\n}\nexport interface BaseLanguageModelTracingCallOptions {\n /**\n * Describes the format of structured outputs.\n * This should be provided if an output is considered to be structured\n */\n ls_structured_output_format?: {\n /**\n * An object containing the method used for structured output (e.g., \"jsonMode\").\n */\n kwargs: {\n method: string;\n };\n /**\n * The JSON schema describing the expected output structure.\n */\n schema?: JSONSchema;\n };\n}\nexport interface BaseLanguageModelCallOptions extends RunnableConfig, BaseLanguageModelTracingCallOptions {\n /**\n * Stop tokens to use for this call.\n * If not provided, the default stop tokens for the model will be used.\n */\n stop?: string[];\n}\nexport interface FunctionDefinition {\n /**\n * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain\n * underscores and dashes, with a maximum length of 64.\n */\n name: string;\n /**\n * The parameters the functions accepts, described as a JSON Schema object. See the\n * [guide](https://platform.openai.com/docs/guides/gpt/function-calling) for\n * examples, and the\n * [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for\n * documentation about the format.\n *\n * To describe a function that accepts no parameters, provide the value\n * `{\"type\": \"object\", \"properties\": {}}`.\n */\n parameters: Record<string, unknown> | JSONSchema;\n /**\n * A description of what the function does, used by the model to choose when and\n * how to call the function.\n */\n description?: string;\n}\nexport interface ToolDefinition {\n type: \"function\";\n function: FunctionDefinition;\n}\nexport type FunctionCallOption = {\n name: string;\n};\nexport interface BaseFunctionCallOptions extends BaseLanguageModelCallOptions {\n function_call?: FunctionCallOption;\n functions?: FunctionDefinition[];\n}\nexport type BaseLanguageModelInput = BasePromptValueInterface | string | BaseMessageLike[];\nexport type StructuredOutputType = InferInteropZodOutput<InteropZodObject>;\nexport type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> = {\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\" | \"jsonSchema\" | string;\n includeRaw?: IncludeRaw;\n /** Whether to use strict mode. Currently only supported by OpenAI models. */\n strict?: boolean;\n};\n/** @deprecated Use StructuredOutputMethodOptions instead */\nexport type StructuredOutputMethodParams<RunOutput, IncludeRaw extends boolean = false> = {\n /** @deprecated Pass schema in as the first argument */\n schema: InteropZodType<RunOutput> | Record<string, any>;\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\";\n includeRaw?: IncludeRaw;\n};\nexport interface BaseLanguageModelInterface<RunOutput = any, CallOptions extends BaseLanguageModelCallOptions = BaseLanguageModelCallOptions> extends RunnableInterface<BaseLanguageModelInput, RunOutput, CallOptions> {\n get callKeys(): string[];\n generatePrompt(promptValues: BasePromptValueInterface[], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<LLMResult>;\n _modelType(): string;\n _llmType(): string;\n getNumTokens(content: MessageContent): Promise<number>;\n /**\n * Get the identifying parameters of the LLM.\n */\n _identifyingParams(): Record<string, any>;\n serialize(): SerializedLLM;\n}\nexport type LanguageModelOutput = BaseMessage | string;\nexport type LanguageModelLike = Runnable<BaseLanguageModelInput, LanguageModelOutput>;\n/**\n * Base class for language models.\n */\nexport declare abstract class BaseLanguageModel<RunOutput = any, CallOptions extends BaseLanguageModelCallOptions = BaseLanguageModelCallOptions> extends BaseLangChain<BaseLanguageModelInput, RunOutput, CallOptions> implements BaseLanguageModelParams, BaseLanguageModelInterface<RunOutput, CallOptions> {\n /**\n * Keys that the language model accepts as call options.\n */\n get callKeys(): string[];\n /**\n * The async caller should be used by subclasses to make any async calls,\n * which will thus benefit from the concurrency and retry logic.\n */\n caller: AsyncCaller;\n cache?: BaseCache;\n constructor({ callbacks, callbackManager, ...params }: BaseLanguageModelParams);\n abstract generatePrompt(promptValues: BasePromptValueInterface[], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<LLMResult>;\n abstract _modelType(): string;\n abstract _llmType(): string;\n private _encoding?;\n /**\n * Get the number of tokens in the content.\n * @param content The content to get the number of tokens for.\n * @returns The number of tokens in the content.\n */\n getNumTokens(content: MessageContent): Promise<number>;\n protected static _convertInputToPromptValue(input: BaseLanguageModelInput): BasePromptValueInterface;\n /**\n * Get the identifying parameters of the LLM.\n */\n _identifyingParams(): Record<string, any>;\n /**\n * Create a unique cache key for a specific call to a specific language model.\n * @param callOptions Call options for the model\n * @returns A unique cache key.\n */\n _getSerializedCacheKeyParametersForCall({ config, ...callOptions }: CallOptions & {\n config?: RunnableConfig;\n }): string;\n /**\n * @deprecated\n * Return a json-like object representing this LLM.\n */\n serialize(): SerializedLLM;\n /**\n * @deprecated\n * Load an LLM from a json-like object describing it.\n */\n static deserialize(_data: SerializedLLM): Promise<BaseLanguageModel>;\n /**\n * Return profiling information for the model.\n *\n * @returns {ModelProfile} An object describing the model's capabilities and constraints\n */\n get profile(): ModelProfile;\n withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;\n withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {\n raw: BaseMessage;\n parsed: RunOutput;\n }>;\n withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;\n withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {\n raw: BaseMessage;\n parsed: RunOutput;\n }>;\n /**\n * Model wrapper that returns outputs formatted to match the given schema.\n *\n * @template {BaseLanguageModelInput} RunInput The input type for the Runnable, expected to be the same input for the LLM.\n * @template {Record<string, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.\n *\n * @param {InteropZodType<RunOutput>} schema The schema for the structured output. Either as a Zod schema or a valid JSON schema object.\n * If a Zod schema is passed, the returned attributes will be validated, whereas with JSON schema they will not be.\n * @param {string} name The name of the function to call.\n * @param {\"functionCalling\" | \"jsonMode\"} [method=functionCalling] The method to use for getting the structured output. Defaults to \"functionCalling\".\n * @param {boolean | undefined} [includeRaw=false] Whether to include the raw output in the result. Defaults to false.\n * @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.\n */\n withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<boolean>): Runnable<BaseLanguageModelInput, RunOutput> | Runnable<BaseLanguageModelInput, {\n raw: BaseMessage;\n parsed: RunOutput;\n }>;\n}\n/**\n * Shared interface for token usage\n * return type from LLM calls.\n */\nexport interface TokenUsage {\n completionTokens?: number;\n promptTokens?: number;\n totalTokens?: number;\n}\nexport {};\n//# sourceMappingURL=base.d.ts.map"],"mappings":";;;;;;;;;;;;;;;;;;cAcqBuB,gDAAgDvB;cAChDwB;;;AADrB;AACA;AAYA;AAMA;AAA4E;AAK5E;;;;AAA6FQ,cAXxEP,mBAWwEO,EAAAA,CAAAA,SAAAA,EAAAA,MAAAA,EAAAA,GAAAA,MAAAA;AAAO;AACpG;AAIA;AASA;;AAAsGf,iBAnB9ES,YAAAA,CAmB8ET,IAAAA,EAAAA,OAAAA,CAAAA,EAAAA,IAAAA,IAnBzCU,cAmByCV;UAlB5FW,sBAAAA,CAkB6HS;EAAUC,MAAAA,EAAAA,MAAAA;EAAWC,SAAAA,EAhB7IvC,aAgB6IuC;;AAO7IL,cArBML,kBAqBNK,EAAAA,CAAAA;EAAAA,MAAAA;EAAAA;AAAAA,CAAAA,EArBmDN,sBAqBnDM,EAAAA,GArB8EF,OAqB9EE,CAAAA,MAAAA,CAAAA;AAISC,KAxBZF,aAAAA,GAwBYE;EAXsGpB,MAAAA,EAAAA,MAAAA;EAAsDoB,KAAAA,EAAAA,MAAAA;AAAmB,CAAA,GAVnMD,MAUmM,CAAA,MAAA,EAAA,GAAA,CAAA;AAkBtLM,UA3BAL,mBAAAA,CA2BuB;EAIlBxB,OAAAA,CAAAA,EAAAA,OAAAA;EACVN,SAAAA,CAAAA,EA9BIO,SA8BJP;EALqCS,IAAAA,CAAAA,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