langchain 0.0.176 → 0.0.178
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/chat_models/iflytek_xinghuo/web.cjs +1 -0
- package/chat_models/iflytek_xinghuo/web.d.ts +1 -0
- package/chat_models/iflytek_xinghuo/web.js +1 -0
- package/chat_models/iflytek_xinghuo.cjs +1 -0
- package/chat_models/iflytek_xinghuo.d.ts +1 -0
- package/chat_models/iflytek_xinghuo.js +1 -0
- package/dist/chat_models/bedrock.cjs +25 -4
- package/dist/chat_models/bedrock.d.ts +2 -1
- package/dist/chat_models/bedrock.js +25 -4
- package/dist/chat_models/cloudflare_workersai.cjs +70 -24
- package/dist/chat_models/cloudflare_workersai.d.ts +6 -2
- package/dist/chat_models/cloudflare_workersai.js +71 -25
- package/dist/chat_models/iflytek_xinghuo/common.cjs +335 -0
- package/dist/chat_models/iflytek_xinghuo/common.d.ts +165 -0
- package/dist/chat_models/iflytek_xinghuo/common.js +331 -0
- package/dist/chat_models/iflytek_xinghuo/index.cjs +35 -0
- package/dist/chat_models/iflytek_xinghuo/index.d.ts +5 -0
- package/dist/chat_models/iflytek_xinghuo/index.js +28 -0
- package/dist/chat_models/iflytek_xinghuo/web.cjs +30 -0
- package/dist/chat_models/iflytek_xinghuo/web.d.ts +5 -0
- package/dist/chat_models/iflytek_xinghuo/web.js +26 -0
- package/dist/chat_models/llama_cpp.cjs +31 -79
- package/dist/chat_models/llama_cpp.d.ts +15 -58
- package/dist/chat_models/llama_cpp.js +32 -80
- package/dist/chat_models/openai.cjs +91 -6
- package/dist/chat_models/openai.d.ts +10 -0
- package/dist/chat_models/openai.js +91 -6
- package/dist/embeddings/hf.cjs +10 -1
- package/dist/embeddings/hf.d.ts +4 -2
- package/dist/embeddings/hf.js +10 -1
- package/dist/embeddings/llama_cpp.cjs +67 -0
- package/dist/embeddings/llama_cpp.d.ts +26 -0
- package/dist/embeddings/llama_cpp.js +63 -0
- package/dist/embeddings/ollama.cjs +7 -1
- package/dist/embeddings/ollama.js +7 -1
- package/dist/graphs/neo4j_graph.cjs +36 -5
- package/dist/graphs/neo4j_graph.js +14 -3
- package/dist/llms/bedrock.cjs +25 -3
- package/dist/llms/bedrock.d.ts +2 -1
- package/dist/llms/bedrock.js +25 -3
- package/dist/llms/cloudflare_workersai.cjs +59 -13
- package/dist/llms/cloudflare_workersai.d.ts +9 -3
- package/dist/llms/cloudflare_workersai.js +59 -13
- package/dist/llms/hf.cjs +10 -1
- package/dist/llms/hf.d.ts +3 -0
- package/dist/llms/hf.js +10 -1
- package/dist/llms/llama_cpp.cjs +25 -65
- package/dist/llms/llama_cpp.d.ts +7 -43
- package/dist/llms/llama_cpp.js +25 -65
- package/dist/load/import_constants.cjs +3 -0
- package/dist/load/import_constants.js +3 -0
- package/dist/prompts/chat.cjs +8 -0
- package/dist/prompts/chat.d.ts +5 -0
- package/dist/prompts/chat.js +8 -0
- package/dist/prompts/few_shot.cjs +162 -1
- package/dist/prompts/few_shot.d.ts +90 -2
- package/dist/prompts/few_shot.js +160 -0
- package/dist/prompts/index.cjs +2 -1
- package/dist/prompts/index.d.ts +1 -1
- package/dist/prompts/index.js +1 -1
- package/dist/retrievers/zep.cjs +26 -3
- package/dist/retrievers/zep.d.ts +11 -2
- package/dist/retrievers/zep.js +26 -3
- package/dist/util/bedrock.d.ts +2 -0
- package/dist/util/event-source-parse.cjs +20 -1
- package/dist/util/event-source-parse.d.ts +2 -0
- package/dist/util/event-source-parse.js +18 -0
- package/dist/util/iflytek_websocket_stream.cjs +81 -0
- package/dist/util/iflytek_websocket_stream.d.ts +27 -0
- package/dist/util/iflytek_websocket_stream.js +77 -0
- package/dist/util/llama_cpp.cjs +34 -0
- package/dist/util/llama_cpp.d.ts +46 -0
- package/dist/util/llama_cpp.js +28 -0
- package/dist/util/openai-format-fndef.cjs +81 -0
- package/dist/util/openai-format-fndef.d.ts +44 -0
- package/dist/util/openai-format-fndef.js +77 -0
- package/dist/util/openapi.d.ts +2 -2
- package/dist/vectorstores/pinecone.cjs +5 -5
- package/dist/vectorstores/pinecone.d.ts +2 -2
- package/dist/vectorstores/pinecone.js +5 -5
- package/embeddings/llama_cpp.cjs +1 -0
- package/embeddings/llama_cpp.d.ts +1 -0
- package/embeddings/llama_cpp.js +1 -0
- package/package.json +34 -5
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { AIMessage, ChatMessage, } from "../../schema/index.js";
|
|
2
|
+
import { getEnvironmentVariable } from "../../util/env.js";
|
|
3
|
+
import { IterableReadableStream } from "../../util/stream.js";
|
|
4
|
+
import { BaseChatModel } from "../base.js";
|
|
5
|
+
/**
|
|
6
|
+
* Function that extracts the custom role of a generic chat message.
|
|
7
|
+
* @param message Chat message from which to extract the custom role.
|
|
8
|
+
* @returns The custom role of the chat message.
|
|
9
|
+
*/
|
|
10
|
+
function extractGenericMessageCustomRole(message) {
|
|
11
|
+
if (message.role !== "assistant" && message.role !== "user") {
|
|
12
|
+
console.warn(`Unknown message role: ${message.role}`);
|
|
13
|
+
}
|
|
14
|
+
return message.role;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Function that converts a base message to a Xinghuo message role.
|
|
18
|
+
* @param message Base message to convert.
|
|
19
|
+
* @returns The Xinghuo message role.
|
|
20
|
+
*/
|
|
21
|
+
function messageToXinghuoRole(message) {
|
|
22
|
+
const type = message._getType();
|
|
23
|
+
switch (type) {
|
|
24
|
+
case "ai":
|
|
25
|
+
return "assistant";
|
|
26
|
+
case "human":
|
|
27
|
+
return "user";
|
|
28
|
+
case "system":
|
|
29
|
+
throw new Error("System messages should not be here");
|
|
30
|
+
case "function":
|
|
31
|
+
throw new Error("Function messages not supported");
|
|
32
|
+
case "generic": {
|
|
33
|
+
if (!ChatMessage.isInstance(message))
|
|
34
|
+
throw new Error("Invalid generic chat message");
|
|
35
|
+
return extractGenericMessageCustomRole(message);
|
|
36
|
+
}
|
|
37
|
+
default:
|
|
38
|
+
throw new Error(`Unknown message type: ${type}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Wrapper around IflytekXingHuo large language models that use the Chat endpoint.
|
|
43
|
+
*
|
|
44
|
+
* To use you should have the `IFLYTEK_API_KEY` and `IFLYTEK_API_SECRET` and `IFLYTEK_APPID`
|
|
45
|
+
* environment variable set.
|
|
46
|
+
*
|
|
47
|
+
* @augments BaseChatModel
|
|
48
|
+
* @augments IflytekXinghuoChatInput
|
|
49
|
+
*/
|
|
50
|
+
export class BaseChatIflytekXinghuo extends BaseChatModel {
|
|
51
|
+
static lc_name() {
|
|
52
|
+
return "ChatIflytekXinghuo";
|
|
53
|
+
}
|
|
54
|
+
get callKeys() {
|
|
55
|
+
return ["stop", "signal", "options"];
|
|
56
|
+
}
|
|
57
|
+
get lc_secrets() {
|
|
58
|
+
return {
|
|
59
|
+
iflytekApiKey: "IFLYTEK_API_KEY",
|
|
60
|
+
iflytekApiSecret: "IFLYTEK_API_SECRET",
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
get lc_aliases() {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
constructor(fields) {
|
|
67
|
+
super(fields ?? {});
|
|
68
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: true
|
|
73
|
+
});
|
|
74
|
+
Object.defineProperty(this, "version", {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
configurable: true,
|
|
77
|
+
writable: true,
|
|
78
|
+
value: "v2.1"
|
|
79
|
+
});
|
|
80
|
+
Object.defineProperty(this, "iflytekAppid", {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
configurable: true,
|
|
83
|
+
writable: true,
|
|
84
|
+
value: void 0
|
|
85
|
+
});
|
|
86
|
+
Object.defineProperty(this, "iflytekApiKey", {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
configurable: true,
|
|
89
|
+
writable: true,
|
|
90
|
+
value: void 0
|
|
91
|
+
});
|
|
92
|
+
Object.defineProperty(this, "iflytekApiSecret", {
|
|
93
|
+
enumerable: true,
|
|
94
|
+
configurable: true,
|
|
95
|
+
writable: true,
|
|
96
|
+
value: void 0
|
|
97
|
+
});
|
|
98
|
+
Object.defineProperty(this, "userId", {
|
|
99
|
+
enumerable: true,
|
|
100
|
+
configurable: true,
|
|
101
|
+
writable: true,
|
|
102
|
+
value: void 0
|
|
103
|
+
});
|
|
104
|
+
Object.defineProperty(this, "apiUrl", {
|
|
105
|
+
enumerable: true,
|
|
106
|
+
configurable: true,
|
|
107
|
+
writable: true,
|
|
108
|
+
value: void 0
|
|
109
|
+
});
|
|
110
|
+
Object.defineProperty(this, "domain", {
|
|
111
|
+
enumerable: true,
|
|
112
|
+
configurable: true,
|
|
113
|
+
writable: true,
|
|
114
|
+
value: void 0
|
|
115
|
+
});
|
|
116
|
+
Object.defineProperty(this, "temperature", {
|
|
117
|
+
enumerable: true,
|
|
118
|
+
configurable: true,
|
|
119
|
+
writable: true,
|
|
120
|
+
value: 0.5
|
|
121
|
+
});
|
|
122
|
+
Object.defineProperty(this, "max_tokens", {
|
|
123
|
+
enumerable: true,
|
|
124
|
+
configurable: true,
|
|
125
|
+
writable: true,
|
|
126
|
+
value: 2048
|
|
127
|
+
});
|
|
128
|
+
Object.defineProperty(this, "top_k", {
|
|
129
|
+
enumerable: true,
|
|
130
|
+
configurable: true,
|
|
131
|
+
writable: true,
|
|
132
|
+
value: 4
|
|
133
|
+
});
|
|
134
|
+
Object.defineProperty(this, "streaming", {
|
|
135
|
+
enumerable: true,
|
|
136
|
+
configurable: true,
|
|
137
|
+
writable: true,
|
|
138
|
+
value: false
|
|
139
|
+
});
|
|
140
|
+
const iflytekAppid = fields?.iflytekAppid ?? getEnvironmentVariable("IFLYTEK_APPID");
|
|
141
|
+
if (!iflytekAppid) {
|
|
142
|
+
throw new Error("Iflytek APPID not found");
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
this.iflytekAppid = iflytekAppid;
|
|
146
|
+
}
|
|
147
|
+
const iflytekApiKey = fields?.iflytekApiKey ?? getEnvironmentVariable("IFLYTEK_API_KEY");
|
|
148
|
+
if (!iflytekApiKey) {
|
|
149
|
+
throw new Error("Iflytek API key not found");
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
this.iflytekApiKey = iflytekApiKey;
|
|
153
|
+
}
|
|
154
|
+
const iflytekApiSecret = fields?.iflytekApiSecret ?? getEnvironmentVariable("IFLYTEK_API_SECRET");
|
|
155
|
+
if (!iflytekApiSecret) {
|
|
156
|
+
throw new Error("Iflytek API secret not found");
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
this.iflytekApiSecret = iflytekApiSecret;
|
|
160
|
+
}
|
|
161
|
+
this.userId = fields?.userId ?? this.userId;
|
|
162
|
+
this.streaming = fields?.streaming ?? this.streaming;
|
|
163
|
+
this.temperature = fields?.temperature ?? this.temperature;
|
|
164
|
+
this.max_tokens = fields?.max_tokens ?? this.max_tokens;
|
|
165
|
+
this.top_k = fields?.top_k ?? this.top_k;
|
|
166
|
+
this.version = fields?.version ?? this.version;
|
|
167
|
+
if (["v1.1", "v2.1", "v3.1"].includes(this.version)) {
|
|
168
|
+
switch (this.version) {
|
|
169
|
+
case "v1.1":
|
|
170
|
+
this.domain = "general";
|
|
171
|
+
break;
|
|
172
|
+
case "v2.1":
|
|
173
|
+
this.domain = "generalv2";
|
|
174
|
+
break;
|
|
175
|
+
case "v3.1":
|
|
176
|
+
this.domain = "generalv3";
|
|
177
|
+
break;
|
|
178
|
+
default:
|
|
179
|
+
this.domain = "generalv2";
|
|
180
|
+
}
|
|
181
|
+
this.apiUrl = `wss://spark-api.xf-yun.com/${this.version}/chat`;
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
throw new Error(`Invalid model version: ${this.version}`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Get the identifying parameters for the model
|
|
189
|
+
*/
|
|
190
|
+
identifyingParams() {
|
|
191
|
+
return {
|
|
192
|
+
version: this.version,
|
|
193
|
+
...this.invocationParams(),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Get the parameters used to invoke the model
|
|
198
|
+
*/
|
|
199
|
+
invocationParams() {
|
|
200
|
+
return {
|
|
201
|
+
streaming: this.streaming,
|
|
202
|
+
temperature: this.temperature,
|
|
203
|
+
top_k: this.top_k,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
async completion(request, stream, signal) {
|
|
207
|
+
const webSocketStream = await this.openWebSocketStream({
|
|
208
|
+
signal,
|
|
209
|
+
});
|
|
210
|
+
const connection = await webSocketStream.connection;
|
|
211
|
+
const header = {
|
|
212
|
+
app_id: this.iflytekAppid,
|
|
213
|
+
uid: this.userId,
|
|
214
|
+
};
|
|
215
|
+
const parameter = {
|
|
216
|
+
chat: {
|
|
217
|
+
domain: this.domain,
|
|
218
|
+
temperature: request.temperature ?? this.temperature,
|
|
219
|
+
max_tokens: request.max_tokens ?? this.max_tokens,
|
|
220
|
+
top_k: request.top_k ?? this.top_k,
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
const payload = {
|
|
224
|
+
message: {
|
|
225
|
+
text: request.messages,
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
const message = JSON.stringify({
|
|
229
|
+
header,
|
|
230
|
+
parameter,
|
|
231
|
+
payload,
|
|
232
|
+
});
|
|
233
|
+
const { writable, readable } = connection;
|
|
234
|
+
const writer = writable.getWriter();
|
|
235
|
+
await writer.write(message);
|
|
236
|
+
const streams = IterableReadableStream.fromReadableStream(readable);
|
|
237
|
+
if (stream) {
|
|
238
|
+
return streams;
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
let response = { result: "" };
|
|
242
|
+
for await (const chunk of streams) {
|
|
243
|
+
const data = JSON.parse(chunk);
|
|
244
|
+
const { header, payload } = data;
|
|
245
|
+
if (header.code === 0) {
|
|
246
|
+
if (header.status === 0) {
|
|
247
|
+
response.result = payload.choices?.text[0]?.content ?? "";
|
|
248
|
+
}
|
|
249
|
+
else if (header.status === 1) {
|
|
250
|
+
response.result += payload.choices?.text[0]?.content ?? "";
|
|
251
|
+
}
|
|
252
|
+
else if (header.status === 2) {
|
|
253
|
+
response = { ...response, usage: payload.usage?.text };
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
void streams.cancel();
|
|
262
|
+
void webSocketStream.close();
|
|
263
|
+
return response;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
async _generate(messages, options, runManager) {
|
|
267
|
+
const tokenUsage = {};
|
|
268
|
+
const params = this.invocationParams();
|
|
269
|
+
const messagesMapped = messages.map((message) => ({
|
|
270
|
+
role: messageToXinghuoRole(message),
|
|
271
|
+
content: message.content,
|
|
272
|
+
}));
|
|
273
|
+
const data = params.streaming
|
|
274
|
+
? await (async () => {
|
|
275
|
+
const streams = await this.completion({ messages: messagesMapped, ...params }, true, options.signal);
|
|
276
|
+
let response = { result: "" };
|
|
277
|
+
for await (const chunk of streams) {
|
|
278
|
+
const data = JSON.parse(chunk);
|
|
279
|
+
const { header, payload } = data;
|
|
280
|
+
if (header.code === 0) {
|
|
281
|
+
if (header.status === 0) {
|
|
282
|
+
response.result = payload.choices?.text[0]?.content ?? "";
|
|
283
|
+
}
|
|
284
|
+
else if (header.status === 1) {
|
|
285
|
+
response.result += payload.choices?.text[0]?.content ?? "";
|
|
286
|
+
}
|
|
287
|
+
else if (header.status === 2) {
|
|
288
|
+
response = { ...response, usage: payload.usage?.text };
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
void runManager?.handleLLMNewToken(payload.choices?.text[0]?.content);
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
void streams.cancel();
|
|
298
|
+
return response;
|
|
299
|
+
})()
|
|
300
|
+
: await this.completion({ messages: messagesMapped, ...params }, false, options.signal);
|
|
301
|
+
const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens, } = data.usage ?? {};
|
|
302
|
+
if (completionTokens) {
|
|
303
|
+
tokenUsage.completionTokens =
|
|
304
|
+
(tokenUsage.completionTokens ?? 0) + completionTokens;
|
|
305
|
+
}
|
|
306
|
+
if (promptTokens) {
|
|
307
|
+
tokenUsage.promptTokens = (tokenUsage.promptTokens ?? 0) + promptTokens;
|
|
308
|
+
}
|
|
309
|
+
if (totalTokens) {
|
|
310
|
+
tokenUsage.totalTokens = (tokenUsage.totalTokens ?? 0) + totalTokens;
|
|
311
|
+
}
|
|
312
|
+
const generations = [];
|
|
313
|
+
const text = data.result ?? "";
|
|
314
|
+
generations.push({
|
|
315
|
+
text,
|
|
316
|
+
message: new AIMessage(text),
|
|
317
|
+
});
|
|
318
|
+
return {
|
|
319
|
+
generations,
|
|
320
|
+
llmOutput: { tokenUsage },
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
/** @ignore */
|
|
324
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
325
|
+
_combineLLMOutput() {
|
|
326
|
+
return [];
|
|
327
|
+
}
|
|
328
|
+
_llmType() {
|
|
329
|
+
return "iflytek_xinghuo";
|
|
330
|
+
}
|
|
331
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ChatIflytekXinghuo = void 0;
|
|
7
|
+
const ws_1 = __importDefault(require("ws"));
|
|
8
|
+
const common_js_1 = require("./common.cjs");
|
|
9
|
+
const iflytek_websocket_stream_js_1 = require("../../util/iflytek_websocket_stream.cjs");
|
|
10
|
+
class WebSocketStream extends iflytek_websocket_stream_js_1.BaseWebSocketStream {
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
openWebSocket(url, options) {
|
|
14
|
+
return new ws_1.default(url, options.protocols ?? []);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
class ChatIflytekXinghuo extends common_js_1.BaseChatIflytekXinghuo {
|
|
18
|
+
async openWebSocketStream(options) {
|
|
19
|
+
const host = "spark-api.xf-yun.com";
|
|
20
|
+
const date = new Date().toUTCString();
|
|
21
|
+
const url = `GET /${this.version}/chat HTTP/1.1`;
|
|
22
|
+
const { createHmac } = await import("crypto");
|
|
23
|
+
const hash = createHmac("sha256", this.iflytekApiSecret)
|
|
24
|
+
.update(`host: ${host}\ndate: ${date}\n${url}`)
|
|
25
|
+
.digest("base64");
|
|
26
|
+
const authorization_origin = `api_key="${this.iflytekApiKey}", algorithm="hmac-sha256", headers="host date request-line", signature="${hash}"`;
|
|
27
|
+
const authorization = Buffer.from(authorization_origin).toString("base64");
|
|
28
|
+
let authWebSocketUrl = this.apiUrl;
|
|
29
|
+
authWebSocketUrl += `?authorization=${authorization}`;
|
|
30
|
+
authWebSocketUrl += `&host=${encodeURIComponent(host)}`;
|
|
31
|
+
authWebSocketUrl += `&date=${encodeURIComponent(date)}`;
|
|
32
|
+
return new WebSocketStream(authWebSocketUrl, options);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.ChatIflytekXinghuo = ChatIflytekXinghuo;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { BaseChatIflytekXinghuo } from "./common.js";
|
|
2
|
+
import { WebSocketStreamOptions } from "../../util/iflytek_websocket_stream.js";
|
|
3
|
+
export declare class ChatIflytekXinghuo extends BaseChatIflytekXinghuo {
|
|
4
|
+
openWebSocketStream<WebSocketStream>(options: WebSocketStreamOptions): Promise<WebSocketStream>;
|
|
5
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import WebSocket from "ws";
|
|
2
|
+
import { BaseChatIflytekXinghuo } from "./common.js";
|
|
3
|
+
import { BaseWebSocketStream, } from "../../util/iflytek_websocket_stream.js";
|
|
4
|
+
class WebSocketStream extends BaseWebSocketStream {
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
openWebSocket(url, options) {
|
|
8
|
+
return new WebSocket(url, options.protocols ?? []);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class ChatIflytekXinghuo extends BaseChatIflytekXinghuo {
|
|
12
|
+
async openWebSocketStream(options) {
|
|
13
|
+
const host = "spark-api.xf-yun.com";
|
|
14
|
+
const date = new Date().toUTCString();
|
|
15
|
+
const url = `GET /${this.version}/chat HTTP/1.1`;
|
|
16
|
+
const { createHmac } = await import("crypto");
|
|
17
|
+
const hash = createHmac("sha256", this.iflytekApiSecret)
|
|
18
|
+
.update(`host: ${host}\ndate: ${date}\n${url}`)
|
|
19
|
+
.digest("base64");
|
|
20
|
+
const authorization_origin = `api_key="${this.iflytekApiKey}", algorithm="hmac-sha256", headers="host date request-line", signature="${hash}"`;
|
|
21
|
+
const authorization = Buffer.from(authorization_origin).toString("base64");
|
|
22
|
+
let authWebSocketUrl = this.apiUrl;
|
|
23
|
+
authWebSocketUrl += `?authorization=${authorization}`;
|
|
24
|
+
authWebSocketUrl += `&host=${encodeURIComponent(host)}`;
|
|
25
|
+
authWebSocketUrl += `&date=${encodeURIComponent(date)}`;
|
|
26
|
+
return new WebSocketStream(authWebSocketUrl, options);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ChatIflytekXinghuo = void 0;
|
|
4
|
+
const common_js_1 = require("./common.cjs");
|
|
5
|
+
const iflytek_websocket_stream_js_1 = require("../../util/iflytek_websocket_stream.cjs");
|
|
6
|
+
class WebSocketStream extends iflytek_websocket_stream_js_1.BaseWebSocketStream {
|
|
7
|
+
openWebSocket(url, options) {
|
|
8
|
+
return new WebSocket(url, options.protocols ?? []);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
class ChatIflytekXinghuo extends common_js_1.BaseChatIflytekXinghuo {
|
|
12
|
+
async openWebSocketStream(options) {
|
|
13
|
+
const host = "spark-api.xf-yun.com";
|
|
14
|
+
const date = new Date().toUTCString();
|
|
15
|
+
const url = `GET /${this.version}/chat HTTP/1.1`;
|
|
16
|
+
const keyBuffer = new TextEncoder().encode(this.iflytekApiSecret);
|
|
17
|
+
const dataBuffer = new TextEncoder().encode(`host: ${host}\ndate: ${date}\n${url}`);
|
|
18
|
+
const cryptoKey = await crypto.subtle.importKey("raw", keyBuffer, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
19
|
+
const signature = await crypto.subtle.sign("HMAC", cryptoKey, dataBuffer);
|
|
20
|
+
const hash = window.btoa(String.fromCharCode(...new Uint8Array(signature)));
|
|
21
|
+
const authorization_origin = `api_key="${this.iflytekApiKey}", algorithm="hmac-sha256", headers="host date request-line", signature="${hash}"`;
|
|
22
|
+
const authorization = window.btoa(authorization_origin);
|
|
23
|
+
let authWebSocketUrl = this.apiUrl;
|
|
24
|
+
authWebSocketUrl += `?authorization=${authorization}`;
|
|
25
|
+
authWebSocketUrl += `&host=${encodeURIComponent(host)}`;
|
|
26
|
+
authWebSocketUrl += `&date=${encodeURIComponent(date)}`;
|
|
27
|
+
return new WebSocketStream(authWebSocketUrl, options);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.ChatIflytekXinghuo = ChatIflytekXinghuo;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { BaseChatIflytekXinghuo } from "./common.js";
|
|
2
|
+
import { WebSocketStreamOptions } from "../../util/iflytek_websocket_stream.js";
|
|
3
|
+
export declare class ChatIflytekXinghuo extends BaseChatIflytekXinghuo {
|
|
4
|
+
openWebSocketStream<WebSocketStream>(options: WebSocketStreamOptions): Promise<WebSocketStream>;
|
|
5
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { BaseChatIflytekXinghuo } from "./common.js";
|
|
2
|
+
import { BaseWebSocketStream, } from "../../util/iflytek_websocket_stream.js";
|
|
3
|
+
class WebSocketStream extends BaseWebSocketStream {
|
|
4
|
+
openWebSocket(url, options) {
|
|
5
|
+
return new WebSocket(url, options.protocols ?? []);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export class ChatIflytekXinghuo extends BaseChatIflytekXinghuo {
|
|
9
|
+
async openWebSocketStream(options) {
|
|
10
|
+
const host = "spark-api.xf-yun.com";
|
|
11
|
+
const date = new Date().toUTCString();
|
|
12
|
+
const url = `GET /${this.version}/chat HTTP/1.1`;
|
|
13
|
+
const keyBuffer = new TextEncoder().encode(this.iflytekApiSecret);
|
|
14
|
+
const dataBuffer = new TextEncoder().encode(`host: ${host}\ndate: ${date}\n${url}`);
|
|
15
|
+
const cryptoKey = await crypto.subtle.importKey("raw", keyBuffer, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
16
|
+
const signature = await crypto.subtle.sign("HMAC", cryptoKey, dataBuffer);
|
|
17
|
+
const hash = window.btoa(String.fromCharCode(...new Uint8Array(signature)));
|
|
18
|
+
const authorization_origin = `api_key="${this.iflytekApiKey}", algorithm="hmac-sha256", headers="host date request-line", signature="${hash}"`;
|
|
19
|
+
const authorization = window.btoa(authorization_origin);
|
|
20
|
+
let authWebSocketUrl = this.apiUrl;
|
|
21
|
+
authWebSocketUrl += `?authorization=${authorization}`;
|
|
22
|
+
authWebSocketUrl += `&host=${encodeURIComponent(host)}`;
|
|
23
|
+
authWebSocketUrl += `&date=${encodeURIComponent(date)}`;
|
|
24
|
+
return new WebSocketStream(authWebSocketUrl, options);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ChatLlamaCpp = void 0;
|
|
4
4
|
const node_llama_cpp_1 = require("node-llama-cpp");
|
|
5
5
|
const base_js_1 = require("./base.cjs");
|
|
6
|
+
const llama_cpp_js_1 = require("../util/llama_cpp.cjs");
|
|
6
7
|
/**
|
|
7
8
|
* To use this model you need to have the `node-llama-cpp` module installed.
|
|
8
9
|
* This can be installed using `npm install -S node-llama-cpp` and the minimum
|
|
@@ -15,73 +16,31 @@ class ChatLlamaCpp extends base_js_1.SimpleChatModel {
|
|
|
15
16
|
}
|
|
16
17
|
constructor(inputs) {
|
|
17
18
|
super(inputs);
|
|
18
|
-
Object.defineProperty(this, "
|
|
19
|
+
Object.defineProperty(this, "maxTokens", {
|
|
19
20
|
enumerable: true,
|
|
20
21
|
configurable: true,
|
|
21
22
|
writable: true,
|
|
22
23
|
value: void 0
|
|
23
24
|
});
|
|
24
|
-
Object.defineProperty(this, "
|
|
25
|
+
Object.defineProperty(this, "temperature", {
|
|
25
26
|
enumerable: true,
|
|
26
27
|
configurable: true,
|
|
27
28
|
writable: true,
|
|
28
29
|
value: void 0
|
|
29
30
|
});
|
|
30
|
-
Object.defineProperty(this, "
|
|
31
|
+
Object.defineProperty(this, "topK", {
|
|
31
32
|
enumerable: true,
|
|
32
33
|
configurable: true,
|
|
33
34
|
writable: true,
|
|
34
35
|
value: void 0
|
|
35
36
|
});
|
|
36
|
-
Object.defineProperty(this, "
|
|
37
|
+
Object.defineProperty(this, "topP", {
|
|
37
38
|
enumerable: true,
|
|
38
39
|
configurable: true,
|
|
39
40
|
writable: true,
|
|
40
41
|
value: void 0
|
|
41
42
|
});
|
|
42
|
-
Object.defineProperty(this, "
|
|
43
|
-
enumerable: true,
|
|
44
|
-
configurable: true,
|
|
45
|
-
writable: true,
|
|
46
|
-
value: void 0
|
|
47
|
-
});
|
|
48
|
-
Object.defineProperty(this, "logitsAll", {
|
|
49
|
-
enumerable: true,
|
|
50
|
-
configurable: true,
|
|
51
|
-
writable: true,
|
|
52
|
-
value: void 0
|
|
53
|
-
});
|
|
54
|
-
Object.defineProperty(this, "lowVram", {
|
|
55
|
-
enumerable: true,
|
|
56
|
-
configurable: true,
|
|
57
|
-
writable: true,
|
|
58
|
-
value: void 0
|
|
59
|
-
});
|
|
60
|
-
Object.defineProperty(this, "seed", {
|
|
61
|
-
enumerable: true,
|
|
62
|
-
configurable: true,
|
|
63
|
-
writable: true,
|
|
64
|
-
value: void 0
|
|
65
|
-
});
|
|
66
|
-
Object.defineProperty(this, "useMlock", {
|
|
67
|
-
enumerable: true,
|
|
68
|
-
configurable: true,
|
|
69
|
-
writable: true,
|
|
70
|
-
value: void 0
|
|
71
|
-
});
|
|
72
|
-
Object.defineProperty(this, "useMmap", {
|
|
73
|
-
enumerable: true,
|
|
74
|
-
configurable: true,
|
|
75
|
-
writable: true,
|
|
76
|
-
value: void 0
|
|
77
|
-
});
|
|
78
|
-
Object.defineProperty(this, "vocabOnly", {
|
|
79
|
-
enumerable: true,
|
|
80
|
-
configurable: true,
|
|
81
|
-
writable: true,
|
|
82
|
-
value: void 0
|
|
83
|
-
});
|
|
84
|
-
Object.defineProperty(this, "modelPath", {
|
|
43
|
+
Object.defineProperty(this, "trimWhitespaceSuffix", {
|
|
85
44
|
enumerable: true,
|
|
86
45
|
configurable: true,
|
|
87
46
|
writable: true,
|
|
@@ -105,47 +64,33 @@ class ChatLlamaCpp extends base_js_1.SimpleChatModel {
|
|
|
105
64
|
writable: true,
|
|
106
65
|
value: void 0
|
|
107
66
|
});
|
|
108
|
-
this.
|
|
109
|
-
this.
|
|
110
|
-
this.
|
|
111
|
-
this.
|
|
112
|
-
this.
|
|
113
|
-
this.
|
|
114
|
-
this.
|
|
115
|
-
this.modelPath = inputs.modelPath;
|
|
116
|
-
this.seed = inputs?.seed;
|
|
117
|
-
this.useMlock = inputs?.useMlock;
|
|
118
|
-
this.useMmap = inputs?.useMmap;
|
|
119
|
-
this.vocabOnly = inputs?.vocabOnly;
|
|
120
|
-
this._model = new node_llama_cpp_1.LlamaModel(inputs);
|
|
121
|
-
this._context = new node_llama_cpp_1.LlamaContext({ model: this._model });
|
|
67
|
+
this.maxTokens = inputs?.maxTokens;
|
|
68
|
+
this.temperature = inputs?.temperature;
|
|
69
|
+
this.topK = inputs?.topK;
|
|
70
|
+
this.topP = inputs?.topP;
|
|
71
|
+
this.trimWhitespaceSuffix = inputs?.trimWhitespaceSuffix;
|
|
72
|
+
this._model = (0, llama_cpp_js_1.createLlamaModel)(inputs);
|
|
73
|
+
this._context = (0, llama_cpp_js_1.createLlamaContext)(this._model, inputs);
|
|
122
74
|
this._session = null;
|
|
123
75
|
}
|
|
124
76
|
_llmType() {
|
|
125
77
|
return "llama2_cpp";
|
|
126
78
|
}
|
|
127
|
-
invocationParams() {
|
|
128
|
-
return {
|
|
129
|
-
batchSize: this.batchSize,
|
|
130
|
-
contextSize: this.contextSize,
|
|
131
|
-
embedding: this.embedding,
|
|
132
|
-
f16Kv: this.f16Kv,
|
|
133
|
-
gpuLayers: this.gpuLayers,
|
|
134
|
-
logitsAll: this.logitsAll,
|
|
135
|
-
lowVram: this.lowVram,
|
|
136
|
-
modelPath: this.modelPath,
|
|
137
|
-
seed: this.seed,
|
|
138
|
-
useMlock: this.useMlock,
|
|
139
|
-
useMmap: this.useMmap,
|
|
140
|
-
vocabOnly: this.vocabOnly,
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
79
|
/** @ignore */
|
|
144
80
|
_combineLLMOutput() {
|
|
145
81
|
return {};
|
|
146
82
|
}
|
|
83
|
+
invocationParams() {
|
|
84
|
+
return {
|
|
85
|
+
maxTokens: this.maxTokens,
|
|
86
|
+
temperature: this.temperature,
|
|
87
|
+
topK: this.topK,
|
|
88
|
+
topP: this.topP,
|
|
89
|
+
trimWhitespaceSuffix: this.trimWhitespaceSuffix,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
147
92
|
/** @ignore */
|
|
148
|
-
async _call(messages,
|
|
93
|
+
async _call(messages, _options) {
|
|
149
94
|
let prompt = "";
|
|
150
95
|
if (messages.length > 1) {
|
|
151
96
|
// We need to build a new _session
|
|
@@ -159,8 +104,15 @@ class ChatLlamaCpp extends base_js_1.SimpleChatModel {
|
|
|
159
104
|
prompt = messages[0].content;
|
|
160
105
|
}
|
|
161
106
|
try {
|
|
107
|
+
const promptOptions = {
|
|
108
|
+
maxTokens: this?.maxTokens,
|
|
109
|
+
temperature: this?.temperature,
|
|
110
|
+
topK: this?.topK,
|
|
111
|
+
topP: this?.topP,
|
|
112
|
+
trimWhitespaceSuffix: this?.trimWhitespaceSuffix,
|
|
113
|
+
};
|
|
162
114
|
// @ts-expect-error - TS2531: Object is possibly 'null'.
|
|
163
|
-
const completion = await this._session.prompt(prompt,
|
|
115
|
+
const completion = await this._session.prompt(prompt, promptOptions);
|
|
164
116
|
return completion;
|
|
165
117
|
}
|
|
166
118
|
catch (e) {
|