parser.mjs 5.9 KB

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