notdiamond 1.1.1 → 1.1.3
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/package.json +4 -2
- package/dist/index.cjs +0 -571
- package/dist/index.d.cts +0 -212
- package/dist/index.d.mts +0 -212
- package/dist/index.d.ts +0 -212
- package/dist/index.mjs +0 -550
package/dist/index.mjs
DELETED
|
@@ -1,550 +0,0 @@
|
|
|
1
|
-
import * as dotenv from 'dotenv';
|
|
2
|
-
import { ChatOpenAI } from '@langchain/openai';
|
|
3
|
-
import { AIMessage, SystemMessage, HumanMessage } from '@langchain/core/messages';
|
|
4
|
-
import { ChatAnthropic } from '@langchain/anthropic';
|
|
5
|
-
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
|
|
6
|
-
import { ChatMistralAI } from '@langchain/mistralai';
|
|
7
|
-
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
8
|
-
import axios from 'axios';
|
|
9
|
-
import { ChatCohere } from '@langchain/cohere';
|
|
10
|
-
import { ChatTogetherAI } from '@langchain/community/chat_models/togetherai';
|
|
11
|
-
|
|
12
|
-
const version = "1.1.0";
|
|
13
|
-
const packageJson = {
|
|
14
|
-
version: version};
|
|
15
|
-
|
|
16
|
-
class ChatPerplexity extends BaseChatModel {
|
|
17
|
-
_generate(messages, options, runManager) {
|
|
18
|
-
throw new Error(
|
|
19
|
-
"Method not implemented." + JSON.stringify(messages) + JSON.stringify(options) + JSON.stringify(runManager)
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
apiKey;
|
|
23
|
-
model;
|
|
24
|
-
constructor({ apiKey, model }) {
|
|
25
|
-
super({});
|
|
26
|
-
this.apiKey = apiKey;
|
|
27
|
-
this.model = model;
|
|
28
|
-
}
|
|
29
|
-
_llmType() {
|
|
30
|
-
return "perplexity";
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Invokes the Perplexity model.
|
|
34
|
-
* @param messages The messages to send to the model.
|
|
35
|
-
* @returns The results of the model.
|
|
36
|
-
*/
|
|
37
|
-
async invoke(messages) {
|
|
38
|
-
try {
|
|
39
|
-
const { data } = await axios.post(
|
|
40
|
-
"https://api.perplexity.ai/chat/completions",
|
|
41
|
-
{
|
|
42
|
-
model: this.model,
|
|
43
|
-
messages: messages.map((m) => ({
|
|
44
|
-
role: m._getType() === "human" ? "user" : m._getType(),
|
|
45
|
-
content: m.content
|
|
46
|
-
}))
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
headers: {
|
|
50
|
-
Authorization: `Bearer ${this.apiKey}`
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
);
|
|
54
|
-
return new AIMessage(data.choices[0].message.content);
|
|
55
|
-
} catch (error) {
|
|
56
|
-
if (axios.isAxiosError(error) && error.response) {
|
|
57
|
-
throw new Error(`Perplexity API error: ${error.response.statusText}`);
|
|
58
|
-
}
|
|
59
|
-
throw error;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const SupportedProvider = {
|
|
65
|
-
OPENAI: "openai",
|
|
66
|
-
ANTHROPIC: "anthropic",
|
|
67
|
-
GOOGLE: "google",
|
|
68
|
-
MISTRAL: "mistral",
|
|
69
|
-
PERPLEXITY: "perplexity",
|
|
70
|
-
COHERE: "cohere",
|
|
71
|
-
TOGETHERAI: "togetherai"
|
|
72
|
-
};
|
|
73
|
-
const SupportedModel = {
|
|
74
|
-
GPT_3_5_TURBO: "gpt-3.5-turbo",
|
|
75
|
-
GPT_3_5_TURBO_0125: "gpt-3.5-turbo-0125",
|
|
76
|
-
GPT_4: "gpt-4",
|
|
77
|
-
GPT_4_0613: "gpt-4-0613",
|
|
78
|
-
GPT_4_1106_PREVIEW: "gpt-4-1106-preview",
|
|
79
|
-
GPT_4_TURBO: "gpt-4-turbo",
|
|
80
|
-
GPT_4_TURBO_PREVIEW: "gpt-4-turbo-preview",
|
|
81
|
-
GPT_4_TURBO_2024_04_09: "gpt-4-turbo-2024-04-09",
|
|
82
|
-
GPT_4O_2024_05_13: "gpt-4o-2024-05-13",
|
|
83
|
-
GPT_4O_2024_08_06: "gpt-4o-2024-08-06",
|
|
84
|
-
GPT_4O: "gpt-4o",
|
|
85
|
-
GPT_4O_MINI_2024_07_18: "gpt-4o-mini-2024-07-18",
|
|
86
|
-
GPT_4O_MINI: "gpt-4o-mini",
|
|
87
|
-
GPT_4_0125_PREVIEW: "gpt-4-0125-preview",
|
|
88
|
-
CHATGPT_4O_LATEST: "chatgpt-4o-latest",
|
|
89
|
-
O1_PREVIEW: "o1-preview",
|
|
90
|
-
O1_PREVIEW_2024_09_12: "o1-preview-2024-09-12",
|
|
91
|
-
O1_MINI: "o1-mini",
|
|
92
|
-
O1_MINI_2024_09_12: "o1-mini-2024-09-12",
|
|
93
|
-
CLAUDE_2_1: "claude-2.1",
|
|
94
|
-
CLAUDE_3_OPUS_20240229: "claude-3-opus-20240229",
|
|
95
|
-
CLAUDE_3_SONNET_20240229: "claude-3-sonnet-20240229",
|
|
96
|
-
CLAUDE_3_5_SONNET_20240620: "claude-3-5-sonnet-20240620",
|
|
97
|
-
CLAUDE_3_5_SONNET_20241022: "claude-3-5-sonnet-20241022",
|
|
98
|
-
CLAUDE_3_5_SONNET_LATEST: "claude-3-5-sonnet-latest",
|
|
99
|
-
CLAUDE_3_HAIKU_20240307: "claude-3-haiku-20240307",
|
|
100
|
-
CLAUDE_3_5_HAIKU_20241022: "claude-3-5-haiku-20241022",
|
|
101
|
-
GEMINI_PRO: "gemini-pro",
|
|
102
|
-
GEMINI_1_PRO_LATEST: "gemini-1.0-pro-latest",
|
|
103
|
-
GEMINI_15_PRO_LATEST: "gemini-1.5-pro-latest",
|
|
104
|
-
GEMINI_15_PRO_EXP_0801: "gemini-1.5-pro-exp-0801",
|
|
105
|
-
GEMINI_15_FLASH_LATEST: "gemini-1.5-flash-latest",
|
|
106
|
-
COMMAND_R: "command-r",
|
|
107
|
-
COMMAND_R_PLUS: "command-r-plus",
|
|
108
|
-
MISTRAL_LARGE_LATEST: "mistral-large-latest",
|
|
109
|
-
MISTRAL_LARGE_2407: "mistral-large-2407",
|
|
110
|
-
MISTRAL_LARGE_2402: "mistral-large-2402",
|
|
111
|
-
MISTRAL_MEDIUM_LATEST: "mistral-medium-latest",
|
|
112
|
-
MISTRAL_SMALL_LATEST: "mistral-small-latest",
|
|
113
|
-
CODESTRAL_LATEST: "codestral-latest",
|
|
114
|
-
OPEN_MISTRAL_7B: "open-mistral-7b",
|
|
115
|
-
OPEN_MIXTRAL_8X7B: "open-mixtral-8x7b",
|
|
116
|
-
OPEN_MIXTRAL_8X22B: "open-mixtral-8x22b",
|
|
117
|
-
MISTRAL_7B_INSTRUCT_V0_2: "Mistral-7B-Instruct-v0.2",
|
|
118
|
-
MIXTRAL_8X7B_INSTRUCT_V0_1: "Mixtral-8x7B-Instruct-v0.1",
|
|
119
|
-
MIXTRAL_8X22B_INSTRUCT_V0_1: "Mixtral-8x22B-Instruct-v0.1",
|
|
120
|
-
LLAMA_3_70B_CHAT_HF: "Llama-3-70b-chat-hf",
|
|
121
|
-
LLAMA_3_8B_CHAT_HF: "Llama-3-8b-chat-hf",
|
|
122
|
-
QWEN2_72B_INSTRUCT: "Qwen2-72B-Instruct",
|
|
123
|
-
LLAMA_3_1_8B_INSTRUCT_TURBO: "Meta-Llama-3.1-8B-Instruct-Turbo",
|
|
124
|
-
LLAMA_3_1_70B_INSTRUCT_TURBO: "Meta-Llama-3.1-70B-Instruct-Turbo",
|
|
125
|
-
LLAMA_3_1_405B_INSTRUCT_TURBO: "Meta-Llama-3.1-405B-Instruct-Turbo",
|
|
126
|
-
PERPLEXITY_SONAR: "sonar",
|
|
127
|
-
OPEN_MISTRAL_NEMO: "open-mistral-nemo",
|
|
128
|
-
DEEPSEEK_R1: "DeepSeek-R1"
|
|
129
|
-
};
|
|
130
|
-
({
|
|
131
|
-
[SupportedProvider.OPENAI]: [
|
|
132
|
-
SupportedModel.GPT_3_5_TURBO,
|
|
133
|
-
SupportedModel.GPT_3_5_TURBO_0125,
|
|
134
|
-
SupportedModel.GPT_4,
|
|
135
|
-
SupportedModel.GPT_4_0613,
|
|
136
|
-
SupportedModel.GPT_4_1106_PREVIEW,
|
|
137
|
-
SupportedModel.GPT_4_TURBO,
|
|
138
|
-
SupportedModel.GPT_4_TURBO_PREVIEW,
|
|
139
|
-
SupportedModel.GPT_4_TURBO_2024_04_09,
|
|
140
|
-
SupportedModel.GPT_4O_2024_05_13,
|
|
141
|
-
SupportedModel.GPT_4O_2024_08_06,
|
|
142
|
-
SupportedModel.GPT_4O,
|
|
143
|
-
SupportedModel.GPT_4O_MINI_2024_07_18,
|
|
144
|
-
SupportedModel.GPT_4O_MINI,
|
|
145
|
-
SupportedModel.GPT_4_0125_PREVIEW,
|
|
146
|
-
SupportedModel.O1_PREVIEW,
|
|
147
|
-
SupportedModel.O1_PREVIEW_2024_09_12,
|
|
148
|
-
SupportedModel.O1_MINI,
|
|
149
|
-
SupportedModel.O1_MINI_2024_09_12,
|
|
150
|
-
SupportedModel.CHATGPT_4O_LATEST
|
|
151
|
-
],
|
|
152
|
-
[SupportedProvider.ANTHROPIC]: [
|
|
153
|
-
SupportedModel.CLAUDE_2_1,
|
|
154
|
-
SupportedModel.CLAUDE_3_OPUS_20240229,
|
|
155
|
-
SupportedModel.CLAUDE_3_SONNET_20240229,
|
|
156
|
-
SupportedModel.CLAUDE_3_5_SONNET_20240620,
|
|
157
|
-
SupportedModel.CLAUDE_3_5_SONNET_20241022,
|
|
158
|
-
SupportedModel.CLAUDE_3_5_SONNET_LATEST,
|
|
159
|
-
SupportedModel.CLAUDE_3_HAIKU_20240307,
|
|
160
|
-
SupportedModel.CLAUDE_3_5_HAIKU_20241022
|
|
161
|
-
],
|
|
162
|
-
[SupportedProvider.GOOGLE]: [
|
|
163
|
-
SupportedModel.GEMINI_PRO,
|
|
164
|
-
SupportedModel.GEMINI_1_PRO_LATEST,
|
|
165
|
-
SupportedModel.GEMINI_15_PRO_LATEST,
|
|
166
|
-
SupportedModel.GEMINI_15_PRO_EXP_0801,
|
|
167
|
-
SupportedModel.GEMINI_15_FLASH_LATEST
|
|
168
|
-
],
|
|
169
|
-
[SupportedProvider.MISTRAL]: [
|
|
170
|
-
SupportedModel.MISTRAL_LARGE_LATEST,
|
|
171
|
-
SupportedModel.MISTRAL_LARGE_2407,
|
|
172
|
-
SupportedModel.MISTRAL_LARGE_2402,
|
|
173
|
-
SupportedModel.MISTRAL_MEDIUM_LATEST,
|
|
174
|
-
SupportedModel.MISTRAL_SMALL_LATEST,
|
|
175
|
-
SupportedModel.CODESTRAL_LATEST,
|
|
176
|
-
SupportedModel.OPEN_MISTRAL_7B,
|
|
177
|
-
SupportedModel.OPEN_MIXTRAL_8X7B,
|
|
178
|
-
SupportedModel.OPEN_MIXTRAL_8X22B,
|
|
179
|
-
SupportedModel.OPEN_MISTRAL_NEMO
|
|
180
|
-
],
|
|
181
|
-
[SupportedProvider.PERPLEXITY]: [
|
|
182
|
-
SupportedModel.PERPLEXITY_SONAR
|
|
183
|
-
],
|
|
184
|
-
[SupportedProvider.COHERE]: [
|
|
185
|
-
SupportedModel.COMMAND_R,
|
|
186
|
-
SupportedModel.COMMAND_R_PLUS
|
|
187
|
-
],
|
|
188
|
-
[SupportedProvider.TOGETHERAI]: [
|
|
189
|
-
SupportedModel.MISTRAL_7B_INSTRUCT_V0_2,
|
|
190
|
-
SupportedModel.MIXTRAL_8X7B_INSTRUCT_V0_1,
|
|
191
|
-
SupportedModel.MIXTRAL_8X22B_INSTRUCT_V0_1,
|
|
192
|
-
SupportedModel.LLAMA_3_70B_CHAT_HF,
|
|
193
|
-
SupportedModel.LLAMA_3_8B_CHAT_HF,
|
|
194
|
-
SupportedModel.QWEN2_72B_INSTRUCT,
|
|
195
|
-
SupportedModel.LLAMA_3_1_8B_INSTRUCT_TURBO,
|
|
196
|
-
SupportedModel.LLAMA_3_1_70B_INSTRUCT_TURBO,
|
|
197
|
-
SupportedModel.LLAMA_3_1_405B_INSTRUCT_TURBO,
|
|
198
|
-
SupportedModel.DEEPSEEK_R1
|
|
199
|
-
]
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
function getLangChainModel(provider, llmKeys, responseModel) {
|
|
203
|
-
const { OPENAI, ANTHROPIC, GOOGLE, MISTRAL, PERPLEXITY, COHERE, TOGETHERAI } = SupportedProvider;
|
|
204
|
-
switch (provider.provider) {
|
|
205
|
-
case OPENAI:
|
|
206
|
-
if (responseModel) {
|
|
207
|
-
return new ChatOpenAI({
|
|
208
|
-
modelName: provider.model,
|
|
209
|
-
apiKey: llmKeys.openai || process.env.OPENAI_API_KEY
|
|
210
|
-
}).withStructuredOutput(responseModel);
|
|
211
|
-
}
|
|
212
|
-
return new ChatOpenAI({
|
|
213
|
-
modelName: provider.model,
|
|
214
|
-
apiKey: llmKeys.openai || process.env.OPENAI_API_KEY
|
|
215
|
-
});
|
|
216
|
-
case ANTHROPIC:
|
|
217
|
-
if (responseModel) {
|
|
218
|
-
return new ChatAnthropic({
|
|
219
|
-
modelName: provider.model,
|
|
220
|
-
anthropicApiKey: llmKeys.anthropic || process.env.ANTHROPIC_API_KEY
|
|
221
|
-
}).withStructuredOutput(responseModel);
|
|
222
|
-
}
|
|
223
|
-
return new ChatAnthropic({
|
|
224
|
-
modelName: provider.model,
|
|
225
|
-
anthropicApiKey: llmKeys.anthropic || process.env.ANTHROPIC_API_KEY
|
|
226
|
-
});
|
|
227
|
-
case GOOGLE:
|
|
228
|
-
if (responseModel) {
|
|
229
|
-
return new ChatGoogleGenerativeAI({
|
|
230
|
-
modelName: provider.model,
|
|
231
|
-
apiKey: llmKeys.google || process.env.GOOGLE_API_KEY
|
|
232
|
-
}).withStructuredOutput(responseModel);
|
|
233
|
-
}
|
|
234
|
-
return new ChatGoogleGenerativeAI({
|
|
235
|
-
modelName: provider.model,
|
|
236
|
-
apiKey: llmKeys.google || process.env.GOOGLE_API_KEY
|
|
237
|
-
});
|
|
238
|
-
case MISTRAL:
|
|
239
|
-
if (responseModel) {
|
|
240
|
-
return new ChatMistralAI({
|
|
241
|
-
modelName: provider.model,
|
|
242
|
-
apiKey: llmKeys.mistral || process.env.MISTRAL_API_KEY
|
|
243
|
-
}).withStructuredOutput(responseModel);
|
|
244
|
-
}
|
|
245
|
-
return new ChatMistralAI({
|
|
246
|
-
modelName: provider.model,
|
|
247
|
-
apiKey: llmKeys.mistral || process.env.MISTRAL_API_KEY
|
|
248
|
-
});
|
|
249
|
-
case PERPLEXITY:
|
|
250
|
-
if (responseModel) {
|
|
251
|
-
return new ChatPerplexity({
|
|
252
|
-
apiKey: llmKeys.perplexity || process.env.PPLX_API_KEY || "",
|
|
253
|
-
model: provider.model
|
|
254
|
-
}).withStructuredOutput(responseModel);
|
|
255
|
-
}
|
|
256
|
-
return new ChatPerplexity({
|
|
257
|
-
apiKey: llmKeys.perplexity || process.env.PPLX_API_KEY || "",
|
|
258
|
-
model: provider.model
|
|
259
|
-
});
|
|
260
|
-
case COHERE:
|
|
261
|
-
if (responseModel) {
|
|
262
|
-
return new ChatCohere({
|
|
263
|
-
apiKey: process.env.COHERE_API_KEY || llmKeys.cohere,
|
|
264
|
-
model: provider.model
|
|
265
|
-
}).withStructuredOutput(responseModel);
|
|
266
|
-
}
|
|
267
|
-
return new ChatCohere({
|
|
268
|
-
apiKey: process.env.COHERE_API_KEY || llmKeys.cohere,
|
|
269
|
-
model: provider.model
|
|
270
|
-
});
|
|
271
|
-
case TOGETHERAI:
|
|
272
|
-
if (responseModel) {
|
|
273
|
-
return new ChatTogetherAI({
|
|
274
|
-
apiKey: process.env.TOGETHERAI_API_KEY || llmKeys.togetherai,
|
|
275
|
-
model: getTogetheraiModel(provider.model)
|
|
276
|
-
}).withStructuredOutput(responseModel);
|
|
277
|
-
}
|
|
278
|
-
return new ChatTogetherAI({
|
|
279
|
-
apiKey: process.env.TOGETHERAI_API_KEY || llmKeys.togetherai,
|
|
280
|
-
model: getTogetheraiModel(provider.model)
|
|
281
|
-
});
|
|
282
|
-
default:
|
|
283
|
-
throw new Error(`Unsupported provider: ${provider.provider}`);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
const getTogetheraiModel = (model) => {
|
|
287
|
-
if (model === SupportedModel.MISTRAL_7B_INSTRUCT_V0_2 || model === SupportedModel.MIXTRAL_8X7B_INSTRUCT_V0_1 || model === SupportedModel.MIXTRAL_8X22B_INSTRUCT_V0_1) {
|
|
288
|
-
return `mistralai/${model}`;
|
|
289
|
-
}
|
|
290
|
-
if (model === SupportedModel.LLAMA_3_70B_CHAT_HF || model === SupportedModel.LLAMA_3_8B_CHAT_HF || model === SupportedModel.LLAMA_3_1_8B_INSTRUCT_TURBO || model === SupportedModel.LLAMA_3_1_70B_INSTRUCT_TURBO || model === SupportedModel.LLAMA_3_1_405B_INSTRUCT_TURBO) {
|
|
291
|
-
return `meta-llama/${model}`;
|
|
292
|
-
}
|
|
293
|
-
if (model === SupportedModel.QWEN2_72B_INSTRUCT) {
|
|
294
|
-
return `Qwen/${model}`;
|
|
295
|
-
}
|
|
296
|
-
return model;
|
|
297
|
-
};
|
|
298
|
-
async function callLLM(provider, options, llmKeys, runtimeArgs) {
|
|
299
|
-
const model = getLangChainModel(provider, llmKeys, options.responseModel);
|
|
300
|
-
const langChainMessages = extendProviderSystemPrompt(
|
|
301
|
-
options.messages.map(convertToLangChainMessage),
|
|
302
|
-
options,
|
|
303
|
-
provider
|
|
304
|
-
);
|
|
305
|
-
const response = await model.invoke(langChainMessages, runtimeArgs);
|
|
306
|
-
return extractContent(response);
|
|
307
|
-
}
|
|
308
|
-
function extendProviderSystemPrompt(messages, options, provider) {
|
|
309
|
-
const matchingProvider = options.llmProviders.find(
|
|
310
|
-
(p) => p.provider === provider.provider && p.model === provider.model
|
|
311
|
-
);
|
|
312
|
-
if (matchingProvider && matchingProvider.systemPrompt) {
|
|
313
|
-
messages.unshift(new SystemMessage(matchingProvider.systemPrompt));
|
|
314
|
-
}
|
|
315
|
-
return messages;
|
|
316
|
-
}
|
|
317
|
-
function convertToLangChainMessage(msg) {
|
|
318
|
-
switch (msg.role) {
|
|
319
|
-
case "user":
|
|
320
|
-
return new HumanMessage(msg.content);
|
|
321
|
-
case "assistant":
|
|
322
|
-
return new AIMessage(msg.content);
|
|
323
|
-
case "system":
|
|
324
|
-
return new SystemMessage(msg.content);
|
|
325
|
-
default:
|
|
326
|
-
return new HumanMessage(msg.content);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
async function* callLLMStream(provider, options, llmKeys, runtimeArgs) {
|
|
330
|
-
const model = getLangChainModel(provider, llmKeys, options.responseModel);
|
|
331
|
-
const langChainMessages = extendProviderSystemPrompt(
|
|
332
|
-
options.messages.map(convertToLangChainMessage),
|
|
333
|
-
options,
|
|
334
|
-
provider
|
|
335
|
-
);
|
|
336
|
-
const stream = await model.stream(langChainMessages, runtimeArgs);
|
|
337
|
-
for await (const chunk of stream) {
|
|
338
|
-
yield extractContent(chunk);
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
function extractContent(response) {
|
|
342
|
-
if ("content" in response) {
|
|
343
|
-
return typeof response.content === "string" ? response.content : JSON.stringify(response.content);
|
|
344
|
-
}
|
|
345
|
-
return typeof response === "string" ? response : JSON.stringify(response);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
const SDK_VERSION = packageJson.version;
|
|
349
|
-
dotenv.config();
|
|
350
|
-
const DEFAULT_TIMEOUT = 5;
|
|
351
|
-
const BASE_URL = "https://api.notdiamond.ai";
|
|
352
|
-
class NotDiamond {
|
|
353
|
-
apiKey;
|
|
354
|
-
apiUrl;
|
|
355
|
-
modelSelectUrl;
|
|
356
|
-
feedbackUrl;
|
|
357
|
-
createUrl;
|
|
358
|
-
llmKeys;
|
|
359
|
-
constructor(options = {}) {
|
|
360
|
-
this.apiKey = options.apiKey || process.env.NOTDIAMOND_API_KEY || "";
|
|
361
|
-
this.apiUrl = options.apiUrl || process.env.NOTDIAMOND_API_URL || BASE_URL;
|
|
362
|
-
this.llmKeys = options.llmKeys || {};
|
|
363
|
-
this.modelSelectUrl = `${this.apiUrl}/v2/modelRouter/modelSelect`;
|
|
364
|
-
this.feedbackUrl = `${this.apiUrl}/v2/report/metrics/feedback`;
|
|
365
|
-
this.createUrl = `${this.apiUrl}/v2/preferences/userPreferenceCreate`;
|
|
366
|
-
}
|
|
367
|
-
getAuthHeader() {
|
|
368
|
-
return `Bearer ${this.apiKey}`;
|
|
369
|
-
}
|
|
370
|
-
async postRequest(url, body) {
|
|
371
|
-
try {
|
|
372
|
-
const response = await axios.post(url, body, {
|
|
373
|
-
headers: {
|
|
374
|
-
Authorization: this.getAuthHeader(),
|
|
375
|
-
Accept: "application/json",
|
|
376
|
-
"Content-Type": "application/json",
|
|
377
|
-
"User-Agent": `TS-SDK/${SDK_VERSION}`
|
|
378
|
-
}
|
|
379
|
-
});
|
|
380
|
-
return response.data;
|
|
381
|
-
} catch (error) {
|
|
382
|
-
if (axios.isAxiosError(error) && error.response) {
|
|
383
|
-
return { detail: "An error occurred." };
|
|
384
|
-
}
|
|
385
|
-
console.error("error", error);
|
|
386
|
-
return { detail: "An unexpected error occurred." };
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* Selects the best model for the given messages.
|
|
391
|
-
* @param options The options for the model.
|
|
392
|
-
* @returns The results of the model.
|
|
393
|
-
*/
|
|
394
|
-
async modelSelect(options) {
|
|
395
|
-
const requestBody = {
|
|
396
|
-
messages: options.messages,
|
|
397
|
-
llm_providers: options.llmProviders.map((provider) => ({
|
|
398
|
-
provider: provider.provider,
|
|
399
|
-
model: provider.model,
|
|
400
|
-
...provider.contextLength !== void 0 && {
|
|
401
|
-
context_length: provider.contextLength
|
|
402
|
-
},
|
|
403
|
-
...provider.customInputPrice !== void 0 && {
|
|
404
|
-
input_price: provider.customInputPrice
|
|
405
|
-
},
|
|
406
|
-
...provider.inputPrice !== void 0 && {
|
|
407
|
-
input_price: provider.inputPrice
|
|
408
|
-
},
|
|
409
|
-
...provider.customOutputPrice !== void 0 && {
|
|
410
|
-
output_price: provider.customOutputPrice
|
|
411
|
-
},
|
|
412
|
-
...provider.outputPrice !== void 0 && {
|
|
413
|
-
output_price: provider.outputPrice
|
|
414
|
-
},
|
|
415
|
-
...provider.customLatency !== void 0 && {
|
|
416
|
-
latency: provider.customLatency
|
|
417
|
-
},
|
|
418
|
-
...provider.latency !== void 0 && { latency: provider.latency },
|
|
419
|
-
...provider.isCustom !== void 0 && {
|
|
420
|
-
is_custom: provider.isCustom
|
|
421
|
-
}
|
|
422
|
-
})),
|
|
423
|
-
...options.tradeoff && {
|
|
424
|
-
tradeoff: options.tradeoff
|
|
425
|
-
},
|
|
426
|
-
...options.maxModelDepth && {
|
|
427
|
-
max_model_depth: options.maxModelDepth
|
|
428
|
-
},
|
|
429
|
-
...options.tools && { tools: options.tools },
|
|
430
|
-
...options.hashContent !== void 0 && {
|
|
431
|
-
hash_content: options.hashContent
|
|
432
|
-
},
|
|
433
|
-
...options.preferenceId && { preference_id: options.preferenceId },
|
|
434
|
-
...options.timeout ? { timeout: options.timeout } : {
|
|
435
|
-
timeout: DEFAULT_TIMEOUT
|
|
436
|
-
},
|
|
437
|
-
...options.default && { default: options.default },
|
|
438
|
-
...options.previousSession && {
|
|
439
|
-
previous_session: options.previousSession
|
|
440
|
-
},
|
|
441
|
-
...options.responseModel && {
|
|
442
|
-
response_model: options.responseModel
|
|
443
|
-
}
|
|
444
|
-
};
|
|
445
|
-
return this.postRequest(
|
|
446
|
-
this.modelSelectUrl,
|
|
447
|
-
requestBody
|
|
448
|
-
);
|
|
449
|
-
}
|
|
450
|
-
/**
|
|
451
|
-
* Sends feedback to the NotDiamond API.
|
|
452
|
-
* @param options The options for the feedback.
|
|
453
|
-
* @returns The results of the feedback.
|
|
454
|
-
*/
|
|
455
|
-
async feedback(options) {
|
|
456
|
-
return this.postRequest(this.feedbackUrl, {
|
|
457
|
-
session_id: options.sessionId,
|
|
458
|
-
feedback: options.feedback,
|
|
459
|
-
provider: options.provider
|
|
460
|
-
});
|
|
461
|
-
}
|
|
462
|
-
/**
|
|
463
|
-
* Creates a preference id.
|
|
464
|
-
* @returns The preference id.
|
|
465
|
-
*/
|
|
466
|
-
async createPreferenceId() {
|
|
467
|
-
const response = await this.postRequest(
|
|
468
|
-
this.createUrl,
|
|
469
|
-
{}
|
|
470
|
-
);
|
|
471
|
-
if ("preference_id" in response) {
|
|
472
|
-
return response.preference_id;
|
|
473
|
-
}
|
|
474
|
-
throw new Error("Invalid response: preference_id not found");
|
|
475
|
-
}
|
|
476
|
-
/**
|
|
477
|
-
*
|
|
478
|
-
* @param options The options for the model.
|
|
479
|
-
* @returns A promise that resolves to the results of the model.
|
|
480
|
-
*/
|
|
481
|
-
async acreate(options, runtimeArgs = {}) {
|
|
482
|
-
const selectedModel = await this.modelSelect(options);
|
|
483
|
-
const { providers } = selectedModel;
|
|
484
|
-
const content = await callLLM(
|
|
485
|
-
providers[0],
|
|
486
|
-
options,
|
|
487
|
-
this.llmKeys,
|
|
488
|
-
runtimeArgs
|
|
489
|
-
);
|
|
490
|
-
return { content, providers };
|
|
491
|
-
}
|
|
492
|
-
/**
|
|
493
|
-
*
|
|
494
|
-
* @param options The options for the model.
|
|
495
|
-
* @param callback Optional callback function to handle the result.
|
|
496
|
-
* @returns A promise that resolves to the results of the model or a callback function
|
|
497
|
-
*/
|
|
498
|
-
create(options, runtimeArgs = {}, callback) {
|
|
499
|
-
const promise = this.acreate(options, runtimeArgs);
|
|
500
|
-
if (callback) {
|
|
501
|
-
promise.then((result) => callback(null, result)).catch((error) => callback(error));
|
|
502
|
-
} else {
|
|
503
|
-
return promise;
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
/**
|
|
507
|
-
* Streams the results of the model asynchronously.
|
|
508
|
-
* @param options The options for the model.
|
|
509
|
-
* @returns A promise that resolves to an object containing the provider and an AsyncIterable of strings.
|
|
510
|
-
*/
|
|
511
|
-
async astream(options, runtimeArgs = {}) {
|
|
512
|
-
const selectedModel = await this.modelSelect(options);
|
|
513
|
-
const { providers } = selectedModel;
|
|
514
|
-
const stream = await Promise.resolve(
|
|
515
|
-
callLLMStream(
|
|
516
|
-
providers?.[0] || options.default,
|
|
517
|
-
options,
|
|
518
|
-
this.llmKeys,
|
|
519
|
-
runtimeArgs
|
|
520
|
-
)
|
|
521
|
-
);
|
|
522
|
-
return {
|
|
523
|
-
provider: providers?.[0] || options.default,
|
|
524
|
-
stream
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
/**
|
|
528
|
-
* Streams the results of the model.
|
|
529
|
-
* @param options The options for the model.
|
|
530
|
-
* @param callback Optional callback function to handle each chunk of the stream.
|
|
531
|
-
* @returns A promise that resolves to an object containing the provider and an AsyncIterable of strings or a callback function
|
|
532
|
-
*/
|
|
533
|
-
stream(options, runtimeArgs = {}, callback) {
|
|
534
|
-
if (!options.llmProviders || options.llmProviders.length === 0) {
|
|
535
|
-
throw new Error("No LLM providers specified");
|
|
536
|
-
}
|
|
537
|
-
const promise = this.astream(options, runtimeArgs);
|
|
538
|
-
if (callback) {
|
|
539
|
-
promise.then(async ({ provider, stream }) => {
|
|
540
|
-
for await (const chunk of stream) {
|
|
541
|
-
callback(null, { provider, chunk });
|
|
542
|
-
}
|
|
543
|
-
}).catch((error) => callback(error));
|
|
544
|
-
} else {
|
|
545
|
-
return promise;
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
export { NotDiamond, SupportedModel, SupportedProvider };
|