notdiamond 0.3.8 → 0.3.9

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 CHANGED
@@ -20,7 +20,7 @@ To use the API, you need to sign up for a NotDiamond account and obtain an API k
20
20
 
21
21
  ### Basic Usage
22
22
 
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:
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:
24
24
 
25
25
  ```typescript
26
26
  import { NotDiamond } from 'notdiamond';
@@ -32,7 +32,7 @@ const notDiamond = new NotDiamond({
32
32
 
33
33
  async function basicExample() {
34
34
  // 1. Select the best model
35
- const result = await notDiamond.modelSelect({
35
+ const result = await notDiamond.create({
36
36
  // Define the user's message
37
37
  messages: [{ content: 'What is 12x12?', role: 'user' }],
38
38
  // Specify the LLM providers and models to choose from
@@ -52,6 +52,8 @@ async function basicExample() {
52
52
  }
53
53
 
54
54
  // 3. Log the results
55
+ // Display the text response
56
+ console.log('LLM output:', result.content);
55
57
  // Display the selected provider(s)
56
58
  console.log('Selected providers:', result.providers);
57
59
  // Show the unique session ID for this request
@@ -118,7 +120,7 @@ toolCallingExample();
118
120
 
119
121
  ## API Reference
120
122
 
121
- For a complete API reference and more detailed documentation, please check our [API Documentation](https://notdiamond.readme.io/v0.1.0-beta/docs/getting-started).
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).
122
124
 
123
125
  ## Key Concepts
124
126
 
package/dist/index.cjs CHANGED
@@ -1,6 +1,17 @@
1
1
  'use strict';
2
2
 
3
3
  const dotenv = require('dotenv');
4
+ const openai = require('@langchain/openai');
5
+ const messages = require('@langchain/core/messages');
6
+ const anthropic = require('@langchain/anthropic');
7
+ const googleGenai = require('@langchain/google-genai');
8
+ const mistralai = require('@langchain/mistralai');
9
+ const chat_models = require('@langchain/core/language_models/chat_models');
10
+ const axios = require('axios');
11
+ const cohere = require('@langchain/cohere');
12
+ const togetherai = require('@langchain/community/chat_models/togetherai');
13
+
14
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
4
15
 
5
16
  function _interopNamespaceCompat(e) {
6
17
  if (e && typeof e === 'object' && 'default' in e) return e;
@@ -15,8 +26,446 @@ function _interopNamespaceCompat(e) {
15
26
  }
16
27
 
17
28
  const dotenv__namespace = /*#__PURE__*/_interopNamespaceCompat(dotenv);
29
+ const axios__default = /*#__PURE__*/_interopDefaultCompat(axios);
30
+
31
+ const name = "notdiamond";
32
+ const type = "module";
33
+ const version = "0.3.9";
34
+ const author = "not-diamond";
35
+ const license = "MIT";
36
+ const description = "TS/JS client for the NotDiamond API";
37
+ const main = "./dist/index.cjs";
38
+ const exports$1 = {
39
+ ".": {
40
+ "import": "./dist/index.mjs",
41
+ require: "./dist/index.cjs"
42
+ }
43
+ };
44
+ const types = "./dist/index.d.ts";
45
+ const repository = {
46
+ type: "git",
47
+ url: "https://github.com/Not-Diamond/notdiamond-node.git"
48
+ };
49
+ const bugs = {
50
+ url: "https://github.com/Not-Diamond/notdiamond-node/issues"
51
+ };
52
+ const homepage = "https://github.com/Not-Diamond/notdiamond-node#readme";
53
+ const files = [
54
+ "dist"
55
+ ];
56
+ const keywords = [
57
+ "ai",
58
+ "not-diamond",
59
+ "typescript",
60
+ "openai",
61
+ "chatgpt",
62
+ "anthropic",
63
+ "claude",
64
+ "gemini",
65
+ "model router"
66
+ ];
67
+ const scripts = {
68
+ prepare: "husky install",
69
+ "start:cjs": "node ./dist/index.cjs",
70
+ "start:esm": "node ./dist/index.mjs",
71
+ dev: "nodemon",
72
+ develop: "node --no-warnings=ExperimentalWarning --loader ts-node/esm ./src/index.ts",
73
+ test: "jest --passWithNoTests",
74
+ "test:watch": "jest --watch --passWithNoTests",
75
+ "test:coverage": "jest --coverage --passWithNoTests",
76
+ clean: "rimraf build && rimraf dist",
77
+ build: "swc ./src -d build",
78
+ "build:watch": "swc ./src -d build -w",
79
+ lint: "eslint ./src --ext .ts",
80
+ "lint:fix": "eslint ./src --ext .ts --fix",
81
+ prettier: "prettier '**/*.{ts,json,md}'",
82
+ "prettier:write": "prettier --write '**/*.{ts,json,md}'",
83
+ "type-check": "tsc --noEmit",
84
+ "lint-staged": "lint-staged",
85
+ release: "semantic-release",
86
+ bundle: "unbuild"
87
+ };
88
+ const dependencies = {
89
+ "@langchain/anthropic": "^0.3.1",
90
+ "@langchain/cohere": "^0.3.0",
91
+ "@langchain/community": "^0.3.3",
92
+ "@langchain/core": "^0.3.3",
93
+ "@langchain/google-genai": "^0.1.0",
94
+ "@langchain/mistralai": "^0.1.1",
95
+ "@langchain/openai": "^0.3.0",
96
+ "ansi-styles": "^6.2.1",
97
+ axios: "^1.7.7",
98
+ camelcase: "^8.0.0",
99
+ decamelize: "^6.0.0",
100
+ dotenv: "^16.4.5",
101
+ eventemitter3: "^5.0.1",
102
+ langchain: "^0.3.2",
103
+ langsmith: "^0.1.60",
104
+ "p-finally": "^2.0.1",
105
+ "p-queue": "^8.0.1",
106
+ "p-retry": "^6.2.0",
107
+ "p-timeout": "^6.1.2",
108
+ retry: "^0.13.1",
109
+ semver: "^7.6.3",
110
+ uuid: "^10.0.0",
111
+ zod: "^3.23.8",
112
+ "zod-to-json-schema": "^3.23.3"
113
+ };
114
+ const devDependencies = {
115
+ "@semantic-release/changelog": "^6.0.3",
116
+ "@semantic-release/commit-analyzer": "^12.0.0",
117
+ "@semantic-release/git": "^10.0.1",
118
+ "@semantic-release/github": "^10.0.0",
119
+ "@semantic-release/npm": "^12.0.0",
120
+ "@semantic-release/release-notes-generator": "^13.0.0",
121
+ "@swc/cli": "0.3.12",
122
+ "@swc/core": "1.5.7",
123
+ "@swc/jest": "0.2.36",
124
+ "@types/jest": "29.5.12",
125
+ "@types/node": "20.12.12",
126
+ "@typescript-eslint/eslint-plugin": "7.9.0",
127
+ "@typescript-eslint/parser": "7.9.0",
128
+ eslint: "8.57.0",
129
+ "eslint-config-prettier": "9.1.0",
130
+ "eslint-plugin-jest": "27.9.0",
131
+ "eslint-plugin-prettier": "5.1.3",
132
+ husky: "^9.0.0",
133
+ jest: "29.7.0",
134
+ "lint-staged": "^15.0.0",
135
+ nodemon: "3.1.0",
136
+ prettier: "3.2.5",
137
+ "regenerator-runtime": "^0.14.0",
138
+ rimraf: "5.0.7",
139
+ "semantic-release": "^23.0.0",
140
+ "ts-node": "^10.9.1",
141
+ typescript: "5.4.5",
142
+ unbuild: "^2.0.0"
143
+ };
144
+ const resolutions = {
145
+ "wrap-ansi": "7.0.0"
146
+ };
147
+ const engines = {
148
+ node: ">=20",
149
+ npm: ">=8"
150
+ };
151
+ const packageJson = {
152
+ name: name,
153
+ type: type,
154
+ version: version,
155
+ author: author,
156
+ license: license,
157
+ description: description,
158
+ main: main,
159
+ exports: exports$1,
160
+ types: types,
161
+ repository: repository,
162
+ bugs: bugs,
163
+ homepage: homepage,
164
+ files: files,
165
+ keywords: keywords,
166
+ scripts: scripts,
167
+ dependencies: dependencies,
168
+ devDependencies: devDependencies,
169
+ resolutions: resolutions,
170
+ engines: engines
171
+ };
172
+
173
+ var __defProp$1 = Object.defineProperty;
174
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
175
+ var __publicField$1 = (obj, key, value) => {
176
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
177
+ return value;
178
+ };
179
+ class ChatPerplexity extends chat_models.BaseChatModel {
180
+ constructor({ apiKey, model }) {
181
+ super({});
182
+ __publicField$1(this, "apiKey");
183
+ __publicField$1(this, "model");
184
+ this.apiKey = apiKey;
185
+ this.model = model;
186
+ }
187
+ _generate(messages, options, runManager) {
188
+ throw new Error(
189
+ "Method not implemented." + JSON.stringify(messages) + JSON.stringify(options) + JSON.stringify(runManager)
190
+ );
191
+ }
192
+ _llmType() {
193
+ return "perplexity";
194
+ }
195
+ /**
196
+ * Invokes the Perplexity model.
197
+ * @param messages The messages to send to the model.
198
+ * @returns The results of the model.
199
+ */
200
+ async invoke(messages$1) {
201
+ try {
202
+ const { data } = await axios__default.post(
203
+ "https://api.perplexity.ai/chat/completions",
204
+ {
205
+ model: this.model,
206
+ messages: messages$1.map((m) => ({
207
+ role: m._getType() === "human" ? "user" : m._getType(),
208
+ content: m.content
209
+ }))
210
+ },
211
+ {
212
+ headers: {
213
+ Authorization: `Bearer ${this.apiKey}`
214
+ }
215
+ }
216
+ );
217
+ return new messages.AIMessage(data.choices[0].message.content);
218
+ } catch (error) {
219
+ if (axios__default.isAxiosError(error) && error.response) {
220
+ throw new Error(`Perplexity API error: ${error.response.statusText}`);
221
+ }
222
+ throw error;
223
+ }
224
+ }
225
+ }
226
+
227
+ const SupportedProvider = {
228
+ OPENAI: "openai",
229
+ ANTHROPIC: "anthropic",
230
+ GOOGLE: "google",
231
+ MISTRAL: "mistral",
232
+ PERPLEXITY: "perplexity",
233
+ COHERE: "cohere",
234
+ TOGETHER: "together"
235
+ };
236
+ const SupportedModel = {
237
+ GPT_3_5_TURBO: "gpt-3.5-turbo",
238
+ GPT_3_5_TURBO_0125: "gpt-3.5-turbo-0125",
239
+ GPT_4: "gpt-4",
240
+ GPT_4_0613: "gpt-4-0613",
241
+ GPT_4_1106_PREVIEW: "gpt-4-1106-preview",
242
+ GPT_4_TURBO: "gpt-4-turbo",
243
+ GPT_4_TURBO_PREVIEW: "gpt-4-turbo-preview",
244
+ GPT_4_TURBO_2024_04_09: "gpt-4-turbo-2024-04-09",
245
+ GPT_4O_2024_05_13: "gpt-4o-2024-05-13",
246
+ GPT_4O_2024_08_06: "gpt-4o-2024-08-06",
247
+ GPT_4O: "gpt-4o",
248
+ GPT_4O_MINI_2024_07_18: "gpt-4o-mini-2024-07-18",
249
+ GPT_4O_MINI: "gpt-4o-mini",
250
+ GPT_4_0125_PREVIEW: "gpt-4-0125-preview",
251
+ O1_PREVIEW: "o1-preview",
252
+ O1_PREVIEW_2024_09_12: "o1-preview-2024-09-12",
253
+ O1_MINI: "o1-mini",
254
+ O1_MINI_2024_09_12: "o1-mini-2024-09-12",
255
+ CLAUDE_2_1: "claude-2.1",
256
+ CLAUDE_3_OPUS_20240229: "claude-3-opus-20240229",
257
+ CLAUDE_3_SONNET_20240229: "claude-3-sonnet-20240229",
258
+ CLAUDE_3_5_SONNET_20240620: "claude-3-5-sonnet-20240620",
259
+ CLAUDE_3_HAIKU_20240307: "claude-3-haiku-20240307",
260
+ GEMINI_PRO: "gemini-pro",
261
+ GEMINI_1_PRO_LATEST: "gemini-1.0-pro-latest",
262
+ GEMINI_15_PRO_LATEST: "gemini-1.5-pro-latest",
263
+ GEMINI_15_PRO_EXP_0801: "gemini-1.5-pro-exp-0801",
264
+ GEMINI_15_FLASH_LATEST: "gemini-1.5-flash-latest",
265
+ COMMAND_R: "command-r",
266
+ COMMAND_R_PLUS: "command-r-plus",
267
+ MISTRAL_LARGE_LATEST: "mistral-large-latest",
268
+ MISTRAL_LARGE_2407: "mistral-large-2407",
269
+ MISTRAL_LARGE_2402: "mistral-large-2402",
270
+ MISTRAL_MEDIUM_LATEST: "mistral-medium-latest",
271
+ MISTRAL_SMALL_LATEST: "mistral-small-latest",
272
+ CODESTRAL_LATEST: "codestral-latest",
273
+ OPEN_MISTRAL_7B: "open-mistral-7b",
274
+ OPEN_MIXTRAL_8X7B: "open-mixtral-8x7b",
275
+ OPEN_MIXTRAL_8X22B: "open-mixtral-8x22b",
276
+ MISTRAL_7B_INSTRUCT_V0_2: "Mistral-7B-Instruct-v0.2",
277
+ MIXTRAL_8X7B_INSTRUCT_V0_1: "Mixtral-8x7B-Instruct-v0.1",
278
+ MIXTRAL_8X22B_INSTRUCT_V0_1: "Mixtral-8x22B-Instruct-v0.1",
279
+ LLAMA_3_70B_CHAT_HF: "Llama-3-70b-chat-hf",
280
+ LLAMA_3_8B_CHAT_HF: "Llama-3-8b-chat-hf",
281
+ QWEN2_72B_INSTRUCT: "Qwen2-72B-Instruct",
282
+ LLAMA_3_1_8B_INSTRUCT_TURBO: "Meta-Llama-3.1-8B-Instruct-Turbo",
283
+ LLAMA_3_1_70B_INSTRUCT_TURBO: "Meta-Llama-3.1-70B-Instruct-Turbo",
284
+ LLAMA_3_1_405B_INSTRUCT_TURBO: "Meta-Llama-3.1-405B-Instruct-Turbo",
285
+ LLAMA_3_1_SONAR_LARGE_128K_ONLINE: "llama-3.1-sonar-large-128k-online"
286
+ };
287
+ ({
288
+ [SupportedProvider.OPENAI]: [
289
+ SupportedModel.GPT_3_5_TURBO,
290
+ SupportedModel.GPT_3_5_TURBO_0125,
291
+ SupportedModel.GPT_4,
292
+ SupportedModel.GPT_4_0613,
293
+ SupportedModel.GPT_4_1106_PREVIEW,
294
+ SupportedModel.GPT_4_TURBO,
295
+ SupportedModel.GPT_4_TURBO_PREVIEW,
296
+ SupportedModel.GPT_4_TURBO_2024_04_09,
297
+ SupportedModel.GPT_4O_2024_05_13,
298
+ SupportedModel.GPT_4O_2024_08_06,
299
+ SupportedModel.GPT_4O,
300
+ SupportedModel.GPT_4O_MINI_2024_07_18,
301
+ SupportedModel.GPT_4O_MINI,
302
+ SupportedModel.GPT_4_0125_PREVIEW,
303
+ SupportedModel.O1_PREVIEW,
304
+ SupportedModel.O1_PREVIEW_2024_09_12,
305
+ SupportedModel.O1_MINI,
306
+ SupportedModel.O1_MINI_2024_09_12
307
+ ],
308
+ [SupportedProvider.ANTHROPIC]: [
309
+ SupportedModel.CLAUDE_2_1,
310
+ SupportedModel.CLAUDE_3_OPUS_20240229,
311
+ SupportedModel.CLAUDE_3_SONNET_20240229,
312
+ SupportedModel.CLAUDE_3_5_SONNET_20240620,
313
+ SupportedModel.CLAUDE_3_HAIKU_20240307
314
+ ],
315
+ [SupportedProvider.GOOGLE]: [
316
+ SupportedModel.GEMINI_PRO,
317
+ SupportedModel.GEMINI_1_PRO_LATEST,
318
+ SupportedModel.GEMINI_15_PRO_LATEST,
319
+ SupportedModel.GEMINI_15_PRO_EXP_0801,
320
+ SupportedModel.GEMINI_15_FLASH_LATEST
321
+ ],
322
+ [SupportedProvider.MISTRAL]: [
323
+ SupportedModel.MISTRAL_LARGE_LATEST,
324
+ SupportedModel.MISTRAL_LARGE_2407,
325
+ SupportedModel.MISTRAL_LARGE_2402,
326
+ SupportedModel.MISTRAL_MEDIUM_LATEST,
327
+ SupportedModel.MISTRAL_SMALL_LATEST,
328
+ SupportedModel.CODESTRAL_LATEST,
329
+ SupportedModel.OPEN_MISTRAL_7B,
330
+ SupportedModel.OPEN_MIXTRAL_8X7B,
331
+ SupportedModel.OPEN_MIXTRAL_8X22B
332
+ ],
333
+ [SupportedProvider.PERPLEXITY]: [
334
+ SupportedModel.LLAMA_3_1_SONAR_LARGE_128K_ONLINE
335
+ ],
336
+ [SupportedProvider.COHERE]: [
337
+ SupportedModel.COMMAND_R,
338
+ SupportedModel.COMMAND_R_PLUS
339
+ ],
340
+ [SupportedProvider.TOGETHER]: [
341
+ SupportedModel.MISTRAL_7B_INSTRUCT_V0_2,
342
+ SupportedModel.MIXTRAL_8X7B_INSTRUCT_V0_1,
343
+ SupportedModel.MIXTRAL_8X22B_INSTRUCT_V0_1,
344
+ SupportedModel.LLAMA_3_70B_CHAT_HF,
345
+ SupportedModel.LLAMA_3_8B_CHAT_HF,
346
+ SupportedModel.QWEN2_72B_INSTRUCT,
347
+ SupportedModel.LLAMA_3_1_8B_INSTRUCT_TURBO,
348
+ SupportedModel.LLAMA_3_1_70B_INSTRUCT_TURBO,
349
+ SupportedModel.LLAMA_3_1_405B_INSTRUCT_TURBO
350
+ ]
351
+ });
18
352
 
19
- const version = "0.3.7";
353
+ function getLangChainModel(provider, llmKeys, responseModel) {
354
+ const { OPENAI, ANTHROPIC, GOOGLE, MISTRAL, PERPLEXITY, COHERE, TOGETHER } = SupportedProvider;
355
+ switch (provider.provider) {
356
+ case OPENAI:
357
+ if (responseModel) {
358
+ return new openai.ChatOpenAI({
359
+ modelName: provider.model,
360
+ apiKey: llmKeys.openai || process.env.OPENAI_API_KEY
361
+ }).withStructuredOutput(responseModel);
362
+ }
363
+ return new openai.ChatOpenAI({
364
+ modelName: provider.model,
365
+ apiKey: llmKeys.openai || process.env.OPENAI_API_KEY
366
+ });
367
+ case ANTHROPIC:
368
+ if (responseModel) {
369
+ return new anthropic.ChatAnthropic({
370
+ modelName: provider.model,
371
+ anthropicApiKey: llmKeys.anthropic || process.env.ANTHROPIC_API_KEY
372
+ }).withStructuredOutput(responseModel);
373
+ }
374
+ return new anthropic.ChatAnthropic({
375
+ modelName: provider.model,
376
+ anthropicApiKey: llmKeys.anthropic || process.env.ANTHROPIC_API_KEY
377
+ });
378
+ case GOOGLE:
379
+ if (responseModel) {
380
+ return new googleGenai.ChatGoogleGenerativeAI({
381
+ modelName: provider.model,
382
+ apiKey: llmKeys.google || process.env.GOOGLE_API_KEY
383
+ }).withStructuredOutput(responseModel);
384
+ }
385
+ return new googleGenai.ChatGoogleGenerativeAI({
386
+ modelName: provider.model,
387
+ apiKey: llmKeys.google || process.env.GOOGLE_API_KEY
388
+ });
389
+ case MISTRAL:
390
+ if (responseModel) {
391
+ return new mistralai.ChatMistralAI({
392
+ modelName: provider.model,
393
+ apiKey: llmKeys.mistral || process.env.MISTRAL_API_KEY
394
+ }).withStructuredOutput(responseModel);
395
+ }
396
+ return new mistralai.ChatMistralAI({
397
+ modelName: provider.model,
398
+ apiKey: llmKeys.mistral || process.env.MISTRAL_API_KEY
399
+ });
400
+ case PERPLEXITY:
401
+ if (responseModel) {
402
+ return new ChatPerplexity({
403
+ apiKey: llmKeys.perplexity || process.env.PPLX_API_KEY || "",
404
+ model: provider.model
405
+ }).withStructuredOutput(responseModel);
406
+ }
407
+ return new ChatPerplexity({
408
+ apiKey: llmKeys.perplexity || process.env.PPLX_API_KEY || "",
409
+ model: provider.model
410
+ });
411
+ case COHERE:
412
+ if (responseModel) {
413
+ return new cohere.ChatCohere({
414
+ apiKey: process.env.COHERE_API_KEY || llmKeys.cohere,
415
+ model: provider.model
416
+ }).withStructuredOutput(responseModel);
417
+ }
418
+ return new cohere.ChatCohere({
419
+ apiKey: process.env.COHERE_API_KEY || llmKeys.cohere,
420
+ model: provider.model
421
+ });
422
+ case TOGETHER:
423
+ if (responseModel) {
424
+ return new togetherai.ChatTogetherAI({
425
+ apiKey: process.env.TOGETHER_API_KEY || llmKeys.together,
426
+ model: provider.model
427
+ }).withStructuredOutput(responseModel);
428
+ }
429
+ return new togetherai.ChatTogetherAI({
430
+ apiKey: process.env.TOGETHER_API_KEY || llmKeys.together,
431
+ model: provider.model
432
+ });
433
+ default:
434
+ throw new Error(`Unsupported provider: ${provider.provider}`);
435
+ }
436
+ }
437
+ async function callLLM(provider, options, llmKeys) {
438
+ const model = getLangChainModel(provider, llmKeys, options.responseModel);
439
+ const langChainMessages = options.messages.map(convertToLangChainMessage);
440
+ const response = await model.invoke(langChainMessages);
441
+ return extractContent(response);
442
+ }
443
+ function convertToLangChainMessage(msg) {
444
+ switch (msg.role) {
445
+ case "user":
446
+ return new messages.HumanMessage(msg.content);
447
+ case "assistant":
448
+ return new messages.AIMessage(msg.content);
449
+ case "system":
450
+ return new messages.SystemMessage(msg.content);
451
+ default:
452
+ return new messages.HumanMessage(msg.content);
453
+ }
454
+ }
455
+ async function* callLLMStream(provider, options, llmKeys) {
456
+ const model = getLangChainModel(provider, llmKeys, options.responseModel);
457
+ const langChainMessages = options.messages.map(convertToLangChainMessage);
458
+ const stream = await model.stream(langChainMessages);
459
+ for await (const chunk of stream) {
460
+ yield extractContent(chunk);
461
+ }
462
+ }
463
+ function extractContent(response) {
464
+ if ("content" in response) {
465
+ return typeof response.content === "string" ? response.content : JSON.stringify(response.content);
466
+ }
467
+ return typeof response === "string" ? response : JSON.stringify(response);
468
+ }
20
469
 
21
470
  var __defProp = Object.defineProperty;
22
471
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -24,6 +473,7 @@ var __publicField = (obj, key, value) => {
24
473
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
25
474
  return value;
26
475
  };
476
+ const SDK_VERSION = packageJson.version;
27
477
  dotenv__namespace.config();
28
478
  const DEFAULT_TIMEOUT = 5;
29
479
  const BASE_URL = "https://not-diamond-server.onrender.com";
@@ -33,35 +483,42 @@ class NotDiamond {
33
483
  __publicField(this, "apiUrl");
34
484
  __publicField(this, "modelSelectUrl");
35
485
  __publicField(this, "feedbackUrl");
486
+ __publicField(this, "createUrl");
487
+ __publicField(this, "llmKeys");
36
488
  this.apiKey = options.apiKey || process.env.NOTDIAMOND_API_KEY || "";
37
489
  this.apiUrl = options.apiUrl || process.env.NOTDIAMOND_API_URL || BASE_URL;
490
+ this.llmKeys = options.llmKeys || {};
38
491
  this.modelSelectUrl = `${this.apiUrl}/v2/modelRouter/modelSelect`;
39
492
  this.feedbackUrl = `${this.apiUrl}/v2/report/metrics/feedback`;
493
+ this.createUrl = `${this.apiUrl}/v2/preferences/userPreferenceCreate`;
40
494
  }
41
495
  getAuthHeader() {
42
496
  return `Bearer ${this.apiKey}`;
43
497
  }
44
498
  async postRequest(url, body) {
45
499
  try {
46
- const response = await fetch(url, {
47
- method: "POST",
500
+ const response = await axios__default.post(url, body, {
48
501
  headers: {
49
502
  Authorization: this.getAuthHeader(),
50
- accept: "application/json",
51
- "content-type": "application/json",
52
- "User-Agent": `TS-SDK/${version}`
53
- },
54
- body: JSON.stringify(body)
503
+ Accept: "application/json",
504
+ "Content-Type": "application/json",
505
+ "User-Agent": `TS-SDK/${SDK_VERSION}`
506
+ }
55
507
  });
56
- if (!response.ok) {
57
- const errorData = await response.json();
58
- return { detail: errorData.detail };
59
- }
60
- return await response.json();
508
+ return response.data;
61
509
  } catch (error) {
510
+ if (axios__default.isAxiosError(error) && error.response) {
511
+ return { detail: "An error occurred." };
512
+ }
513
+ console.error("error", error);
62
514
  return { detail: "An unexpected error occurred." };
63
515
  }
64
516
  }
517
+ /**
518
+ * Selects the best model for the given messages.
519
+ * @param options The options for the model.
520
+ * @returns The results of the model.
521
+ */
65
522
  async modelSelect(options) {
66
523
  const requestBody = {
67
524
  messages: options.messages,
@@ -77,7 +534,10 @@ class NotDiamond {
77
534
  ...provider.outputPrice !== void 0 && {
78
535
  output_price: provider.outputPrice
79
536
  },
80
- ...provider.latency !== void 0 && { latency: provider.latency }
537
+ ...provider.latency !== void 0 && { latency: provider.latency },
538
+ ...provider.isCustom !== void 0 && {
539
+ is_custom: provider.isCustom
540
+ }
81
541
  })),
82
542
  ...options.tradeoff && {
83
543
  tradeoff: options.tradeoff
@@ -96,6 +556,9 @@ class NotDiamond {
96
556
  ...options.default && { default: options.default },
97
557
  ...options.previousSession && {
98
558
  previous_session: options.previousSession
559
+ },
560
+ ...options.responseModel && {
561
+ response_model: options.responseModel
99
562
  }
100
563
  };
101
564
  return this.postRequest(
@@ -103,6 +566,11 @@ class NotDiamond {
103
566
  requestBody
104
567
  );
105
568
  }
569
+ /**
570
+ * Sends feedback to the NotDiamond API.
571
+ * @param options The options for the feedback.
572
+ * @returns The results of the feedback.
573
+ */
106
574
  async feedback(options) {
107
575
  return this.postRequest(this.feedbackUrl, {
108
576
  session_id: options.sessionId,
@@ -110,6 +578,74 @@ class NotDiamond {
110
578
  provider: options.provider
111
579
  });
112
580
  }
581
+ /**
582
+ * Creates a preference id.
583
+ * @returns The preference id.
584
+ */
585
+ async createPreferenceId() {
586
+ return this.postRequest(this.createUrl, {});
587
+ }
588
+ /**
589
+ *
590
+ * @param options The options for the model.
591
+ * @returns A promise that resolves to the results of the model.
592
+ */
593
+ async acreate(options) {
594
+ const selectedModel = await this.modelSelect(options);
595
+ const { providers } = selectedModel;
596
+ const content = await callLLM(providers[0], options, this.llmKeys);
597
+ return { content, providers };
598
+ }
599
+ /**
600
+ *
601
+ * @param options The options for the model.
602
+ * @param callback Optional callback function to handle the result.
603
+ * @returns A promise that resolves to the results of the model or a callback function
604
+ */
605
+ create(options, callback) {
606
+ const promise = this.acreate(options);
607
+ if (callback) {
608
+ promise.then((result) => callback(null, result)).catch((error) => callback(error));
609
+ } else {
610
+ return promise;
611
+ }
612
+ }
613
+ /**
614
+ * Streams the results of the model asynchronously.
615
+ * @param options The options for the model.
616
+ * @returns A promise that resolves to an object containing the provider and an AsyncIterable of strings.
617
+ */
618
+ async astream(options) {
619
+ const selectedModel = await this.modelSelect(options);
620
+ const { providers } = selectedModel;
621
+ const stream = await Promise.resolve(
622
+ callLLMStream(providers?.[0] || "openai", options, this.llmKeys)
623
+ );
624
+ return { provider: providers?.[0] || "openai", stream };
625
+ }
626
+ /**
627
+ * Streams the results of the model.
628
+ * @param options The options for the model.
629
+ * @param callback Optional callback function to handle each chunk of the stream.
630
+ * @returns A promise that resolves to an object containing the provider and an AsyncIterable of strings or a callback function
631
+ */
632
+ stream(options, callback) {
633
+ if (!options.llmProviders || options.llmProviders.length === 0) {
634
+ throw new Error("No LLM providers specified");
635
+ }
636
+ const promise = this.astream(options);
637
+ if (callback) {
638
+ promise.then(async ({ provider, stream }) => {
639
+ for await (const chunk of stream) {
640
+ callback(null, { provider, chunk });
641
+ }
642
+ }).catch((error) => callback(error));
643
+ } else {
644
+ return promise;
645
+ }
646
+ }
113
647
  }
114
648
 
115
649
  exports.NotDiamond = NotDiamond;
650
+ exports.SupportedModel = SupportedModel;
651
+ exports.SupportedProvider = SupportedProvider;