parser.mjs 5.0 KB

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