structured.cjs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.StructuredPrompt = void 0;
  4. const base_js_1 = require("../runnables/base.cjs");
  5. const chat_js_1 = require("./chat.cjs");
  6. function isWithStructuredOutput(x
  7. // eslint-disable-next-line @typescript-eslint/ban-types
  8. ) {
  9. return (typeof x === "object" &&
  10. x != null &&
  11. "withStructuredOutput" in x &&
  12. typeof x.withStructuredOutput === "function");
  13. }
  14. function isRunnableBinding(x) {
  15. return (typeof x === "object" &&
  16. x != null &&
  17. "lc_id" in x &&
  18. Array.isArray(x.lc_id) &&
  19. x.lc_id.join("/") === "langchain_core/runnables/RunnableBinding");
  20. }
  21. class StructuredPrompt extends chat_js_1.ChatPromptTemplate {
  22. get lc_aliases() {
  23. return {
  24. ...super.lc_aliases,
  25. schema: "schema_",
  26. };
  27. }
  28. constructor(input) {
  29. super(input);
  30. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  31. Object.defineProperty(this, "schema", {
  32. enumerable: true,
  33. configurable: true,
  34. writable: true,
  35. value: void 0
  36. });
  37. Object.defineProperty(this, "method", {
  38. enumerable: true,
  39. configurable: true,
  40. writable: true,
  41. value: void 0
  42. });
  43. Object.defineProperty(this, "lc_namespace", {
  44. enumerable: true,
  45. configurable: true,
  46. writable: true,
  47. value: ["langchain_core", "prompts", "structured"]
  48. });
  49. this.schema = input.schema;
  50. this.method = input.method;
  51. }
  52. pipe(coerceable) {
  53. if (isWithStructuredOutput(coerceable)) {
  54. return super.pipe(coerceable.withStructuredOutput(this.schema));
  55. }
  56. if (isRunnableBinding(coerceable) &&
  57. isWithStructuredOutput(coerceable.bound)) {
  58. return super.pipe(new base_js_1.RunnableBinding({
  59. bound: coerceable.bound.withStructuredOutput(this.schema, ...(this.method ? [{ method: this.method }] : [])),
  60. kwargs: coerceable.kwargs ?? {},
  61. config: coerceable.config,
  62. configFactories: coerceable.configFactories,
  63. }));
  64. }
  65. throw new Error(`Structured prompts need to be piped to a language model that supports the "withStructuredOutput()" method.`);
  66. }
  67. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  68. static fromMessagesAndSchema(promptMessages, schema, method
  69. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  70. ) {
  71. return StructuredPrompt.fromMessages(promptMessages, { schema, method });
  72. }
  73. }
  74. exports.StructuredPrompt = StructuredPrompt;