This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
llm-adapter.ts
94 lines (81 loc) · 2.19 KB
/
llm-adapter.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
export type FinishReason = undefined | "length" | "stop";
export enum Role {
Assistant = "assistant",
System = "system",
User = "user",
}
export type LlmAdapterModel = { id: string; created: number; ownedBy: string };
export type LlmAdapterCreateEmbeddingRequest = {
model: string;
input: string;
};
export type LlmAdapterCreateEmbeddingResponse = number[];
export type LlmAdapterCreateCompletionRequest = {
bestOf?: number;
echo?: boolean;
frequencyPenalty?: number;
logitBias?: Record<string, any>;
logprobs?: number;
maxTokens?: number;
model: string;
n?: number;
presencePenalty?: number;
// TODO: Support other types
prompt: string[];
stop?: string[];
suffix?: string;
temperature?: number;
topP?: number;
};
export type LlmAdapterCreateChatCompletionRequest = {
frequencyPenalty?: number;
logitBias?: Record<string, any>;
maxTokens?: number;
messages: Array<{
content: string;
name?: string;
role: Role;
}>;
model: string;
n?: number;
presencePenalty?: number;
stop?: string[];
temperature?: number;
topP?: number;
};
export type LlmAdapterCreateCompletionResponse = {
index: number;
// TODO: Figure out the type
logprobs?: unknown;
text: string;
finishReason: FinishReason;
};
export type ChatCompletionDelta = {
role?: Role;
content?: string;
};
export type LlmAdapterCreateChatCompletionResponse = {
index: number;
delta: ChatCompletionDelta;
finishReason?: string;
};
export abstract class LlmAdapter {
static get defaultConfig(): Record<string, unknown> {
throw new Error("Not implemented");
}
abstract listModels(): Promise<LlmAdapterModel[]>;
abstract createEmbedding({
model,
input,
}: LlmAdapterCreateEmbeddingRequest): Promise<LlmAdapterCreateEmbeddingResponse>;
abstract createCompletion(
createCompletionRequest: LlmAdapterCreateCompletionRequest,
abortSignal: AbortSignal,
onData: (data: LlmAdapterCreateCompletionResponse) => void,
): Promise<void>;
abstract createChatCompletion(
createChatCompletionRequest: LlmAdapterCreateChatCompletionRequest,
abortSignal: AbortSignal,
onData: (data: LlmAdapterCreateChatCompletionResponse) => void,
): Promise<void>;
}