@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 5.45 kB
Source Map (JSON)
{"version":3,"file":"structured.cjs","names":["x: unknown","ChatPromptTemplate","input: StructuredPromptInput<RunInput, PartialVariableName>","coerceable: RunnableLike<ChatPromptValueInterface, NewRunOutput>","RunnableBinding","promptMessages: (\n | ChatPromptTemplate<InputValues, string>\n | BaseMessagePromptTemplateLike\n )[]","schema: StructuredPromptInput[\"schema\"]","method?: \"jsonMode\" | \"jsonSchema\" | \"functionMode\""],"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 // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunInput extends InputValues = any,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n PartialVariableName extends string = any\n> extends ChatPromptTemplateInput<RunInput, PartialVariableName> {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n schema: Record<string, any>;\n method?: \"jsonMode\" | \"jsonSchema\" | \"functionMode\";\n}\n\nexport class StructuredPrompt<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunInput extends InputValues = any,\n // eslint-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 // eslint-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 // eslint-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 // eslint-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,uBAAuBA,GAE9B;AACA,QACE,OAAO,MAAM,YACb,KAAK,QACL,0BAA0B,KAC1B,OAAO,EAAE,yBAAyB;AAErC;AAED,SAAS,kBAAkBA,GAAoD;AAC7E,QACE,OAAO,MAAM,YACb,KAAK,QACL,WAAW,KACX,MAAM,QAAQ,EAAE,MAAM,IACtB,EAAE,MAAM,KAAK,IAAI,KAAK;AAEzB;AAgBD,IAAa,mBAAb,MAAa,yBAMHC,gCAEV;CAEE;CAEA;CAEA,eAAe;EAAC;EAAkB;EAAW;CAAa;CAE1D,IAAI,aAAqC;AACvC,SAAO;GACL,GAAG,MAAM;GACT,QAAQ;EACT;CACF;CAED,YAAYC,OAA6D;EACvE,MAAM,MAAM;EACZ,KAAK,SAAS,MAAM;EACpB,KAAK,SAAS,MAAM;CACrB;CAED,KACEC,YACkE;AAClE,MAAI,uBAAuB,WAAW,CACpC,QAAO,MAAM,KAAK,WAAW,qBAAqB,KAAK,OAAO,CAAC;AAGjE,MACE,kBAAkB,WAAW,IAC7B,uBAAuB,WAAW,MAAM,CAExC,QAAO,MAAM,KACX,IAAIC,6BAAgB;GAClB,OAAO,WAAW,MAAM,qBACtB,KAAK,QACL,GAAI,KAAK,SAAS,CAAC,EAAE,QAAQ,KAAK,OAAQ,CAAC,IAAG,CAAE,EACjD;GACD,QAAQ,WAAW,UAAU,CAAE;GAC/B,QAAQ,WAAW;GACnB,iBAAiB,WAAW;EAC7B,GACF;AAGH,QAAM,IAAI,MACR,CAAC,0GAA0G,CAAC;CAE/G;CAGD,OAAO,sBACLC,gBAIAC,QACAC,QAEmC;AACnC,SAAO,iBAAiB,aAGtB,gBAAgB;GAAE;GAAQ;EAAQ,EAAC;CACtC;AACF"}