Elffuss BraidHub

kikocis/continue public

b4db4cbf8943 · 11.1 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 expect(usage!.total_tokens).toEqual(
110 usage!.prompt_tokens + usage!.completion_tokens,
111 );
112 }
113 });
114
115 test("should successfully stream multi-part chat with empty text", async () => {
116 const stream = api.chatCompletionStream(
117 {
118 model,
119 messages: [
120 {
121 role: "user",
122 content: [
123 {
124 type: "text",
125 text: "Hello! Who are you?",
126 },
127 {
128 // @ts-ignore
129 type: "text",
130 text: "",
131 },
132 ],
133 },
134 ],
135 stream: true,
136 },
137 new AbortController().signal,
138 );
139 let completion = "";
140 for await (const result of stream) {
141 // Skip usage chunks that have empty choices array
142 if (result.choices.length === 0) {
143 continue;
144 }
145 completion += result.choices[0].delta.content ?? "";
146
147 expect(result.choices.length).toBeGreaterThan(0);
148 }
149 expect(completion.length).toBeGreaterThan(0);
150 });
151
152 test.skip("should successfully stream multi-part chat with image", async () => {
153 const stream = api.chatCompletionStream(
154 {
155 model,
156 messages: [
157 {
158 role: "user",
159 content: [
160 {
161 type: "text",
162 text: "Hello! Who are you?",
163 },
164 {
165 // @ts-ignore
166 type: "image_url",
167 image_url: {
168 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",
169 detail: "low",
170 },
171 },
172 ],
173 },
174 ],
175 stream: true,
176 },
177 new AbortController().signal,
178 );
179 let completion = "";
180 for await (const result of stream) {
181 completion += result.choices[0].delta.content ?? "";
182 expect(result.choices.length).toBeGreaterThan(0);
183 }
184 expect(completion.length).toBeGreaterThan(0);
185 });
186
187 test("should successfully non-stream chat", async () => {
188 const response = await api.chatCompletionNonStream(
189 {
190 model,
191 messages: [{ role: "user", content: "Hello! Who are you?" }],
192 stream: false,
193 },
194 new AbortController().signal,
195 );
196
197 expect(response.choices.length).toBeGreaterThan(0);
198
199 const completion = response.choices[0].message.content;
200 expect(typeof completion).toBe("string");
201 expect(completion?.length).toBeGreaterThan(0);
202 });
203
204 test("should acknowledge system message in chat", async () => {
205 const response = await api.chatCompletionNonStream(
206 {
207 model,
208 messages: [
209 {
210 role: "system",
211 content:
212 "Regardless of what is asked of you, your answer should start with 'RESPONSE: '.",
213 },
214 { role: "user", content: "Who are you?" },
215 ],
216 stream: false,
217 },
218 new AbortController().signal,
219 );
220 expect(response.choices.length).toBeGreaterThan(0);
221 const completion = response.choices[0].message.content;
222 expect(typeof completion).toBe("string");
223 expect(completion?.length).toBeGreaterThan(0);
224 expect(completion?.startsWith("RESPONSE: ")).toBe(true);
225 });
226
227 if (options?.skipTools === false) {
228 test("Tool Call works", async () => {
229 let args = "";
230 let isFirstChunk = true;
231 for await (const chunk of api.chatCompletionStream(
232 {
233 messages: [
234 {
235 role: "user",
236 content:
237 "Hi, my name is Nate. Please call the say_hello function to respond to me.",
238 },
239 ],
240 tools: [
241 {
242 function: {
243 name: "say_hello",
244 description: "Say Hello",
245 parameters: {
246 type: "object",
247 properties: {
248 name: {
249 type: "string",
250 description: "The name of the person to greet",
251 },
252 },
253 },
254 },
255 type: "function",
256 },
257 ],
258 tool_choice: {
259 type: "function",
260 function: {
261 name: "say_hello",
262 },
263 },
264 stream: true,
265 model,
266 },
267 new AbortController().signal,
268 )) {
269 // Skip usage chunks that have empty choices array
270 if (chunk.choices.length === 0) {
271 continue;
272 }
273 const toolCall = chunk.choices[0].delta.tool_calls?.[0];
274 if (!toolCall) {
275 continue;
276 }
277 args += toolCall.function?.arguments ?? "";
278
279 expect(chunk.choices[0].delta.tool_calls).toHaveLength(1);
280
281 if (isFirstChunk) {
282 isFirstChunk = false;
283 expect(toolCall.id).toBeDefined();
284 expect(toolCall.function!.name).toBe("say_hello");
285 }
286 }
287
288 const parsedArgs = JSON.parse(args);
289 expect(parsedArgs.name).toBe("Nate");
290 });
291
292 test("Tool Call second message works", async () => {
293 let response = "";
294 for await (const chunk of api.chatCompletionStream(
295 {
296 messages: [
297 {
298 role: "user",
299 content:
300 "Hi, my name is Nate. Please call the say_hello function to respond to me, then respond to me again.",
301 },
302 {
303 role: "assistant",
304 tool_calls: [
305 {
306 function: {
307 arguments: '{"name":"Nate"}',
308 name: "say_hello",
309 },
310 id: "tool_call_1",
311 type: "function",
312 },
313 ],
314 },
315 {
316 role: "tool",
317 tool_call_id: "tool_call_1",
318 content: "Nate was greeted",
319 },
320 ],
321 tools: [
322 {
323 function: {
324 name: "say_hello",
325 description: "Say Hello",
326 parameters: {
327 type: "object",
328 properties: {
329 name: {
330 type: "string",
331 description: "The name of the person to greet",
332 },
333 },
334 },
335 },
336 type: "function",
337 },
338 ],
339 stream: true,
340 model,
341 },
342 new AbortController().signal,
343 )) {
344 // Skip usage chunks that have empty choices array
345 if (chunk.choices.length === 0) {
346 continue;
347 }
348 response += chunk.choices[0].delta.content ?? "";
349 }
350
351 expect(response.length).toBeGreaterThan(0);
352 });
353 }
354}
355
356export function testCompletion(api: BaseLlmApi, model: string) {
357 test("should successfully stream complete", async () => {
358 const stream = api.completionStream(
359 {
360 model: model,
361 prompt: "Hello! Who are you?",
362 stream: true,
363 },
364 new AbortController().signal,
365 );
366 let completion = "";
367 for await (const result of stream) {
368 completion += result.choices[0].text ?? "";
369
370 expect(typeof result.choices[0].text).toBe("string");
371 expect(result.choices.length).toBeGreaterThan(0);
372 }
373 expect(completion.length).toBeGreaterThan(0);
374 });
375
376 test("should successfully non-stream complete", async () => {
377 const response = await api.completionNonStream(
378 {
379 model,
380 prompt: "Hello! Who are you?",
381 stream: false,
382 },
383 new AbortController().signal,
384 );
385
386 expect(response.choices.length).toBeGreaterThan(0);
387
388 const completion = response.choices[0].text;
389 expect(typeof completion).toBe("string");
390 expect(completion.length).toBeGreaterThan(0);
391 });
392}