Elffuss BraidHub

kikocis/continue public

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