Elffuss BraidHub

kikocis/continue public

87f75892af56 · 11.4 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 const response = await api.chatCompletionNonStream(
214 {
215 model,
216 messages: [
217 {
218 role: "system",
219 content:
220 "Regardless of what is asked of you, your answer should start with 'RESPONSE: '.",
221 },
222 { role: "user", content: "Who are you?" },
223 ],
224 stream: false,
225 },
226 new AbortController().signal,
227 );
228 expect(response.choices.length).toBeGreaterThan(0);
229 const completion = response.choices[0].message.content;
230 expect(typeof completion).toBe("string");
231 expect(completion?.length).toBeGreaterThan(0);
232 expect(completion?.startsWith("RESPONSE: ")).toBe(true);
233 });
234
235 if (options?.skipTools === false) {
236 test("Tool Call works", async () => {
237 let args = "";
238 let isFirstChunk = true;
239 for await (const chunk of api.chatCompletionStream(
240 {
241 messages: [
242 {
243 role: "user",
244 content:
245 "Hi, my name is Nate. Please call the say_hello function to respond to me.",
246 },
247 ],
248 tools: [
249 {
250 function: {
251 name: "say_hello",
252 description: "Say Hello",
253 parameters: {
254 type: "object",
255 properties: {
256 name: {
257 type: "string",
258 description: "The name of the person to greet",
259 },
260 },
261 },
262 },
263 type: "function",
264 },
265 ],
266 tool_choice: {
267 type: "function",
268 function: {
269 name: "say_hello",
270 },
271 },
272 stream: true,
273 model,
274 },
275 new AbortController().signal,
276 )) {
277 // Skip usage chunks that have empty choices array
278 if (chunk.choices.length === 0) {
279 continue;
280 }
281 const toolCall = chunk.choices[0].delta.tool_calls?.[0];
282 if (!toolCall) {
283 continue;
284 }
285 args += toolCall.function?.arguments ?? "";
286
287 expect(chunk.choices[0].delta.tool_calls).toHaveLength(1);
288
289 if (isFirstChunk) {
290 isFirstChunk = false;
291 expect(toolCall.id).toBeDefined();
292 expect(toolCall.function!.name).toBe("say_hello");
293 }
294 }
295
296 const parsedArgs = JSON.parse(args);
297 expect(parsedArgs.name).toBe("Nate");
298 });
299
300 test("Tool Call second message works", async () => {
301 let response = "";
302 for await (const chunk of api.chatCompletionStream(
303 {
304 messages: [
305 {
306 role: "user",
307 content:
308 "Hi, my name is Nate. Please call the say_hello function to respond to me, then respond to me again.",
309 },
310 {
311 role: "assistant",
312 tool_calls: [
313 {
314 function: {
315 arguments: '{"name":"Nate"}',
316 name: "say_hello",
317 },
318 id: "tool_call_1",
319 type: "function",
320 },
321 ],
322 },
323 {
324 role: "tool",
325 tool_call_id: "tool_call_1",
326 content: "Nate was greeted",
327 },
328 ],
329 tools: [
330 {
331 function: {
332 name: "say_hello",
333 description: "Say Hello",
334 parameters: {
335 type: "object",
336 properties: {
337 name: {
338 type: "string",
339 description: "The name of the person to greet",
340 },
341 },
342 },
343 },
344 type: "function",
345 },
346 ],
347 stream: true,
348 model,
349 },
350 new AbortController().signal,
351 )) {
352 // Skip usage chunks that have empty choices array
353 if (chunk.choices.length === 0) {
354 continue;
355 }
356 response += chunk.choices[0].delta.content ?? "";
357 }
358
359 expect(response.length).toBeGreaterThan(0);
360 });
361 }
362}
363
364export function testCompletion(api: BaseLlmApi, model: string) {
365 test("should successfully stream complete", async () => {
366 const stream = api.completionStream(
367 {
368 model: model,
369 prompt: "Hello! Who are you?",
370 stream: true,
371 },
372 new AbortController().signal,
373 );
374 let completion = "";
375 for await (const result of stream) {
376 completion += result.choices[0].text ?? "";
377
378 expect(typeof result.choices[0].text).toBe("string");
379 expect(result.choices.length).toBeGreaterThan(0);
380 }
381 expect(completion.length).toBeGreaterThan(0);
382 });
383
384 test("should successfully non-stream complete", async () => {
385 const response = await api.completionNonStream(
386 {
387 model,
388 prompt: "Hello! Who are you?",
389 stream: false,
390 },
391 new AbortController().signal,
392 );
393
394 expect(response.choices.length).toBeGreaterThan(0);
395
396 const completion = response.choices[0].text;
397 expect(typeof completion).toBe("string");
398 expect(completion.length).toBeGreaterThan(0);
399 });
400}