@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 5.08 kB
Source Map (JSON)
{"version":3,"file":"structured.cjs","names":["ChatPromptTemplate","RunnableBinding"],"sources":["../../src/prompts/structured.ts"],"sourcesContent":["import { ChatPromptValueInterface } from \"../prompt_values.js\";\nimport { RunnableLike, Runnable, RunnableBinding } from \"../runnables/base.js\";\nimport { RunnableConfig } from \"../runnables/config.js\";\nimport { InputValues } from \"../utils/types/index.js\";\nimport {\n BaseMessagePromptTemplateLike,\n ChatPromptTemplate,\n ChatPromptTemplateInput,\n} from \"./chat.js\";\n\nfunction isWithStructuredOutput(x: unknown): x is {\n withStructuredOutput: (...arg: unknown[]) => Runnable;\n} {\n return (\n typeof x === \"object\" &&\n x != null &&\n \"withStructuredOutput\" in x &&\n typeof x.withStructuredOutput === \"function\"\n );\n}\n\nfunction isRunnableBinding(x: unknown): x is RunnableBinding<unknown, unknown> {\n return (\n typeof x === \"object\" &&\n x != null &&\n \"lc_id\" in x &&\n Array.isArray(x.lc_id) &&\n x.lc_id.join(\"/\") === \"langchain_core/runnables/RunnableBinding\"\n );\n}\n\n/**\n * Interface for the input of a ChatPromptTemplate.\n */\nexport interface StructuredPromptInput<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunInput extends InputValues = any,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n PartialVariableName extends string = any,\n> extends ChatPromptTemplateInput<RunInput, PartialVariableName> {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n schema: Record<string, any>;\n method?: \"jsonMode\" | \"jsonSchema\" | \"functionMode\";\n}\n\nexport class StructuredPrompt<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunInput extends InputValues = any,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n PartialVariableName extends string = any,\n>\n extends ChatPromptTemplate<RunInput, PartialVariableName>\n implements StructuredPromptInput<RunInput, PartialVariableName>\n{\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n schema: Record<string, any>;\n\n method?: \"jsonMode\" | \"jsonSchema\" | \"functionMode\";\n\n lc_namespace = [\"langchain_core\", \"prompts\", \"structured\"];\n\n get lc_aliases(): Record<string, string> {\n return {\n ...super.lc_aliases,\n schema: \"schema_\",\n };\n }\n\n constructor(input: StructuredPromptInput<RunInput, PartialVariableName>) {\n super(input);\n this.schema = input.schema;\n this.method = input.method;\n }\n\n pipe<NewRunOutput>(\n coerceable: RunnableLike<ChatPromptValueInterface, NewRunOutput>\n ): Runnable<RunInput, Exclude<NewRunOutput, Error>, RunnableConfig> {\n if (isWithStructuredOutput(coerceable)) {\n return super.pipe(coerceable.withStructuredOutput(this.schema));\n }\n\n if (\n isRunnableBinding(coerceable) &&\n isWithStructuredOutput(coerceable.bound)\n ) {\n return super.pipe(\n new RunnableBinding({\n bound: coerceable.bound.withStructuredOutput(\n this.schema,\n ...(this.method ? [{ method: this.method }] : [])\n ),\n kwargs: coerceable.kwargs ?? {},\n config: coerceable.config,\n configFactories: coerceable.configFactories,\n })\n );\n }\n\n throw new Error(\n `Structured prompts need to be piped to a language model that supports the \"withStructuredOutput()\" method.`\n );\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n static fromMessagesAndSchema<RunInput extends InputValues = any>(\n promptMessages: (\n | ChatPromptTemplate<InputValues, string>\n | BaseMessagePromptTemplateLike\n )[],\n schema: StructuredPromptInput[\"schema\"],\n method?: \"jsonMode\" | \"jsonSchema\" | \"functionMode\"\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n ): ChatPromptTemplate<RunInput, any> {\n return StructuredPrompt.fromMessages<\n RunInput,\n StructuredPromptInput<RunInput>\n >(promptMessages, { schema, method });\n }\n}\n"],"mappings":";;;AAUA,SAAS,uBAAuB,GAE9B;CACA,OACE,OAAO,MAAM,YACb,KAAK,QACL,0BAA0B,KAC1B,OAAO,EAAE,yBAAyB;AAEtC;AAEA,SAAS,kBAAkB,GAAoD;CAC7E,OACE,OAAO,MAAM,YACb,KAAK,QACL,WAAW,KACX,MAAM,QAAQ,EAAE,KAAK,KACrB,EAAE,MAAM,KAAK,GAAG,MAAM;AAE1B;AAgBA,IAAa,mBAAb,MAAa,yBAMHA,aAAAA,mBAEV;CAEE;CAEA;CAEA,eAAe;EAAC;EAAkB;EAAW;CAAY;CAEzD,IAAI,aAAqC;EACvC,OAAO;GACL,GAAG,MAAM;GACT,QAAQ;EACV;CACF;CAEA,YAAY,OAA6D;EACvE,MAAM,KAAK;EACX,KAAK,SAAS,MAAM;EACpB,KAAK,SAAS,MAAM;CACtB;CAEA,KACE,YACkE;EAClE,IAAI,uBAAuB,UAAU,GACnC,OAAO,MAAM,KAAK,WAAW,qBAAqB,KAAK,MAAM,CAAC;EAGhE,IACE,kBAAkB,UAAU,KAC5B,uBAAuB,WAAW,KAAK,GAEvC,OAAO,MAAM,KACX,IAAIC,aAAAA,gBAAgB;GAClB,OAAO,WAAW,MAAM,qBACtB,KAAK,QACL,GAAI,KAAK,SAAS,CAAC,EAAE,QAAQ,KAAK,OAAO,CAAC,IAAI,CAAC,CACjD;GACA,QAAQ,WAAW,UAAU,CAAC;GAC9B,QAAQ,WAAW;GACnB,iBAAiB,WAAW;EAC9B,CAAC,CACH;EAGF,MAAM,IAAI,MACR,4GACF;CACF;CAGA,OAAO,sBACL,gBAIA,QACA,QAEmC;EACnC,OAAO,iBAAiB,aAGtB,gBAAgB;GAAE;GAAQ;EAAO,CAAC;CACtC;AACF"}