dict.cjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DictPromptTemplate = void 0;
  4. const base_js_1 = require("../runnables/base.cjs");
  5. const template_js_1 = require("./template.cjs");
  6. class DictPromptTemplate extends base_js_1.Runnable {
  7. static lc_name() {
  8. return "DictPromptTemplate";
  9. }
  10. constructor(fields) {
  11. const templateFormat = fields.templateFormat ?? "f-string";
  12. const inputVariables = _getInputVariables(fields.template, templateFormat);
  13. super({ inputVariables, ...fields });
  14. Object.defineProperty(this, "lc_namespace", {
  15. enumerable: true,
  16. configurable: true,
  17. writable: true,
  18. value: ["langchain_core", "prompts", "dict"]
  19. });
  20. Object.defineProperty(this, "lc_serializable", {
  21. enumerable: true,
  22. configurable: true,
  23. writable: true,
  24. value: true
  25. });
  26. Object.defineProperty(this, "template", {
  27. enumerable: true,
  28. configurable: true,
  29. writable: true,
  30. value: void 0
  31. });
  32. Object.defineProperty(this, "templateFormat", {
  33. enumerable: true,
  34. configurable: true,
  35. writable: true,
  36. value: void 0
  37. });
  38. Object.defineProperty(this, "inputVariables", {
  39. enumerable: true,
  40. configurable: true,
  41. writable: true,
  42. value: void 0
  43. });
  44. this.template = fields.template;
  45. this.templateFormat = templateFormat;
  46. this.inputVariables = inputVariables;
  47. }
  48. async format(values) {
  49. return _insertInputVariables(this.template, values, this.templateFormat);
  50. }
  51. async invoke(values) {
  52. return await this._callWithConfig(this.format.bind(this), values, {
  53. runType: "prompt",
  54. });
  55. }
  56. }
  57. exports.DictPromptTemplate = DictPromptTemplate;
  58. function _getInputVariables(template, templateFormat) {
  59. const inputVariables = [];
  60. for (const v of Object.values(template)) {
  61. if (typeof v === "string") {
  62. (0, template_js_1.parseTemplate)(v, templateFormat).forEach((t) => {
  63. if (t.type === "variable") {
  64. inputVariables.push(t.name);
  65. }
  66. });
  67. }
  68. else if (Array.isArray(v)) {
  69. for (const x of v) {
  70. if (typeof x === "string") {
  71. (0, template_js_1.parseTemplate)(x, templateFormat).forEach((t) => {
  72. if (t.type === "variable") {
  73. inputVariables.push(t.name);
  74. }
  75. });
  76. }
  77. else if (typeof x === "object") {
  78. inputVariables.push(..._getInputVariables(x, templateFormat));
  79. }
  80. }
  81. }
  82. else if (typeof v === "object" && v !== null) {
  83. inputVariables.push(..._getInputVariables(v, templateFormat));
  84. }
  85. }
  86. return Array.from(new Set(inputVariables));
  87. }
  88. function _insertInputVariables(template, inputs, templateFormat) {
  89. const formatted = {};
  90. for (const [k, v] of Object.entries(template)) {
  91. if (typeof v === "string") {
  92. formatted[k] = (0, template_js_1.renderTemplate)(v, templateFormat, inputs);
  93. }
  94. else if (Array.isArray(v)) {
  95. const formattedV = [];
  96. for (const x of v) {
  97. if (typeof x === "string") {
  98. formattedV.push((0, template_js_1.renderTemplate)(x, templateFormat, inputs));
  99. }
  100. else if (typeof x === "object") {
  101. formattedV.push(_insertInputVariables(x, inputs, templateFormat));
  102. }
  103. }
  104. formatted[k] = formattedV;
  105. }
  106. else if (typeof v === "object" && v !== null) {
  107. formatted[k] = _insertInputVariables(v, inputs, templateFormat);
  108. }
  109. else {
  110. formatted[k] = v;
  111. }
  112. }
  113. return formatted;
  114. }