UNPKG

@langchain/core

Version:
1 lines 6.81 kB
{"version":3,"file":"metadata.cjs","names":["a?: ResponseMetadata","b?: ResponseMetadata","output: ResponseMetadata","_mergeDicts","a?: ModalitiesTokenDetails","b?: ModalitiesTokenDetails","output: ModalitiesTokenDetails","a?: InputTokenDetails","b?: InputTokenDetails","output: InputTokenDetails","a?: OutputTokenDetails","b?: OutputTokenDetails","output: OutputTokenDetails","a?: UsageMetadata","b?: UsageMetadata"],"sources":["../../src/messages/metadata.ts"],"sourcesContent":["import { _mergeDicts } from \"./base.js\";\nimport type { MessageOutputVersion } from \"./message.js\";\n\nexport type ResponseMetadata = {\n model_provider?: string;\n model_name?: string;\n output_version?: MessageOutputVersion;\n [key: string]: unknown;\n};\n\nexport function mergeResponseMetadata(\n a?: ResponseMetadata,\n b?: ResponseMetadata\n): ResponseMetadata {\n const output: ResponseMetadata = _mergeDicts(a ?? {}, b ?? {});\n return output;\n}\n\nexport type ModalitiesTokenDetails = {\n /**\n * Text tokens.\n * Does not need to be reported, but some models will do so.\n */\n text?: number;\n\n /**\n * Image (non-video) tokens.\n */\n image?: number;\n\n /**\n * Audio tokens.\n */\n audio?: number;\n\n /**\n * Video tokens.\n */\n video?: number;\n\n /**\n * Document tokens.\n * e.g. PDF\n */\n document?: number;\n};\n\nfunction mergeModalitiesTokenDetails(\n a?: ModalitiesTokenDetails,\n b?: ModalitiesTokenDetails\n): ModalitiesTokenDetails {\n const output: ModalitiesTokenDetails = {};\n if (a?.audio !== undefined || b?.audio !== undefined) {\n output.audio = (a?.audio ?? 0) + (b?.audio ?? 0);\n }\n if (a?.image !== undefined || b?.image !== undefined) {\n output.image = (a?.image ?? 0) + (b?.image ?? 0);\n }\n if (a?.video !== undefined || b?.video !== undefined) {\n output.video = (a?.video ?? 0) + (b?.video ?? 0);\n }\n if (a?.document !== undefined || b?.document !== undefined) {\n output.document = (a?.document ?? 0) + (b?.document ?? 0);\n }\n if (a?.text !== undefined || b?.text !== undefined) {\n output.text = (a?.text ?? 0) + (b?.text ?? 0);\n }\n return output;\n}\n\n/**\n * Breakdown of input token counts.\n *\n * Does not *need* to sum to full input token count. Does *not* need to have all keys.\n */\nexport type InputTokenDetails = ModalitiesTokenDetails & {\n /**\n * Input tokens that were cached and there was a cache hit.\n *\n * Since there was a cache hit, the tokens were read from the cache.\n * More precisely, the model state given these tokens was read from the cache.\n */\n cache_read?: number;\n\n /**\n * Input tokens that were cached and there was a cache miss.\n *\n * Since there was a cache miss, the cache was created from these tokens.\n */\n cache_creation?: number;\n};\n\nfunction mergeInputTokenDetails(\n a?: InputTokenDetails,\n b?: InputTokenDetails\n): InputTokenDetails {\n const output: InputTokenDetails = {\n ...mergeModalitiesTokenDetails(a, b),\n };\n if (a?.cache_read !== undefined || b?.cache_read !== undefined) {\n output.cache_read = (a?.cache_read ?? 0) + (b?.cache_read ?? 0);\n }\n if (a?.cache_creation !== undefined || b?.cache_creation !== undefined) {\n output.cache_creation = (a?.cache_creation ?? 0) + (b?.cache_creation ?? 0);\n }\n return output;\n}\n\n/**\n * Breakdown of output token counts.\n *\n * Does *not* need to sum to full output token count. Does *not* need to have all keys.\n */\nexport type OutputTokenDetails = ModalitiesTokenDetails & {\n /**\n * Reasoning output tokens.\n *\n * Tokens generated by the model in a chain of thought process (i.e. by\n * OpenAI's o1 models) that are not returned as part of model output.\n */\n reasoning?: number;\n};\n\nfunction mergeOutputTokenDetails(\n a?: OutputTokenDetails,\n b?: OutputTokenDetails\n): OutputTokenDetails {\n const output: OutputTokenDetails = {\n ...mergeModalitiesTokenDetails(a, b),\n };\n if (a?.reasoning !== undefined || b?.reasoning !== undefined) {\n output.reasoning = (a?.reasoning ?? 0) + (b?.reasoning ?? 0);\n }\n return output;\n}\n\n/**\n * Usage metadata for a message, such as token counts.\n */\nexport type UsageMetadata = {\n /**\n * Count of input (or prompt) tokens. Sum of all input token types.\n */\n input_tokens: number;\n /**\n * Count of output (or completion) tokens. Sum of all output token types.\n */\n output_tokens: number;\n /**\n * Total token count. Sum of input_tokens + output_tokens.\n */\n total_tokens: number;\n\n /**\n * Breakdown of input token counts.\n *\n * Does *not* need to sum to full input token count. Does *not* need to have all keys.\n */\n input_token_details?: InputTokenDetails;\n\n /**\n * Breakdown of output token counts.\n *\n * Does *not* need to sum to full output token count. Does *not* need to have all keys.\n */\n output_token_details?: OutputTokenDetails;\n};\n\nexport function mergeUsageMetadata(\n a?: UsageMetadata,\n b?: UsageMetadata\n): UsageMetadata {\n return {\n input_tokens: (a?.input_tokens ?? 0) + (b?.input_tokens ?? 0),\n output_tokens: (a?.output_tokens ?? 0) + (b?.output_tokens ?? 0),\n total_tokens: (a?.total_tokens ?? 0) + (b?.total_tokens ?? 0),\n input_token_details: mergeInputTokenDetails(\n a?.input_token_details,\n b?.input_token_details\n ),\n output_token_details: mergeOutputTokenDetails(\n a?.output_token_details,\n b?.output_token_details\n ),\n };\n}\n"],"mappings":";;;AAUA,SAAgB,sBACdA,GACAC,GACkB;CAClB,MAAMC,SAA2BC,yBAAY,KAAK,CAAE,GAAE,KAAK,CAAE,EAAC;AAC9D,QAAO;AACR;AA+BD,SAAS,4BACPC,GACAC,GACwB;CACxB,MAAMC,SAAiC,CAAE;AACzC,KAAI,GAAG,UAAU,UAAa,GAAG,UAAU,QACzC,OAAO,SAAS,GAAG,SAAS,MAAM,GAAG,SAAS;AAEhD,KAAI,GAAG,UAAU,UAAa,GAAG,UAAU,QACzC,OAAO,SAAS,GAAG,SAAS,MAAM,GAAG,SAAS;AAEhD,KAAI,GAAG,UAAU,UAAa,GAAG,UAAU,QACzC,OAAO,SAAS,GAAG,SAAS,MAAM,GAAG,SAAS;AAEhD,KAAI,GAAG,aAAa,UAAa,GAAG,aAAa,QAC/C,OAAO,YAAY,GAAG,YAAY,MAAM,GAAG,YAAY;AAEzD,KAAI,GAAG,SAAS,UAAa,GAAG,SAAS,QACvC,OAAO,QAAQ,GAAG,QAAQ,MAAM,GAAG,QAAQ;AAE7C,QAAO;AACR;AAwBD,SAAS,uBACPC,GAC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