notdiamond 1.1.3 → 2.0.0-rc1

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 (409) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/LICENSE +201 -0
  3. package/README.md +449 -105
  4. package/api-promise.d.mts +2 -0
  5. package/api-promise.d.mts.map +1 -0
  6. package/api-promise.d.ts +2 -0
  7. package/api-promise.d.ts.map +1 -0
  8. package/api-promise.js +6 -0
  9. package/api-promise.js.map +1 -0
  10. package/api-promise.mjs +2 -0
  11. package/api-promise.mjs.map +1 -0
  12. package/client.d.mts +206 -0
  13. package/client.d.mts.map +1 -0
  14. package/client.d.ts +206 -0
  15. package/client.d.ts.map +1 -0
  16. package/client.js +479 -0
  17. package/client.js.map +1 -0
  18. package/client.mjs +475 -0
  19. package/client.mjs.map +1 -0
  20. package/core/api-promise.d.mts +46 -0
  21. package/core/api-promise.d.mts.map +1 -0
  22. package/core/api-promise.d.ts +46 -0
  23. package/core/api-promise.d.ts.map +1 -0
  24. package/core/api-promise.js +74 -0
  25. package/core/api-promise.js.map +1 -0
  26. package/core/api-promise.mjs +70 -0
  27. package/core/api-promise.mjs.map +1 -0
  28. package/core/error.d.mts +46 -0
  29. package/core/error.d.mts.map +1 -0
  30. package/core/error.d.ts +46 -0
  31. package/core/error.d.ts.map +1 -0
  32. package/core/error.js +113 -0
  33. package/core/error.js.map +1 -0
  34. package/core/error.mjs +97 -0
  35. package/core/error.mjs.map +1 -0
  36. package/core/resource.d.mts +6 -0
  37. package/core/resource.d.mts.map +1 -0
  38. package/core/resource.d.ts +6 -0
  39. package/core/resource.d.ts.map +1 -0
  40. package/core/resource.js +11 -0
  41. package/core/resource.js.map +1 -0
  42. package/core/resource.mjs +7 -0
  43. package/core/resource.mjs.map +1 -0
  44. package/core/uploads.d.mts +3 -0
  45. package/core/uploads.d.mts.map +1 -0
  46. package/core/uploads.d.ts +3 -0
  47. package/core/uploads.d.ts.map +1 -0
  48. package/core/uploads.js +6 -0
  49. package/core/uploads.js.map +1 -0
  50. package/core/uploads.mjs +2 -0
  51. package/core/uploads.mjs.map +1 -0
  52. package/error.d.mts +2 -0
  53. package/error.d.mts.map +1 -0
  54. package/error.d.ts +2 -0
  55. package/error.d.ts.map +1 -0
  56. package/error.js +6 -0
  57. package/error.js.map +1 -0
  58. package/error.mjs +2 -0
  59. package/error.mjs.map +1 -0
  60. package/index.d.mts +6 -0
  61. package/index.d.mts.map +1 -0
  62. package/index.d.ts +6 -0
  63. package/index.d.ts.map +1 -0
  64. package/index.js +30 -0
  65. package/index.js.map +1 -0
  66. package/index.mjs +7 -0
  67. package/index.mjs.map +1 -0
  68. package/internal/builtin-types.d.mts +73 -0
  69. package/internal/builtin-types.d.mts.map +1 -0
  70. package/internal/builtin-types.d.ts +73 -0
  71. package/internal/builtin-types.d.ts.map +1 -0
  72. package/internal/builtin-types.js +4 -0
  73. package/internal/builtin-types.js.map +1 -0
  74. package/internal/builtin-types.mjs +3 -0
  75. package/internal/builtin-types.mjs.map +1 -0
  76. package/internal/detect-platform.d.mts +15 -0
  77. package/internal/detect-platform.d.mts.map +1 -0
  78. package/internal/detect-platform.d.ts +15 -0
  79. package/internal/detect-platform.d.ts.map +1 -0
  80. package/internal/detect-platform.js +162 -0
  81. package/internal/detect-platform.js.map +1 -0
  82. package/internal/detect-platform.mjs +157 -0
  83. package/internal/detect-platform.mjs.map +1 -0
  84. package/internal/errors.d.mts +3 -0
  85. package/internal/errors.d.mts.map +1 -0
  86. package/internal/errors.d.ts +3 -0
  87. package/internal/errors.d.ts.map +1 -0
  88. package/internal/errors.js +41 -0
  89. package/internal/errors.js.map +1 -0
  90. package/internal/errors.mjs +36 -0
  91. package/internal/errors.mjs.map +1 -0
  92. package/internal/headers.d.mts +20 -0
  93. package/internal/headers.d.mts.map +1 -0
  94. package/internal/headers.d.ts +20 -0
  95. package/internal/headers.d.ts.map +1 -0
  96. package/internal/headers.js +79 -0
  97. package/internal/headers.js.map +1 -0
  98. package/internal/headers.mjs +74 -0
  99. package/internal/headers.mjs.map +1 -0
  100. package/internal/parse.d.mts +12 -0
  101. package/internal/parse.d.mts.map +1 -0
  102. package/internal/parse.d.ts +12 -0
  103. package/internal/parse.d.ts.map +1 -0
  104. package/internal/parse.js +35 -0
  105. package/internal/parse.js.map +1 -0
  106. package/internal/parse.mjs +32 -0
  107. package/internal/parse.mjs.map +1 -0
  108. package/internal/qs/formats.d.mts +7 -0
  109. package/internal/qs/formats.d.mts.map +1 -0
  110. package/internal/qs/formats.d.ts +7 -0
  111. package/internal/qs/formats.d.ts.map +1 -0
  112. package/internal/qs/formats.js +13 -0
  113. package/internal/qs/formats.js.map +1 -0
  114. package/internal/qs/formats.mjs +9 -0
  115. package/internal/qs/formats.mjs.map +1 -0
  116. package/internal/qs/index.d.mts +10 -0
  117. package/internal/qs/index.d.mts.map +1 -0
  118. package/internal/qs/index.d.ts +10 -0
  119. package/internal/qs/index.d.ts.map +1 -0
  120. package/internal/qs/index.js +14 -0
  121. package/internal/qs/index.js.map +1 -0
  122. package/internal/qs/index.mjs +10 -0
  123. package/internal/qs/index.mjs.map +1 -0
  124. package/internal/qs/stringify.d.mts +3 -0
  125. package/internal/qs/stringify.d.mts.map +1 -0
  126. package/internal/qs/stringify.d.ts +3 -0
  127. package/internal/qs/stringify.d.ts.map +1 -0
  128. package/internal/qs/stringify.js +277 -0
  129. package/internal/qs/stringify.js.map +1 -0
  130. package/internal/qs/stringify.mjs +274 -0
  131. package/internal/qs/stringify.mjs.map +1 -0
  132. package/internal/qs/types.d.mts +57 -0
  133. package/internal/qs/types.d.mts.map +1 -0
  134. package/internal/qs/types.d.ts +57 -0
  135. package/internal/qs/types.d.ts.map +1 -0
  136. package/internal/qs/types.js +3 -0
  137. package/internal/qs/types.js.map +1 -0
  138. package/internal/qs/types.mjs +2 -0
  139. package/internal/qs/types.mjs.map +1 -0
  140. package/internal/qs/utils.d.mts +15 -0
  141. package/internal/qs/utils.d.mts.map +1 -0
  142. package/internal/qs/utils.d.ts +15 -0
  143. package/internal/qs/utils.d.ts.map +1 -0
  144. package/internal/qs/utils.js +230 -0
  145. package/internal/qs/utils.js.map +1 -0
  146. package/internal/qs/utils.mjs +217 -0
  147. package/internal/qs/utils.mjs.map +1 -0
  148. package/internal/request-options.d.mts +75 -0
  149. package/internal/request-options.d.mts.map +1 -0
  150. package/internal/request-options.d.ts +75 -0
  151. package/internal/request-options.d.ts.map +1 -0
  152. package/internal/request-options.js +14 -0
  153. package/internal/request-options.js.map +1 -0
  154. package/internal/request-options.mjs +10 -0
  155. package/internal/request-options.mjs.map +1 -0
  156. package/internal/shim-types.d.mts +17 -0
  157. package/internal/shim-types.d.mts.map +1 -0
  158. package/internal/shim-types.d.ts +17 -0
  159. package/internal/shim-types.d.ts.map +1 -0
  160. package/internal/shim-types.js +4 -0
  161. package/internal/shim-types.js.map +1 -0
  162. package/internal/shim-types.mjs +3 -0
  163. package/internal/shim-types.mjs.map +1 -0
  164. package/internal/shims.d.mts +20 -0
  165. package/internal/shims.d.mts.map +1 -0
  166. package/internal/shims.d.ts +20 -0
  167. package/internal/shims.d.ts.map +1 -0
  168. package/internal/shims.js +92 -0
  169. package/internal/shims.js.map +1 -0
  170. package/internal/shims.mjs +85 -0
  171. package/internal/shims.mjs.map +1 -0
  172. package/internal/to-file.d.mts +45 -0
  173. package/internal/to-file.d.mts.map +1 -0
  174. package/internal/to-file.d.ts +45 -0
  175. package/internal/to-file.d.ts.map +1 -0
  176. package/internal/to-file.js +91 -0
  177. package/internal/to-file.js.map +1 -0
  178. package/internal/to-file.mjs +88 -0
  179. package/internal/to-file.mjs.map +1 -0
  180. package/internal/tslib.js +81 -0
  181. package/internal/tslib.mjs +17 -0
  182. package/internal/types.d.mts +69 -0
  183. package/internal/types.d.mts.map +1 -0
  184. package/internal/types.d.ts +69 -0
  185. package/internal/types.d.ts.map +1 -0
  186. package/internal/types.js +4 -0
  187. package/internal/types.js.map +1 -0
  188. package/internal/types.mjs +3 -0
  189. package/internal/types.mjs.map +1 -0
  190. package/internal/uploads.d.mts +42 -0
  191. package/internal/uploads.d.mts.map +1 -0
  192. package/internal/uploads.d.ts +42 -0
  193. package/internal/uploads.d.ts.map +1 -0
  194. package/internal/uploads.js +141 -0
  195. package/internal/uploads.js.map +1 -0
  196. package/internal/uploads.mjs +131 -0
  197. package/internal/uploads.mjs.map +1 -0
  198. package/internal/utils/base64.d.mts +3 -0
  199. package/internal/utils/base64.d.mts.map +1 -0
  200. package/internal/utils/base64.d.ts +3 -0
  201. package/internal/utils/base64.d.ts.map +1 -0
  202. package/internal/utils/base64.js +38 -0
  203. package/internal/utils/base64.js.map +1 -0
  204. package/internal/utils/base64.mjs +33 -0
  205. package/internal/utils/base64.mjs.map +1 -0
  206. package/internal/utils/bytes.d.mts +4 -0
  207. package/internal/utils/bytes.d.mts.map +1 -0
  208. package/internal/utils/bytes.d.ts +4 -0
  209. package/internal/utils/bytes.d.ts.map +1 -0
  210. package/internal/utils/bytes.js +31 -0
  211. package/internal/utils/bytes.js.map +1 -0
  212. package/internal/utils/bytes.mjs +26 -0
  213. package/internal/utils/bytes.mjs.map +1 -0
  214. package/internal/utils/env.d.mts +9 -0
  215. package/internal/utils/env.d.mts.map +1 -0
  216. package/internal/utils/env.d.ts +9 -0
  217. package/internal/utils/env.d.ts.map +1 -0
  218. package/internal/utils/env.js +22 -0
  219. package/internal/utils/env.js.map +1 -0
  220. package/internal/utils/env.mjs +18 -0
  221. package/internal/utils/env.mjs.map +1 -0
  222. package/internal/utils/log.d.mts +37 -0
  223. package/internal/utils/log.d.mts.map +1 -0
  224. package/internal/utils/log.d.ts +37 -0
  225. package/internal/utils/log.d.ts.map +1 -0
  226. package/internal/utils/log.js +85 -0
  227. package/internal/utils/log.js.map +1 -0
  228. package/internal/utils/log.mjs +79 -0
  229. package/internal/utils/log.mjs.map +1 -0
  230. package/internal/utils/path.d.mts +15 -0
  231. package/internal/utils/path.d.mts.map +1 -0
  232. package/internal/utils/path.d.ts +15 -0
  233. package/internal/utils/path.d.ts.map +1 -0
  234. package/internal/utils/path.js +79 -0
  235. package/internal/utils/path.js.map +1 -0
  236. package/internal/utils/path.mjs +74 -0
  237. package/internal/utils/path.mjs.map +1 -0
  238. package/internal/utils/sleep.d.mts +2 -0
  239. package/internal/utils/sleep.d.mts.map +1 -0
  240. package/internal/utils/sleep.d.ts +2 -0
  241. package/internal/utils/sleep.d.ts.map +1 -0
  242. package/internal/utils/sleep.js +7 -0
  243. package/internal/utils/sleep.js.map +1 -0
  244. package/internal/utils/sleep.mjs +3 -0
  245. package/internal/utils/sleep.mjs.map +1 -0
  246. package/internal/utils/uuid.d.mts +5 -0
  247. package/internal/utils/uuid.d.mts.map +1 -0
  248. package/internal/utils/uuid.d.ts +5 -0
  249. package/internal/utils/uuid.d.ts.map +1 -0
  250. package/internal/utils/uuid.js +19 -0
  251. package/internal/utils/uuid.js.map +1 -0
  252. package/internal/utils/uuid.mjs +15 -0
  253. package/internal/utils/uuid.mjs.map +1 -0
  254. package/internal/utils/values.d.mts +18 -0
  255. package/internal/utils/values.d.mts.map +1 -0
  256. package/internal/utils/values.d.ts +18 -0
  257. package/internal/utils/values.d.ts.map +1 -0
  258. package/internal/utils/values.js +112 -0
  259. package/internal/utils/values.js.map +1 -0
  260. package/internal/utils/values.mjs +94 -0
  261. package/internal/utils/values.mjs.map +1 -0
  262. package/internal/utils.d.mts +7 -0
  263. package/internal/utils.d.mts.map +1 -0
  264. package/internal/utils.d.ts +7 -0
  265. package/internal/utils.d.ts.map +1 -0
  266. package/internal/utils.js +11 -0
  267. package/internal/utils.js.map +1 -0
  268. package/internal/utils.mjs +8 -0
  269. package/internal/utils.mjs.map +1 -0
  270. package/package.json +131 -124
  271. package/resource.d.mts +2 -0
  272. package/resource.d.mts.map +1 -0
  273. package/resource.d.ts +2 -0
  274. package/resource.d.ts.map +1 -0
  275. package/resource.js +6 -0
  276. package/resource.js.map +1 -0
  277. package/resource.mjs +2 -0
  278. package/resource.mjs.map +1 -0
  279. package/resources/admin.d.mts +4 -0
  280. package/resources/admin.d.mts.map +1 -0
  281. package/resources/admin.d.ts +4 -0
  282. package/resources/admin.d.ts.map +1 -0
  283. package/resources/admin.js +9 -0
  284. package/resources/admin.js.map +1 -0
  285. package/resources/admin.mjs +5 -0
  286. package/resources/admin.mjs.map +1 -0
  287. package/resources/index.d.mts +7 -0
  288. package/resources/index.d.mts.map +1 -0
  289. package/resources/index.d.ts +7 -0
  290. package/resources/index.d.ts.map +1 -0
  291. package/resources/index.js +17 -0
  292. package/resources/index.js.map +1 -0
  293. package/resources/index.mjs +8 -0
  294. package/resources/index.mjs.map +1 -0
  295. package/resources/models.d.mts +85 -0
  296. package/resources/models.d.mts.map +1 -0
  297. package/resources/models.d.ts +85 -0
  298. package/resources/models.d.ts.map +1 -0
  299. package/resources/models.js +44 -0
  300. package/resources/models.js.map +1 -0
  301. package/resources/models.mjs +40 -0
  302. package/resources/models.mjs.map +1 -0
  303. package/resources/preferences.d.mts +111 -0
  304. package/resources/preferences.d.mts.map +1 -0
  305. package/resources/preferences.d.ts +111 -0
  306. package/resources/preferences.d.ts.map +1 -0
  307. package/resources/preferences.js +92 -0
  308. package/resources/preferences.js.map +1 -0
  309. package/resources/preferences.mjs +88 -0
  310. package/resources/preferences.mjs.map +1 -0
  311. package/resources/prompt-adaptation.d.mts +574 -0
  312. package/resources/prompt-adaptation.d.mts.map +1 -0
  313. package/resources/prompt-adaptation.d.ts +574 -0
  314. package/resources/prompt-adaptation.d.ts.map +1 -0
  315. package/resources/prompt-adaptation.js +289 -0
  316. package/resources/prompt-adaptation.js.map +1 -0
  317. package/resources/prompt-adaptation.mjs +285 -0
  318. package/resources/prompt-adaptation.mjs.map +1 -0
  319. package/resources/report.d.mts +245 -0
  320. package/resources/report.d.mts.map +1 -0
  321. package/resources/report.d.ts +245 -0
  322. package/resources/report.d.ts.map +1 -0
  323. package/resources/report.js +86 -0
  324. package/resources/report.js.map +1 -0
  325. package/resources/report.mjs +82 -0
  326. package/resources/report.mjs.map +1 -0
  327. package/resources/routing.d.mts +391 -0
  328. package/resources/routing.d.mts.map +1 -0
  329. package/resources/routing.d.ts +391 -0
  330. package/resources/routing.d.ts.map +1 -0
  331. package/resources/routing.js +163 -0
  332. package/resources/routing.js.map +1 -0
  333. package/resources/routing.mjs +159 -0
  334. package/resources/routing.mjs.map +1 -0
  335. package/resources.d.mts +2 -0
  336. package/resources.d.mts.map +1 -0
  337. package/resources.d.ts +2 -0
  338. package/resources.d.ts.map +1 -0
  339. package/resources.js +5 -0
  340. package/resources.js.map +1 -0
  341. package/resources.mjs +2 -0
  342. package/resources.mjs.map +1 -0
  343. package/src/api-promise.ts +2 -0
  344. package/src/client.ts +842 -0
  345. package/src/core/README.md +3 -0
  346. package/src/core/api-promise.ts +92 -0
  347. package/src/core/error.ts +130 -0
  348. package/src/core/resource.ts +11 -0
  349. package/src/core/uploads.ts +2 -0
  350. package/src/error.ts +2 -0
  351. package/src/index.ts +22 -0
  352. package/src/internal/README.md +3 -0
  353. package/src/internal/builtin-types.ts +93 -0
  354. package/src/internal/detect-platform.ts +196 -0
  355. package/src/internal/errors.ts +33 -0
  356. package/src/internal/headers.ts +97 -0
  357. package/src/internal/parse.ts +50 -0
  358. package/src/internal/qs/LICENSE.md +13 -0
  359. package/src/internal/qs/README.md +3 -0
  360. package/src/internal/qs/formats.ts +10 -0
  361. package/src/internal/qs/index.ts +13 -0
  362. package/src/internal/qs/stringify.ts +385 -0
  363. package/src/internal/qs/types.ts +71 -0
  364. package/src/internal/qs/utils.ts +265 -0
  365. package/src/internal/request-options.ts +91 -0
  366. package/src/internal/shim-types.ts +26 -0
  367. package/src/internal/shims.ts +107 -0
  368. package/src/internal/to-file.ts +154 -0
  369. package/src/internal/types.ts +95 -0
  370. package/src/internal/uploads.ts +187 -0
  371. package/src/internal/utils/base64.ts +40 -0
  372. package/src/internal/utils/bytes.ts +32 -0
  373. package/src/internal/utils/env.ts +18 -0
  374. package/src/internal/utils/log.ts +126 -0
  375. package/src/internal/utils/path.ts +88 -0
  376. package/src/internal/utils/sleep.ts +3 -0
  377. package/src/internal/utils/uuid.ts +17 -0
  378. package/src/internal/utils/values.ts +105 -0
  379. package/src/internal/utils.ts +8 -0
  380. package/src/lib/.keep +4 -0
  381. package/src/resource.ts +2 -0
  382. package/src/resources/admin.ts +5 -0
  383. package/src/resources/index.ts +44 -0
  384. package/src/resources/models.ts +110 -0
  385. package/src/resources/preferences.ts +158 -0
  386. package/src/resources/prompt-adaptation.ts +700 -0
  387. package/src/resources/report.ts +300 -0
  388. package/src/resources/routing.ts +476 -0
  389. package/src/resources.ts +1 -0
  390. package/src/tsconfig.json +11 -0
  391. package/src/uploads.ts +2 -0
  392. package/src/version.ts +1 -0
  393. package/uploads.d.mts +2 -0
  394. package/uploads.d.mts.map +1 -0
  395. package/uploads.d.ts +2 -0
  396. package/uploads.d.ts.map +1 -0
  397. package/uploads.js +6 -0
  398. package/uploads.js.map +1 -0
  399. package/uploads.mjs +2 -0
  400. package/uploads.mjs.map +1 -0
  401. package/version.d.mts +2 -0
  402. package/version.d.mts.map +1 -0
  403. package/version.d.ts +2 -0
  404. package/version.d.ts.map +1 -0
  405. package/version.js +5 -0
  406. package/version.js.map +1 -0
  407. package/version.mjs +2 -0
  408. package/version.mjs.map +1 -0
  409. package/LICENCE +0 -21
package/README.md CHANGED
@@ -1,6 +1,12 @@
1
- # NotDiamond Node API Library
1
+ # Not Diamond TypeScript API Library
2
2
 
3
- This library provides convenient access to the NotDiamond model routing API from TypeScript or JavaScript. NotDiamond helps you select the best AI model for your specific use case, optimizing for factors like cost, latency, or performance.
3
+ [![NPM version](<https://img.shields.io/npm/v/notdiamond.svg?label=npm%20(stable)>)](https://npmjs.org/package/notdiamond) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/notdiamond)
4
+
5
+ This library provides convenient access to the Not Diamond REST API from server-side TypeScript or JavaScript.
6
+
7
+ The REST API documentation can be found on [docs.notdiamond.ai](https://docs.notdiamond.ai). The full API of this library can be found in [api.md](api.md).
8
+
9
+ It is generated with [Stainless](https://www.stainless.com/).
4
10
 
5
11
  ## Installation
6
12
 
@@ -8,135 +14,473 @@ This library provides convenient access to the NotDiamond model routing API from
8
14
  npm install notdiamond
9
15
  ```
10
16
 
11
- For Deno users:
17
+ ## Usage
18
+
19
+ The full API of this library can be found in [api.md](api.md).
20
+
21
+ <!-- prettier-ignore -->
22
+ ```js
23
+ import NotDiamond from 'notdiamond';
24
+
25
+ const client = new NotDiamond({
26
+ apiKey: process.env['NOT_DIAMOND_API_KEY'], // This is the default and can be omitted
27
+ environment: 'staging', // defaults to 'production'
28
+ });
29
+
30
+ const response = await client.routing.selectModel({
31
+ llm_providers: [
32
+ { model: 'gpt-4o', provider: 'openai' },
33
+ { model: 'claude-3-5-sonnet-20241022', provider: 'anthropic' },
34
+ { model: 'gemini-1.5-pro', provider: 'google' },
35
+ ],
36
+ messages: [
37
+ { role: 'system', content: 'You are a helpful assistant.' },
38
+ { role: 'user', content: 'Explain quantum computing in simple terms' },
39
+ ],
40
+ });
41
+
42
+ console.log(response.providers);
43
+ ```
44
+
45
+ ### Request & Response types
12
46
 
47
+ This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:
48
+
49
+ <!-- prettier-ignore -->
13
50
  ```ts
14
- import { NotDiamond } from 'https://deno.land/x/notdiamond@v1.0.0/mod.ts';
51
+ import NotDiamond from 'notdiamond';
52
+
53
+ const client = new NotDiamond({
54
+ apiKey: process.env['NOT_DIAMOND_API_KEY'], // This is the default and can be omitted
55
+ environment: 'staging', // defaults to 'production'
56
+ });
57
+
58
+ const params: NotDiamond.RoutingSelectModelParams = {
59
+ llm_providers: [
60
+ { model: 'gpt-4o', provider: 'openai' },
61
+ { model: 'claude-3-5-sonnet-20241022', provider: 'anthropic' },
62
+ { model: 'gemini-1.5-pro', provider: 'google' },
63
+ ],
64
+ messages: [
65
+ { role: 'system', content: 'You are a helpful assistant.' },
66
+ { role: 'user', content: 'Explain quantum computing in simple terms' },
67
+ ],
68
+ };
69
+ const response: NotDiamond.RoutingSelectModelResponse = await client.routing.selectModel(params);
15
70
  ```
16
71
 
17
- ## Quick Start
72
+ Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.
73
+
74
+ ## File uploads
75
+
76
+ Request parameters that correspond to file uploads can be passed in many different forms:
18
77
 
19
- To use the API, you need to sign up for a NotDiamond account and obtain an API key. [Sign up here](https://app.notdiamond.ai).
78
+ - `File` (or an object with the same structure)
79
+ - a `fetch` `Response` (or an object with the same structure)
80
+ - an `fs.ReadStream`
81
+ - the return value of our `toFile` helper
20
82
 
21
- ### Basic Usage
83
+ ```ts
84
+ import fs from 'fs';
85
+ import NotDiamond, { toFile } from 'notdiamond';
86
+
87
+ const client = new NotDiamond();
88
+
89
+ // If you have access to Node `fs` we recommend using `fs.createReadStream()`:
90
+ await client.routing.createSurveyResponse({
91
+ constraint_priorities: 'constraint_priorities',
92
+ email: 'email',
93
+ llm_providers: 'llm_providers',
94
+ use_case_desc: 'use_case_desc',
95
+ user_id: 'user_id',
96
+ 'x-token': 'x-token',
97
+ dataset_file: fs.createReadStream('/path/to/file'),
98
+ });
22
99
 
23
- Here's a simple example of how to use NotDiamond to select the best model between GPT-4o, Claude 3.5 Sonnet, and Gemini 1.5 Pro, while optimizing for latency and outputting the raw text:
100
+ // Or if you have the web `File` API you can pass a `File` instance:
101
+ await client.routing.createSurveyResponse({
102
+ constraint_priorities: 'constraint_priorities',
103
+ email: 'email',
104
+ llm_providers: 'llm_providers',
105
+ use_case_desc: 'use_case_desc',
106
+ user_id: 'user_id',
107
+ 'x-token': 'x-token',
108
+ dataset_file: new File(['my bytes'], 'file'),
109
+ });
24
110
 
25
- ```typescript
26
- import { NotDiamond } from 'notdiamond';
111
+ // You can also pass a `fetch` `Response`:
112
+ await client.routing.createSurveyResponse({
113
+ constraint_priorities: 'constraint_priorities',
114
+ email: 'email',
115
+ llm_providers: 'llm_providers',
116
+ use_case_desc: 'use_case_desc',
117
+ user_id: 'user_id',
118
+ 'x-token': 'x-token',
119
+ dataset_file: await fetch('https://somesite/file'),
120
+ });
27
121
 
28
- const notDiamond = new NotDiamond({
29
- // Optional - automatically loads from environment variable
30
- apiKey: process.env.NOTDIAMOND_API_KEY,
122
+ // Finally, if none of the above are convenient, you can use our `toFile` helper:
123
+ await client.routing.createSurveyResponse({
124
+ constraint_priorities: 'constraint_priorities',
125
+ email: 'email',
126
+ llm_providers: 'llm_providers',
127
+ use_case_desc: 'use_case_desc',
128
+ user_id: 'user_id',
129
+ 'x-token': 'x-token',
130
+ dataset_file: await toFile(Buffer.from('my bytes'), 'file'),
131
+ });
132
+ await client.routing.createSurveyResponse({
133
+ constraint_priorities: 'constraint_priorities',
134
+ email: 'email',
135
+ llm_providers: 'llm_providers',
136
+ use_case_desc: 'use_case_desc',
137
+ user_id: 'user_id',
138
+ 'x-token': 'x-token',
139
+ dataset_file: await toFile(new Uint8Array([0, 1, 2]), 'file'),
31
140
  });
141
+ ```
142
+
143
+ ## Handling errors
32
144
 
33
- async function basicExample() {
34
- // 1. Select the best model
35
- const result = await notDiamond.create({
36
- // Define the user's message
37
- messages: [{ content: 'What is 12x12?', role: 'user' }],
38
- // Specify the LLM providers and models to choose from
39
- llmProviders: [
40
- { provider: 'openai', model: 'gpt-4o-2024-05-13' },
41
- { provider: 'anthropic', model: 'claude-3-5-sonnet-20240620' },
42
- { provider: 'google', model: 'gemini-1.5-pro-latest' },
145
+ When the library is unable to connect to the API,
146
+ or if the API returns a non-success status code (i.e., 4xx or 5xx response),
147
+ a subclass of `APIError` will be thrown:
148
+
149
+ <!-- prettier-ignore -->
150
+ ```ts
151
+ const response = await client.routing
152
+ .selectModel({
153
+ llm_providers: [
154
+ { model: 'gpt-4o', provider: 'openai' },
155
+ { model: 'claude-3-5-sonnet-20241022', provider: 'anthropic' },
156
+ { model: 'gemini-1.5-pro', provider: 'google' },
43
157
  ],
44
- // Set the optimization criteria to latency
45
- tradeoff: 'latency',
158
+ messages: [
159
+ { role: 'system', content: 'You are a helpful assistant.' },
160
+ { role: 'user', content: 'Explain quantum computing in simple terms' },
161
+ ],
162
+ })
163
+ .catch(async (err) => {
164
+ if (err instanceof NotDiamond.APIError) {
165
+ console.log(err.status); // 400
166
+ console.log(err.name); // BadRequestError
167
+ console.log(err.headers); // {server: 'nginx', ...}
168
+ } else {
169
+ throw err;
170
+ }
46
171
  });
172
+ ```
47
173
 
48
- // 2. Handle potential errors
49
- if ('detail' in result) {
50
- console.error('Error:', result.detail);
51
- return;
52
- }
53
-
54
- // 3. Log the results
55
- // Display the text response
56
- console.log('LLM output:', result.content);
57
- // Display the selected provider(s)
58
- console.log('Selected providers:', result.providers);
59
- // Show the unique session ID for this request
60
- console.log('Session ID:', result.session_id);
61
- }
62
-
63
- basicExample();
64
- ```
65
-
66
- ### Advanced Usage with Tools
67
-
68
- You can also use NotDiamond with custom tools:
69
-
70
- ```typescript
71
- import { NotDiamond, Tool } from 'notdiamond';
72
-
73
- const notDiamond = new NotDiamond();
74
-
75
- // Define custom tools for the AI to use
76
- const tools: Tool[] = [
77
- {
78
- type: 'function',
79
- function: {
80
- name: 'multiply',
81
- description: 'Multiplies two numbers.',
82
- parameters: {
83
- type: 'object',
84
- properties: {
85
- a: { type: 'integer' },
86
- b: { type: 'integer' },
87
- },
88
- required: ['a', 'b'],
89
- },
90
- },
91
- },
92
- ];
93
-
94
- async function toolCallingExample() {
95
- const result = await notDiamond.modelSelect({
96
- messages: [{ content: 'What is 12x12?', role: 'user' }],
97
- llmProviders: [
98
- { provider: 'openai', model: 'gpt-4-1106-preview' },
99
- { provider: 'anthropic', model: 'claude-3-sonnet-20240229' },
174
+ Error codes are as follows:
175
+
176
+ | Status Code | Error Type |
177
+ | ----------- | -------------------------- |
178
+ | 400 | `BadRequestError` |
179
+ | 401 | `AuthenticationError` |
180
+ | 403 | `PermissionDeniedError` |
181
+ | 404 | `NotFoundError` |
182
+ | 422 | `UnprocessableEntityError` |
183
+ | 429 | `RateLimitError` |
184
+ | >=500 | `InternalServerError` |
185
+ | N/A | `APIConnectionError` |
186
+
187
+ ### Retries
188
+
189
+ Certain errors will be automatically retried 2 times by default, with a short exponential backoff.
190
+ Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict,
191
+ 429 Rate Limit, and >=500 Internal errors will all be retried by default.
192
+
193
+ You can use the `maxRetries` option to configure or disable this:
194
+
195
+ <!-- prettier-ignore -->
196
+ ```js
197
+ // Configure the default for all requests:
198
+ const client = new NotDiamond({
199
+ maxRetries: 0, // default is 2
200
+ });
201
+
202
+ // Or, configure per-request:
203
+ await client.routing.selectModel({ llm_providers: [{ model: 'gpt-4o', provider: 'openai' }, { model: 'claude-3-5-sonnet-20241022', provider: 'anthropic' }, { model: 'gemini-1.5-pro', provider: 'google' }], messages: [{ role: 'system', content: 'You are a helpful assistant.' }, { role: 'user', content: 'Explain quantum computing in simple terms' }] }, {
204
+ maxRetries: 5,
205
+ });
206
+ ```
207
+
208
+ ### Timeouts
209
+
210
+ Requests time out after 1 minute by default. You can configure this with a `timeout` option:
211
+
212
+ <!-- prettier-ignore -->
213
+ ```ts
214
+ // Configure the default for all requests:
215
+ const client = new NotDiamond({
216
+ timeout: 20 * 1000, // 20 seconds (default is 1 minute)
217
+ });
218
+
219
+ // Override per-request:
220
+ await client.routing.selectModel({ llm_providers: [{ model: 'gpt-4o', provider: 'openai' }, { model: 'claude-3-5-sonnet-20241022', provider: 'anthropic' }, { model: 'gemini-1.5-pro', provider: 'google' }], messages: [{ role: 'system', content: 'You are a helpful assistant.' }, { role: 'user', content: 'Explain quantum computing in simple terms' }] }, {
221
+ timeout: 5 * 1000,
222
+ });
223
+ ```
224
+
225
+ On timeout, an `APIConnectionTimeoutError` is thrown.
226
+
227
+ Note that requests which time out will be [retried twice by default](#retries).
228
+
229
+ ## Advanced Usage
230
+
231
+ ### Accessing raw Response data (e.g., headers)
232
+
233
+ The "raw" `Response` returned by `fetch()` can be accessed through the `.asResponse()` method on the `APIPromise` type that all methods return.
234
+ This method returns as soon as the headers for a successful response are received and does not consume the response body, so you are free to write custom parsing or streaming logic.
235
+
236
+ You can also use the `.withResponse()` method to get the raw `Response` along with the parsed data.
237
+ Unlike `.asResponse()` this method consumes the body, returning once it is parsed.
238
+
239
+ <!-- prettier-ignore -->
240
+ ```ts
241
+ const client = new NotDiamond();
242
+
243
+ const response = await client.routing
244
+ .selectModel({
245
+ llm_providers: [
246
+ { model: 'gpt-4o', provider: 'openai' },
247
+ { model: 'claude-3-5-sonnet-20241022', provider: 'anthropic' },
248
+ { model: 'gemini-1.5-pro', provider: 'google' },
100
249
  ],
101
- // Include custom tools in the request
102
- tools: tools,
103
- // Optimize for cost instead of latency
104
- tradeoff: 'cost',
105
- // Allow selection of up to 2 models
106
- maxModelDepth: 2,
107
- });
250
+ messages: [
251
+ { role: 'system', content: 'You are a helpful assistant.' },
252
+ { role: 'user', content: 'Explain quantum computing in simple terms' },
253
+ ],
254
+ })
255
+ .asResponse();
256
+ console.log(response.headers.get('X-My-Header'));
257
+ console.log(response.statusText); // access the underlying Response object
258
+
259
+ const { data: response, response: raw } = await client.routing
260
+ .selectModel({
261
+ llm_providers: [
262
+ { model: 'gpt-4o', provider: 'openai' },
263
+ { model: 'claude-3-5-sonnet-20241022', provider: 'anthropic' },
264
+ { model: 'gemini-1.5-pro', provider: 'google' },
265
+ ],
266
+ messages: [
267
+ { role: 'system', content: 'You are a helpful assistant.' },
268
+ { role: 'user', content: 'Explain quantum computing in simple terms' },
269
+ ],
270
+ })
271
+ .withResponse();
272
+ console.log(raw.headers.get('X-My-Header'));
273
+ console.log(response.providers);
274
+ ```
275
+
276
+ ### Logging
277
+
278
+ > [!IMPORTANT]
279
+ > All log messages are intended for debugging only. The format and content of log messages
280
+ > may change between releases.
281
+
282
+ #### Log levels
108
283
 
109
- if ('detail' in result) {
110
- console.error('Error:', result.detail);
111
- return;
112
- }
284
+ The log level can be configured in two ways:
113
285
 
114
- console.log('Selected providers:', result.providers);
115
- console.log('Session ID:', result.session_id);
116
- }
286
+ 1. Via the `NOT_DIAMOND_LOG` environment variable
287
+ 2. Using the `logLevel` client option (overrides the environment variable if set)
117
288
 
118
- toolCallingExample();
289
+ ```ts
290
+ import NotDiamond from 'notdiamond';
291
+
292
+ const client = new NotDiamond({
293
+ logLevel: 'debug', // Show all log messages
294
+ });
119
295
  ```
120
296
 
121
- ## API Reference
297
+ Available log levels, from most to least verbose:
298
+
299
+ - `'debug'` - Show debug messages, info, warnings, and errors
300
+ - `'info'` - Show info messages, warnings, and errors
301
+ - `'warn'` - Show warnings and errors (default)
302
+ - `'error'` - Show only errors
303
+ - `'off'` - Disable all logging
304
+
305
+ At the `'debug'` level, all HTTP requests and responses are logged, including headers and bodies.
306
+ Some authentication-related headers are redacted, but sensitive data in request and response bodies
307
+ may still be visible.
308
+
309
+ #### Custom logger
310
+
311
+ By default, this library logs to `globalThis.console`. You can also provide a custom logger.
312
+ Most logging libraries are supported, including [pino](https://www.npmjs.com/package/pino), [winston](https://www.npmjs.com/package/winston), [bunyan](https://www.npmjs.com/package/bunyan), [consola](https://www.npmjs.com/package/consola), [signale](https://www.npmjs.com/package/signale), and [@std/log](https://jsr.io/@std/log). If your logger doesn't work, please open an issue.
313
+
314
+ When providing a custom logger, the `logLevel` option still controls which messages are emitted, messages
315
+ below the configured level will not be sent to your logger.
316
+
317
+ ```ts
318
+ import NotDiamond from 'notdiamond';
319
+ import pino from 'pino';
320
+
321
+ const logger = pino();
322
+
323
+ const client = new NotDiamond({
324
+ logger: logger.child({ name: 'NotDiamond' }),
325
+ logLevel: 'debug', // Send all messages to pino, allowing it to filter
326
+ });
327
+ ```
328
+
329
+ ### Making custom/undocumented requests
330
+
331
+ This library is typed for convenient access to the documented API. If you need to access undocumented
332
+ endpoints, params, or response properties, the library can still be used.
333
+
334
+ #### Undocumented endpoints
335
+
336
+ To make requests to undocumented endpoints, you can use `client.get`, `client.post`, and other HTTP verbs.
337
+ Options on the client, such as retries, will be respected when making these requests.
338
+
339
+ ```ts
340
+ await client.post('/some/path', {
341
+ body: { some_prop: 'foo' },
342
+ query: { some_query_arg: 'bar' },
343
+ });
344
+ ```
345
+
346
+ #### Undocumented request params
347
+
348
+ To make requests using undocumented parameters, you may use `// @ts-expect-error` on the undocumented
349
+ parameter. This library doesn't validate at runtime that the request matches the type, so any extra values you
350
+ send will be sent as-is.
351
+
352
+ ```ts
353
+ client.routing.selectModel({
354
+ // ...
355
+ // @ts-expect-error baz is not yet public
356
+ baz: 'undocumented option',
357
+ });
358
+ ```
359
+
360
+ For requests with the `GET` verb, any extra params will be in the query, all other requests will send the
361
+ extra param in the body.
362
+
363
+ If you want to explicitly send an extra argument, you can do so with the `query`, `body`, and `headers` request
364
+ options.
365
+
366
+ #### Undocumented response properties
367
+
368
+ To access undocumented response properties, you may access the response object with `// @ts-expect-error` on
369
+ the response object, or cast the response object to the requisite type. Like the request params, we do not
370
+ validate or strip extra properties from the response from the API.
371
+
372
+ ### Customizing the fetch client
373
+
374
+ By default, this library expects a global `fetch` function is defined.
375
+
376
+ If you want to use a different `fetch` function, you can either polyfill the global:
377
+
378
+ ```ts
379
+ import fetch from 'my-fetch';
380
+
381
+ globalThis.fetch = fetch;
382
+ ```
383
+
384
+ Or pass it to the client:
385
+
386
+ ```ts
387
+ import NotDiamond from 'notdiamond';
388
+ import fetch from 'my-fetch';
389
+
390
+ const client = new NotDiamond({ fetch });
391
+ ```
392
+
393
+ ### Fetch options
394
+
395
+ If you want to set custom `fetch` options without overriding the `fetch` function, you can provide a `fetchOptions` object when instantiating the client or making a request. (Request-specific options override client options.)
396
+
397
+ ```ts
398
+ import NotDiamond from 'notdiamond';
399
+
400
+ const client = new NotDiamond({
401
+ fetchOptions: {
402
+ // `RequestInit` options
403
+ },
404
+ });
405
+ ```
406
+
407
+ #### Configuring proxies
408
+
409
+ To modify proxy behavior, you can provide custom `fetchOptions` that add runtime-specific proxy
410
+ options to requests:
411
+
412
+ <img src="https://raw.githubusercontent.com/stainless-api/sdk-assets/refs/heads/main/node.svg" align="top" width="18" height="21"> **Node** <sup>[[docs](https://github.com/nodejs/undici/blob/main/docs/docs/api/ProxyAgent.md#example---proxyagent-with-fetch)]</sup>
413
+
414
+ ```ts
415
+ import NotDiamond from 'notdiamond';
416
+ import * as undici from 'undici';
417
+
418
+ const proxyAgent = new undici.ProxyAgent('http://localhost:8888');
419
+ const client = new NotDiamond({
420
+ fetchOptions: {
421
+ dispatcher: proxyAgent,
422
+ },
423
+ });
424
+ ```
425
+
426
+ <img src="https://raw.githubusercontent.com/stainless-api/sdk-assets/refs/heads/main/bun.svg" align="top" width="18" height="21"> **Bun** <sup>[[docs](https://bun.sh/guides/http/proxy)]</sup>
427
+
428
+ ```ts
429
+ import NotDiamond from 'notdiamond';
430
+
431
+ const client = new NotDiamond({
432
+ fetchOptions: {
433
+ proxy: 'http://localhost:8888',
434
+ },
435
+ });
436
+ ```
437
+
438
+ <img src="https://raw.githubusercontent.com/stainless-api/sdk-assets/refs/heads/main/deno.svg" align="top" width="18" height="21"> **Deno** <sup>[[docs](https://docs.deno.com/api/deno/~/Deno.createHttpClient)]</sup>
439
+
440
+ ```ts
441
+ import NotDiamond from 'npm:notdiamond';
442
+
443
+ const httpClient = Deno.createHttpClient({ proxy: { url: 'http://localhost:8888' } });
444
+ const client = new NotDiamond({
445
+ fetchOptions: {
446
+ client: httpClient,
447
+ },
448
+ });
449
+ ```
450
+
451
+ ## Frequently Asked Questions
452
+
453
+ ## Semantic versioning
454
+
455
+ This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions:
456
+
457
+ 1. Changes that only affect static types, without breaking runtime behavior.
458
+ 2. Changes to library internals which are technically public but not intended or documented for external use. _(Please open a GitHub issue to let us know if you are relying on such internals.)_
459
+ 3. Changes that we do not expect to impact the vast majority of users in practice.
460
+
461
+ We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.
122
462
 
123
- For a complete API reference and more detailed documentation, please check our [API Documentation](https://notdiamond.readme.io/docs/what-is-not-diamond#getting-started).
463
+ We are keen for your feedback; please open an [issue](https://www.github.com/Not-Diamond/not-diamond-typescript/issues) with questions, bugs, or suggestions.
124
464
 
125
- ## Key Concepts
465
+ ## Requirements
126
466
 
127
- - **llmProviders**: An array of AI providers and models you want NotDiamond to choose from.
128
- - **tradeoff**: The factor to optimize for (e.g., 'latency', 'cost', 'performance').
129
- - **tools**: Custom functions that the AI can use to perform specific tasks.
130
- - **maxModelDepth**: The maximum number of models to include in the response (for advanced use cases).
467
+ TypeScript >= 4.9 is supported.
131
468
 
132
- ## Error Handling
469
+ The following runtimes are supported:
133
470
 
134
- NotDiamond uses typed responses. If there's an error, the response will have a `detail` property with the error message. Always check for this property when handling responses.
471
+ - Web browsers (Up-to-date Chrome, Firefox, Safari, Edge, and more)
472
+ - Node.js 20 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
473
+ - Deno v1.28.0 or higher.
474
+ - Bun 1.0 or later.
475
+ - Cloudflare Workers.
476
+ - Vercel Edge Runtime.
477
+ - Jest 28 or greater with the `"node"` environment (`"jsdom"` is not supported at this time).
478
+ - Nitro v2.6 or greater.
135
479
 
136
- ## Support
480
+ Note that React Native is not supported at this time.
137
481
 
138
- If you encounter any issues or have questions, please [open an issue](https://github.com/Not-Diamond/notdiamond-node) on our GitHub repository.
482
+ If you are interested in other runtime environments, please open or upvote an issue on GitHub.
139
483
 
140
- ## License
484
+ ## Contributing
141
485
 
142
- This library is released under the MIT License.
486
+ See [the contributing documentation](./CONTRIBUTING.md).
@@ -0,0 +1,2 @@
1
+ export * from "./core/api-promise.mjs";
2
+ //# sourceMappingURL=api-promise.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-promise.d.mts","sourceRoot":"","sources":["src/api-promise.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export * from "./core/api-promise.js";
2
+ //# sourceMappingURL=api-promise.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-promise.d.ts","sourceRoot":"","sources":["src/api-promise.ts"],"names":[],"mappings":""}
package/api-promise.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("./internal/tslib.js");
4
+ /** @deprecated Import from ./core/api-promise instead */
5
+ tslib_1.__exportStar(require("./core/api-promise.js"), exports);
6
+ //# sourceMappingURL=api-promise.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-promise.js","sourceRoot":"","sources":["src/api-promise.ts"],"names":[],"mappings":";;;AAAA,yDAAyD;AACzD,gEAAmC"}
@@ -0,0 +1,2 @@
1
+ export * from "./core/api-promise.mjs";
2
+ //# sourceMappingURL=api-promise.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-promise.mjs","sourceRoot":"","sources":["src/api-promise.ts"],"names":[],"mappings":""}