-
Notifications
You must be signed in to change notification settings - Fork 39
/
base.ts
439 lines (368 loc) 路 11.3 KB
/
base.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
import type { ReadableStream } from 'stream/web';
import type {
AIPromptConfig,
AIService,
AIServiceActionOptions,
AIServiceOptions
} from '../text/types.js';
import {
AITextTraceStepBuilder,
TextRequestBuilder,
TextResponseBuilder
} from '../tracing/index.js';
import type {
AITextChatRequest,
AITextEmbedRequest,
AITextTraceStep,
TextModelInfoWithProvider
} from '../tracing/types.js';
import { type API, apiCall } from '../util/apicall.js';
import { ColorLog } from '../util/log.js';
import { RespTransformStream } from '../util/transform.js';
import { MemoryCache } from './cache.js';
import type {
EmbedResponse,
RateLimiterFunction,
TextModelConfig,
TextModelInfo,
TextResponse
} from './types.js';
import { hashObject, mergeTextResponses } from './util.js';
const cache = new MemoryCache<TextResponse>();
const colorLog = new ColorLog();
interface BaseAIArgs {
name: string;
apiURL: string;
headers: Record<string, string>;
modelInfo: Readonly<TextModelInfo[]>;
models: Readonly<{ model: string; embedModel?: string }>;
options?: Readonly<AIServiceOptions>;
supportFor: { functions: boolean };
}
export class BaseAI<
TChatRequest,
TEmbedRequest,
TChatResponse,
TChatResponseDelta,
TEmbedResponse
> implements AIService
{
generateChatReq?: (
req: Readonly<AITextChatRequest>,
config: Readonly<AIPromptConfig>
) => [API, TChatRequest];
generateEmbedReq?: (
req: Readonly<AITextEmbedRequest>
) => [API, TEmbedRequest];
generateChatResp?: (resp: Readonly<TChatResponse>) => TextResponse;
generateChatStreamResp?: (resp: Readonly<TChatResponseDelta>) => TextResponse;
generateEmbedResp?: (resp: Readonly<TEmbedResponse>) => EmbedResponse;
private debug = false;
private disableLog = false;
private rt?: RateLimiterFunction;
private log?: (traceStep: Readonly<AITextTraceStep>) => void;
private traceStepBuilder?: AITextTraceStepBuilder;
private traceStepReqBuilder?: TextRequestBuilder;
private traceStepRespBuilder?: TextResponseBuilder;
protected apiURL: string;
protected name: string;
protected headers: Record<string, string>;
protected modelInfo: TextModelInfo;
protected embedModelInfo?: TextModelInfo;
protected supportFor: BaseAIArgs['supportFor'];
constructor({
name,
apiURL,
headers,
modelInfo,
models,
options = {},
supportFor
}: Readonly<BaseAIArgs>) {
this.name = name;
this.apiURL = apiURL;
this.headers = headers;
this.supportFor = supportFor;
if (models.model.length === 0) {
throw new Error('No model defined');
}
this.modelInfo = modelInfo.filter((v) => v.name === models.model).at(0) ?? {
name: models.model,
currency: 'usd',
promptTokenCostPer1M: 0,
completionTokenCostPer1M: 0
};
this.embedModelInfo = modelInfo
.filter((v) => v.name === models.embedModel)
.at(0);
this.setOptions(options);
}
public setName(name: string): void {
this.name = name;
}
public setAPIURL(apiURL: string): void {
this.apiURL = apiURL;
}
public setHeaders(headers: Record<string, string>): void {
this.headers = headers;
}
setOptions(options: Readonly<AIServiceOptions>): void {
if (options.debug) {
this.debug = options.debug;
}
if (options.disableLog) {
this.disableLog = options.disableLog;
}
if (options.log) {
this.log = options.log;
}
if (options.rateLimiter) {
this.rt = options.rateLimiter;
}
}
getModelInfo(): Readonly<TextModelInfoWithProvider> {
return { ...this.modelInfo, provider: this.name };
}
getEmbedModelInfo(): TextModelInfoWithProvider | undefined {
return this.embedModelInfo
? { ...this.embedModelInfo, provider: this.name }
: undefined;
}
getName(): string {
return this.name;
}
getFeatures(): BaseAIArgs['supportFor'] {
return this.supportFor;
}
getModelConfig(): TextModelConfig {
throw new Error('getModelConfig not implemented');
}
getTraceRequest(): Readonly<TextRequestBuilder> | undefined {
return this.traceStepReqBuilder;
}
getTraceResponse(): Readonly<TextResponseBuilder> | undefined {
return this.traceStepRespBuilder;
}
traceExists(): boolean {
return (
this.traceStepBuilder !== undefined &&
this.traceStepReqBuilder !== undefined &&
this.traceStepRespBuilder !== undefined
);
}
async logTrace(): Promise<void> {
if (
!this.traceStepBuilder ||
!this.traceStepReqBuilder ||
!this.traceStepRespBuilder
) {
throw new Error('Trace not initialized');
}
const traceStep = this.traceStepBuilder
.setRequest(this.traceStepReqBuilder)
.setResponse(this.traceStepRespBuilder)
.build();
if (this.log) {
this.log?.(traceStep);
}
// if (this.debug) {
// this.consoleLog.log(traceStep);
// }
}
async chat(
_req: Readonly<AITextChatRequest>,
options?: Readonly<AIPromptConfig & AIServiceActionOptions>
): Promise<TextResponse | ReadableStream<TextResponse>> {
let hashKey: string | undefined;
if (options?.cache) {
hashKey = hashObject(_req);
const cached = await cache.get(hashKey);
if (cached) {
return cached;
}
}
if (!this.generateChatReq) {
throw new Error('generateChatReq not implemented');
}
let startTime = 0;
const reqFn = this.generateChatReq;
const stream = options?.stream ?? _req.modelConfig?.stream;
const functions =
_req.functions && _req.functions.length > 0 ? _req.functions : undefined;
const req = {
..._req,
functions,
modelConfig: { ..._req.modelConfig, stream }
} as Readonly<AITextChatRequest>;
const fn = async () => {
startTime = new Date().getTime();
const [apiConfig, reqValue] = reqFn(req, options as AIPromptConfig);
const res = await apiCall(
{
name: apiConfig.name,
url: this.apiURL,
headers: this.buildHeaders(apiConfig.headers),
stream,
debug: this.debug
},
reqValue
);
return res;
};
this.setStep(options);
this.traceStepReqBuilder = new TextRequestBuilder().setChatStep(
req,
this.getModelConfig(),
this.getModelInfo()
);
if (this.debug) {
logChatRequest(req);
}
const rv = this.rt ? await this.rt(fn) : await fn();
if (stream) {
if (!this.generateChatStreamResp) {
throw new Error('generateChatResp not implemented');
}
const respFn = this.generateChatStreamResp;
const wrappedRespFn = (resp: Readonly<TChatResponseDelta>) => {
const res = respFn(resp);
res.sessionId = options?.sessionId;
return res;
};
const doneCb = async (values: readonly TextResponse[]) => {
const res = mergeTextResponses(values);
await this.setStepTextResponse(res, startTime);
if (options?.cache && hashKey) {
cache.set(hashKey, res, options?.cacheMaxAgeSeconds ?? 3600);
}
};
const st = (rv as ReadableStream<TChatResponseDelta>).pipeThrough(
new RespTransformStream<TChatResponseDelta, TextResponse>(
wrappedRespFn,
doneCb
)
);
return st;
}
if (!this.generateChatResp) {
throw new Error('generateChatResp not implemented');
}
const res = this.generateChatResp(rv as TChatResponse);
await this.setStepTextResponse(res, new Date().getTime() - startTime);
res.sessionId = options?.sessionId;
if (options?.cache && hashKey) {
cache.set(hashKey, res, options?.cacheMaxAgeSeconds ?? 3600);
}
return res;
}
async embed(
req: Readonly<AITextEmbedRequest>,
options?: Readonly<AIServiceActionOptions>
): Promise<EmbedResponse> {
let modelResponseTime;
if (!this.generateEmbedReq) {
throw new Error('generateEmbedReq not implemented');
}
if (!this.generateEmbedResp) {
throw new Error('generateEmbedResp not implemented');
}
const fn = async () => {
const st = new Date().getTime();
const [apiConfig, reqValue] = this.generateEmbedReq!(req);
const res = await apiCall(
{
name: apiConfig.name,
url: this.apiURL,
headers: this.buildHeaders(apiConfig.headers),
debug: this.debug
},
reqValue
);
modelResponseTime = new Date().getTime() - st;
return res;
};
this.setStep(options);
this.traceStepReqBuilder = new TextRequestBuilder().setEmbedStep(
req,
this.getEmbedModelInfo()
);
const resValue = this.rt ? await this.rt(async () => fn()) : await fn();
const res = this.generateEmbedResp!(resValue as TEmbedResponse);
this.traceStepRespBuilder = new TextResponseBuilder()
.setModelUsage(res.modelUsage)
.setModelResponseTime(modelResponseTime);
res.sessionId = options?.sessionId;
return res;
}
// async _transcribe(
// // eslint-disable-next-line @typescript-eslint/no-unused-vars
// _file: string,
// // eslint-disable-next-line @typescript-eslint/no-unused-vars
// _prompt?: string,
// // eslint-disable-next-line @typescript-eslint/no-unused-vars
// _options?: Readonly<AITranscribeConfig & AIServiceActionOptions>
// ): Promise<TranscriptResponse> {
// throw new Error('_transcribe not implemented');
// }
// async transcribe(
// file: string,
// prompt?: string,
// options?: Readonly<AITranscribeConfig & AIServiceActionOptions>
// ): Promise<TranscriptResponse> {
// const res = this.rt
// ? await this.rt<Promise<TranscriptResponse>>(
// async () => await this._transcribe(file, prompt, options)
// )
// : await this._transcribe(file, prompt, options);
// res.sessionId = options?.sessionId;
// return res;
// }
// async apiCallWithUpload<Request, Response, APIType extends API >(
// api: APIType,
// json: Request,
// file: string
// ): Promise<Response> {
// return apiCallWithUpload<Request, Response, APIType>(this.mergeAPIConfig<APIType>(api), json, file);
// }
setStep(options?: Readonly<AIServiceActionOptions>) {
this.traceStepBuilder = new AITextTraceStepBuilder()
.setTraceId(options?.traceId)
.setSessionId(options?.sessionId);
}
async setStepTextResponse(res: Readonly<TextResponse>, startTime: number) {
if (this.debug) {
logResponse(res as TextResponse);
}
this.traceStepRespBuilder = new TextResponseBuilder()
.setResults(res.results)
.setModelUsage(res.modelUsage)
.setModelResponseTime(new Date().getTime() - startTime);
if (!this.disableLog) {
await this.logTrace();
}
}
private buildHeaders(
headers: Record<string, string> = {}
): Record<string, string> {
return { ...headers, ...this.headers };
}
}
const logChatRequest = (req: Readonly<AITextChatRequest>) => {
const items = req.chatPrompt?.map((v) => v.content).join('\n');
if (items) {
console.log(colorLog.whiteBright(items));
}
};
const logResponse = (res: Readonly<TextResponse>) => {
const items = res.results.map((v) => v.content).join('\n');
if (items) {
console.log(colorLog.greenBright(items));
}
const funcs = res.results
.at(0)
?.functionCalls?.map((v) => v.function.name + ': ' + v.function.arguments)
?.join('\n');
if (funcs) {
console.log(colorLog.greenBright(funcs));
}
};