parser.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.validateInputTools = exports.hasAutoParseableInput = exports.shouldParseToolCall = exports.parseChatCompletion = exports.maybeParseChatCompletion = exports.isAutoParsableTool = exports.makeParseableTool = exports.isAutoParsableResponseFormat = exports.makeParseableTextFormat = exports.makeParseableResponseFormat = void 0;
  4. const error_1 = require("../error.js");
  5. function makeParseableResponseFormat(response_format, parser) {
  6. const obj = { ...response_format };
  7. Object.defineProperties(obj, {
  8. $brand: {
  9. value: 'auto-parseable-response-format',
  10. enumerable: false,
  11. },
  12. $parseRaw: {
  13. value: parser,
  14. enumerable: false,
  15. },
  16. });
  17. return obj;
  18. }
  19. exports.makeParseableResponseFormat = makeParseableResponseFormat;
  20. function makeParseableTextFormat(response_format, parser) {
  21. const obj = { ...response_format };
  22. Object.defineProperties(obj, {
  23. $brand: {
  24. value: 'auto-parseable-response-format',
  25. enumerable: false,
  26. },
  27. $parseRaw: {
  28. value: parser,
  29. enumerable: false,
  30. },
  31. });
  32. return obj;
  33. }
  34. exports.makeParseableTextFormat = makeParseableTextFormat;
  35. function isAutoParsableResponseFormat(response_format) {
  36. return response_format?.['$brand'] === 'auto-parseable-response-format';
  37. }
  38. exports.isAutoParsableResponseFormat = isAutoParsableResponseFormat;
  39. function makeParseableTool(tool, { parser, callback, }) {
  40. const obj = { ...tool };
  41. Object.defineProperties(obj, {
  42. $brand: {
  43. value: 'auto-parseable-tool',
  44. enumerable: false,
  45. },
  46. $parseRaw: {
  47. value: parser,
  48. enumerable: false,
  49. },
  50. $callback: {
  51. value: callback,
  52. enumerable: false,
  53. },
  54. });
  55. return obj;
  56. }
  57. exports.makeParseableTool = makeParseableTool;
  58. function isAutoParsableTool(tool) {
  59. return tool?.['$brand'] === 'auto-parseable-tool';
  60. }
  61. exports.isAutoParsableTool = isAutoParsableTool;
  62. function maybeParseChatCompletion(completion, params) {
  63. if (!params || !hasAutoParseableInput(params)) {
  64. return {
  65. ...completion,
  66. choices: completion.choices.map((choice) => ({
  67. ...choice,
  68. message: {
  69. ...choice.message,
  70. parsed: null,
  71. ...(choice.message.tool_calls ?
  72. {
  73. tool_calls: choice.message.tool_calls,
  74. }
  75. : undefined),
  76. },
  77. })),
  78. };
  79. }
  80. return parseChatCompletion(completion, params);
  81. }
  82. exports.maybeParseChatCompletion = maybeParseChatCompletion;
  83. function parseChatCompletion(completion, params) {
  84. const choices = completion.choices.map((choice) => {
  85. if (choice.finish_reason === 'length') {
  86. throw new error_1.LengthFinishReasonError();
  87. }
  88. if (choice.finish_reason === 'content_filter') {
  89. throw new error_1.ContentFilterFinishReasonError();
  90. }
  91. return {
  92. ...choice,
  93. message: {
  94. ...choice.message,
  95. ...(choice.message.tool_calls ?
  96. {
  97. tool_calls: choice.message.tool_calls?.map((toolCall) => parseToolCall(params, toolCall)) ?? undefined,
  98. }
  99. : undefined),
  100. parsed: choice.message.content && !choice.message.refusal ?
  101. parseResponseFormat(params, choice.message.content)
  102. : null,
  103. },
  104. };
  105. });
  106. return { ...completion, choices };
  107. }
  108. exports.parseChatCompletion = parseChatCompletion;
  109. function parseResponseFormat(params, content) {
  110. if (params.response_format?.type !== 'json_schema') {
  111. return null;
  112. }
  113. if (params.response_format?.type === 'json_schema') {
  114. if ('$parseRaw' in params.response_format) {
  115. const response_format = params.response_format;
  116. return response_format.$parseRaw(content);
  117. }
  118. return JSON.parse(content);
  119. }
  120. return null;
  121. }
  122. function parseToolCall(params, toolCall) {
  123. const inputTool = params.tools?.find((inputTool) => inputTool.function?.name === toolCall.function.name);
  124. return {
  125. ...toolCall,
  126. function: {
  127. ...toolCall.function,
  128. parsed_arguments: isAutoParsableTool(inputTool) ? inputTool.$parseRaw(toolCall.function.arguments)
  129. : inputTool?.function.strict ? JSON.parse(toolCall.function.arguments)
  130. : null,
  131. },
  132. };
  133. }
  134. function shouldParseToolCall(params, toolCall) {
  135. if (!params) {
  136. return false;
  137. }
  138. const inputTool = params.tools?.find((inputTool) => inputTool.function?.name === toolCall.function.name);
  139. return isAutoParsableTool(inputTool) || inputTool?.function.strict || false;
  140. }
  141. exports.shouldParseToolCall = shouldParseToolCall;
  142. function hasAutoParseableInput(params) {
  143. if (isAutoParsableResponseFormat(params.response_format)) {
  144. return true;
  145. }
  146. return (params.tools?.some((t) => isAutoParsableTool(t) || (t.type === 'function' && t.function.strict === true)) ?? false);
  147. }
  148. exports.hasAutoParseableInput = hasAutoParseableInput;
  149. function validateInputTools(tools) {
  150. for (const tool of tools ?? []) {
  151. if (tool.type !== 'function') {
  152. throw new error_1.OpenAIError(`Currently only \`function\` tool types support auto-parsing; Received \`${tool.type}\``);
  153. }
  154. if (tool.function.strict !== true) {
  155. throw new error_1.OpenAIError(`The \`${tool.function.name}\` tool is not marked with \`strict: true\`. Only strict function tools can be auto-parsed`);
  156. }
  157. }
  158. }
  159. exports.validateInputTools = validateInputTools;
  160. //# sourceMappingURL=parser.js.map