Elffuss BraidHub

kikocis/continue public

2ff6e1c400a1 · 9.8 KiBRawPermalink
1import { BaseLlmApi, constructLlmApi } from "../index.js";
2import { LLMConfig } from "../types.js";
3
4export function getLlmApi(config: LLMConfig) {
5 const api = constructLlmApi(config);
6 if (!api) {
7 throw new Error("Failed to construct LLM");
8 }
9 return api;
10}
11
12export function testEmbed(api: BaseLlmApi, model: string) {
13 test("should successfully embed", async () => {
14 const response = await api.embed({
15 model,
16 input: ["This is a test", "Hello world!"],
17 });
18 expect(response.model).toBe(model);
19 expect(response.object).toBe("list");
20 expect(response.data.length).toEqual(2);
21 response.data.forEach((val, index) => {
22 expect(val.index).toBe(index);
23 expect(val.object).toBe("embedding");
24 expect(val.embedding.some((v) => typeof v !== "number")).toBe(false);
25 });
26 });
27}
28
29export function testRerank(api: BaseLlmApi, model: string) {
30 test("should successfully rerank", async () => {
31 const response = await api.rerank({
32 model: model,
33 query: "What is the capital of spain?",
34 documents: [
35 "The capital of spain is Madrid",
36 "The largest breed of dog is the Great Dane",
37 ],
38 });
39 expect(response.model).toBe(model);
40 expect(response.object).toBe("list");
41 expect(response.data.length).toEqual(2);
42 response.data.forEach((val, index) => {
43 expect(val.index).toBe(index);
44 expect(typeof val.relevance_score).toBe("number");
45 });
46 expect(response.data[0].relevance_score).toBeGreaterThan(
47 response.data[1].relevance_score,
48 );
49 });
50}
51
52export function testFim(api: BaseLlmApi, model: string) {
53 test("should successfully fim", async () => {
54 const response = api.fimStream(
55 {
56 model: model,
57 prompt: "This is a ",
58 suffix: " .",
59 stream: true,
60 },
61 new AbortController().signal,
62 );
63
64 let completion = "";
65 for await (const result of response) {
66 expect(result.choices.length).toBeGreaterThan(0);
67 expect(typeof result.choices[0].delta.content).toBe("string");
68
69 completion += result.choices[0].delta.content;
70 }
71
72 expect(completion.length).toBeGreaterThan(0);
73 });
74}
75
76export function testChat(api: BaseLlmApi, model: string) {
77 test("should successfully stream chat", async () => {
78 const stream = api.chatCompletionStream(
79 {
80 model,
81 messages: [{ role: "user", content: "Hello! Who are you?" }],
82 stream: true,
83 },
84 new AbortController().signal,
85 );
86 let completion = "";
87 for await (const result of stream) {
88 completion += result.choices[0].delta.content ?? "";
89
90 expect(result.choices.length).toBeGreaterThan(0);
91 }
92 expect(completion.length).toBeGreaterThan(0);
93 });
94
95 test("should successfully stream multi-part chat with empty text", async () => {
96 const stream = api.chatCompletionStream(
97 {
98 model,
99 messages: [
100 {
101 role: "user",
102 content: [
103 {
104 type: "text",
105 text: "Hello! Who are you?",
106 },
107 {
108 // @ts-ignore
109 type: "text",
110 text: "",
111 },
112 ],
113 },
114 ],
115 stream: true,
116 },
117 new AbortController().signal,
118 );
119 let completion = "";
120 for await (const result of stream) {
121 completion += result.choices[0].delta.content ?? "";
122
123 expect(result.choices.length).toBeGreaterThan(0);
124 }
125 expect(completion.length).toBeGreaterThan(0);
126 });
127
128 test.skip("should successfully stream multi-part chat with image", async () => {
129 const stream = api.chatCompletionStream(
130 {
131 model,
132 messages: [
133 {
134 role: "user",
135 content: [
136 {
137 type: "text",
138 text: "Hello! Who are you?",
139 },
140 {
141 // @ts-ignore
142 type: "image_url",
143 image_url: {
144 url: "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Image_created_with_a_mobile_phone.png/1280px-Image_created_with_a_mobile_phone.png",
145 detail: "low",
146 },
147 },
148 ],
149 },
150 ],
151 stream: true,
152 },
153 new AbortController().signal,
154 );
155 let completion = "";
156 for await (const result of stream) {
157 completion += result.choices[0].delta.content ?? "";
158
159 expect(result.choices.length).toBeGreaterThan(0);
160 }
161 expect(completion.length).toBeGreaterThan(0);
162 });
163
164 test("should successfully non-stream chat", async () => {
165 const response = await api.chatCompletionNonStream(
166 {
167 model,
168 messages: [{ role: "user", content: "Hello! Who are you?" }],
169 stream: false,
170 },
171 new AbortController().signal,
172 );
173
174 expect(response.choices.length).toBeGreaterThan(0);
175
176 const completion = response.choices[0].message.content;
177 expect(typeof completion).toBe("string");
178 expect(completion?.length).toBeGreaterThan(0);
179 });
180
181 test("should acknowledge system message in chat", async () => {
182 const response = await api.chatCompletionNonStream(
183 {
184 model,
185 messages: [
186 {
187 role: "system",
188 content:
189 "Regardless of what is asked of you, your answer should start with 'RESPONSE: '.",
190 },
191 { role: "user", content: "Who are you?" },
192 ],
193 stream: false,
194 },
195 new AbortController().signal,
196 );
197 expect(response.choices.length).toBeGreaterThan(0);
198 const completion = response.choices[0].message.content;
199 expect(typeof completion).toBe("string");
200 expect(completion?.length).toBeGreaterThan(0);
201 expect(completion?.startsWith("RESPONSE: ")).toBe(true);
202 });
203
204 test("Tool Call works", async () => {
205 let args = "";
206 let isFirstChunk = true;
207 for await (const chunk of api.chatCompletionStream(
208 {
209 messages: [
210 {
211 role: "user",
212 content:
213 "Hi, my name is Nate. Please call the say_hello function to respond to me.",
214 },
215 ],
216 tools: [
217 {
218 function: {
219 name: "say_hello",
220 description: "Say Hello",
221 parameters: {
222 type: "object",
223 properties: {
224 name: {
225 type: "string",
226 description: "The name of the person to greet",
227 },
228 },
229 },
230 },
231 type: "function",
232 },
233 ],
234 tool_choice: {
235 type: "function",
236 function: {
237 name: "say_hello",
238 },
239 },
240 stream: true,
241 model,
242 },
243 new AbortController().signal,
244 )) {
245 const toolCall = chunk.choices[0].delta.tool_calls?.[0];
246 if (!toolCall) {
247 continue;
248 }
249 args += toolCall.function?.arguments ?? "";
250
251 expect(chunk.choices[0].delta.tool_calls).toHaveLength(1);
252
253 if (isFirstChunk) {
254 isFirstChunk = false;
255 expect(toolCall.id).toBeDefined();
256 expect(toolCall.function!.name).toBe("say_hello");
257 }
258 }
259
260 const parsedArgs = JSON.parse(args);
261 expect(parsedArgs.name).toBe("Nate");
262 });
263
264 test("Tool Call second message works", async () => {
265 let response = "";
266 for await (const chunk of api.chatCompletionStream(
267 {
268 messages: [
269 {
270 role: "user",
271 content:
272 "Hi, my name is Nate. Please call the say_hello function to respond to me, then respond to me again.",
273 },
274 {
275 role: "assistant",
276 tool_calls: [
277 {
278 function: {
279 arguments: '{"name":"Nate"}',
280 name: "say_hello",
281 },
282 id: "tool_call_1",
283 type: "function",
284 },
285 ],
286 },
287 {
288 role: "tool",
289 tool_call_id: "tool_call_1",
290 content: "Nate was greeted",
291 },
292 ],
293 tools: [
294 {
295 function: {
296 name: "say_hello",
297 description: "Say Hello",
298 parameters: {
299 type: "object",
300 properties: {
301 name: {
302 type: "string",
303 description: "The name of the person to greet",
304 },
305 },
306 },
307 },
308 type: "function",
309 },
310 ],
311 stream: true,
312 model,
313 },
314 new AbortController().signal,
315 )) {
316 console.log(JSON.stringify(chunk, null, 2));
317 response += chunk.choices[0].delta.content ?? "";
318 }
319
320 expect(response.length).toBeGreaterThan(0);
321 });
322}
323
324export function testCompletion(api: BaseLlmApi, model: string) {
325 test("should successfully stream complete", async () => {
326 const stream = api.completionStream(
327 {
328 model: model,
329 prompt: "Hello! Who are you?",
330 stream: true,
331 },
332 new AbortController().signal,
333 );
334 let completion = "";
335 for await (const result of stream) {
336 completion += result.choices[0].text ?? "";
337
338 expect(typeof result.choices[0].text).toBe("string");
339 expect(result.choices.length).toBeGreaterThan(0);
340 }
341 expect(completion.length).toBeGreaterThan(0);
342 });
343
344 test("should successfully non-stream complete", async () => {
345 const response = await api.completionNonStream(
346 {
347 model,
348 prompt: "Hello! Who are you?",
349 stream: false,
350 },
351 new AbortController().signal,
352 );
353
354 expect(response.choices.length).toBeGreaterThan(0);
355
356 const completion = response.choices[0].text;
357 expect(typeof completion).toBe("string");
358 expect(completion.length).toBeGreaterThan(0);
359 });
360}