forked from xiangsx/gpt4free-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.ts
63 lines (52 loc) · 1.66 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
import {EventStream, getTokenSize} from "../utils";
export interface ChatOptions {
}
export interface ChatResponse {
content?: string;
error?: string;
}
export type Message = {
role: string;
content: string;
}
export enum ModelType {
GPT3p5Turbo = 'gpt-3.5-turbo',
GPT3p5_16k = 'gpt-3.5-turbo-16k',
GPT4 = 'gpt-4',
GPT4_32k = 'gpt-4-32k',
Sage = 'sage',
NetGpt3p5 = 'net-gpt3.5-turbo',
ClaudeInstance = 'claude-instance',
Claude = 'claude',
Claude100k = 'claude-100k',
Claude2_100k = 'claude-2-100k',
Gpt4free = 'gpt4free',
GooglePalm = 'google-palm',
}
export interface ChatRequest {
prompt: string;
model: ModelType;
messages: Message[];
}
export function PromptToString(prompt: string, limit: number): [string, Message[]] {
try {
const messages: Message[] = JSON.parse(prompt);
const res = `${messages.map(item => `${item.role}: ${item.content}`).join('\n')}\nassistant: `;
console.log(prompt.length, limit, getTokenSize(res));
if (getTokenSize(res) >= limit && messages.length > 1) {
return PromptToString(JSON.stringify(messages.slice(1, messages.length)), limit);
}
return [res, messages];
} catch (e) {
return [prompt, [{role: 'user', content: prompt}]];
}
}
export abstract class Chat {
protected options: ChatOptions | undefined;
protected constructor(options?: ChatOptions) {
this.options = options;
}
public abstract support(model: ModelType): number
public abstract ask(req: ChatRequest): Promise<ChatResponse>
public abstract askStream(req: ChatRequest, stream: EventStream): Promise<void>
}