-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
common.ts
484 lines (430 loc) Β· 12.6 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
import { AIMessage, BaseMessage, ChatMessage } from "@langchain/core/messages";
import { ChatGeneration, ChatResult } from "@langchain/core/outputs";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { IterableReadableStream } from "@langchain/core/utils/stream";
import {
BaseChatModel,
type BaseChatModelParams,
} from "@langchain/core/language_models/chat_models";
import {
BaseWebSocketStream,
WebSocketStreamOptions,
} from "../../utils/iflytek_websocket_stream.js";
/**
* Type representing the role of a message in the Xinghuo chat model.
*/
export type XinghuoMessageRole = "assistant" | "user";
/**
* Interface representing a message in the Xinghuo chat model.
*/
interface XinghuoMessage {
role: XinghuoMessageRole;
content: string;
}
/**
* Interface representing the usage of tokens in a chat completion.
*/
interface TokenUsage {
completionTokens?: number;
promptTokens?: number;
totalTokens?: number;
}
/**
* Interface representing a request for a chat completion.
*/
interface ChatCompletionRequest {
messages: XinghuoMessage[];
temperature?: number;
max_tokens?: number;
top_k?: number;
chat_id?: string;
}
export interface ChatCompletionChunk {
header: {
code: number;
message: string;
sid: string;
status: number;
};
payload: {
choices: {
status: number;
seq: number;
text: {
content: string;
role: XinghuoMessageRole;
index: number;
}[];
};
usage?: {
text: {
question_tokens: number;
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
};
};
}
/**
* Interface representing a response from a chat completion.
*/
interface ChatCompletionResponse {
result: string;
usage?: {
completion_tokens: number;
prompt_tokens: number;
total_tokens: number;
};
}
/**
* Function that extracts the custom role of a generic chat message.
* @param message Chat message from which to extract the custom role.
* @returns The custom role of the chat message.
*/
function extractGenericMessageCustomRole(message: ChatMessage) {
if (message.role !== "assistant" && message.role !== "user") {
console.warn(`Unknown message role: ${message.role}`);
}
return message.role as XinghuoMessageRole;
}
/**
* Function that converts a base message to a Xinghuo message role.
* @param message Base message to convert.
* @returns The Xinghuo message role.
*/
function messageToXinghuoRole(message: BaseMessage): XinghuoMessageRole {
const type = message._getType();
switch (type) {
case "ai":
return "assistant";
case "human":
return "user";
case "system":
throw new Error("System messages should not be here");
case "function":
throw new Error("Function messages not supported");
case "generic": {
if (!ChatMessage.isInstance(message))
throw new Error("Invalid generic chat message");
return extractGenericMessageCustomRole(message);
}
default:
throw new Error(`Unknown message type: ${type}`);
}
}
declare interface IflytekXinghuoChatInput {
/** Model version to use. Available options are: v1.1, v2.1, v3.1
* @default "v2.1"
*/
version: string;
/**
* ID of the end-user who made requests.
*/
userId?: string;
/**
* APPID to use when making requests. Defaults to the value of
* `IFLYTEK_APPID` environment variable.
*/
iflytekAppid?: string;
/**
* API key to use when making requests. Defaults to the value of
* `IFLYTEK_API_KEY` environment variable.
*/
iflytekApiKey?: string;
/**
* API Secret to use when making requests. Defaults to the value of
* `IFLYTEK_API_SECRET` environment variable.
*/
iflytekApiSecret?: string;
/** Amount of randomness injected into the response. Ranges
* from 0 to 1 (0 is not included). Use temp closer to 0 for analytical /
* multiple choice, and temp closer to 1 for creative
* and generative tasks. Defaults to 0.5.
*/
temperature?: number;
max_tokens?: number;
top_k?: number;
streaming?: boolean;
}
/**
* Wrapper around IflytekXingHuo large language models that use the Chat endpoint.
*
* To use you should have the `IFLYTEK_API_KEY` and `IFLYTEK_API_SECRET` and `IFLYTEK_APPID`
* environment variable set.
*
* @augments BaseChatModel
* @augments IflytekXinghuoChatInput
*/
export abstract class BaseChatIflytekXinghuo
extends BaseChatModel
implements IflytekXinghuoChatInput
{
static lc_name() {
return "ChatIflytekXinghuo";
}
get callKeys(): string[] {
return ["stop", "signal", "options"];
}
get lc_secrets(): { [key: string]: string } | undefined {
return {
iflytekApiKey: "IFLYTEK_API_KEY",
iflytekApiSecret: "IFLYTEK_API_SECRET",
};
}
get lc_aliases(): { [key: string]: string } | undefined {
return undefined;
}
lc_serializable = true;
version = "v2.1";
iflytekAppid: string;
iflytekApiKey: string;
iflytekApiSecret: string;
userId?: string;
apiUrl: string;
domain: string;
temperature = 0.5;
max_tokens = 2048;
top_k = 4;
streaming = false;
constructor(fields?: Partial<IflytekXinghuoChatInput> & BaseChatModelParams) {
super(fields ?? {});
const iflytekAppid =
fields?.iflytekAppid ?? getEnvironmentVariable("IFLYTEK_APPID");
if (!iflytekAppid) {
throw new Error("Iflytek APPID not found");
} else {
this.iflytekAppid = iflytekAppid;
}
const iflytekApiKey =
fields?.iflytekApiKey ?? getEnvironmentVariable("IFLYTEK_API_KEY");
if (!iflytekApiKey) {
throw new Error("Iflytek API key not found");
} else {
this.iflytekApiKey = iflytekApiKey;
}
const iflytekApiSecret =
fields?.iflytekApiSecret ?? getEnvironmentVariable("IFLYTEK_API_SECRET");
if (!iflytekApiSecret) {
throw new Error("Iflytek API secret not found");
} else {
this.iflytekApiSecret = iflytekApiSecret;
}
this.userId = fields?.userId ?? this.userId;
this.streaming = fields?.streaming ?? this.streaming;
this.temperature = fields?.temperature ?? this.temperature;
this.max_tokens = fields?.max_tokens ?? this.max_tokens;
this.top_k = fields?.top_k ?? this.top_k;
this.version = fields?.version ?? this.version;
if (["v1.1", "v2.1", "v3.1"].includes(this.version)) {
switch (this.version) {
case "v1.1":
this.domain = "general";
break;
case "v2.1":
this.domain = "generalv2";
break;
case "v3.1":
this.domain = "generalv3";
break;
default:
this.domain = "generalv2";
}
this.apiUrl = `wss://spark-api.xf-yun.com/${this.version}/chat`;
} else {
throw new Error(`Invalid model version: ${this.version}`);
}
}
/**
* Get the identifying parameters for the model
*/
identifyingParams() {
return {
version: this.version,
...this.invocationParams(),
};
}
/**
* Get the parameters used to invoke the model
*/
invocationParams(): Omit<ChatCompletionRequest, "messages"> & {
streaming: boolean;
} {
return {
streaming: this.streaming,
temperature: this.temperature,
top_k: this.top_k,
};
}
/**
* Method that retrieves the auth websocketStream for making requests to the Iflytek Xinghuo API.
* @returns The auth websocketStream for making requests to the Iflytek Xinghuo API.
*/
abstract openWebSocketStream<T extends BaseWebSocketStream<string>>(
options: WebSocketStreamOptions
): Promise<T>;
/**
* Calls the Xinghuo API completion.
* @param request The request to send to the Xinghuo API.
* @param signal The signal for the API call.
* @returns The response from the Xinghuo API.
*/
async completion(
request: ChatCompletionRequest,
stream: true,
signal?: AbortSignal
): Promise<IterableReadableStream<string>>;
async completion(
request: ChatCompletionRequest,
stream: false,
signal?: AbortSignal
): Promise<ChatCompletionResponse>;
async completion(
request: ChatCompletionRequest,
stream: boolean,
signal?: AbortSignal
): Promise<IterableReadableStream<string> | ChatCompletionResponse> {
const webSocketStream = await this.openWebSocketStream({
signal,
});
const connection = await webSocketStream.connection;
const header = {
app_id: this.iflytekAppid,
uid: this.userId,
};
const parameter = {
chat: {
domain: this.domain,
temperature: request.temperature ?? this.temperature,
max_tokens: request.max_tokens ?? this.max_tokens,
top_k: request.top_k ?? this.top_k,
},
};
const payload = {
message: {
text: request.messages,
},
};
const message = JSON.stringify({
header,
parameter,
payload,
});
const { writable, readable } = connection;
const writer = writable.getWriter();
await writer.write(message);
const streams = IterableReadableStream.fromReadableStream(readable);
if (stream) {
return streams;
} else {
let response: ChatCompletionResponse = { result: "" };
for await (const chunk of streams) {
const data = JSON.parse(chunk) as ChatCompletionChunk;
const { header, payload } = data;
if (header.code === 0) {
if (header.status === 0) {
response.result = payload.choices?.text[0]?.content ?? "";
} else if (header.status === 1) {
response.result += payload.choices?.text[0]?.content ?? "";
} else if (header.status === 2) {
response = { ...response, usage: payload.usage?.text };
break;
}
} else {
break;
}
}
void streams.cancel();
void webSocketStream.close();
return response;
}
}
async _generate(
messages: BaseMessage[],
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun | undefined
): Promise<ChatResult> {
const tokenUsage: TokenUsage = {};
const params = this.invocationParams();
const messagesMapped: XinghuoMessage[] = messages.map((message) => {
if (typeof message.content !== "string") {
throw new Error(
"ChatIflytekXinghuo does not support non-string message content."
);
}
return {
role: messageToXinghuoRole(message),
content: message.content,
};
});
const data = params.streaming
? await (async () => {
const streams = await this.completion(
{ messages: messagesMapped, ...params },
true,
options.signal
);
let response: ChatCompletionResponse = { result: "" };
for await (const chunk of streams) {
const data = JSON.parse(chunk) as ChatCompletionChunk;
const { header, payload } = data;
if (header.code === 0) {
if (header.status === 0) {
response.result = payload.choices?.text[0]?.content ?? "";
} else if (header.status === 1) {
response.result += payload.choices?.text[0]?.content ?? "";
} else if (header.status === 2) {
response = { ...response, usage: payload.usage?.text };
break;
}
void runManager?.handleLLMNewToken(
payload.choices?.text[0]?.content
);
} else {
break;
}
}
void streams.cancel();
return response;
})()
: await this.completion(
{ messages: messagesMapped, ...params },
false,
options.signal
);
const {
completion_tokens: completionTokens,
prompt_tokens: promptTokens,
total_tokens: totalTokens,
} = data.usage ?? {};
if (completionTokens) {
tokenUsage.completionTokens =
(tokenUsage.completionTokens ?? 0) + completionTokens;
}
if (promptTokens) {
tokenUsage.promptTokens = (tokenUsage.promptTokens ?? 0) + promptTokens;
}
if (totalTokens) {
tokenUsage.totalTokens = (tokenUsage.totalTokens ?? 0) + totalTokens;
}
const generations: ChatGeneration[] = [];
const text = data.result ?? "";
generations.push({
text,
message: new AIMessage(text),
});
return {
generations,
llmOutput: { tokenUsage },
};
}
/** @ignore */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_combineLLMOutput(): Record<string, any> | undefined {
return [];
}
_llmType(): string {
return "iflytek_xinghuo";
}
}