Elffuss BraidHub

kikocis/continue public

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