-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
163 lines (143 loc) · 3.92 KB
/
index.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
import { events } from "@lukeed/fetch-event-stream";
const STATUS_URL = "https://duckduckgo.com/duckchat/v1/status";
const CHAT_URL = "https://duckduckgo.com/duckchat/v1/chat";
const STATUS_HEADERS = { "x-vqd-accept": "1" };
type Model =
| "gpt-4o-mini"
| "claude-3-haiku-20240307"
| "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo"
| "mistralai/Mixtral-8x7B-Instruct-v0.1"
| "o3-mini";
type ModelAlias =
| "gpt-4o-mini"
| "claude-3-haiku"
| "llama"
| "mixtral"
| "o3-mini";
type Messages = { content: string; role: "user" | "assistant" }[];
type ChatPayload = {
model: Model;
messages: Messages;
};
const _model: { [property: string]: Model } = {
"gpt-4o-mini": "gpt-4o-mini",
"claude-3-haiku": "claude-3-haiku-20240307",
"llama": "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
"mixtral": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"o3-mini": "o3-mini",
};
class Chat {
oldVqd: string;
newVqd: string;
model: Model;
messages: Messages;
constructor(vqd: string, model: Model) {
this.oldVqd = vqd;
this.newVqd = vqd;
this.model = model;
this.messages = [];
}
/**
* Fetching the original message.
*
* @param content The content to send.
* @returns The original message.
*/
async fetch(content: string): Promise<Response> {
this.messages.push({ content, role: "user" });
const payload: ChatPayload = {
model: this.model,
messages: this.messages,
};
const message = await fetch(CHAT_URL, {
headers: { "x-vqd-4": this.newVqd, "Content-Type": "application/json" },
method: "POST",
body: JSON.stringify(payload),
});
if (!message.ok) {
throw new Error(
`${message.status}: Failed to send message. ${message.statusText}`,
);
} else {
return message;
}
}
/**
* Fetching the full message.
*
* @param content The content to send.
* @returns The full message.
*/
async fetchFull(content: string): Promise<string> {
const message = await this.fetch(content);
let text = "";
const stream = events(message);
for await (const event of stream) {
if (event.data && event.data != "[DONE]") {
const messageData = JSON.parse(event.data);
if (messageData["message"] == undefined) {
break;
} else {
text += messageData["message"];
}
}
}
const newVqd = message.headers.get("x-vqd-4") as string;
this.oldVqd = this.newVqd;
this.newVqd = newVqd;
this.messages.push({ content: text, role: "assistant" });
return text;
}
/**
* Fetching the streaming message.
*
* @param content The content to send.
* @returns The streaming message.
*/
async *fetchStream(content: string): AsyncGenerator<string, void> {
const message = await this.fetch(content);
const stream = events(message);
let text = "";
for await (const event of stream) {
if (event.data) {
const messageData = JSON.parse(event.data);
if (messageData["message"] == undefined) {
break;
} else {
const data = messageData["message"];
text += data;
yield data;
}
}
}
const newVqd = message.headers.get("x-vqd-4") as string;
this.oldVqd = newVqd;
this.newVqd = newVqd;
this.messages.push({ content: text, role: "assistant" });
}
/**
* Redo.
*/
redo() {
this.newVqd = this.oldVqd;
this.messages.pop();
this.messages.pop();
}
}
/** Init chat.
*
* @param model The model used by chat.
* @returns A Chat instance.
*/
async function initChat(model: ModelAlias): Promise<Chat> {
const status = await fetch(STATUS_URL, { headers: STATUS_HEADERS });
const vqd = status.headers.get("x-vqd-4");
if (!vqd) {
throw new Error(
`${status.status}: Failed to initialize chat. ${status.statusText}`,
);
}
return new Chat(vqd, _model[model]);
}
export { initChat };
export type { Chat, ModelAlias };