-
Notifications
You must be signed in to change notification settings - Fork 295
/
base.ts
71 lines (67 loc) · 1.93 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
import type {
ChatResponse,
ChatResponseChunk,
CompletionResponse,
LLM,
LLMChatParamsNonStreaming,
LLMChatParamsStreaming,
LLMCompletionParamsNonStreaming,
LLMCompletionParamsStreaming,
LLMMetadata,
ToolCallLLMMessageOptions,
} from "./types.js";
import { extractText, streamConverter } from "./utils.js";
export abstract class BaseLLM<
AdditionalChatOptions extends object = object,
AdditionalMessageOptions extends object = object,
> implements LLM<AdditionalChatOptions>
{
abstract metadata: LLMMetadata;
complete(
params: LLMCompletionParamsStreaming,
): Promise<AsyncIterable<CompletionResponse>>;
complete(
params: LLMCompletionParamsNonStreaming,
): Promise<CompletionResponse>;
async complete(
params: LLMCompletionParamsStreaming | LLMCompletionParamsNonStreaming,
): Promise<CompletionResponse | AsyncIterable<CompletionResponse>> {
const { prompt, stream } = params;
if (stream) {
const stream = await this.chat({
messages: [{ content: prompt, role: "user" }],
stream: true,
});
return streamConverter(stream, (chunk) => {
return {
raw: null,
text: chunk.delta,
};
});
}
const chatResponse = await this.chat({
messages: [{ content: prompt, role: "user" }],
});
return {
text: extractText(chatResponse.message.content),
raw: chatResponse.raw,
};
}
abstract chat(
params: LLMChatParamsStreaming<
AdditionalChatOptions,
AdditionalMessageOptions
>,
): Promise<AsyncIterable<ChatResponseChunk>>;
abstract chat(
params: LLMChatParamsNonStreaming<
AdditionalChatOptions,
AdditionalMessageOptions
>,
): Promise<ChatResponse<AdditionalMessageOptions>>;
}
export abstract class ToolCallLLM<
AdditionalChatOptions extends object = object,
> extends BaseLLM<AdditionalChatOptions, ToolCallLLMMessageOptions> {
abstract supportToolCall: boolean;
}