dict.js 3.8 KB

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