openai 4.0.0-beta.0 → 4.0.0-beta.1
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/README.md +39 -18
- package/_shims/agent.node.ts +20 -0
- package/_shims/agent.ts +12 -0
- package/_shims/fetch.node.ts +11 -0
- package/_shims/fetch.ts +32 -0
- package/_shims/fileFromPath.node.ts +30 -0
- package/_shims/fileFromPath.ts +30 -0
- package/_shims/formdata.node.ts +9 -0
- package/_shims/formdata.ts +22 -0
- package/_shims/getMultipartRequestOptions.node.ts +24 -0
- package/_shims/getMultipartRequestOptions.ts +13 -0
- package/_shims/newFileArgs.ts +94 -0
- package/_shims/toFile.node.ts +25 -0
- package/_shims/toFile.ts +26 -0
- package/_shims/uploadable.node.ts +21 -0
- package/_shims/uploadable.ts +82 -0
- package/core.ts +34 -87
- package/dist/cjs/_shims/agent.d.ts +9 -0
- package/dist/cjs/_shims/agent.d.ts.map +1 -0
- package/dist/cjs/_shims/agent.js +14 -0
- package/dist/cjs/_shims/agent.js.map +1 -0
- package/dist/cjs/_shims/agent.node.d.ts +6 -0
- package/dist/cjs/_shims/agent.node.d.ts.map +1 -0
- package/dist/cjs/_shims/agent.node.js +28 -0
- package/dist/cjs/_shims/agent.node.js.map +1 -0
- package/dist/cjs/_shims/fetch.d.ts +19 -0
- package/dist/cjs/_shims/fetch.d.ts.map +1 -0
- package/dist/cjs/_shims/fetch.js +24 -0
- package/dist/cjs/_shims/fetch.js.map +1 -0
- package/dist/cjs/_shims/fetch.node.d.ts +9 -0
- package/dist/cjs/_shims/fetch.node.d.ts.map +1 -0
- package/dist/cjs/_shims/fetch.node.js +68 -0
- package/dist/cjs/_shims/fetch.node.js.map +1 -0
- package/dist/cjs/_shims/fileFromPath.d.ts +23 -0
- package/dist/cjs/_shims/fileFromPath.d.ts.map +1 -0
- package/dist/cjs/{tests/api-resources/moderations.test.js → _shims/fileFromPath.js} +16 -22
- package/dist/cjs/_shims/fileFromPath.js.map +1 -0
- package/dist/cjs/_shims/fileFromPath.node.d.ts +18 -0
- package/dist/cjs/_shims/fileFromPath.node.d.ts.map +1 -0
- package/dist/cjs/{tests/form.test.js → _shims/fileFromPath.node.js} +16 -27
- package/dist/cjs/_shims/fileFromPath.node.js.map +1 -0
- package/dist/cjs/_shims/formdata.d.ts +13 -0
- package/dist/cjs/_shims/formdata.d.ts.map +1 -0
- package/dist/cjs/_shims/formdata.js +20 -0
- package/dist/cjs/_shims/formdata.js.map +1 -0
- package/dist/cjs/_shims/formdata.node.d.ts +7 -0
- package/dist/cjs/_shims/formdata.node.d.ts.map +1 -0
- package/dist/cjs/_shims/formdata.node.js +27 -0
- package/dist/cjs/_shims/formdata.node.js.map +1 -0
- package/dist/cjs/_shims/getMultipartRequestOptions.d.ts +10 -0
- package/dist/cjs/_shims/getMultipartRequestOptions.d.ts.map +1 -0
- package/dist/cjs/_shims/getMultipartRequestOptions.js +44 -0
- package/dist/cjs/_shims/getMultipartRequestOptions.js.map +1 -0
- package/dist/cjs/_shims/getMultipartRequestOptions.node.d.ts +10 -0
- package/dist/cjs/_shims/getMultipartRequestOptions.node.d.ts.map +1 -0
- package/dist/cjs/_shims/getMultipartRequestOptions.node.js +52 -0
- package/dist/cjs/_shims/getMultipartRequestOptions.node.js.map +1 -0
- package/dist/cjs/_shims/newFileArgs.d.ts +19 -0
- package/dist/cjs/_shims/newFileArgs.d.ts.map +1 -0
- package/dist/cjs/_shims/newFileArgs.js +169 -0
- package/dist/cjs/_shims/newFileArgs.js.map +1 -0
- package/dist/cjs/_shims/toFile.d.ts +21 -0
- package/dist/cjs/_shims/toFile.d.ts.map +1 -0
- package/dist/cjs/_shims/toFile.js +56 -0
- package/dist/cjs/_shims/toFile.js.map +1 -0
- package/dist/cjs/_shims/toFile.node.d.ts +20 -0
- package/dist/cjs/_shims/toFile.node.d.ts.map +1 -0
- package/dist/cjs/_shims/toFile.node.js +56 -0
- package/dist/cjs/_shims/toFile.node.js.map +1 -0
- package/dist/cjs/_shims/uploadable.d.ts +56 -0
- package/dist/cjs/_shims/uploadable.d.ts.map +1 -0
- package/dist/cjs/_shims/uploadable.js +37 -0
- package/dist/cjs/_shims/uploadable.js.map +1 -0
- package/dist/cjs/_shims/uploadable.node.d.ts +7 -0
- package/dist/cjs/_shims/uploadable.node.d.ts.map +1 -0
- package/dist/cjs/_shims/uploadable.node.js +23 -0
- package/dist/cjs/_shims/uploadable.node.js.map +1 -0
- package/dist/cjs/core.d.ts +6 -17
- package/dist/cjs/core.d.ts.map +1 -1
- package/dist/cjs/core.js +45 -74
- package/dist/cjs/core.js.map +1 -1
- package/dist/cjs/index.d.ts +19 -13
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +20 -6
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/resources/audio/audio.d.ts +9 -0
- package/dist/cjs/resources/audio/audio.d.ts.map +1 -1
- package/dist/cjs/resources/audio/audio.js +45 -0
- package/dist/cjs/resources/audio/audio.js.map +1 -1
- package/dist/cjs/resources/audio/index.d.ts +3 -2
- package/dist/cjs/resources/audio/index.d.ts.map +1 -1
- package/dist/cjs/resources/audio/index.js +22 -0
- package/dist/cjs/resources/audio/index.js.map +1 -1
- package/dist/cjs/resources/audio/transcriptions.d.ts +8 -3
- package/dist/cjs/resources/audio/transcriptions.d.ts.map +1 -1
- package/dist/cjs/resources/audio/transcriptions.js +38 -4
- package/dist/cjs/resources/audio/transcriptions.js.map +1 -1
- package/dist/cjs/resources/audio/translations.d.ts +9 -4
- package/dist/cjs/resources/audio/translations.d.ts.map +1 -1
- package/dist/cjs/resources/audio/translations.js +39 -5
- package/dist/cjs/resources/audio/translations.js.map +1 -1
- package/dist/cjs/resources/chat/chat.d.ts +7 -0
- package/dist/cjs/resources/chat/chat.d.ts.map +1 -1
- package/dist/cjs/resources/chat/chat.js +44 -0
- package/dist/cjs/resources/chat/chat.js.map +1 -1
- package/dist/cjs/resources/chat/completions.d.ts +49 -25
- package/dist/cjs/resources/chat/completions.d.ts.map +1 -1
- package/dist/cjs/resources/chat/completions.js +1 -0
- package/dist/cjs/resources/chat/completions.js.map +1 -1
- package/dist/cjs/resources/chat/index.d.ts +2 -1
- package/dist/cjs/resources/chat/index.d.ts.map +1 -1
- package/dist/cjs/resources/chat/index.js +15 -0
- package/dist/cjs/resources/chat/index.js.map +1 -1
- package/dist/cjs/resources/completions.d.ts +24 -2
- package/dist/cjs/resources/completions.d.ts.map +1 -1
- package/dist/cjs/resources/completions.js +1 -0
- package/dist/cjs/resources/completions.js.map +1 -1
- package/dist/cjs/resources/edits.d.ts +10 -5
- package/dist/cjs/resources/edits.d.ts.map +1 -1
- package/dist/cjs/resources/edits.js +1 -0
- package/dist/cjs/resources/edits.js.map +1 -1
- package/dist/cjs/resources/embeddings.d.ts +6 -1
- package/dist/cjs/resources/embeddings.d.ts.map +1 -1
- package/dist/cjs/resources/embeddings.js +1 -0
- package/dist/cjs/resources/embeddings.js.map +1 -1
- package/dist/cjs/resources/files.d.ts +22 -13
- package/dist/cjs/resources/files.d.ts.map +1 -1
- package/dist/cjs/resources/files.js +38 -1
- package/dist/cjs/resources/files.js.map +1 -1
- package/dist/cjs/resources/fine-tunes.d.ts +13 -4
- package/dist/cjs/resources/fine-tunes.d.ts.map +1 -1
- package/dist/cjs/resources/fine-tunes.js +1 -0
- package/dist/cjs/resources/fine-tunes.js.map +1 -1
- package/dist/cjs/resources/images.d.ts +12 -4
- package/dist/cjs/resources/images.d.ts.map +1 -1
- package/dist/cjs/resources/images.js +44 -8
- package/dist/cjs/resources/images.js.map +1 -1
- package/dist/cjs/resources/index.d.ts +4 -6
- package/dist/cjs/resources/index.d.ts.map +1 -1
- package/dist/cjs/resources/index.js +8 -24
- package/dist/cjs/resources/index.js.map +1 -1
- package/dist/cjs/resources/models.d.ts +12 -6
- package/dist/cjs/resources/models.d.ts.map +1 -1
- package/dist/cjs/resources/models.js +1 -0
- package/dist/cjs/resources/models.js.map +1 -1
- package/dist/cjs/resources/moderations.d.ts +7 -1
- package/dist/cjs/resources/moderations.d.ts.map +1 -1
- package/dist/cjs/resources/moderations.js +1 -0
- package/dist/cjs/resources/moderations.js.map +1 -1
- package/dist/cjs/streaming.d.ts +1 -2
- package/dist/cjs/streaming.d.ts.map +1 -1
- package/dist/cjs/streaming.js +2 -0
- package/dist/cjs/streaming.js.map +1 -1
- package/dist/cjs/uploads.d.ts +24 -0
- package/dist/cjs/uploads.d.ts.map +1 -0
- package/dist/cjs/uploads.js +121 -0
- package/dist/cjs/uploads.js.map +1 -0
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.d.ts.map +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/version.js.map +1 -1
- package/dist/esm/_shims/agent.d.ts +9 -0
- package/dist/esm/_shims/agent.d.ts.map +1 -0
- package/dist/esm/_shims/agent.js +10 -0
- package/dist/esm/_shims/agent.js.map +1 -0
- package/dist/esm/_shims/agent.node.d.ts +6 -0
- package/dist/esm/_shims/agent.node.d.ts.map +1 -0
- package/dist/esm/_shims/agent.node.js +16 -0
- package/dist/esm/_shims/agent.node.js.map +1 -0
- package/dist/esm/_shims/fetch.d.ts +19 -0
- package/dist/esm/_shims/fetch.d.ts.map +1 -0
- package/dist/esm/_shims/fetch.js +18 -0
- package/dist/esm/_shims/fetch.js.map +1 -0
- package/dist/esm/_shims/fetch.node.d.ts +9 -0
- package/dist/esm/_shims/fetch.node.d.ts.map +1 -0
- package/dist/esm/_shims/fetch.node.js +7 -0
- package/dist/esm/_shims/fetch.node.js.map +1 -0
- package/dist/esm/_shims/fileFromPath.d.ts +23 -0
- package/dist/esm/_shims/fileFromPath.d.ts.map +1 -0
- package/dist/esm/_shims/fileFromPath.js +12 -0
- package/dist/esm/_shims/fileFromPath.js.map +1 -0
- package/dist/esm/_shims/fileFromPath.node.d.ts +18 -0
- package/dist/esm/_shims/fileFromPath.node.d.ts.map +1 -0
- package/dist/esm/_shims/fileFromPath.node.js +13 -0
- package/dist/esm/_shims/fileFromPath.node.js.map +1 -0
- package/dist/esm/_shims/formdata.d.ts +13 -0
- package/dist/esm/_shims/formdata.d.ts.map +1 -0
- package/dist/esm/_shims/formdata.js +15 -0
- package/dist/esm/_shims/formdata.js.map +1 -0
- package/dist/esm/_shims/formdata.node.d.ts +7 -0
- package/dist/esm/_shims/formdata.node.d.ts.map +1 -0
- package/dist/esm/_shims/formdata.node.js +7 -0
- package/dist/esm/_shims/formdata.node.js.map +1 -0
- package/dist/esm/_shims/getMultipartRequestOptions.d.ts +10 -0
- package/dist/esm/_shims/getMultipartRequestOptions.d.ts.map +1 -0
- package/dist/esm/_shims/getMultipartRequestOptions.js +7 -0
- package/dist/esm/_shims/getMultipartRequestOptions.js.map +1 -0
- package/dist/esm/_shims/getMultipartRequestOptions.node.d.ts +10 -0
- package/dist/esm/_shims/getMultipartRequestOptions.node.d.ts.map +1 -0
- package/dist/esm/_shims/getMultipartRequestOptions.node.js +17 -0
- package/dist/esm/_shims/getMultipartRequestOptions.node.js.map +1 -0
- package/dist/esm/_shims/newFileArgs.d.ts +19 -0
- package/dist/esm/_shims/newFileArgs.d.ts.map +1 -0
- package/dist/esm/_shims/newFileArgs.js +66 -0
- package/dist/esm/_shims/newFileArgs.js.map +1 -0
- package/dist/esm/_shims/toFile.d.ts +21 -0
- package/dist/esm/_shims/toFile.d.ts.map +1 -0
- package/dist/esm/_shims/toFile.js +19 -0
- package/dist/esm/_shims/toFile.js.map +1 -0
- package/dist/esm/_shims/toFile.node.d.ts +20 -0
- package/dist/esm/_shims/toFile.node.d.ts.map +1 -0
- package/dist/esm/_shims/toFile.node.js +19 -0
- package/dist/esm/_shims/toFile.node.js.map +1 -0
- package/dist/esm/_shims/uploadable.d.ts +56 -0
- package/dist/esm/_shims/uploadable.d.ts.map +1 -0
- package/dist/esm/_shims/uploadable.js +30 -0
- package/dist/esm/_shims/uploadable.js.map +1 -0
- package/dist/esm/_shims/uploadable.node.d.ts +7 -0
- package/dist/esm/_shims/uploadable.node.d.ts.map +1 -0
- package/dist/esm/_shims/uploadable.node.js +10 -0
- package/dist/esm/_shims/uploadable.node.js.map +1 -0
- package/dist/esm/check-version.d.ts +2 -0
- package/dist/esm/check-version.d.ts.map +1 -0
- package/dist/esm/check-version.js +16 -0
- package/dist/esm/check-version.js.map +1 -0
- package/dist/esm/core.d.ts +221 -0
- package/dist/esm/core.d.ts.map +1 -0
- package/dist/esm/core.js +556 -0
- package/dist/esm/core.js.map +1 -0
- package/dist/esm/error.d.ts +51 -0
- package/dist/esm/error.d.ts.map +1 -0
- package/dist/esm/error.js +104 -0
- package/dist/esm/error.js.map +1 -0
- package/dist/esm/fetch-polyfill.d.ts +6 -0
- package/dist/esm/fetch-polyfill.d.ts.map +1 -0
- package/dist/esm/fetch-polyfill.js +52 -0
- package/dist/esm/fetch-polyfill.js.map +1 -0
- package/dist/esm/index.d.ts +106 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +93 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/resource.d.ts +12 -0
- package/dist/esm/resource.d.ts.map +1 -0
- package/dist/esm/resource.js +13 -0
- package/dist/esm/resource.js.map +1 -0
- package/dist/esm/resources/answers.d.ts +150 -0
- package/dist/esm/resources/answers.d.ts.map +1 -0
- package/dist/esm/resources/answers.js +16 -0
- package/dist/esm/resources/answers.js.map +1 -0
- package/dist/esm/resources/audio/audio.d.ts +17 -0
- package/dist/esm/resources/audio/audio.d.ts.map +1 -0
- package/dist/esm/resources/audio/audio.js +17 -0
- package/dist/esm/resources/audio/audio.js.map +1 -0
- package/dist/esm/resources/audio/index.d.ts +4 -0
- package/dist/esm/resources/audio/index.d.ts.map +1 -0
- package/dist/esm/resources/audio/index.js +5 -0
- package/dist/esm/resources/audio/index.js.map +1 -0
- package/dist/esm/resources/audio/transcriptions.d.ts +57 -0
- package/dist/esm/resources/audio/transcriptions.d.ts.map +1 -0
- package/dist/esm/resources/audio/transcriptions.js +13 -0
- package/dist/esm/resources/audio/transcriptions.js.map +1 -0
- package/dist/esm/resources/audio/translations.d.ts +51 -0
- package/dist/esm/resources/audio/translations.d.ts.map +1 -0
- package/dist/esm/resources/audio/translations.js +13 -0
- package/dist/esm/resources/audio/translations.js.map +1 -0
- package/dist/esm/resources/chat/chat.d.ts +13 -0
- package/dist/esm/resources/chat/chat.d.ts.map +1 -0
- package/dist/esm/resources/chat/chat.js +14 -0
- package/dist/esm/resources/chat/chat.js.map +1 -0
- package/dist/esm/resources/chat/completions.d.ts +496 -0
- package/dist/esm/resources/chat/completions.d.ts.map +1 -0
- package/dist/esm/resources/chat/completions.js +9 -0
- package/dist/esm/resources/chat/completions.js.map +1 -0
- package/dist/esm/resources/chat/index.d.ts +3 -0
- package/dist/esm/resources/chat/index.d.ts.map +1 -0
- package/dist/esm/resources/chat/index.js +4 -0
- package/dist/esm/resources/chat/index.js.map +1 -0
- package/dist/esm/resources/classifications.d.ts +142 -0
- package/dist/esm/resources/classifications.d.ts.map +1 -0
- package/dist/esm/resources/classifications.js +20 -0
- package/dist/esm/resources/classifications.js.map +1 -0
- package/dist/esm/resources/completions.d.ts +335 -0
- package/dist/esm/resources/completions.d.ts.map +1 -0
- package/dist/esm/resources/completions.js +9 -0
- package/dist/esm/resources/completions.js.map +1 -0
- package/dist/esm/resources/edits.d.ts +76 -0
- package/dist/esm/resources/edits.d.ts.map +1 -0
- package/dist/esm/resources/edits.js +12 -0
- package/dist/esm/resources/edits.js.map +1 -0
- package/dist/esm/resources/embeddings.d.ts +54 -0
- package/dist/esm/resources/embeddings.d.ts.map +1 -0
- package/dist/esm/resources/embeddings.js +12 -0
- package/dist/esm/resources/embeddings.js.map +1 -0
- package/dist/esm/resources/files.d.ts +78 -0
- package/dist/esm/resources/files.d.ts.map +1 -0
- package/dist/esm/resources/files.js +43 -0
- package/dist/esm/resources/files.js.map +1 -0
- package/dist/esm/resources/fine-tunes.d.ts +225 -0
- package/dist/esm/resources/fine-tunes.d.ts.map +1 -0
- package/dist/esm/resources/fine-tunes.js +44 -0
- package/dist/esm/resources/fine-tunes.js.map +1 -0
- package/dist/esm/resources/images.d.ts +129 -0
- package/dist/esm/resources/images.d.ts.map +1 -0
- package/dist/esm/resources/images.js +25 -0
- package/dist/esm/resources/images.js.map +1 -0
- package/dist/esm/resources/index.d.ts +33 -0
- package/dist/esm/resources/index.d.ts.map +1 -0
- package/dist/esm/resources/index.js +12 -0
- package/dist/esm/resources/index.js.map +1 -0
- package/dist/esm/resources/models.d.ts +40 -0
- package/dist/esm/resources/models.d.ts.map +1 -0
- package/dist/esm/resources/models.js +26 -0
- package/dist/esm/resources/models.js.map +1 -0
- package/dist/esm/resources/moderations.d.ts +65 -0
- package/dist/esm/resources/moderations.d.ts.map +1 -0
- package/dist/esm/resources/moderations.js +12 -0
- package/dist/esm/resources/moderations.js.map +1 -0
- package/dist/esm/streaming.d.ts +12 -0
- package/dist/esm/streaming.d.ts.map +1 -0
- package/dist/esm/streaming.js +155 -0
- package/dist/esm/streaming.js.map +1 -0
- package/dist/esm/uploads.d.ts +24 -0
- package/dist/esm/uploads.d.ts.map +1 -0
- package/dist/esm/uploads.js +61 -0
- package/dist/esm/uploads.js.map +1 -0
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.d.ts.map +1 -0
- package/dist/esm/version.js +2 -0
- package/dist/esm/version.js.map +1 -0
- package/error.ts +1 -1
- package/index.ts +22 -18
- package/package.json +85 -5
- package/resources/audio/audio.ts +11 -0
- package/resources/audio/index.ts +3 -2
- package/resources/audio/transcriptions.ts +11 -6
- package/resources/audio/translations.ts +12 -7
- package/resources/chat/chat.ts +8 -0
- package/resources/chat/completions.ts +51 -26
- package/resources/chat/index.ts +2 -1
- package/resources/completions.ts +25 -2
- package/resources/edits.ts +11 -5
- package/resources/embeddings.ts +7 -1
- package/resources/files.ts +32 -19
- package/resources/fine-tunes.ts +14 -4
- package/resources/images.ts +20 -9
- package/resources/index.ts +4 -6
- package/resources/models.ts +16 -9
- package/resources/moderations.ts +8 -1
- package/streaming.ts +3 -1
- package/uploads.ts +81 -0
- package/version.ts +1 -1
- package/.eslintrc.js +0 -10
- package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -56
- package/.github/ISSUE_TEMPLATE/config.yml +0 -7
- package/.github/ISSUE_TEMPLATE/feature_request.yml +0 -20
- package/.github/workflows/test.yml +0 -24
- package/.prettierignore +0 -1
- package/.prettierrc +0 -6
- package/.stats.yml +0 -1
- package/api.md +0 -157
- package/bin/check-test-server +0 -50
- package/build +0 -12
- package/dist/cjs/examples/demo.d.ts +0 -3
- package/dist/cjs/examples/demo.d.ts.map +0 -1
- package/dist/cjs/examples/demo.js +0 -119
- package/dist/cjs/examples/demo.js.map +0 -1
- package/dist/cjs/examples/errors.d.ts +0 -3
- package/dist/cjs/examples/errors.d.ts.map +0 -1
- package/dist/cjs/examples/errors.js +0 -100
- package/dist/cjs/examples/errors.js.map +0 -1
- package/dist/cjs/examples/fine-tunes.d.ts +0 -10
- package/dist/cjs/examples/fine-tunes.d.ts.map +0 -1
- package/dist/cjs/examples/fine-tunes.js +0 -160
- package/dist/cjs/examples/fine-tunes.js.map +0 -1
- package/dist/cjs/tests/api-resources/answers.test.d.ts +0 -2
- package/dist/cjs/tests/api-resources/answers.test.d.ts.map +0 -1
- package/dist/cjs/tests/api-resources/answers.test.js +0 -279
- package/dist/cjs/tests/api-resources/answers.test.js.map +0 -1
- package/dist/cjs/tests/api-resources/audio/transcriptions.test.d.ts +0 -2
- package/dist/cjs/tests/api-resources/audio/transcriptions.test.d.ts.map +0 -1
- package/dist/cjs/tests/api-resources/audio/transcriptions.test.js +0 -65
- package/dist/cjs/tests/api-resources/audio/transcriptions.test.js.map +0 -1
- package/dist/cjs/tests/api-resources/audio/translations.test.d.ts +0 -2
- package/dist/cjs/tests/api-resources/audio/translations.test.d.ts.map +0 -1
- package/dist/cjs/tests/api-resources/audio/translations.test.js +0 -64
- package/dist/cjs/tests/api-resources/audio/translations.test.js.map +0 -1
- package/dist/cjs/tests/api-resources/chat/completions.test.d.ts +0 -2
- package/dist/cjs/tests/api-resources/chat/completions.test.d.ts.map +0 -1
- package/dist/cjs/tests/api-resources/chat/completions.test.js +0 -77
- package/dist/cjs/tests/api-resources/chat/completions.test.js.map +0 -1
- package/dist/cjs/tests/api-resources/classifications.test.d.ts +0 -2
- package/dist/cjs/tests/api-resources/classifications.test.d.ts.map +0 -1
- package/dist/cjs/tests/api-resources/classifications.test.js +0 -73
- package/dist/cjs/tests/api-resources/classifications.test.js.map +0 -1
- package/dist/cjs/tests/api-resources/completions.test.d.ts +0 -2
- package/dist/cjs/tests/api-resources/completions.test.d.ts.map +0 -1
- package/dist/cjs/tests/api-resources/completions.test.js +0 -69
- package/dist/cjs/tests/api-resources/completions.test.js.map +0 -1
- package/dist/cjs/tests/api-resources/edits.test.d.ts +0 -2
- package/dist/cjs/tests/api-resources/edits.test.d.ts.map +0 -1
- package/dist/cjs/tests/api-resources/edits.test.js +0 -62
- package/dist/cjs/tests/api-resources/edits.test.js.map +0 -1
- package/dist/cjs/tests/api-resources/embeddings.test.d.ts +0 -2
- package/dist/cjs/tests/api-resources/embeddings.test.d.ts.map +0 -1
- package/dist/cjs/tests/api-resources/embeddings.test.js +0 -59
- package/dist/cjs/tests/api-resources/embeddings.test.js.map +0 -1
- package/dist/cjs/tests/api-resources/files.test.d.ts +0 -2
- package/dist/cjs/tests/api-resources/files.test.d.ts.map +0 -1
- package/dist/cjs/tests/api-resources/files.test.js +0 -107
- package/dist/cjs/tests/api-resources/files.test.js.map +0 -1
- package/dist/cjs/tests/api-resources/fine-tunes.test.d.ts +0 -2
- package/dist/cjs/tests/api-resources/fine-tunes.test.d.ts.map +0 -1
- package/dist/cjs/tests/api-resources/fine-tunes.test.js +0 -115
- package/dist/cjs/tests/api-resources/fine-tunes.test.js.map +0 -1
- package/dist/cjs/tests/api-resources/images.test.d.ts +0 -2
- package/dist/cjs/tests/api-resources/images.test.d.ts.map +0 -1
- package/dist/cjs/tests/api-resources/images.test.js +0 -100
- package/dist/cjs/tests/api-resources/images.test.js.map +0 -1
- package/dist/cjs/tests/api-resources/models.test.d.ts +0 -2
- package/dist/cjs/tests/api-resources/models.test.d.ts.map +0 -1
- package/dist/cjs/tests/api-resources/models.test.js +0 -77
- package/dist/cjs/tests/api-resources/models.test.js.map +0 -1
- package/dist/cjs/tests/api-resources/moderations.test.d.ts +0 -2
- package/dist/cjs/tests/api-resources/moderations.test.d.ts.map +0 -1
- package/dist/cjs/tests/api-resources/moderations.test.js.map +0 -1
- package/dist/cjs/tests/form.test.d.ts +0 -2
- package/dist/cjs/tests/form.test.d.ts.map +0 -1
- package/dist/cjs/tests/form.test.js.map +0 -1
- package/dist/cjs/tests/index.test.d.ts +0 -2
- package/dist/cjs/tests/index.test.d.ts.map +0 -1
- package/dist/cjs/tests/index.test.js +0 -75
- package/dist/cjs/tests/index.test.js.map +0 -1
- package/dist/cjs/tests/responses.test.d.ts +0 -2
- package/dist/cjs/tests/responses.test.d.ts.map +0 -1
- package/dist/cjs/tests/responses.test.js +0 -60
- package/dist/cjs/tests/responses.test.js.map +0 -1
- package/examples/demo.ts +0 -28
- package/examples/errors.ts +0 -28
- package/examples/fine-tune-data.jsonl +0 -3
- package/examples/fine-tunes.ts +0 -37
- package/jest.config.js +0 -8
- package/tests/api-resources/answers.test.ts +0 -242
- package/tests/api-resources/audio/transcriptions.test.ts +0 -28
- package/tests/api-resources/audio/translations.test.ts +0 -27
- package/tests/api-resources/chat/completions.test.ts +0 -40
- package/tests/api-resources/classifications.test.ts +0 -36
- package/tests/api-resources/completions.test.ts +0 -32
- package/tests/api-resources/edits.test.ts +0 -25
- package/tests/api-resources/embeddings.test.ts +0 -22
- package/tests/api-resources/files.test.ts +0 -64
- package/tests/api-resources/fine-tunes.test.ts +0 -78
- package/tests/api-resources/images.test.ts +0 -61
- package/tests/api-resources/models.test.ts +0 -40
- package/tests/api-resources/moderations.test.ts +0 -18
- package/tests/form.test.ts +0 -27
- package/tests/index.test.ts +0 -76
- package/tests/responses.test.ts +0 -25
- package/tsconfig.cjs.json +0 -8
- package/tsconfig.json +0 -39
- package/typings/digest-fetch/index.d.ts +0 -33
package/README.md
CHANGED
|
@@ -24,12 +24,12 @@ yarn add openai
|
|
|
24
24
|
```js
|
|
25
25
|
import OpenAI from 'openai';
|
|
26
26
|
|
|
27
|
-
const
|
|
27
|
+
const openai = new OpenAI({
|
|
28
28
|
apiKey: 'my api key', // defaults to process.env["OPENAI_API_KEY"]
|
|
29
29
|
});
|
|
30
30
|
|
|
31
31
|
async function main() {
|
|
32
|
-
const completion = await
|
|
32
|
+
const completion = await openai.completions.create({
|
|
33
33
|
model: 'text-davinci-002',
|
|
34
34
|
prompt: 'Say this is a test',
|
|
35
35
|
max_tokens: 6,
|
|
@@ -71,7 +71,7 @@ If you like, you may reference our types directly:
|
|
|
71
71
|
```ts
|
|
72
72
|
import OpenAI from 'openai';
|
|
73
73
|
|
|
74
|
-
const
|
|
74
|
+
const openai = new OpenAI({
|
|
75
75
|
apiKey: 'my api key', // defaults to process.env["OPENAI_API_KEY"]
|
|
76
76
|
});
|
|
77
77
|
|
|
@@ -82,7 +82,7 @@ async function main() {
|
|
|
82
82
|
max_tokens: 6,
|
|
83
83
|
temperature: 0,
|
|
84
84
|
};
|
|
85
|
-
const completion: OpenAI.Completion = await
|
|
85
|
+
const completion: OpenAI.Completion = await openai.completions.create(params);
|
|
86
86
|
}
|
|
87
87
|
main().catch(console.error);
|
|
88
88
|
```
|
|
@@ -91,17 +91,38 @@ Documentation for each method, request param, and response field are available i
|
|
|
91
91
|
|
|
92
92
|
## File Uploads
|
|
93
93
|
|
|
94
|
-
Request parameters that correspond to file uploads can be passed
|
|
94
|
+
Request parameters that correspond to file uploads can be passed in many different forms:
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
- `File` (or an object with the same structure)
|
|
97
|
+
- a `fetch` `Response` (or an object with the same structure)
|
|
98
|
+
- an `fs.ReadStream`
|
|
99
|
+
- the return value of our `toFile` helper
|
|
97
100
|
|
|
98
101
|
```ts
|
|
99
|
-
import
|
|
102
|
+
import fs from 'fs';
|
|
103
|
+
import fetch from 'node-fetch';
|
|
104
|
+
import OpenAI, { toFile } from 'openai';
|
|
100
105
|
|
|
101
|
-
const
|
|
106
|
+
const openai = new OpenAI();
|
|
102
107
|
|
|
103
|
-
|
|
104
|
-
await
|
|
108
|
+
// If you have access to Node `fs` we recommend using `fs.createReadStream()`:
|
|
109
|
+
await openai.files.create({ file: fs.createReadStream('input.jsonl'), purpose: 'fine-tune' });
|
|
110
|
+
|
|
111
|
+
// Or if you have the web `File` API you can pass a `File` instance:
|
|
112
|
+
await openai.files.create({ file: new File(['my bytes'], 'input.jsonl'), purpose: 'fine-tune' });
|
|
113
|
+
|
|
114
|
+
// You can also pass a `fetch` `Response`:
|
|
115
|
+
await openai.files.create({ file: await fetch('https://somesite/input.jsonl'), purpose: 'fine-tune' });
|
|
116
|
+
|
|
117
|
+
// Finally, if none of the above are convenient, you can use our `toFile` helper:
|
|
118
|
+
await openai.files.create({
|
|
119
|
+
file: await toFile(Buffer.from('my bytes'), 'input.jsonl'),
|
|
120
|
+
purpose: 'fine-tune',
|
|
121
|
+
});
|
|
122
|
+
await openai.files.create({
|
|
123
|
+
file: await toFile(new Uint8Array([0, 1, 2]), 'input.jsonl'),
|
|
124
|
+
purpose: 'fine-tune',
|
|
125
|
+
});
|
|
105
126
|
```
|
|
106
127
|
|
|
107
128
|
## Handling errors
|
|
@@ -112,7 +133,7 @@ a subclass of `APIError` will be thrown:
|
|
|
112
133
|
|
|
113
134
|
```ts
|
|
114
135
|
async function main() {
|
|
115
|
-
const fineTune = await
|
|
136
|
+
const fineTune = await openai.fineTunes
|
|
116
137
|
.create({ training_file: 'file-XGinujblHPwGLSztz8cPS8XY' })
|
|
117
138
|
.catch((err) => {
|
|
118
139
|
if (err instanceof OpenAI.APIError) {
|
|
@@ -150,12 +171,12 @@ You can use the `maxRetries` option to configure or disable this:
|
|
|
150
171
|
<!-- prettier-ignore -->
|
|
151
172
|
```js
|
|
152
173
|
// Configure the default for all requests:
|
|
153
|
-
const
|
|
174
|
+
const openai = new OpenAI({
|
|
154
175
|
maxRetries: 0, // default is 2
|
|
155
176
|
});
|
|
156
177
|
|
|
157
178
|
// Or, configure per-request:
|
|
158
|
-
|
|
179
|
+
openai.embeddings.create({ model: 'text-similarity-babbage-001',input: 'The food was delicious and the waiter...' }, {
|
|
159
180
|
maxRetries: 5,
|
|
160
181
|
});
|
|
161
182
|
```
|
|
@@ -167,12 +188,12 @@ Requests time out after 60 seconds by default. You can configure this with a `ti
|
|
|
167
188
|
<!-- prettier-ignore -->
|
|
168
189
|
```ts
|
|
169
190
|
// Configure the default for all requests:
|
|
170
|
-
const
|
|
191
|
+
const openai = new OpenAI({
|
|
171
192
|
timeout: 20 * 1000, // 20 seconds (default is 60s)
|
|
172
193
|
});
|
|
173
194
|
|
|
174
195
|
// Override per-request:
|
|
175
|
-
|
|
196
|
+
openai.edits.create({ model: 'text-davinci-edit-001',input: 'What day of the wek is it?',instruction: 'Fix the spelling mistakes' }, {
|
|
176
197
|
timeout: 5 * 1000,
|
|
177
198
|
});
|
|
178
199
|
```
|
|
@@ -193,12 +214,12 @@ import http from 'http';
|
|
|
193
214
|
import HttpsProxyAgent from 'https-proxy-agent';
|
|
194
215
|
|
|
195
216
|
// Configure the default for all requests:
|
|
196
|
-
const
|
|
217
|
+
const openai = new OpenAI({
|
|
197
218
|
httpAgent: new HttpsProxyAgent(process.env.PROXY_URL),
|
|
198
219
|
});
|
|
199
220
|
|
|
200
221
|
// Override per-request:
|
|
201
|
-
|
|
222
|
+
openai.models.list({
|
|
202
223
|
baseURL: 'http://localhost:8080/test-api',
|
|
203
224
|
httpAgent: new http.Agent({ keepAlive: false }),
|
|
204
225
|
})
|
|
@@ -216,7 +237,7 @@ We are keen for your feedback; please open an [issue](https://www.github.com/ope
|
|
|
216
237
|
|
|
217
238
|
The following runtimes are supported:
|
|
218
239
|
|
|
219
|
-
- Node.js
|
|
240
|
+
- Node.js 16 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
|
|
220
241
|
- Deno v1.28.0 or higher (experimental).
|
|
221
242
|
Use `import OpenAI from "npm:openai"`.
|
|
222
243
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import KeepAliveAgent from 'agentkeepalive';
|
|
6
|
+
import type { Agent } from 'node:http';
|
|
7
|
+
import { AbortController as AbortControllerPolyfill } from 'abort-controller';
|
|
8
|
+
|
|
9
|
+
const defaultHttpAgent: Agent = new KeepAliveAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });
|
|
10
|
+
const defaultHttpsAgent: Agent = new KeepAliveAgent.HttpsAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });
|
|
11
|
+
|
|
12
|
+
// Polyfill global object if needed.
|
|
13
|
+
if (typeof AbortController === 'undefined') {
|
|
14
|
+
AbortController = AbortControllerPolyfill as any as typeof AbortController;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const getDefaultAgent = (url: string): Agent | undefined => {
|
|
18
|
+
if (defaultHttpsAgent && url.startsWith('https')) return defaultHttpsAgent;
|
|
19
|
+
return defaultHttpAgent;
|
|
20
|
+
};
|
package/_shims/agent.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*
|
|
4
|
+
* This is a stub for non-node environments.
|
|
5
|
+
* In node environments, it gets replaced agent.node.ts by the package export map
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Agent } from 'node:http';
|
|
9
|
+
|
|
10
|
+
export const getDefaultAgent = (url: string): Agent | undefined => {
|
|
11
|
+
return undefined;
|
|
12
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fetch, { Request, Response, Headers } from 'node-fetch';
|
|
6
|
+
import type { RequestInfo, RequestInit, BodyInit } from 'node-fetch';
|
|
7
|
+
|
|
8
|
+
export { fetch, Request, Response, Headers };
|
|
9
|
+
export type { RequestInfo, RequestInit, BodyInit };
|
|
10
|
+
|
|
11
|
+
export const isPolyfilled = true;
|
package/_shims/fetch.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type * as nf from 'node-fetch';
|
|
6
|
+
|
|
7
|
+
const _fetch: typeof nf.default =
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
fetch as any;
|
|
10
|
+
type _fetch = typeof nf.default;
|
|
11
|
+
const _Request: typeof nf.Request =
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
Request as any;
|
|
14
|
+
type _Request = nf.Request;
|
|
15
|
+
const _Response: typeof nf.Response =
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
Response as any;
|
|
18
|
+
type _Response = nf.Response;
|
|
19
|
+
const _Headers: typeof nf.Headers =
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
Headers as any;
|
|
22
|
+
type _Headers = nf.Headers;
|
|
23
|
+
|
|
24
|
+
export const isPolyfilled = false;
|
|
25
|
+
|
|
26
|
+
export { _fetch as fetch, _Request as Request, _Response as Response, _Headers as Headers };
|
|
27
|
+
|
|
28
|
+
type _RequestInfo = nf.RequestInfo;
|
|
29
|
+
type _RequestInit = nf.RequestInit;
|
|
30
|
+
type _BodyInit = nf.BodyInit;
|
|
31
|
+
|
|
32
|
+
export type { _RequestInit as RequestInit, _RequestInfo as RequestInfo, _BodyInit as BodyInit };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { FilePropertyBag } from 'formdata-node';
|
|
6
|
+
import { fileFromPath as _fileFromPath } from 'formdata-node/file-from-path';
|
|
7
|
+
import type { File } from './formdata.node';
|
|
8
|
+
|
|
9
|
+
export type FileFromPathOptions = Omit<FilePropertyBag, 'lastModified'>;
|
|
10
|
+
|
|
11
|
+
let warned = false;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated use fs.createReadStream('./my/file.txt') instead
|
|
15
|
+
*/
|
|
16
|
+
export async function fileFromPath(path: string): Promise<File>;
|
|
17
|
+
export async function fileFromPath(path: string, filename?: string): Promise<File>;
|
|
18
|
+
export async function fileFromPath(path: string, options?: FileFromPathOptions): Promise<File>;
|
|
19
|
+
export async function fileFromPath(
|
|
20
|
+
path: string,
|
|
21
|
+
filename?: string,
|
|
22
|
+
options?: FileFromPathOptions,
|
|
23
|
+
): Promise<File>;
|
|
24
|
+
export async function fileFromPath(path: string, ...args: any[]): Promise<File> {
|
|
25
|
+
if (!warned) {
|
|
26
|
+
console.warn(`fileFromPath is deprecated; use fs.createReadStream(${JSON.stringify(path)}) instead`);
|
|
27
|
+
warned = true;
|
|
28
|
+
}
|
|
29
|
+
return await _fileFromPath(path, ...args);
|
|
30
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*
|
|
4
|
+
* This is a stub that gets replaced by fileFromPath.node.js for node environments
|
|
5
|
+
* in the package export map
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { FilePropertyBag } from 'formdata-node';
|
|
9
|
+
import type { File } from './formdata';
|
|
10
|
+
|
|
11
|
+
export type FileFromPathOptions = Omit<FilePropertyBag, 'lastModified'>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* This is a stub for non-node environments that just throws an error.
|
|
15
|
+
* In node environments, this module will be replaced by util/node/fileFromPath by the
|
|
16
|
+
* package import map.
|
|
17
|
+
*/
|
|
18
|
+
export async function fileFromPath(path: string): Promise<File>;
|
|
19
|
+
export async function fileFromPath(path: string, filename?: string): Promise<File>;
|
|
20
|
+
export async function fileFromPath(path: string, options?: FileFromPathOptions): Promise<File>;
|
|
21
|
+
export async function fileFromPath(
|
|
22
|
+
path: string,
|
|
23
|
+
filename?: string,
|
|
24
|
+
options?: FileFromPathOptions,
|
|
25
|
+
): Promise<File>;
|
|
26
|
+
export async function fileFromPath(): Promise<File> {
|
|
27
|
+
throw new Error(
|
|
28
|
+
'The `fileFromPath` function is only supported in Node. See the README for more details: https://www.github.com/openai/openai-node#file-uploads',
|
|
29
|
+
);
|
|
30
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type * as fd from 'formdata-node';
|
|
6
|
+
|
|
7
|
+
const _FormData: typeof fd.FormData =
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
FormData as any;
|
|
10
|
+
type _FormData = fd.FormData;
|
|
11
|
+
const _File: typeof fd.File =
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
File as any;
|
|
14
|
+
type _File = fd.File;
|
|
15
|
+
const _Blob: typeof fd.Blob =
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
Blob as any;
|
|
18
|
+
type _Blob = fd.Blob;
|
|
19
|
+
|
|
20
|
+
export const isPolyfilled = false;
|
|
21
|
+
|
|
22
|
+
export { _FormData as FormData, _File as File, _Blob as Blob };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { FormData } from './formdata.node';
|
|
6
|
+
import type { RequestOptions } from '../core';
|
|
7
|
+
import { Readable } from 'node:stream';
|
|
8
|
+
import { FormDataEncoder } from 'form-data-encoder';
|
|
9
|
+
|
|
10
|
+
export async function getMultipartRequestOptions<T extends {} = Record<string, unknown>>(
|
|
11
|
+
form: FormData,
|
|
12
|
+
opts: RequestOptions<T>,
|
|
13
|
+
): Promise<RequestOptions<T>> {
|
|
14
|
+
const encoder = new FormDataEncoder(form);
|
|
15
|
+
const readable = Readable.from(encoder);
|
|
16
|
+
const body = { __multipartBody__: readable };
|
|
17
|
+
const headers = {
|
|
18
|
+
...opts.headers,
|
|
19
|
+
...encoder.headers,
|
|
20
|
+
'Content-Length': encoder.contentLength,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
return { ...opts, body: body as any, headers };
|
|
24
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { FormData } from './formdata';
|
|
6
|
+
import type { RequestOptions } from '../core';
|
|
7
|
+
|
|
8
|
+
export async function getMultipartRequestOptions<T extends {} = Record<string, unknown>>(
|
|
9
|
+
form: FormData,
|
|
10
|
+
opts: RequestOptions<T>,
|
|
11
|
+
): Promise<RequestOptions<T>> {
|
|
12
|
+
return { ...opts, body: { __multipartBody__: form } as any };
|
|
13
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { type BlobPart, type Uploadable, isResponseLike, isBlobLike } from './uploadable';
|
|
6
|
+
|
|
7
|
+
export type ToFileInput = Uploadable | Exclude<BlobPart, string> | AsyncIterable<BlobPart>;
|
|
8
|
+
|
|
9
|
+
export type FilePropertyBag = {
|
|
10
|
+
type?: string;
|
|
11
|
+
lastModified?: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export async function newFileArgs(
|
|
15
|
+
value: ToFileInput,
|
|
16
|
+
name?: string | null,
|
|
17
|
+
options: FilePropertyBag = {},
|
|
18
|
+
): Promise<{
|
|
19
|
+
bits: BlobPart[];
|
|
20
|
+
name: string;
|
|
21
|
+
options: FilePropertyBag;
|
|
22
|
+
}> {
|
|
23
|
+
if (isResponseLike(value)) {
|
|
24
|
+
const blob = await value.blob();
|
|
25
|
+
name ||= new URL(value.url).pathname.split(/[\\/]/).pop() ?? 'unknown_file';
|
|
26
|
+
|
|
27
|
+
return { bits: [blob as any], name, options };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const bits = await getBytes(value);
|
|
31
|
+
|
|
32
|
+
name ||= getName(value) ?? 'unknown_file';
|
|
33
|
+
|
|
34
|
+
if (!options.type) {
|
|
35
|
+
const type = (bits[0] as any)?.type;
|
|
36
|
+
if (typeof type === 'string') {
|
|
37
|
+
options = { ...options, type };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return { bits, name, options };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function getBytes(value: ToFileInput): Promise<Array<BlobPart>> {
|
|
45
|
+
if (value instanceof Promise) return getBytes(await (value as any));
|
|
46
|
+
|
|
47
|
+
let parts: Array<BlobPart> = [];
|
|
48
|
+
if (
|
|
49
|
+
typeof value === 'string' ||
|
|
50
|
+
ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
|
|
51
|
+
value instanceof ArrayBuffer
|
|
52
|
+
) {
|
|
53
|
+
parts.push(value);
|
|
54
|
+
} else if (isBlobLike(value)) {
|
|
55
|
+
parts.push(await value.arrayBuffer());
|
|
56
|
+
} else if (
|
|
57
|
+
isAsyncIterableIterator(value) // includes Readable, ReadableStream, etc.
|
|
58
|
+
) {
|
|
59
|
+
for await (const chunk of value) {
|
|
60
|
+
parts.push(chunk as BlobPart); // TODO, consider validating?
|
|
61
|
+
}
|
|
62
|
+
} else {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Unexpected data type: ${typeof value}; constructor: ${
|
|
65
|
+
value?.constructor?.name
|
|
66
|
+
}; props: ${propsForError(value)}`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return parts;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function propsForError(value: any): string {
|
|
74
|
+
const props = Object.getOwnPropertyNames(value);
|
|
75
|
+
return `[${props.map((p) => `"${p}"`).join(', ')}]`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function getName(value: any): string | undefined {
|
|
79
|
+
return (
|
|
80
|
+
getStringFromMaybeBuffer(value.name) ||
|
|
81
|
+
getStringFromMaybeBuffer(value.filename) ||
|
|
82
|
+
// For fs.ReadStream
|
|
83
|
+
getStringFromMaybeBuffer(value.path)?.split(/[\\/]/).pop()
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const getStringFromMaybeBuffer = (x: string | Buffer | unknown): string | undefined => {
|
|
88
|
+
if (typeof x === 'string') return x;
|
|
89
|
+
if (typeof Buffer !== 'undefined' && x instanceof Buffer) return String(x);
|
|
90
|
+
return undefined;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const isAsyncIterableIterator = (value: any): value is AsyncIterableIterator<unknown> =>
|
|
94
|
+
value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { File } from 'formdata-node';
|
|
6
|
+
import { type ToFileInput, newFileArgs, type FilePropertyBag } from './newFileArgs';
|
|
7
|
+
import type { Uploadable, BlobPart, FileLike } from './uploadable.node'; // eslint-disable-line
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
|
|
11
|
+
* @param bits the raw content of the file. Can be an {@link Uploadable}, {@link BlobPart}, or {@link AsyncIterable} of {@link BlobPart}s
|
|
12
|
+
* @param name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
|
|
13
|
+
* @param {Object=} options additional properties
|
|
14
|
+
* @param {string=} options.type the MIME type of the content
|
|
15
|
+
* @param {number=} options.lastModified the last modified timestamp
|
|
16
|
+
* @returns a {@link File} with the given properties
|
|
17
|
+
*/
|
|
18
|
+
export async function toFile(
|
|
19
|
+
bits: ToFileInput,
|
|
20
|
+
name?: string | null | undefined,
|
|
21
|
+
options: FilePropertyBag | undefined = {},
|
|
22
|
+
): Promise<FileLike> {
|
|
23
|
+
const args = await newFileArgs(bits, name, options);
|
|
24
|
+
return new File(args.bits, args.name, args.options);
|
|
25
|
+
}
|
package/_shims/toFile.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/// <reference lib="dom" />;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { type ToFileInput, newFileArgs, type FilePropertyBag } from './newFileArgs';
|
|
8
|
+
import type { FileLike, Uploadable } from './uploadable'; // eslint-disable-line
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
|
|
12
|
+
* @param bits the raw content of the file. Can be an {@link Uploadable}, {@link BlobPart}, or {@link AsyncIterable} of {@link BlobPart}s
|
|
13
|
+
* @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
|
|
14
|
+
* @param {Object=} options additional properties
|
|
15
|
+
* @param {string=} options.type the MIME type of the content
|
|
16
|
+
* @param {number=} options.lastModified the last modified timestamp
|
|
17
|
+
* @returns a {@link File} with the given properties
|
|
18
|
+
*/
|
|
19
|
+
export async function toFile(
|
|
20
|
+
bits: ToFileInput,
|
|
21
|
+
name?: string | null | undefined,
|
|
22
|
+
options: FilePropertyBag | undefined = {},
|
|
23
|
+
): Promise<FileLike> {
|
|
24
|
+
const args = await newFileArgs(bits, name, options);
|
|
25
|
+
return new File(args.bits as BlobPart[], args.name, args.options);
|
|
26
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { ReadStream } from 'node:fs';
|
|
6
|
+
import {
|
|
7
|
+
BlobPart,
|
|
8
|
+
Uploadable,
|
|
9
|
+
BlobLike,
|
|
10
|
+
FileLike,
|
|
11
|
+
ResponseLike,
|
|
12
|
+
isBlobLike,
|
|
13
|
+
isFileLike,
|
|
14
|
+
isResponseLike,
|
|
15
|
+
} from './uploadable';
|
|
16
|
+
|
|
17
|
+
export { BlobPart, BlobLike, FileLike, ResponseLike, isBlobLike, Uploadable };
|
|
18
|
+
|
|
19
|
+
export const isUploadable = (value: any): value is Uploadable => {
|
|
20
|
+
return isFileLike(value) || isResponseLike(value) || value instanceof ReadStream;
|
|
21
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type * as fs from 'node:fs';
|
|
6
|
+
import type { toFile } from 'openai/_shims/toFile'; // eslint-disable-line
|
|
7
|
+
|
|
8
|
+
export type BlobPart = string | ArrayBuffer | ArrayBufferView | BlobLike | Uint8Array | DataView;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Typically, this is a native "File" class.
|
|
12
|
+
*
|
|
13
|
+
* We provide the {@link toFile} utility to convert a variety of objects
|
|
14
|
+
* into the File class.
|
|
15
|
+
*
|
|
16
|
+
* For convenience, you can also pass a fetch Response
|
|
17
|
+
* or, on Node, the result of {@link fs.createReadStream}('./myfile').
|
|
18
|
+
*/
|
|
19
|
+
export type Uploadable = FileLike | ResponseLike | fs.ReadStream;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Intended to match web.Blob, node.Blob, node-fetch.Blob, etc.
|
|
23
|
+
*/
|
|
24
|
+
export interface BlobLike {
|
|
25
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
|
|
26
|
+
readonly size: number;
|
|
27
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
|
|
28
|
+
readonly type: string;
|
|
29
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
|
|
30
|
+
text(): Promise<string>;
|
|
31
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
|
|
32
|
+
slice(start?: number, end?: number): BlobLike;
|
|
33
|
+
// unfortunately @types/node-fetch@^2.6.4 doesn't type the arrayBuffer method
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Intended to match web.File, node.File, node-fetch.File, etc.
|
|
38
|
+
*/
|
|
39
|
+
export interface FileLike extends BlobLike {
|
|
40
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
|
|
41
|
+
readonly lastModified: number;
|
|
42
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
|
|
43
|
+
readonly name: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Intended to match web.Response, node.Response, node-fetch.Response, etc.
|
|
48
|
+
*/
|
|
49
|
+
export interface ResponseLike {
|
|
50
|
+
url: string;
|
|
51
|
+
blob(): Promise<BlobLike>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const isResponseLike = (value: any): value is ResponseLike =>
|
|
55
|
+
value != null &&
|
|
56
|
+
typeof value === 'object' &&
|
|
57
|
+
typeof value.url === 'string' &&
|
|
58
|
+
typeof value.blob === 'function';
|
|
59
|
+
|
|
60
|
+
export const isFileLike = (value: any): value is FileLike =>
|
|
61
|
+
value != null &&
|
|
62
|
+
typeof value === 'object' &&
|
|
63
|
+
typeof value.name === 'string' &&
|
|
64
|
+
typeof value.lastModified === 'number' &&
|
|
65
|
+
isBlobLike(value);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The BlobLike type omits arrayBuffer() because @types/node-fetch@^2.6.4 lacks it; but this check
|
|
69
|
+
* adds the arrayBuffer() method type because it is available and used at runtime
|
|
70
|
+
*/
|
|
71
|
+
export const isBlobLike = (value: any): value is BlobLike & { arrayBuffer(): Promise<ArrayBuffer> } =>
|
|
72
|
+
value != null &&
|
|
73
|
+
typeof value === 'object' &&
|
|
74
|
+
typeof value.size === 'number' &&
|
|
75
|
+
typeof value.type === 'string' &&
|
|
76
|
+
typeof value.text === 'function' &&
|
|
77
|
+
typeof value.slice === 'function' &&
|
|
78
|
+
typeof value.arrayBuffer === 'function';
|
|
79
|
+
|
|
80
|
+
export const isUploadable = (value: any): value is Uploadable => {
|
|
81
|
+
return isFileLike(value) || isResponseLike(value);
|
|
82
|
+
};
|