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.
Files changed (461) hide show
  1. package/README.md +39 -18
  2. package/_shims/agent.node.ts +20 -0
  3. package/_shims/agent.ts +12 -0
  4. package/_shims/fetch.node.ts +11 -0
  5. package/_shims/fetch.ts +32 -0
  6. package/_shims/fileFromPath.node.ts +30 -0
  7. package/_shims/fileFromPath.ts +30 -0
  8. package/_shims/formdata.node.ts +9 -0
  9. package/_shims/formdata.ts +22 -0
  10. package/_shims/getMultipartRequestOptions.node.ts +24 -0
  11. package/_shims/getMultipartRequestOptions.ts +13 -0
  12. package/_shims/newFileArgs.ts +94 -0
  13. package/_shims/toFile.node.ts +25 -0
  14. package/_shims/toFile.ts +26 -0
  15. package/_shims/uploadable.node.ts +21 -0
  16. package/_shims/uploadable.ts +82 -0
  17. package/core.ts +34 -87
  18. package/dist/cjs/_shims/agent.d.ts +9 -0
  19. package/dist/cjs/_shims/agent.d.ts.map +1 -0
  20. package/dist/cjs/_shims/agent.js +14 -0
  21. package/dist/cjs/_shims/agent.js.map +1 -0
  22. package/dist/cjs/_shims/agent.node.d.ts +6 -0
  23. package/dist/cjs/_shims/agent.node.d.ts.map +1 -0
  24. package/dist/cjs/_shims/agent.node.js +28 -0
  25. package/dist/cjs/_shims/agent.node.js.map +1 -0
  26. package/dist/cjs/_shims/fetch.d.ts +19 -0
  27. package/dist/cjs/_shims/fetch.d.ts.map +1 -0
  28. package/dist/cjs/_shims/fetch.js +24 -0
  29. package/dist/cjs/_shims/fetch.js.map +1 -0
  30. package/dist/cjs/_shims/fetch.node.d.ts +9 -0
  31. package/dist/cjs/_shims/fetch.node.d.ts.map +1 -0
  32. package/dist/cjs/_shims/fetch.node.js +68 -0
  33. package/dist/cjs/_shims/fetch.node.js.map +1 -0
  34. package/dist/cjs/_shims/fileFromPath.d.ts +23 -0
  35. package/dist/cjs/_shims/fileFromPath.d.ts.map +1 -0
  36. package/dist/cjs/{tests/api-resources/moderations.test.js → _shims/fileFromPath.js} +16 -22
  37. package/dist/cjs/_shims/fileFromPath.js.map +1 -0
  38. package/dist/cjs/_shims/fileFromPath.node.d.ts +18 -0
  39. package/dist/cjs/_shims/fileFromPath.node.d.ts.map +1 -0
  40. package/dist/cjs/{tests/form.test.js → _shims/fileFromPath.node.js} +16 -27
  41. package/dist/cjs/_shims/fileFromPath.node.js.map +1 -0
  42. package/dist/cjs/_shims/formdata.d.ts +13 -0
  43. package/dist/cjs/_shims/formdata.d.ts.map +1 -0
  44. package/dist/cjs/_shims/formdata.js +20 -0
  45. package/dist/cjs/_shims/formdata.js.map +1 -0
  46. package/dist/cjs/_shims/formdata.node.d.ts +7 -0
  47. package/dist/cjs/_shims/formdata.node.d.ts.map +1 -0
  48. package/dist/cjs/_shims/formdata.node.js +27 -0
  49. package/dist/cjs/_shims/formdata.node.js.map +1 -0
  50. package/dist/cjs/_shims/getMultipartRequestOptions.d.ts +10 -0
  51. package/dist/cjs/_shims/getMultipartRequestOptions.d.ts.map +1 -0
  52. package/dist/cjs/_shims/getMultipartRequestOptions.js +44 -0
  53. package/dist/cjs/_shims/getMultipartRequestOptions.js.map +1 -0
  54. package/dist/cjs/_shims/getMultipartRequestOptions.node.d.ts +10 -0
  55. package/dist/cjs/_shims/getMultipartRequestOptions.node.d.ts.map +1 -0
  56. package/dist/cjs/_shims/getMultipartRequestOptions.node.js +52 -0
  57. package/dist/cjs/_shims/getMultipartRequestOptions.node.js.map +1 -0
  58. package/dist/cjs/_shims/newFileArgs.d.ts +19 -0
  59. package/dist/cjs/_shims/newFileArgs.d.ts.map +1 -0
  60. package/dist/cjs/_shims/newFileArgs.js +169 -0
  61. package/dist/cjs/_shims/newFileArgs.js.map +1 -0
  62. package/dist/cjs/_shims/toFile.d.ts +21 -0
  63. package/dist/cjs/_shims/toFile.d.ts.map +1 -0
  64. package/dist/cjs/_shims/toFile.js +56 -0
  65. package/dist/cjs/_shims/toFile.js.map +1 -0
  66. package/dist/cjs/_shims/toFile.node.d.ts +20 -0
  67. package/dist/cjs/_shims/toFile.node.d.ts.map +1 -0
  68. package/dist/cjs/_shims/toFile.node.js +56 -0
  69. package/dist/cjs/_shims/toFile.node.js.map +1 -0
  70. package/dist/cjs/_shims/uploadable.d.ts +56 -0
  71. package/dist/cjs/_shims/uploadable.d.ts.map +1 -0
  72. package/dist/cjs/_shims/uploadable.js +37 -0
  73. package/dist/cjs/_shims/uploadable.js.map +1 -0
  74. package/dist/cjs/_shims/uploadable.node.d.ts +7 -0
  75. package/dist/cjs/_shims/uploadable.node.d.ts.map +1 -0
  76. package/dist/cjs/_shims/uploadable.node.js +23 -0
  77. package/dist/cjs/_shims/uploadable.node.js.map +1 -0
  78. package/dist/cjs/core.d.ts +6 -17
  79. package/dist/cjs/core.d.ts.map +1 -1
  80. package/dist/cjs/core.js +45 -74
  81. package/dist/cjs/core.js.map +1 -1
  82. package/dist/cjs/index.d.ts +19 -13
  83. package/dist/cjs/index.d.ts.map +1 -1
  84. package/dist/cjs/index.js +20 -6
  85. package/dist/cjs/index.js.map +1 -1
  86. package/dist/cjs/resources/audio/audio.d.ts +9 -0
  87. package/dist/cjs/resources/audio/audio.d.ts.map +1 -1
  88. package/dist/cjs/resources/audio/audio.js +45 -0
  89. package/dist/cjs/resources/audio/audio.js.map +1 -1
  90. package/dist/cjs/resources/audio/index.d.ts +3 -2
  91. package/dist/cjs/resources/audio/index.d.ts.map +1 -1
  92. package/dist/cjs/resources/audio/index.js +22 -0
  93. package/dist/cjs/resources/audio/index.js.map +1 -1
  94. package/dist/cjs/resources/audio/transcriptions.d.ts +8 -3
  95. package/dist/cjs/resources/audio/transcriptions.d.ts.map +1 -1
  96. package/dist/cjs/resources/audio/transcriptions.js +38 -4
  97. package/dist/cjs/resources/audio/transcriptions.js.map +1 -1
  98. package/dist/cjs/resources/audio/translations.d.ts +9 -4
  99. package/dist/cjs/resources/audio/translations.d.ts.map +1 -1
  100. package/dist/cjs/resources/audio/translations.js +39 -5
  101. package/dist/cjs/resources/audio/translations.js.map +1 -1
  102. package/dist/cjs/resources/chat/chat.d.ts +7 -0
  103. package/dist/cjs/resources/chat/chat.d.ts.map +1 -1
  104. package/dist/cjs/resources/chat/chat.js +44 -0
  105. package/dist/cjs/resources/chat/chat.js.map +1 -1
  106. package/dist/cjs/resources/chat/completions.d.ts +49 -25
  107. package/dist/cjs/resources/chat/completions.d.ts.map +1 -1
  108. package/dist/cjs/resources/chat/completions.js +1 -0
  109. package/dist/cjs/resources/chat/completions.js.map +1 -1
  110. package/dist/cjs/resources/chat/index.d.ts +2 -1
  111. package/dist/cjs/resources/chat/index.d.ts.map +1 -1
  112. package/dist/cjs/resources/chat/index.js +15 -0
  113. package/dist/cjs/resources/chat/index.js.map +1 -1
  114. package/dist/cjs/resources/completions.d.ts +24 -2
  115. package/dist/cjs/resources/completions.d.ts.map +1 -1
  116. package/dist/cjs/resources/completions.js +1 -0
  117. package/dist/cjs/resources/completions.js.map +1 -1
  118. package/dist/cjs/resources/edits.d.ts +10 -5
  119. package/dist/cjs/resources/edits.d.ts.map +1 -1
  120. package/dist/cjs/resources/edits.js +1 -0
  121. package/dist/cjs/resources/edits.js.map +1 -1
  122. package/dist/cjs/resources/embeddings.d.ts +6 -1
  123. package/dist/cjs/resources/embeddings.d.ts.map +1 -1
  124. package/dist/cjs/resources/embeddings.js +1 -0
  125. package/dist/cjs/resources/embeddings.js.map +1 -1
  126. package/dist/cjs/resources/files.d.ts +22 -13
  127. package/dist/cjs/resources/files.d.ts.map +1 -1
  128. package/dist/cjs/resources/files.js +38 -1
  129. package/dist/cjs/resources/files.js.map +1 -1
  130. package/dist/cjs/resources/fine-tunes.d.ts +13 -4
  131. package/dist/cjs/resources/fine-tunes.d.ts.map +1 -1
  132. package/dist/cjs/resources/fine-tunes.js +1 -0
  133. package/dist/cjs/resources/fine-tunes.js.map +1 -1
  134. package/dist/cjs/resources/images.d.ts +12 -4
  135. package/dist/cjs/resources/images.d.ts.map +1 -1
  136. package/dist/cjs/resources/images.js +44 -8
  137. package/dist/cjs/resources/images.js.map +1 -1
  138. package/dist/cjs/resources/index.d.ts +4 -6
  139. package/dist/cjs/resources/index.d.ts.map +1 -1
  140. package/dist/cjs/resources/index.js +8 -24
  141. package/dist/cjs/resources/index.js.map +1 -1
  142. package/dist/cjs/resources/models.d.ts +12 -6
  143. package/dist/cjs/resources/models.d.ts.map +1 -1
  144. package/dist/cjs/resources/models.js +1 -0
  145. package/dist/cjs/resources/models.js.map +1 -1
  146. package/dist/cjs/resources/moderations.d.ts +7 -1
  147. package/dist/cjs/resources/moderations.d.ts.map +1 -1
  148. package/dist/cjs/resources/moderations.js +1 -0
  149. package/dist/cjs/resources/moderations.js.map +1 -1
  150. package/dist/cjs/streaming.d.ts +1 -2
  151. package/dist/cjs/streaming.d.ts.map +1 -1
  152. package/dist/cjs/streaming.js +2 -0
  153. package/dist/cjs/streaming.js.map +1 -1
  154. package/dist/cjs/uploads.d.ts +24 -0
  155. package/dist/cjs/uploads.d.ts.map +1 -0
  156. package/dist/cjs/uploads.js +121 -0
  157. package/dist/cjs/uploads.js.map +1 -0
  158. package/dist/cjs/version.d.ts +1 -1
  159. package/dist/cjs/version.d.ts.map +1 -1
  160. package/dist/cjs/version.js +1 -1
  161. package/dist/cjs/version.js.map +1 -1
  162. package/dist/esm/_shims/agent.d.ts +9 -0
  163. package/dist/esm/_shims/agent.d.ts.map +1 -0
  164. package/dist/esm/_shims/agent.js +10 -0
  165. package/dist/esm/_shims/agent.js.map +1 -0
  166. package/dist/esm/_shims/agent.node.d.ts +6 -0
  167. package/dist/esm/_shims/agent.node.d.ts.map +1 -0
  168. package/dist/esm/_shims/agent.node.js +16 -0
  169. package/dist/esm/_shims/agent.node.js.map +1 -0
  170. package/dist/esm/_shims/fetch.d.ts +19 -0
  171. package/dist/esm/_shims/fetch.d.ts.map +1 -0
  172. package/dist/esm/_shims/fetch.js +18 -0
  173. package/dist/esm/_shims/fetch.js.map +1 -0
  174. package/dist/esm/_shims/fetch.node.d.ts +9 -0
  175. package/dist/esm/_shims/fetch.node.d.ts.map +1 -0
  176. package/dist/esm/_shims/fetch.node.js +7 -0
  177. package/dist/esm/_shims/fetch.node.js.map +1 -0
  178. package/dist/esm/_shims/fileFromPath.d.ts +23 -0
  179. package/dist/esm/_shims/fileFromPath.d.ts.map +1 -0
  180. package/dist/esm/_shims/fileFromPath.js +12 -0
  181. package/dist/esm/_shims/fileFromPath.js.map +1 -0
  182. package/dist/esm/_shims/fileFromPath.node.d.ts +18 -0
  183. package/dist/esm/_shims/fileFromPath.node.d.ts.map +1 -0
  184. package/dist/esm/_shims/fileFromPath.node.js +13 -0
  185. package/dist/esm/_shims/fileFromPath.node.js.map +1 -0
  186. package/dist/esm/_shims/formdata.d.ts +13 -0
  187. package/dist/esm/_shims/formdata.d.ts.map +1 -0
  188. package/dist/esm/_shims/formdata.js +15 -0
  189. package/dist/esm/_shims/formdata.js.map +1 -0
  190. package/dist/esm/_shims/formdata.node.d.ts +7 -0
  191. package/dist/esm/_shims/formdata.node.d.ts.map +1 -0
  192. package/dist/esm/_shims/formdata.node.js +7 -0
  193. package/dist/esm/_shims/formdata.node.js.map +1 -0
  194. package/dist/esm/_shims/getMultipartRequestOptions.d.ts +10 -0
  195. package/dist/esm/_shims/getMultipartRequestOptions.d.ts.map +1 -0
  196. package/dist/esm/_shims/getMultipartRequestOptions.js +7 -0
  197. package/dist/esm/_shims/getMultipartRequestOptions.js.map +1 -0
  198. package/dist/esm/_shims/getMultipartRequestOptions.node.d.ts +10 -0
  199. package/dist/esm/_shims/getMultipartRequestOptions.node.d.ts.map +1 -0
  200. package/dist/esm/_shims/getMultipartRequestOptions.node.js +17 -0
  201. package/dist/esm/_shims/getMultipartRequestOptions.node.js.map +1 -0
  202. package/dist/esm/_shims/newFileArgs.d.ts +19 -0
  203. package/dist/esm/_shims/newFileArgs.d.ts.map +1 -0
  204. package/dist/esm/_shims/newFileArgs.js +66 -0
  205. package/dist/esm/_shims/newFileArgs.js.map +1 -0
  206. package/dist/esm/_shims/toFile.d.ts +21 -0
  207. package/dist/esm/_shims/toFile.d.ts.map +1 -0
  208. package/dist/esm/_shims/toFile.js +19 -0
  209. package/dist/esm/_shims/toFile.js.map +1 -0
  210. package/dist/esm/_shims/toFile.node.d.ts +20 -0
  211. package/dist/esm/_shims/toFile.node.d.ts.map +1 -0
  212. package/dist/esm/_shims/toFile.node.js +19 -0
  213. package/dist/esm/_shims/toFile.node.js.map +1 -0
  214. package/dist/esm/_shims/uploadable.d.ts +56 -0
  215. package/dist/esm/_shims/uploadable.d.ts.map +1 -0
  216. package/dist/esm/_shims/uploadable.js +30 -0
  217. package/dist/esm/_shims/uploadable.js.map +1 -0
  218. package/dist/esm/_shims/uploadable.node.d.ts +7 -0
  219. package/dist/esm/_shims/uploadable.node.d.ts.map +1 -0
  220. package/dist/esm/_shims/uploadable.node.js +10 -0
  221. package/dist/esm/_shims/uploadable.node.js.map +1 -0
  222. package/dist/esm/check-version.d.ts +2 -0
  223. package/dist/esm/check-version.d.ts.map +1 -0
  224. package/dist/esm/check-version.js +16 -0
  225. package/dist/esm/check-version.js.map +1 -0
  226. package/dist/esm/core.d.ts +221 -0
  227. package/dist/esm/core.d.ts.map +1 -0
  228. package/dist/esm/core.js +556 -0
  229. package/dist/esm/core.js.map +1 -0
  230. package/dist/esm/error.d.ts +51 -0
  231. package/dist/esm/error.d.ts.map +1 -0
  232. package/dist/esm/error.js +104 -0
  233. package/dist/esm/error.js.map +1 -0
  234. package/dist/esm/fetch-polyfill.d.ts +6 -0
  235. package/dist/esm/fetch-polyfill.d.ts.map +1 -0
  236. package/dist/esm/fetch-polyfill.js +52 -0
  237. package/dist/esm/fetch-polyfill.js.map +1 -0
  238. package/dist/esm/index.d.ts +106 -0
  239. package/dist/esm/index.d.ts.map +1 -0
  240. package/dist/esm/index.js +93 -0
  241. package/dist/esm/index.js.map +1 -0
  242. package/dist/esm/package.json +3 -0
  243. package/dist/esm/resource.d.ts +12 -0
  244. package/dist/esm/resource.d.ts.map +1 -0
  245. package/dist/esm/resource.js +13 -0
  246. package/dist/esm/resource.js.map +1 -0
  247. package/dist/esm/resources/answers.d.ts +150 -0
  248. package/dist/esm/resources/answers.d.ts.map +1 -0
  249. package/dist/esm/resources/answers.js +16 -0
  250. package/dist/esm/resources/answers.js.map +1 -0
  251. package/dist/esm/resources/audio/audio.d.ts +17 -0
  252. package/dist/esm/resources/audio/audio.d.ts.map +1 -0
  253. package/dist/esm/resources/audio/audio.js +17 -0
  254. package/dist/esm/resources/audio/audio.js.map +1 -0
  255. package/dist/esm/resources/audio/index.d.ts +4 -0
  256. package/dist/esm/resources/audio/index.d.ts.map +1 -0
  257. package/dist/esm/resources/audio/index.js +5 -0
  258. package/dist/esm/resources/audio/index.js.map +1 -0
  259. package/dist/esm/resources/audio/transcriptions.d.ts +57 -0
  260. package/dist/esm/resources/audio/transcriptions.d.ts.map +1 -0
  261. package/dist/esm/resources/audio/transcriptions.js +13 -0
  262. package/dist/esm/resources/audio/transcriptions.js.map +1 -0
  263. package/dist/esm/resources/audio/translations.d.ts +51 -0
  264. package/dist/esm/resources/audio/translations.d.ts.map +1 -0
  265. package/dist/esm/resources/audio/translations.js +13 -0
  266. package/dist/esm/resources/audio/translations.js.map +1 -0
  267. package/dist/esm/resources/chat/chat.d.ts +13 -0
  268. package/dist/esm/resources/chat/chat.d.ts.map +1 -0
  269. package/dist/esm/resources/chat/chat.js +14 -0
  270. package/dist/esm/resources/chat/chat.js.map +1 -0
  271. package/dist/esm/resources/chat/completions.d.ts +496 -0
  272. package/dist/esm/resources/chat/completions.d.ts.map +1 -0
  273. package/dist/esm/resources/chat/completions.js +9 -0
  274. package/dist/esm/resources/chat/completions.js.map +1 -0
  275. package/dist/esm/resources/chat/index.d.ts +3 -0
  276. package/dist/esm/resources/chat/index.d.ts.map +1 -0
  277. package/dist/esm/resources/chat/index.js +4 -0
  278. package/dist/esm/resources/chat/index.js.map +1 -0
  279. package/dist/esm/resources/classifications.d.ts +142 -0
  280. package/dist/esm/resources/classifications.d.ts.map +1 -0
  281. package/dist/esm/resources/classifications.js +20 -0
  282. package/dist/esm/resources/classifications.js.map +1 -0
  283. package/dist/esm/resources/completions.d.ts +335 -0
  284. package/dist/esm/resources/completions.d.ts.map +1 -0
  285. package/dist/esm/resources/completions.js +9 -0
  286. package/dist/esm/resources/completions.js.map +1 -0
  287. package/dist/esm/resources/edits.d.ts +76 -0
  288. package/dist/esm/resources/edits.d.ts.map +1 -0
  289. package/dist/esm/resources/edits.js +12 -0
  290. package/dist/esm/resources/edits.js.map +1 -0
  291. package/dist/esm/resources/embeddings.d.ts +54 -0
  292. package/dist/esm/resources/embeddings.d.ts.map +1 -0
  293. package/dist/esm/resources/embeddings.js +12 -0
  294. package/dist/esm/resources/embeddings.js.map +1 -0
  295. package/dist/esm/resources/files.d.ts +78 -0
  296. package/dist/esm/resources/files.d.ts.map +1 -0
  297. package/dist/esm/resources/files.js +43 -0
  298. package/dist/esm/resources/files.js.map +1 -0
  299. package/dist/esm/resources/fine-tunes.d.ts +225 -0
  300. package/dist/esm/resources/fine-tunes.d.ts.map +1 -0
  301. package/dist/esm/resources/fine-tunes.js +44 -0
  302. package/dist/esm/resources/fine-tunes.js.map +1 -0
  303. package/dist/esm/resources/images.d.ts +129 -0
  304. package/dist/esm/resources/images.d.ts.map +1 -0
  305. package/dist/esm/resources/images.js +25 -0
  306. package/dist/esm/resources/images.js.map +1 -0
  307. package/dist/esm/resources/index.d.ts +33 -0
  308. package/dist/esm/resources/index.d.ts.map +1 -0
  309. package/dist/esm/resources/index.js +12 -0
  310. package/dist/esm/resources/index.js.map +1 -0
  311. package/dist/esm/resources/models.d.ts +40 -0
  312. package/dist/esm/resources/models.d.ts.map +1 -0
  313. package/dist/esm/resources/models.js +26 -0
  314. package/dist/esm/resources/models.js.map +1 -0
  315. package/dist/esm/resources/moderations.d.ts +65 -0
  316. package/dist/esm/resources/moderations.d.ts.map +1 -0
  317. package/dist/esm/resources/moderations.js +12 -0
  318. package/dist/esm/resources/moderations.js.map +1 -0
  319. package/dist/esm/streaming.d.ts +12 -0
  320. package/dist/esm/streaming.d.ts.map +1 -0
  321. package/dist/esm/streaming.js +155 -0
  322. package/dist/esm/streaming.js.map +1 -0
  323. package/dist/esm/uploads.d.ts +24 -0
  324. package/dist/esm/uploads.d.ts.map +1 -0
  325. package/dist/esm/uploads.js +61 -0
  326. package/dist/esm/uploads.js.map +1 -0
  327. package/dist/esm/version.d.ts +2 -0
  328. package/dist/esm/version.d.ts.map +1 -0
  329. package/dist/esm/version.js +2 -0
  330. package/dist/esm/version.js.map +1 -0
  331. package/error.ts +1 -1
  332. package/index.ts +22 -18
  333. package/package.json +85 -5
  334. package/resources/audio/audio.ts +11 -0
  335. package/resources/audio/index.ts +3 -2
  336. package/resources/audio/transcriptions.ts +11 -6
  337. package/resources/audio/translations.ts +12 -7
  338. package/resources/chat/chat.ts +8 -0
  339. package/resources/chat/completions.ts +51 -26
  340. package/resources/chat/index.ts +2 -1
  341. package/resources/completions.ts +25 -2
  342. package/resources/edits.ts +11 -5
  343. package/resources/embeddings.ts +7 -1
  344. package/resources/files.ts +32 -19
  345. package/resources/fine-tunes.ts +14 -4
  346. package/resources/images.ts +20 -9
  347. package/resources/index.ts +4 -6
  348. package/resources/models.ts +16 -9
  349. package/resources/moderations.ts +8 -1
  350. package/streaming.ts +3 -1
  351. package/uploads.ts +81 -0
  352. package/version.ts +1 -1
  353. package/.eslintrc.js +0 -10
  354. package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -56
  355. package/.github/ISSUE_TEMPLATE/config.yml +0 -7
  356. package/.github/ISSUE_TEMPLATE/feature_request.yml +0 -20
  357. package/.github/workflows/test.yml +0 -24
  358. package/.prettierignore +0 -1
  359. package/.prettierrc +0 -6
  360. package/.stats.yml +0 -1
  361. package/api.md +0 -157
  362. package/bin/check-test-server +0 -50
  363. package/build +0 -12
  364. package/dist/cjs/examples/demo.d.ts +0 -3
  365. package/dist/cjs/examples/demo.d.ts.map +0 -1
  366. package/dist/cjs/examples/demo.js +0 -119
  367. package/dist/cjs/examples/demo.js.map +0 -1
  368. package/dist/cjs/examples/errors.d.ts +0 -3
  369. package/dist/cjs/examples/errors.d.ts.map +0 -1
  370. package/dist/cjs/examples/errors.js +0 -100
  371. package/dist/cjs/examples/errors.js.map +0 -1
  372. package/dist/cjs/examples/fine-tunes.d.ts +0 -10
  373. package/dist/cjs/examples/fine-tunes.d.ts.map +0 -1
  374. package/dist/cjs/examples/fine-tunes.js +0 -160
  375. package/dist/cjs/examples/fine-tunes.js.map +0 -1
  376. package/dist/cjs/tests/api-resources/answers.test.d.ts +0 -2
  377. package/dist/cjs/tests/api-resources/answers.test.d.ts.map +0 -1
  378. package/dist/cjs/tests/api-resources/answers.test.js +0 -279
  379. package/dist/cjs/tests/api-resources/answers.test.js.map +0 -1
  380. package/dist/cjs/tests/api-resources/audio/transcriptions.test.d.ts +0 -2
  381. package/dist/cjs/tests/api-resources/audio/transcriptions.test.d.ts.map +0 -1
  382. package/dist/cjs/tests/api-resources/audio/transcriptions.test.js +0 -65
  383. package/dist/cjs/tests/api-resources/audio/transcriptions.test.js.map +0 -1
  384. package/dist/cjs/tests/api-resources/audio/translations.test.d.ts +0 -2
  385. package/dist/cjs/tests/api-resources/audio/translations.test.d.ts.map +0 -1
  386. package/dist/cjs/tests/api-resources/audio/translations.test.js +0 -64
  387. package/dist/cjs/tests/api-resources/audio/translations.test.js.map +0 -1
  388. package/dist/cjs/tests/api-resources/chat/completions.test.d.ts +0 -2
  389. package/dist/cjs/tests/api-resources/chat/completions.test.d.ts.map +0 -1
  390. package/dist/cjs/tests/api-resources/chat/completions.test.js +0 -77
  391. package/dist/cjs/tests/api-resources/chat/completions.test.js.map +0 -1
  392. package/dist/cjs/tests/api-resources/classifications.test.d.ts +0 -2
  393. package/dist/cjs/tests/api-resources/classifications.test.d.ts.map +0 -1
  394. package/dist/cjs/tests/api-resources/classifications.test.js +0 -73
  395. package/dist/cjs/tests/api-resources/classifications.test.js.map +0 -1
  396. package/dist/cjs/tests/api-resources/completions.test.d.ts +0 -2
  397. package/dist/cjs/tests/api-resources/completions.test.d.ts.map +0 -1
  398. package/dist/cjs/tests/api-resources/completions.test.js +0 -69
  399. package/dist/cjs/tests/api-resources/completions.test.js.map +0 -1
  400. package/dist/cjs/tests/api-resources/edits.test.d.ts +0 -2
  401. package/dist/cjs/tests/api-resources/edits.test.d.ts.map +0 -1
  402. package/dist/cjs/tests/api-resources/edits.test.js +0 -62
  403. package/dist/cjs/tests/api-resources/edits.test.js.map +0 -1
  404. package/dist/cjs/tests/api-resources/embeddings.test.d.ts +0 -2
  405. package/dist/cjs/tests/api-resources/embeddings.test.d.ts.map +0 -1
  406. package/dist/cjs/tests/api-resources/embeddings.test.js +0 -59
  407. package/dist/cjs/tests/api-resources/embeddings.test.js.map +0 -1
  408. package/dist/cjs/tests/api-resources/files.test.d.ts +0 -2
  409. package/dist/cjs/tests/api-resources/files.test.d.ts.map +0 -1
  410. package/dist/cjs/tests/api-resources/files.test.js +0 -107
  411. package/dist/cjs/tests/api-resources/files.test.js.map +0 -1
  412. package/dist/cjs/tests/api-resources/fine-tunes.test.d.ts +0 -2
  413. package/dist/cjs/tests/api-resources/fine-tunes.test.d.ts.map +0 -1
  414. package/dist/cjs/tests/api-resources/fine-tunes.test.js +0 -115
  415. package/dist/cjs/tests/api-resources/fine-tunes.test.js.map +0 -1
  416. package/dist/cjs/tests/api-resources/images.test.d.ts +0 -2
  417. package/dist/cjs/tests/api-resources/images.test.d.ts.map +0 -1
  418. package/dist/cjs/tests/api-resources/images.test.js +0 -100
  419. package/dist/cjs/tests/api-resources/images.test.js.map +0 -1
  420. package/dist/cjs/tests/api-resources/models.test.d.ts +0 -2
  421. package/dist/cjs/tests/api-resources/models.test.d.ts.map +0 -1
  422. package/dist/cjs/tests/api-resources/models.test.js +0 -77
  423. package/dist/cjs/tests/api-resources/models.test.js.map +0 -1
  424. package/dist/cjs/tests/api-resources/moderations.test.d.ts +0 -2
  425. package/dist/cjs/tests/api-resources/moderations.test.d.ts.map +0 -1
  426. package/dist/cjs/tests/api-resources/moderations.test.js.map +0 -1
  427. package/dist/cjs/tests/form.test.d.ts +0 -2
  428. package/dist/cjs/tests/form.test.d.ts.map +0 -1
  429. package/dist/cjs/tests/form.test.js.map +0 -1
  430. package/dist/cjs/tests/index.test.d.ts +0 -2
  431. package/dist/cjs/tests/index.test.d.ts.map +0 -1
  432. package/dist/cjs/tests/index.test.js +0 -75
  433. package/dist/cjs/tests/index.test.js.map +0 -1
  434. package/dist/cjs/tests/responses.test.d.ts +0 -2
  435. package/dist/cjs/tests/responses.test.d.ts.map +0 -1
  436. package/dist/cjs/tests/responses.test.js +0 -60
  437. package/dist/cjs/tests/responses.test.js.map +0 -1
  438. package/examples/demo.ts +0 -28
  439. package/examples/errors.ts +0 -28
  440. package/examples/fine-tune-data.jsonl +0 -3
  441. package/examples/fine-tunes.ts +0 -37
  442. package/jest.config.js +0 -8
  443. package/tests/api-resources/answers.test.ts +0 -242
  444. package/tests/api-resources/audio/transcriptions.test.ts +0 -28
  445. package/tests/api-resources/audio/translations.test.ts +0 -27
  446. package/tests/api-resources/chat/completions.test.ts +0 -40
  447. package/tests/api-resources/classifications.test.ts +0 -36
  448. package/tests/api-resources/completions.test.ts +0 -32
  449. package/tests/api-resources/edits.test.ts +0 -25
  450. package/tests/api-resources/embeddings.test.ts +0 -22
  451. package/tests/api-resources/files.test.ts +0 -64
  452. package/tests/api-resources/fine-tunes.test.ts +0 -78
  453. package/tests/api-resources/images.test.ts +0 -61
  454. package/tests/api-resources/models.test.ts +0 -40
  455. package/tests/api-resources/moderations.test.ts +0 -18
  456. package/tests/form.test.ts +0 -27
  457. package/tests/index.test.ts +0 -76
  458. package/tests/responses.test.ts +0 -25
  459. package/tsconfig.cjs.json +0 -8
  460. package/tsconfig.json +0 -39
  461. 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 openAI = new OpenAI({
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 openAI.completions.create({
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 openAI = new OpenAI({
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 openAI.completions.create(params);
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 as either a `FormData.Blob` or a `FormData.File` instance.
94
+ Request parameters that correspond to file uploads can be passed in many different forms:
95
95
 
96
- We provide a `fileFromPath` helper function to easily create `FormData.File` instances from a given class.
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 OpenAI, { fileFromPath } from 'openai';
102
+ import fs from 'fs';
103
+ import fetch from 'node-fetch';
104
+ import OpenAI, { toFile } from 'openai';
100
105
 
101
- const openAI = new OpenAI();
106
+ const openai = new OpenAI();
102
107
 
103
- const file = await fileFromPath('input.jsonl');
104
- await openAI.files.create({ file: file, purpose: 'fine-tune' });
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 openAI.fineTunes
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 openAI = new OpenAI({
174
+ const openai = new OpenAI({
154
175
  maxRetries: 0, // default is 2
155
176
  });
156
177
 
157
178
  // Or, configure per-request:
158
- openAI.embeddings.create({ model: 'text-similarity-babbage-001',input: 'The food was delicious and the waiter...' }, {
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 openAI = new OpenAI({
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
- openAI.edits.create({ model: 'text-davinci-edit-001',input: 'What day of the wek is it?',instruction: 'Fix the spelling mistakes' }, {
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 openAI = new OpenAI({
217
+ const openai = new OpenAI({
197
218
  httpAgent: new HttpsProxyAgent(process.env.PROXY_URL),
198
219
  });
199
220
 
200
221
  // Override per-request:
201
- openAI.models.list({
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 version 12 or higher.
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
+ };
@@ -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;
@@ -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,9 @@
1
+ /**
2
+ * Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3
+ */
4
+
5
+ import { FormData, File, Blob } from 'formdata-node';
6
+
7
+ export { FormData, File, Blob };
8
+
9
+ export const isPolyfilled = true;
@@ -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
+ }
@@ -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
+ };