openai 4.53.2 → 4.55.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/README.md +2 -2
- package/_vendor/partial-json-parser/parser.d.ts +3 -0
- package/_vendor/partial-json-parser/parser.d.ts.map +1 -0
- package/_vendor/partial-json-parser/parser.js +226 -0
- package/_vendor/partial-json-parser/parser.js.map +1 -0
- package/_vendor/partial-json-parser/parser.mjs +223 -0
- package/_vendor/partial-json-parser/parser.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/Options.d.ts +32 -0
- package/_vendor/zod-to-json-schema/Options.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/Options.js +36 -0
- package/_vendor/zod-to-json-schema/Options.js.map +1 -0
- package/_vendor/zod-to-json-schema/Options.mjs +32 -0
- package/_vendor/zod-to-json-schema/Options.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/Refs.d.ts +15 -0
- package/_vendor/zod-to-json-schema/Refs.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/Refs.js +26 -0
- package/_vendor/zod-to-json-schema/Refs.js.map +1 -0
- package/_vendor/zod-to-json-schema/Refs.mjs +22 -0
- package/_vendor/zod-to-json-schema/Refs.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/errorMessages.d.ts +12 -0
- package/_vendor/zod-to-json-schema/errorMessages.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/errorMessages.js +20 -0
- package/_vendor/zod-to-json-schema/errorMessages.js.map +1 -0
- package/_vendor/zod-to-json-schema/errorMessages.mjs +15 -0
- package/_vendor/zod-to-json-schema/errorMessages.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/index.d.ts +38 -0
- package/_vendor/zod-to-json-schema/index.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/index.js +54 -0
- package/_vendor/zod-to-json-schema/index.js.map +1 -0
- package/_vendor/zod-to-json-schema/index.mjs +38 -0
- package/_vendor/zod-to-json-schema/index.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parseDef.d.ts +38 -0
- package/_vendor/zod-to-json-schema/parseDef.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parseDef.js +168 -0
- package/_vendor/zod-to-json-schema/parseDef.js.map +1 -0
- package/_vendor/zod-to-json-schema/parseDef.mjs +164 -0
- package/_vendor/zod-to-json-schema/parseDef.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/any.d.ts +3 -0
- package/_vendor/zod-to-json-schema/parsers/any.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/any.js +8 -0
- package/_vendor/zod-to-json-schema/parsers/any.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/any.mjs +4 -0
- package/_vendor/zod-to-json-schema/parsers/any.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/array.d.ts +13 -0
- package/_vendor/zod-to-json-schema/parsers/array.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/array.js +30 -0
- package/_vendor/zod-to-json-schema/parsers/array.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/array.mjs +26 -0
- package/_vendor/zod-to-json-schema/parsers/array.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/bigint.d.ts +15 -0
- package/_vendor/zod-to-json-schema/parsers/bigint.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/bigint.js +54 -0
- package/_vendor/zod-to-json-schema/parsers/bigint.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/bigint.mjs +50 -0
- package/_vendor/zod-to-json-schema/parsers/bigint.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/boolean.d.ts +5 -0
- package/_vendor/zod-to-json-schema/parsers/boolean.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/boolean.js +10 -0
- package/_vendor/zod-to-json-schema/parsers/boolean.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/boolean.mjs +6 -0
- package/_vendor/zod-to-json-schema/parsers/boolean.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/branded.d.ts +4 -0
- package/_vendor/zod-to-json-schema/parsers/branded.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/branded.js +9 -0
- package/_vendor/zod-to-json-schema/parsers/branded.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/branded.mjs +5 -0
- package/_vendor/zod-to-json-schema/parsers/branded.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/catch.d.ts +4 -0
- package/_vendor/zod-to-json-schema/parsers/catch.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/catch.js +9 -0
- package/_vendor/zod-to-json-schema/parsers/catch.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/catch.mjs +5 -0
- package/_vendor/zod-to-json-schema/parsers/catch.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/date.d.ts +16 -0
- package/_vendor/zod-to-json-schema/parsers/date.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/date.js +51 -0
- package/_vendor/zod-to-json-schema/parsers/date.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/date.mjs +47 -0
- package/_vendor/zod-to-json-schema/parsers/date.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/default.d.ts +7 -0
- package/_vendor/zod-to-json-schema/parsers/default.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/default.js +12 -0
- package/_vendor/zod-to-json-schema/parsers/default.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/default.mjs +8 -0
- package/_vendor/zod-to-json-schema/parsers/default.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/effects.d.ts +5 -0
- package/_vendor/zod-to-json-schema/parsers/effects.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/effects.js +9 -0
- package/_vendor/zod-to-json-schema/parsers/effects.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/effects.mjs +5 -0
- package/_vendor/zod-to-json-schema/parsers/effects.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/enum.d.ts +7 -0
- package/_vendor/zod-to-json-schema/parsers/enum.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/enum.js +11 -0
- package/_vendor/zod-to-json-schema/parsers/enum.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/enum.mjs +7 -0
- package/_vendor/zod-to-json-schema/parsers/enum.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/intersection.d.ts +9 -0
- package/_vendor/zod-to-json-schema/parsers/intersection.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/intersection.js +54 -0
- package/_vendor/zod-to-json-schema/parsers/intersection.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/intersection.mjs +50 -0
- package/_vendor/zod-to-json-schema/parsers/intersection.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/literal.d.ts +10 -0
- package/_vendor/zod-to-json-schema/parsers/literal.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/literal.js +26 -0
- package/_vendor/zod-to-json-schema/parsers/literal.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/literal.mjs +22 -0
- package/_vendor/zod-to-json-schema/parsers/literal.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/map.d.ts +16 -0
- package/_vendor/zod-to-json-schema/parsers/map.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/map.js +30 -0
- package/_vendor/zod-to-json-schema/parsers/map.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/map.mjs +26 -0
- package/_vendor/zod-to-json-schema/parsers/map.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/nativeEnum.d.ts +7 -0
- package/_vendor/zod-to-json-schema/parsers/nativeEnum.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/nativeEnum.js +21 -0
- package/_vendor/zod-to-json-schema/parsers/nativeEnum.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/nativeEnum.mjs +17 -0
- package/_vendor/zod-to-json-schema/parsers/nativeEnum.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/never.d.ts +5 -0
- package/_vendor/zod-to-json-schema/parsers/never.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/never.js +10 -0
- package/_vendor/zod-to-json-schema/parsers/never.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/never.mjs +6 -0
- package/_vendor/zod-to-json-schema/parsers/never.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/null.d.ts +6 -0
- package/_vendor/zod-to-json-schema/parsers/null.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/null.js +15 -0
- package/_vendor/zod-to-json-schema/parsers/null.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/null.mjs +11 -0
- package/_vendor/zod-to-json-schema/parsers/null.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/nullable.d.ts +11 -0
- package/_vendor/zod-to-json-schema/parsers/nullable.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/nullable.js +35 -0
- package/_vendor/zod-to-json-schema/parsers/nullable.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/nullable.mjs +31 -0
- package/_vendor/zod-to-json-schema/parsers/nullable.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/number.d.ts +14 -0
- package/_vendor/zod-to-json-schema/parsers/number.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/number.js +57 -0
- package/_vendor/zod-to-json-schema/parsers/number.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/number.mjs +53 -0
- package/_vendor/zod-to-json-schema/parsers/number.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/object.d.ts +11 -0
- package/_vendor/zod-to-json-schema/parsers/object.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/object.js +51 -0
- package/_vendor/zod-to-json-schema/parsers/object.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/object.mjs +47 -0
- package/_vendor/zod-to-json-schema/parsers/object.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/optional.d.ts +5 -0
- package/_vendor/zod-to-json-schema/parsers/optional.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/optional.js +25 -0
- package/_vendor/zod-to-json-schema/parsers/optional.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/optional.mjs +21 -0
- package/_vendor/zod-to-json-schema/parsers/optional.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/pipeline.d.ts +6 -0
- package/_vendor/zod-to-json-schema/parsers/pipeline.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/pipeline.js +25 -0
- package/_vendor/zod-to-json-schema/parsers/pipeline.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/pipeline.mjs +21 -0
- package/_vendor/zod-to-json-schema/parsers/pipeline.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/promise.d.ts +5 -0
- package/_vendor/zod-to-json-schema/parsers/promise.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/promise.js +9 -0
- package/_vendor/zod-to-json-schema/parsers/promise.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/promise.mjs +5 -0
- package/_vendor/zod-to-json-schema/parsers/promise.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/readonly.d.ts +4 -0
- package/_vendor/zod-to-json-schema/parsers/readonly.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/readonly.js +9 -0
- package/_vendor/zod-to-json-schema/parsers/readonly.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/readonly.mjs +5 -0
- package/_vendor/zod-to-json-schema/parsers/readonly.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/record.d.ts +14 -0
- package/_vendor/zod-to-json-schema/parsers/record.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/record.js +50 -0
- package/_vendor/zod-to-json-schema/parsers/record.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/record.mjs +46 -0
- package/_vendor/zod-to-json-schema/parsers/record.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/set.d.ts +14 -0
- package/_vendor/zod-to-json-schema/parsers/set.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/set.js +25 -0
- package/_vendor/zod-to-json-schema/parsers/set.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/set.mjs +21 -0
- package/_vendor/zod-to-json-schema/parsers/set.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/string.d.ts +70 -0
- package/_vendor/zod-to-json-schema/parsers/string.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/string.js +316 -0
- package/_vendor/zod-to-json-schema/parsers/string.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/string.mjs +312 -0
- package/_vendor/zod-to-json-schema/parsers/string.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/tuple.d.ts +14 -0
- package/_vendor/zod-to-json-schema/parsers/tuple.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/tuple.js +37 -0
- package/_vendor/zod-to-json-schema/parsers/tuple.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/tuple.mjs +33 -0
- package/_vendor/zod-to-json-schema/parsers/tuple.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/undefined.d.ts +5 -0
- package/_vendor/zod-to-json-schema/parsers/undefined.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/undefined.js +10 -0
- package/_vendor/zod-to-json-schema/parsers/undefined.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/undefined.mjs +6 -0
- package/_vendor/zod-to-json-schema/parsers/undefined.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/union.d.ts +24 -0
- package/_vendor/zod-to-json-schema/parsers/union.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/union.js +77 -0
- package/_vendor/zod-to-json-schema/parsers/union.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/union.mjs +73 -0
- package/_vendor/zod-to-json-schema/parsers/union.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/unknown.d.ts +3 -0
- package/_vendor/zod-to-json-schema/parsers/unknown.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/unknown.js +8 -0
- package/_vendor/zod-to-json-schema/parsers/unknown.js.map +1 -0
- package/_vendor/zod-to-json-schema/parsers/unknown.mjs +4 -0
- package/_vendor/zod-to-json-schema/parsers/unknown.mjs.map +1 -0
- package/_vendor/zod-to-json-schema/zodToJsonSchema.d.ts +11 -0
- package/_vendor/zod-to-json-schema/zodToJsonSchema.d.ts.map +1 -0
- package/_vendor/zod-to-json-schema/zodToJsonSchema.js +53 -0
- package/_vendor/zod-to-json-schema/zodToJsonSchema.js.map +1 -0
- package/_vendor/zod-to-json-schema/zodToJsonSchema.mjs +50 -0
- package/_vendor/zod-to-json-schema/zodToJsonSchema.mjs.map +1 -0
- package/error.d.ts +6 -0
- package/error.d.ts.map +1 -1
- package/error.js +13 -1
- package/error.js.map +1 -1
- package/error.mjs +10 -0
- package/error.mjs.map +1 -1
- package/helpers/zod.d.ts +57 -0
- package/helpers/zod.d.ts.map +1 -0
- package/helpers/zod.js +79 -0
- package/helpers/zod.js.map +1 -0
- package/helpers/zod.mjs +74 -0
- package/helpers/zod.mjs.map +1 -0
- package/index.d.mts +8 -0
- package/index.d.ts +8 -0
- package/index.d.ts.map +1 -1
- package/index.js +1 -0
- package/index.js.map +1 -1
- package/index.mjs +1 -0
- package/index.mjs.map +1 -1
- package/lib/AbstractChatCompletionRunner.d.ts +15 -68
- package/lib/AbstractChatCompletionRunner.d.ts.map +1 -1
- package/lib/AbstractChatCompletionRunner.js +47 -200
- package/lib/AbstractChatCompletionRunner.js.map +1 -1
- package/lib/AbstractChatCompletionRunner.mjs +48 -201
- package/lib/AbstractChatCompletionRunner.mjs.map +1 -1
- package/lib/AssistantStream.d.ts +10 -6
- package/lib/AssistantStream.d.ts.map +1 -1
- package/lib/AssistantStream.js +18 -6
- package/lib/AssistantStream.js.map +1 -1
- package/lib/AssistantStream.mjs +18 -6
- package/lib/AssistantStream.mjs.map +1 -1
- package/lib/ChatCompletionRunner.d.ts +8 -6
- package/lib/ChatCompletionRunner.d.ts.map +1 -1
- package/lib/ChatCompletionRunner.js +4 -4
- package/lib/ChatCompletionRunner.js.map +1 -1
- package/lib/ChatCompletionRunner.mjs +4 -4
- package/lib/ChatCompletionRunner.mjs.map +1 -1
- package/lib/ChatCompletionStream.d.ts +71 -12
- package/lib/ChatCompletionStream.d.ts.map +1 -1
- package/lib/ChatCompletionStream.js +233 -42
- package/lib/ChatCompletionStream.js.map +1 -1
- package/lib/ChatCompletionStream.mjs +234 -43
- package/lib/ChatCompletionStream.mjs.map +1 -1
- package/lib/ChatCompletionStreamingRunner.d.ts +8 -6
- package/lib/ChatCompletionStreamingRunner.d.ts.map +1 -1
- package/lib/ChatCompletionStreamingRunner.js +9 -7
- package/lib/ChatCompletionStreamingRunner.js.map +1 -1
- package/lib/ChatCompletionStreamingRunner.mjs +9 -7
- package/lib/ChatCompletionStreamingRunner.mjs.map +1 -1
- package/lib/EventStream.d.ts +63 -0
- package/lib/EventStream.d.ts.map +1 -0
- package/lib/EventStream.js +200 -0
- package/lib/EventStream.js.map +1 -0
- package/lib/EventStream.mjs +196 -0
- package/lib/EventStream.mjs.map +1 -0
- package/lib/RunnableFunction.d.ts +4 -2
- package/lib/RunnableFunction.d.ts.map +1 -1
- package/lib/RunnableFunction.js.map +1 -1
- package/lib/RunnableFunction.mjs.map +1 -1
- package/lib/parser.d.ts +37 -0
- package/lib/parser.d.ts.map +1 -0
- package/lib/parser.js +133 -0
- package/lib/parser.js.map +1 -0
- package/lib/parser.mjs +121 -0
- package/lib/parser.mjs.map +1 -0
- package/package.json +10 -2
- package/resources/audio/audio.d.ts +4 -0
- package/resources/audio/audio.d.ts.map +1 -1
- package/resources/audio/audio.js.map +1 -1
- package/resources/audio/audio.mjs.map +1 -1
- package/resources/audio/index.d.ts +2 -2
- package/resources/audio/index.d.ts.map +1 -1
- package/resources/audio/index.js.map +1 -1
- package/resources/audio/index.mjs.map +1 -1
- package/resources/audio/speech.d.ts +3 -1
- package/resources/audio/speech.d.ts.map +1 -1
- package/resources/audio/speech.js.map +1 -1
- package/resources/audio/speech.mjs.map +1 -1
- package/resources/audio/transcriptions.d.ts +2 -1
- package/resources/audio/transcriptions.d.ts.map +1 -1
- package/resources/audio/transcriptions.js.map +1 -1
- package/resources/audio/transcriptions.mjs.map +1 -1
- package/resources/audio/translations.d.ts +2 -1
- package/resources/audio/translations.d.ts.map +1 -1
- package/resources/audio/translations.js.map +1 -1
- package/resources/audio/translations.mjs.map +1 -1
- package/resources/beta/assistants.d.ts +19 -3
- package/resources/beta/assistants.d.ts.map +1 -1
- package/resources/beta/assistants.js.map +1 -1
- package/resources/beta/assistants.mjs.map +1 -1
- package/resources/beta/beta.d.ts +0 -1
- package/resources/beta/beta.d.ts.map +1 -1
- package/resources/beta/beta.js.map +1 -1
- package/resources/beta/beta.mjs.map +1 -1
- package/resources/beta/chat/completions.d.ts +25 -5
- package/resources/beta/chat/completions.d.ts.map +1 -1
- package/resources/beta/chat/completions.js +17 -5
- package/resources/beta/chat/completions.js.map +1 -1
- package/resources/beta/chat/completions.mjs +17 -5
- package/resources/beta/chat/completions.mjs.map +1 -1
- package/resources/beta/index.d.ts +1 -1
- package/resources/beta/index.d.ts.map +1 -1
- package/resources/beta/index.js.map +1 -1
- package/resources/beta/index.mjs.map +1 -1
- package/resources/beta/threads/index.d.ts +2 -2
- package/resources/beta/threads/index.d.ts.map +1 -1
- package/resources/beta/threads/index.js.map +1 -1
- package/resources/beta/threads/index.mjs.map +1 -1
- package/resources/beta/threads/messages.d.ts +28 -2
- package/resources/beta/threads/messages.d.ts.map +1 -1
- package/resources/beta/threads/messages.js.map +1 -1
- package/resources/beta/threads/messages.mjs.map +1 -1
- package/resources/beta/threads/runs/runs.d.ts +12 -1
- package/resources/beta/threads/runs/runs.d.ts.map +1 -1
- package/resources/beta/threads/runs/runs.js.map +1 -1
- package/resources/beta/threads/runs/runs.mjs.map +1 -1
- package/resources/beta/threads/threads.d.ts +22 -8
- package/resources/beta/threads/threads.d.ts.map +1 -1
- package/resources/beta/threads/threads.js.map +1 -1
- package/resources/beta/threads/threads.mjs.map +1 -1
- package/resources/beta/vector-stores/files.d.ts +1 -1
- package/resources/beta/vector-stores/files.d.ts.map +1 -1
- package/resources/chat/chat.d.ts +2 -1
- package/resources/chat/chat.d.ts.map +1 -1
- package/resources/chat/chat.js.map +1 -1
- package/resources/chat/chat.mjs.map +1 -1
- package/resources/chat/completions.d.ts +37 -26
- package/resources/chat/completions.d.ts.map +1 -1
- package/resources/chat/completions.js.map +1 -1
- package/resources/chat/completions.mjs.map +1 -1
- package/resources/chat/index.d.ts +1 -1
- package/resources/chat/index.d.ts.map +1 -1
- package/resources/chat/index.js.map +1 -1
- package/resources/chat/index.mjs.map +1 -1
- package/resources/fine-tuning/jobs/jobs.d.ts +3 -3
- package/resources/fine-tuning/jobs/jobs.d.ts.map +1 -1
- package/resources/images.d.ts +5 -3
- package/resources/images.d.ts.map +1 -1
- package/resources/images.js.map +1 -1
- package/resources/images.mjs.map +1 -1
- package/resources/index.d.ts +3 -3
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js.map +1 -1
- package/resources/index.mjs +1 -1
- package/resources/index.mjs.map +1 -1
- package/resources/moderations.d.ts +3 -1
- package/resources/moderations.d.ts.map +1 -1
- package/resources/moderations.js.map +1 -1
- package/resources/moderations.mjs.map +1 -1
- package/resources/shared.d.ts +53 -0
- package/resources/shared.d.ts.map +1 -1
- package/src/_vendor/partial-json-parser/README.md +3 -0
- package/src/_vendor/partial-json-parser/parser.ts +264 -0
- package/src/_vendor/zod-to-json-schema/Options.ts +73 -0
- package/src/_vendor/zod-to-json-schema/Refs.ts +39 -0
- package/src/_vendor/zod-to-json-schema/errorMessages.ts +31 -0
- package/src/_vendor/zod-to-json-schema/index.ts +37 -0
- package/src/_vendor/zod-to-json-schema/parseDef.ts +231 -0
- package/src/_vendor/zod-to-json-schema/parsers/any.ts +5 -0
- package/src/_vendor/zod-to-json-schema/parsers/array.ts +36 -0
- package/src/_vendor/zod-to-json-schema/parsers/bigint.ts +60 -0
- package/src/_vendor/zod-to-json-schema/parsers/boolean.ts +9 -0
- package/src/_vendor/zod-to-json-schema/parsers/branded.ts +7 -0
- package/src/_vendor/zod-to-json-schema/parsers/catch.ts +7 -0
- package/src/_vendor/zod-to-json-schema/parsers/date.ts +83 -0
- package/src/_vendor/zod-to-json-schema/parsers/default.ts +10 -0
- package/src/_vendor/zod-to-json-schema/parsers/effects.ts +7 -0
- package/src/_vendor/zod-to-json-schema/parsers/enum.ts +13 -0
- package/src/_vendor/zod-to-json-schema/parsers/intersection.ts +64 -0
- package/src/_vendor/zod-to-json-schema/parsers/literal.ts +37 -0
- package/src/_vendor/zod-to-json-schema/parsers/map.ts +42 -0
- package/src/_vendor/zod-to-json-schema/parsers/nativeEnum.ts +27 -0
- package/src/_vendor/zod-to-json-schema/parsers/never.ts +9 -0
- package/src/_vendor/zod-to-json-schema/parsers/null.ts +16 -0
- package/src/_vendor/zod-to-json-schema/parsers/nullable.ts +49 -0
- package/src/_vendor/zod-to-json-schema/parsers/number.ts +62 -0
- package/src/_vendor/zod-to-json-schema/parsers/object.ts +63 -0
- package/src/_vendor/zod-to-json-schema/parsers/optional.ts +25 -0
- package/src/_vendor/zod-to-json-schema/parsers/pipeline.ts +28 -0
- package/src/_vendor/zod-to-json-schema/parsers/promise.ts +7 -0
- package/src/_vendor/zod-to-json-schema/parsers/readonly.ts +7 -0
- package/src/_vendor/zod-to-json-schema/parsers/record.ts +73 -0
- package/src/_vendor/zod-to-json-schema/parsers/set.ts +36 -0
- package/src/_vendor/zod-to-json-schema/parsers/string.ts +400 -0
- package/src/_vendor/zod-to-json-schema/parsers/tuple.ts +54 -0
- package/src/_vendor/zod-to-json-schema/parsers/undefined.ts +9 -0
- package/src/_vendor/zod-to-json-schema/parsers/union.ts +119 -0
- package/src/_vendor/zod-to-json-schema/parsers/unknown.ts +5 -0
- package/src/_vendor/zod-to-json-schema/zodToJsonSchema.ts +91 -0
- package/src/error.ts +12 -0
- package/src/helpers/zod.ts +102 -0
- package/src/index.ts +8 -0
- package/src/lib/AbstractChatCompletionRunner.ts +72 -258
- package/src/lib/AssistantStream.ts +47 -19
- package/src/lib/ChatCompletionRunner.ts +16 -12
- package/src/lib/ChatCompletionStream.ts +423 -47
- package/src/lib/ChatCompletionStreamingRunner.ts +19 -15
- package/src/lib/{AbstractAssistantStreamRunner.ts → EventStream.ts} +37 -138
- package/src/lib/RunnableFunction.ts +4 -2
- package/src/lib/parser.ts +235 -0
- package/src/resources/audio/audio.ts +5 -0
- package/src/resources/audio/index.ts +2 -2
- package/src/resources/audio/speech.ts +4 -1
- package/src/resources/audio/transcriptions.ts +2 -1
- package/src/resources/audio/translations.ts +2 -1
- package/src/resources/beta/assistants.ts +19 -26
- package/src/resources/beta/beta.ts +0 -1
- package/src/resources/beta/chat/completions.ts +77 -27
- package/src/resources/beta/index.ts +0 -1
- package/src/resources/beta/threads/index.ts +2 -1
- package/src/resources/beta/threads/messages.ts +41 -2
- package/src/resources/beta/threads/runs/runs.ts +12 -25
- package/src/resources/beta/threads/threads.ts +26 -32
- package/src/resources/beta/vector-stores/files.ts +1 -1
- package/src/resources/chat/chat.ts +2 -0
- package/src/resources/chat/completions.ts +47 -27
- package/src/resources/chat/index.ts +1 -0
- package/src/resources/fine-tuning/jobs/jobs.ts +3 -3
- package/src/resources/images.ts +6 -3
- package/src/resources/index.ts +9 -2
- package/src/resources/moderations.ts +4 -1
- package/src/resources/shared.ts +62 -0
- package/src/version.ts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
- package/lib/AbstractAssistantStreamRunner.d.ts +0 -74
- package/lib/AbstractAssistantStreamRunner.d.ts.map +0 -1
- package/lib/AbstractAssistantStreamRunner.js +0 -246
- package/lib/AbstractAssistantStreamRunner.js.map +0 -1
- package/lib/AbstractAssistantStreamRunner.mjs +0 -242
- package/lib/AbstractAssistantStreamRunner.mjs.map +0 -1
- package/lib/ChatCompletionRunFunctions.test.d.ts +0 -2
- package/lib/ChatCompletionRunFunctions.test.d.ts.map +0 -1
- package/lib/ChatCompletionRunFunctions.test.js +0 -2177
- package/lib/ChatCompletionRunFunctions.test.js.map +0 -1
- package/lib/ChatCompletionRunFunctions.test.mjs +0 -2172
- package/lib/ChatCompletionRunFunctions.test.mjs.map +0 -1
- package/src/lib/ChatCompletionRunFunctions.test.ts +0 -2328
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ResponseFormatJSONSchema } from "../resources";
|
|
2
|
+
import type z from 'zod';
|
|
3
|
+
import {
|
|
4
|
+
AutoParseableResponseFormat,
|
|
5
|
+
AutoParseableTool,
|
|
6
|
+
makeParseableResponseFormat,
|
|
7
|
+
makeParseableTool,
|
|
8
|
+
} from '../lib/parser';
|
|
9
|
+
import { zodToJsonSchema as _zodToJsonSchema } from '../_vendor/zod-to-json-schema';
|
|
10
|
+
|
|
11
|
+
function zodToJsonSchema(schema: z.ZodType): Record<string, unknown> {
|
|
12
|
+
return _zodToJsonSchema(schema, { openaiStrictMode: true });
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates a chat completion `JSONSchema` response format object from
|
|
17
|
+
* the given Zod schema.
|
|
18
|
+
*
|
|
19
|
+
* If this is passed to the `.parse()`, `.stream()` or `.runTools()`
|
|
20
|
+
* chat completion methods then the response message will contain a
|
|
21
|
+
* `.parsed` property that is the result of parsing the content with
|
|
22
|
+
* the given Zod object.
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* const completion = await client.beta.chat.completions.parse({
|
|
26
|
+
* model: 'gpt-4o-2024-08-06',
|
|
27
|
+
* messages: [
|
|
28
|
+
* { role: 'system', content: 'You are a helpful math tutor.' },
|
|
29
|
+
* { role: 'user', content: 'solve 8x + 31 = 2' },
|
|
30
|
+
* ],
|
|
31
|
+
* response_format: zodResponseFormat(
|
|
32
|
+
* z.object({
|
|
33
|
+
* steps: z.array(z.object({
|
|
34
|
+
* explanation: z.string(),
|
|
35
|
+
* answer: z.string(),
|
|
36
|
+
* })),
|
|
37
|
+
* final_answer: z.string(),
|
|
38
|
+
* }),
|
|
39
|
+
* 'math_answer',
|
|
40
|
+
* ),
|
|
41
|
+
* });
|
|
42
|
+
* const message = completion.choices[0]?.message;
|
|
43
|
+
* if (message?.parsed) {
|
|
44
|
+
* console.log(message.parsed);
|
|
45
|
+
* console.log(message.parsed.final_answer);
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* This can be passed directly to the `.create()` method but will not
|
|
50
|
+
* result in any automatic parsing, you'll have to parse the response yourself.
|
|
51
|
+
*/
|
|
52
|
+
export function zodResponseFormat<ZodInput extends z.ZodType>(
|
|
53
|
+
zodObject: ZodInput,
|
|
54
|
+
name: string,
|
|
55
|
+
props?: Omit<ResponseFormatJSONSchema, 'schema' | 'strict' | 'name'>,
|
|
56
|
+
): AutoParseableResponseFormat<z.infer<ZodInput>> {
|
|
57
|
+
return makeParseableResponseFormat(
|
|
58
|
+
{
|
|
59
|
+
type: 'json_schema',
|
|
60
|
+
json_schema: {
|
|
61
|
+
...props,
|
|
62
|
+
name,
|
|
63
|
+
strict: true,
|
|
64
|
+
schema: zodToJsonSchema(zodObject),
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
(content) => zodObject.parse(JSON.parse(content)),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Creates a chat completion `function` tool that can be invoked
|
|
73
|
+
* automatically by the chat completion `.runTools()` method or automatically
|
|
74
|
+
* parsed by `.parse()` / `.stream()`.
|
|
75
|
+
*/
|
|
76
|
+
export function zodFunction<Parameters extends z.ZodType>(options: {
|
|
77
|
+
name: string;
|
|
78
|
+
parameters: Parameters;
|
|
79
|
+
function?: ((args: z.infer<Parameters>) => unknown | Promise<unknown>) | undefined;
|
|
80
|
+
description?: string | undefined;
|
|
81
|
+
}): AutoParseableTool<{
|
|
82
|
+
arguments: Parameters;
|
|
83
|
+
name: string;
|
|
84
|
+
function: (args: z.infer<Parameters>) => unknown;
|
|
85
|
+
}> {
|
|
86
|
+
// @ts-expect-error TODO
|
|
87
|
+
return makeParseableTool<any>(
|
|
88
|
+
{
|
|
89
|
+
type: 'function',
|
|
90
|
+
function: {
|
|
91
|
+
name: options.name,
|
|
92
|
+
parameters: zodToJsonSchema(options.parameters),
|
|
93
|
+
strict: true,
|
|
94
|
+
...(options.description ? { description: options.description } : undefined),
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
callback: options.function,
|
|
99
|
+
parser: (args) => options.parameters.parse(JSON.parse(args)),
|
|
100
|
+
},
|
|
101
|
+
);
|
|
102
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -184,6 +184,7 @@ export class OpenAI extends Core.APIClient {
|
|
|
184
184
|
}
|
|
185
185
|
|
|
186
186
|
static OpenAI = this;
|
|
187
|
+
static DEFAULT_TIMEOUT = 600000; // 10 minutes
|
|
187
188
|
|
|
188
189
|
static OpenAIError = Errors.OpenAIError;
|
|
189
190
|
static APIError = Errors.APIError;
|
|
@@ -247,6 +248,7 @@ export namespace OpenAI {
|
|
|
247
248
|
export import ChatCompletionChunk = API.ChatCompletionChunk;
|
|
248
249
|
export import ChatCompletionContentPart = API.ChatCompletionContentPart;
|
|
249
250
|
export import ChatCompletionContentPartImage = API.ChatCompletionContentPartImage;
|
|
251
|
+
export import ChatCompletionContentPartRefusal = API.ChatCompletionContentPartRefusal;
|
|
250
252
|
export import ChatCompletionContentPartText = API.ChatCompletionContentPartText;
|
|
251
253
|
export import ChatCompletionFunctionCallOption = API.ChatCompletionFunctionCallOption;
|
|
252
254
|
export import ChatCompletionFunctionMessageParam = API.ChatCompletionFunctionMessageParam;
|
|
@@ -281,15 +283,18 @@ export namespace OpenAI {
|
|
|
281
283
|
|
|
282
284
|
export import Images = API.Images;
|
|
283
285
|
export import Image = API.Image;
|
|
286
|
+
export import ImageModel = API.ImageModel;
|
|
284
287
|
export import ImagesResponse = API.ImagesResponse;
|
|
285
288
|
export import ImageCreateVariationParams = API.ImageCreateVariationParams;
|
|
286
289
|
export import ImageEditParams = API.ImageEditParams;
|
|
287
290
|
export import ImageGenerateParams = API.ImageGenerateParams;
|
|
288
291
|
|
|
289
292
|
export import Audio = API.Audio;
|
|
293
|
+
export import AudioModel = API.AudioModel;
|
|
290
294
|
|
|
291
295
|
export import Moderations = API.Moderations;
|
|
292
296
|
export import Moderation = API.Moderation;
|
|
297
|
+
export import ModerationModel = API.ModerationModel;
|
|
293
298
|
export import ModerationCreateResponse = API.ModerationCreateResponse;
|
|
294
299
|
export import ModerationCreateParams = API.ModerationCreateParams;
|
|
295
300
|
|
|
@@ -318,6 +323,9 @@ export namespace OpenAI {
|
|
|
318
323
|
export import ErrorObject = API.ErrorObject;
|
|
319
324
|
export import FunctionDefinition = API.FunctionDefinition;
|
|
320
325
|
export import FunctionParameters = API.FunctionParameters;
|
|
326
|
+
export import ResponseFormatJSONObject = API.ResponseFormatJSONObject;
|
|
327
|
+
export import ResponseFormatJSONSchema = API.ResponseFormatJSONSchema;
|
|
328
|
+
export import ResponseFormatText = API.ResponseFormatText;
|
|
321
329
|
}
|
|
322
330
|
|
|
323
331
|
// ---------------------- Azure ----------------------
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import * as Core from "../core";
|
|
2
2
|
import { type CompletionUsage } from "../resources/completions";
|
|
3
3
|
import {
|
|
4
|
-
type Completions,
|
|
5
4
|
type ChatCompletion,
|
|
6
5
|
type ChatCompletionMessage,
|
|
7
6
|
type ChatCompletionMessageParam,
|
|
8
7
|
type ChatCompletionCreateParams,
|
|
9
8
|
type ChatCompletionTool,
|
|
10
9
|
} from "../resources/chat/completions";
|
|
11
|
-
import {
|
|
10
|
+
import { OpenAIError } from "../error";
|
|
12
11
|
import {
|
|
13
12
|
type RunnableFunction,
|
|
14
13
|
isRunnableFunctionWithParse,
|
|
15
14
|
type BaseFunctionsArgs,
|
|
15
|
+
RunnableToolFunction,
|
|
16
16
|
} from './RunnableFunction';
|
|
17
17
|
import { ChatCompletionFunctionRunnerParams, ChatCompletionToolRunnerParams } from './ChatCompletionRunner';
|
|
18
18
|
import {
|
|
@@ -20,6 +20,10 @@ import {
|
|
|
20
20
|
ChatCompletionStreamingToolRunnerParams,
|
|
21
21
|
} from './ChatCompletionStreamingRunner';
|
|
22
22
|
import { isAssistantMessage, isFunctionMessage, isToolMessage } from './chatCompletionUtils';
|
|
23
|
+
import { BaseEvents, EventStream } from './EventStream';
|
|
24
|
+
import { ParsedChatCompletion } from '../resources/beta/chat/completions';
|
|
25
|
+
import OpenAI from '../index';
|
|
26
|
+
import { isAutoParsableTool, parseChatCompletion } from "./parser";
|
|
23
27
|
|
|
24
28
|
const DEFAULT_MAX_CHAT_COMPLETIONS = 10;
|
|
25
29
|
export interface RunnerOptions extends Core.RequestOptions {
|
|
@@ -27,60 +31,17 @@ export interface RunnerOptions extends Core.RequestOptions {
|
|
|
27
31
|
maxChatCompletions?: number;
|
|
28
32
|
}
|
|
29
33
|
|
|
30
|
-
export
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
#connectedPromise: Promise<void>;
|
|
36
|
-
#resolveConnectedPromise: () => void = () => {};
|
|
37
|
-
#rejectConnectedPromise: (error: OpenAIError) => void = () => {};
|
|
38
|
-
|
|
39
|
-
#endPromise: Promise<void>;
|
|
40
|
-
#resolveEndPromise: () => void = () => {};
|
|
41
|
-
#rejectEndPromise: (error: OpenAIError) => void = () => {};
|
|
42
|
-
|
|
43
|
-
#listeners: { [Event in keyof Events]?: ListenersForEvent<Events, Event> } = {};
|
|
44
|
-
|
|
45
|
-
protected _chatCompletions: ChatCompletion[] = [];
|
|
34
|
+
export class AbstractChatCompletionRunner<
|
|
35
|
+
EventTypes extends AbstractChatCompletionRunnerEvents,
|
|
36
|
+
ParsedT,
|
|
37
|
+
> extends EventStream<EventTypes> {
|
|
38
|
+
protected _chatCompletions: ParsedChatCompletion<ParsedT>[] = [];
|
|
46
39
|
messages: ChatCompletionMessageParam[] = [];
|
|
47
40
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
constructor() {
|
|
54
|
-
this.#connectedPromise = new Promise<void>((resolve, reject) => {
|
|
55
|
-
this.#resolveConnectedPromise = resolve;
|
|
56
|
-
this.#rejectConnectedPromise = reject;
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
this.#endPromise = new Promise<void>((resolve, reject) => {
|
|
60
|
-
this.#resolveEndPromise = resolve;
|
|
61
|
-
this.#rejectEndPromise = reject;
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
// Don't let these promises cause unhandled rejection errors.
|
|
65
|
-
// we will manually cause an unhandled rejection error later
|
|
66
|
-
// if the user hasn't registered any error listener or called
|
|
67
|
-
// any promise-returning method.
|
|
68
|
-
this.#connectedPromise.catch(() => {});
|
|
69
|
-
this.#endPromise.catch(() => {});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
protected _run(executor: () => Promise<any>) {
|
|
73
|
-
// Unfortunately if we call `executor()` immediately we get runtime errors about
|
|
74
|
-
// references to `this` before the `super()` constructor call returns.
|
|
75
|
-
setTimeout(() => {
|
|
76
|
-
executor().then(() => {
|
|
77
|
-
this._emitFinal();
|
|
78
|
-
this._emit('end');
|
|
79
|
-
}, this.#handleError);
|
|
80
|
-
}, 0);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
protected _addChatCompletion(chatCompletion: ChatCompletion): ChatCompletion {
|
|
41
|
+
protected _addChatCompletion(
|
|
42
|
+
this: AbstractChatCompletionRunner<AbstractChatCompletionRunnerEvents, ParsedT>,
|
|
43
|
+
chatCompletion: ParsedChatCompletion<ParsedT>,
|
|
44
|
+
): ParsedChatCompletion<ParsedT> {
|
|
84
45
|
this._chatCompletions.push(chatCompletion);
|
|
85
46
|
this._emit('chatCompletion', chatCompletion);
|
|
86
47
|
const message = chatCompletion.choices[0]?.message;
|
|
@@ -88,7 +49,11 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
88
49
|
return chatCompletion;
|
|
89
50
|
}
|
|
90
51
|
|
|
91
|
-
protected _addMessage(
|
|
52
|
+
protected _addMessage(
|
|
53
|
+
this: AbstractChatCompletionRunner<AbstractChatCompletionRunnerEvents, ParsedT>,
|
|
54
|
+
message: ChatCompletionMessageParam,
|
|
55
|
+
emit = true,
|
|
56
|
+
) {
|
|
92
57
|
if (!('content' in message)) message.content = null;
|
|
93
58
|
|
|
94
59
|
this.messages.push(message);
|
|
@@ -110,104 +75,11 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
110
75
|
}
|
|
111
76
|
}
|
|
112
77
|
|
|
113
|
-
protected _connected() {
|
|
114
|
-
if (this.ended) return;
|
|
115
|
-
this.#resolveConnectedPromise();
|
|
116
|
-
this._emit('connect');
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
get ended(): boolean {
|
|
120
|
-
return this.#ended;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
get errored(): boolean {
|
|
124
|
-
return this.#errored;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
get aborted(): boolean {
|
|
128
|
-
return this.#aborted;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
abort() {
|
|
132
|
-
this.controller.abort();
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Adds the listener function to the end of the listeners array for the event.
|
|
137
|
-
* No checks are made to see if the listener has already been added. Multiple calls passing
|
|
138
|
-
* the same combination of event and listener will result in the listener being added, and
|
|
139
|
-
* called, multiple times.
|
|
140
|
-
* @returns this ChatCompletionStream, so that calls can be chained
|
|
141
|
-
*/
|
|
142
|
-
on<Event extends keyof Events>(event: Event, listener: ListenerForEvent<Events, Event>): this {
|
|
143
|
-
const listeners: ListenersForEvent<Events, Event> =
|
|
144
|
-
this.#listeners[event] || (this.#listeners[event] = []);
|
|
145
|
-
listeners.push({ listener });
|
|
146
|
-
return this;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Removes the specified listener from the listener array for the event.
|
|
151
|
-
* off() will remove, at most, one instance of a listener from the listener array. If any single
|
|
152
|
-
* listener has been added multiple times to the listener array for the specified event, then
|
|
153
|
-
* off() must be called multiple times to remove each instance.
|
|
154
|
-
* @returns this ChatCompletionStream, so that calls can be chained
|
|
155
|
-
*/
|
|
156
|
-
off<Event extends keyof Events>(event: Event, listener: ListenerForEvent<Events, Event>): this {
|
|
157
|
-
const listeners = this.#listeners[event];
|
|
158
|
-
if (!listeners) return this;
|
|
159
|
-
const index = listeners.findIndex((l) => l.listener === listener);
|
|
160
|
-
if (index >= 0) listeners.splice(index, 1);
|
|
161
|
-
return this;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Adds a one-time listener function for the event. The next time the event is triggered,
|
|
166
|
-
* this listener is removed and then invoked.
|
|
167
|
-
* @returns this ChatCompletionStream, so that calls can be chained
|
|
168
|
-
*/
|
|
169
|
-
once<Event extends keyof Events>(event: Event, listener: ListenerForEvent<Events, Event>): this {
|
|
170
|
-
const listeners: ListenersForEvent<Events, Event> =
|
|
171
|
-
this.#listeners[event] || (this.#listeners[event] = []);
|
|
172
|
-
listeners.push({ listener, once: true });
|
|
173
|
-
return this;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* This is similar to `.once()`, but returns a Promise that resolves the next time
|
|
178
|
-
* the event is triggered, instead of calling a listener callback.
|
|
179
|
-
* @returns a Promise that resolves the next time given event is triggered,
|
|
180
|
-
* or rejects if an error is emitted. (If you request the 'error' event,
|
|
181
|
-
* returns a promise that resolves with the error).
|
|
182
|
-
*
|
|
183
|
-
* Example:
|
|
184
|
-
*
|
|
185
|
-
* const message = await stream.emitted('message') // rejects if the stream errors
|
|
186
|
-
*/
|
|
187
|
-
emitted<Event extends keyof Events>(
|
|
188
|
-
event: Event,
|
|
189
|
-
): Promise<
|
|
190
|
-
EventParameters<Events, Event> extends [infer Param] ? Param
|
|
191
|
-
: EventParameters<Events, Event> extends [] ? void
|
|
192
|
-
: EventParameters<Events, Event>
|
|
193
|
-
> {
|
|
194
|
-
return new Promise((resolve, reject) => {
|
|
195
|
-
this.#catchingPromiseCreated = true;
|
|
196
|
-
if (event !== 'error') this.once('error', reject);
|
|
197
|
-
this.once(event, resolve as any);
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
async done(): Promise<void> {
|
|
202
|
-
this.#catchingPromiseCreated = true;
|
|
203
|
-
await this.#endPromise;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
78
|
/**
|
|
207
79
|
* @returns a promise that resolves with the final ChatCompletion, or rejects
|
|
208
80
|
* if an error occurred or the stream ended prematurely without producing a ChatCompletion.
|
|
209
81
|
*/
|
|
210
|
-
async finalChatCompletion(): Promise<
|
|
82
|
+
async finalChatCompletion(): Promise<ParsedChatCompletion<ParsedT>> {
|
|
211
83
|
await this.done();
|
|
212
84
|
const completion = this._chatCompletions[this._chatCompletions.length - 1];
|
|
213
85
|
if (!completion) throw new OpenAIError('stream ended without producing a ChatCompletion');
|
|
@@ -233,7 +105,11 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
233
105
|
const message = this.messages[i];
|
|
234
106
|
if (isAssistantMessage(message)) {
|
|
235
107
|
const { function_call, ...rest } = message;
|
|
236
|
-
const ret: ChatCompletionMessage = {
|
|
108
|
+
const ret: ChatCompletionMessage = {
|
|
109
|
+
...rest,
|
|
110
|
+
content: (message as ChatCompletionMessage).content ?? null,
|
|
111
|
+
refusal: (message as ChatCompletionMessage).refusal ?? null,
|
|
112
|
+
};
|
|
237
113
|
if (function_call) {
|
|
238
114
|
ret.function_call = function_call;
|
|
239
115
|
}
|
|
@@ -284,6 +160,7 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
284
160
|
if (
|
|
285
161
|
isToolMessage(message) &&
|
|
286
162
|
message.content != null &&
|
|
163
|
+
typeof message.content === 'string' &&
|
|
287
164
|
this.messages.some(
|
|
288
165
|
(x) =>
|
|
289
166
|
x.role === 'assistant' &&
|
|
@@ -327,75 +204,9 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
327
204
|
return [...this._chatCompletions];
|
|
328
205
|
}
|
|
329
206
|
|
|
330
|
-
|
|
331
|
-
this
|
|
332
|
-
|
|
333
|
-
error = new APIUserAbortError();
|
|
334
|
-
}
|
|
335
|
-
if (error instanceof APIUserAbortError) {
|
|
336
|
-
this.#aborted = true;
|
|
337
|
-
return this._emit('abort', error);
|
|
338
|
-
}
|
|
339
|
-
if (error instanceof OpenAIError) {
|
|
340
|
-
return this._emit('error', error);
|
|
341
|
-
}
|
|
342
|
-
if (error instanceof Error) {
|
|
343
|
-
const openAIError: OpenAIError = new OpenAIError(error.message);
|
|
344
|
-
// @ts-ignore
|
|
345
|
-
openAIError.cause = error;
|
|
346
|
-
return this._emit('error', openAIError);
|
|
347
|
-
}
|
|
348
|
-
return this._emit('error', new OpenAIError(String(error)));
|
|
349
|
-
};
|
|
350
|
-
|
|
351
|
-
protected _emit<Event extends keyof Events>(event: Event, ...args: EventParameters<Events, Event>) {
|
|
352
|
-
// make sure we don't emit any events after end
|
|
353
|
-
if (this.#ended) {
|
|
354
|
-
return;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
if (event === 'end') {
|
|
358
|
-
this.#ended = true;
|
|
359
|
-
this.#resolveEndPromise();
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
const listeners: ListenersForEvent<Events, Event> | undefined = this.#listeners[event];
|
|
363
|
-
if (listeners) {
|
|
364
|
-
this.#listeners[event] = listeners.filter((l) => !l.once) as any;
|
|
365
|
-
listeners.forEach(({ listener }: any) => listener(...args));
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
if (event === 'abort') {
|
|
369
|
-
const error = args[0] as APIUserAbortError;
|
|
370
|
-
if (!this.#catchingPromiseCreated && !listeners?.length) {
|
|
371
|
-
Promise.reject(error);
|
|
372
|
-
}
|
|
373
|
-
this.#rejectConnectedPromise(error);
|
|
374
|
-
this.#rejectEndPromise(error);
|
|
375
|
-
this._emit('end');
|
|
376
|
-
return;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
if (event === 'error') {
|
|
380
|
-
// NOTE: _emit('error', error) should only be called from #handleError().
|
|
381
|
-
|
|
382
|
-
const error = args[0] as OpenAIError;
|
|
383
|
-
if (!this.#catchingPromiseCreated && !listeners?.length) {
|
|
384
|
-
// Trigger an unhandled rejection if the user hasn't registered any error handlers.
|
|
385
|
-
// If you are seeing stack traces here, make sure to handle errors via either:
|
|
386
|
-
// - runner.on('error', () => ...)
|
|
387
|
-
// - await runner.done()
|
|
388
|
-
// - await runner.finalChatCompletion()
|
|
389
|
-
// - etc.
|
|
390
|
-
Promise.reject(error);
|
|
391
|
-
}
|
|
392
|
-
this.#rejectConnectedPromise(error);
|
|
393
|
-
this.#rejectEndPromise(error);
|
|
394
|
-
this._emit('end');
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
protected _emitFinal() {
|
|
207
|
+
protected override _emitFinal(
|
|
208
|
+
this: AbstractChatCompletionRunner<AbstractChatCompletionRunnerEvents, ParsedT>,
|
|
209
|
+
) {
|
|
399
210
|
const completion = this._chatCompletions[this._chatCompletions.length - 1];
|
|
400
211
|
if (completion) this._emit('finalChatCompletion', completion);
|
|
401
212
|
const finalMessage = this.#getFinalMessage();
|
|
@@ -423,10 +234,10 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
423
234
|
}
|
|
424
235
|
|
|
425
236
|
protected async _createChatCompletion(
|
|
426
|
-
|
|
237
|
+
client: OpenAI,
|
|
427
238
|
params: ChatCompletionCreateParams,
|
|
428
239
|
options?: Core.RequestOptions,
|
|
429
|
-
): Promise<
|
|
240
|
+
): Promise<ParsedChatCompletion<ParsedT>> {
|
|
430
241
|
const signal = options?.signal;
|
|
431
242
|
if (signal) {
|
|
432
243
|
if (signal.aborted) this.controller.abort();
|
|
@@ -434,27 +245,27 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
434
245
|
}
|
|
435
246
|
this.#validateParams(params);
|
|
436
247
|
|
|
437
|
-
const chatCompletion = await completions.create(
|
|
248
|
+
const chatCompletion = await client.chat.completions.create(
|
|
438
249
|
{ ...params, stream: false },
|
|
439
250
|
{ ...options, signal: this.controller.signal },
|
|
440
251
|
);
|
|
441
252
|
this._connected();
|
|
442
|
-
return this._addChatCompletion(chatCompletion);
|
|
253
|
+
return this._addChatCompletion(parseChatCompletion(chatCompletion, params));
|
|
443
254
|
}
|
|
444
255
|
|
|
445
256
|
protected async _runChatCompletion(
|
|
446
|
-
|
|
257
|
+
client: OpenAI,
|
|
447
258
|
params: ChatCompletionCreateParams,
|
|
448
259
|
options?: Core.RequestOptions,
|
|
449
260
|
): Promise<ChatCompletion> {
|
|
450
261
|
for (const message of params.messages) {
|
|
451
262
|
this._addMessage(message, false);
|
|
452
263
|
}
|
|
453
|
-
return await this._createChatCompletion(
|
|
264
|
+
return await this._createChatCompletion(client, params, options);
|
|
454
265
|
}
|
|
455
266
|
|
|
456
267
|
protected async _runFunctions<FunctionsArgs extends BaseFunctionsArgs>(
|
|
457
|
-
|
|
268
|
+
client: OpenAI,
|
|
458
269
|
params:
|
|
459
270
|
| ChatCompletionFunctionRunnerParams<FunctionsArgs>
|
|
460
271
|
| ChatCompletionStreamingFunctionRunnerParams<FunctionsArgs>,
|
|
@@ -484,7 +295,7 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
484
295
|
|
|
485
296
|
for (let i = 0; i < maxChatCompletions; ++i) {
|
|
486
297
|
const chatCompletion: ChatCompletion = await this._createChatCompletion(
|
|
487
|
-
|
|
298
|
+
client,
|
|
488
299
|
{
|
|
489
300
|
...restParams,
|
|
490
301
|
function_call,
|
|
@@ -539,7 +350,7 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
539
350
|
}
|
|
540
351
|
|
|
541
352
|
protected async _runTools<FunctionsArgs extends BaseFunctionsArgs>(
|
|
542
|
-
|
|
353
|
+
client: OpenAI,
|
|
543
354
|
params:
|
|
544
355
|
| ChatCompletionToolRunnerParams<FunctionsArgs>
|
|
545
356
|
| ChatCompletionStreamingToolRunnerParams<FunctionsArgs>,
|
|
@@ -550,8 +361,31 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
550
361
|
const singleFunctionToCall = typeof tool_choice !== 'string' && tool_choice?.function?.name;
|
|
551
362
|
const { maxChatCompletions = DEFAULT_MAX_CHAT_COMPLETIONS } = options || {};
|
|
552
363
|
|
|
364
|
+
// TODO(someday): clean this logic up
|
|
365
|
+
const inputTools = params.tools.map((tool): RunnableToolFunction<any> => {
|
|
366
|
+
if (isAutoParsableTool(tool)) {
|
|
367
|
+
if (!tool.$callback) {
|
|
368
|
+
throw new OpenAIError('Tool given to `.runTools()` that does not have an associated function');
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return {
|
|
372
|
+
type: 'function',
|
|
373
|
+
function: {
|
|
374
|
+
function: tool.$callback,
|
|
375
|
+
name: tool.function.name,
|
|
376
|
+
description: tool.function.description || '',
|
|
377
|
+
parameters: tool.function.parameters as any,
|
|
378
|
+
parse: tool.$parseRaw,
|
|
379
|
+
strict: true,
|
|
380
|
+
},
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return tool as any as RunnableToolFunction<any>;
|
|
385
|
+
});
|
|
386
|
+
|
|
553
387
|
const functionsByName: Record<string, RunnableFunction<any>> = {};
|
|
554
|
-
for (const f of
|
|
388
|
+
for (const f of inputTools) {
|
|
555
389
|
if (f.type === 'function') {
|
|
556
390
|
functionsByName[f.function.name || f.function.function.name] = f.function;
|
|
557
391
|
}
|
|
@@ -559,7 +393,7 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
559
393
|
|
|
560
394
|
const tools: ChatCompletionTool[] =
|
|
561
395
|
'tools' in params ?
|
|
562
|
-
|
|
396
|
+
inputTools.map((t) =>
|
|
563
397
|
t.type === 'function' ?
|
|
564
398
|
{
|
|
565
399
|
type: 'function',
|
|
@@ -567,6 +401,7 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
567
401
|
name: t.function.name || t.function.function.name,
|
|
568
402
|
parameters: t.function.parameters as Record<string, unknown>,
|
|
569
403
|
description: t.function.description,
|
|
404
|
+
strict: t.function.strict,
|
|
570
405
|
},
|
|
571
406
|
}
|
|
572
407
|
: (t as unknown as ChatCompletionTool),
|
|
@@ -579,7 +414,7 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
579
414
|
|
|
580
415
|
for (let i = 0; i < maxChatCompletions; ++i) {
|
|
581
416
|
const chatCompletion: ChatCompletion = await this._createChatCompletion(
|
|
582
|
-
|
|
417
|
+
client,
|
|
583
418
|
{
|
|
584
419
|
...restParams,
|
|
585
420
|
tool_choice,
|
|
@@ -592,7 +427,7 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
592
427
|
if (!message) {
|
|
593
428
|
throw new OpenAIError(`missing message in ChatCompletion response`);
|
|
594
429
|
}
|
|
595
|
-
if (!message.tool_calls) {
|
|
430
|
+
if (!message.tool_calls?.length) {
|
|
596
431
|
return;
|
|
597
432
|
}
|
|
598
433
|
|
|
@@ -603,8 +438,10 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
603
438
|
const fn = functionsByName[name];
|
|
604
439
|
|
|
605
440
|
if (!fn) {
|
|
606
|
-
const content = `Invalid tool_call: ${JSON.stringify(name)}. Available options are: ${
|
|
607
|
-
|
|
441
|
+
const content = `Invalid tool_call: ${JSON.stringify(name)}. Available options are: ${Object.keys(
|
|
442
|
+
functionsByName,
|
|
443
|
+
)
|
|
444
|
+
.map((name) => JSON.stringify(name))
|
|
608
445
|
.join(', ')}. Please try again`;
|
|
609
446
|
|
|
610
447
|
this._addMessage({ role, tool_call_id, content });
|
|
@@ -650,27 +487,7 @@ export abstract class AbstractChatCompletionRunner<
|
|
|
650
487
|
}
|
|
651
488
|
}
|
|
652
489
|
|
|
653
|
-
|
|
654
|
-
[k in Event]: k extends keyof AbstractChatCompletionRunnerEvents ? AbstractChatCompletionRunnerEvents[k]
|
|
655
|
-
: (...args: any[]) => void;
|
|
656
|
-
};
|
|
657
|
-
|
|
658
|
-
type ListenerForEvent<Events extends CustomEvents<any>, Event extends keyof Events> = Event extends (
|
|
659
|
-
keyof AbstractChatCompletionRunnerEvents
|
|
660
|
-
) ?
|
|
661
|
-
AbstractChatCompletionRunnerEvents[Event]
|
|
662
|
-
: Events[Event];
|
|
663
|
-
|
|
664
|
-
type ListenersForEvent<Events extends CustomEvents<any>, Event extends keyof Events> = Array<{
|
|
665
|
-
listener: ListenerForEvent<Events, Event>;
|
|
666
|
-
once?: boolean;
|
|
667
|
-
}>;
|
|
668
|
-
type EventParameters<Events extends CustomEvents<any>, Event extends keyof Events> = Parameters<
|
|
669
|
-
ListenerForEvent<Events, Event>
|
|
670
|
-
>;
|
|
671
|
-
|
|
672
|
-
export interface AbstractChatCompletionRunnerEvents {
|
|
673
|
-
connect: () => void;
|
|
490
|
+
export interface AbstractChatCompletionRunnerEvents extends BaseEvents {
|
|
674
491
|
functionCall: (functionCall: ChatCompletionMessage.FunctionCall) => void;
|
|
675
492
|
message: (message: ChatCompletionMessageParam) => void;
|
|
676
493
|
chatCompletion: (completion: ChatCompletion) => void;
|
|
@@ -680,8 +497,5 @@ export interface AbstractChatCompletionRunnerEvents {
|
|
|
680
497
|
finalFunctionCall: (functionCall: ChatCompletionMessage.FunctionCall) => void;
|
|
681
498
|
functionCallResult: (content: string) => void;
|
|
682
499
|
finalFunctionCallResult: (content: string) => void;
|
|
683
|
-
error: (error: OpenAIError) => void;
|
|
684
|
-
abort: (error: APIUserAbortError) => void;
|
|
685
|
-
end: () => void;
|
|
686
500
|
totalUsage: (usage: CompletionUsage) => void;
|
|
687
501
|
}
|