Elffuss BraidHub

kikocis/continue public

71fd2e7d4456 · 6.5 KiBRawPermalink
1import * as dotenv from "dotenv";
2
3import { AssistantChatMessage, CompletionOptions } from "..";
4
5import Anthropic from "./llms/Anthropic";
6import Azure from "./llms/Azure";
7import Gemini from "./llms/Gemini";
8import Mistral from "./llms/Mistral";
9import OpenAI from "./llms/OpenAI";
10
11import { BaseLLM } from ".";
12
13dotenv.config();
14
15const COMPLETION_OPTIONS: Partial<CompletionOptions> = {
16 // temperature: 0.5,
17 topP: 1,
18 topK: 40,
19 presencePenalty: 0,
20 frequencyPenalty: 0,
21 // stop: ["\n"],
22 // maxTokens: 5,
23};
24
25/**
26 * Retries a test function if it fails on the first attempt
27 * @param testFn The test function to run
28 * @returns A function that will retry once if the test fails
29 */
30const retryOnce = (testFn: () => Promise<any>) => async () => {
31 try {
32 return await testFn();
33 } catch (error) {
34 console.log("Test failed on first attempt, retrying once...");
35 return await testFn();
36 }
37};
38
39function testLLM(
40 llm: BaseLLM,
41 {
42 skip,
43 testFim,
44 testToolCall,
45 timeout,
46 }: {
47 skip?: boolean;
48 testFim?: boolean;
49 testToolCall?: boolean;
50 timeout?: number;
51 },
52) {
53 if (skip) {
54 return;
55 }
56
57 beforeAll(() => {
58 llm.completionOptions = { ...llm.completionOptions, ...COMPLETION_OPTIONS };
59 });
60
61 describe(llm.providerName + "/" + llm.model, () => {
62 test(
63 "Stream Chat works",
64 retryOnce(async () => {
65 let total = "";
66 for await (const chunk of llm.streamChat(
67 [{ role: "user", content: "Hi" }],
68 new AbortController().signal,
69 )) {
70 total += chunk.content;
71 }
72
73 expect(total.length).toBeGreaterThan(0);
74 return;
75 }),
76 timeout,
77 );
78
79 test(
80 "Stream Complete works",
81 retryOnce(async () => {
82 let total = "";
83 for await (const chunk of llm.streamComplete(
84 "Hi",
85 new AbortController().signal,
86 )) {
87 total += chunk;
88 }
89
90 expect(total.length).toBeGreaterThan(0);
91 return;
92 }),
93 timeout,
94 );
95
96 test(
97 "Complete works",
98 retryOnce(async () => {
99 const completion = await llm.complete(
100 "Hi",
101 new AbortController().signal,
102 );
103
104 expect(completion.length).toBeGreaterThan(0);
105 return;
106 }),
107 timeout,
108 );
109
110 if (testFim) {
111 test(
112 "FIM works",
113 retryOnce(async () => {
114 let total = "";
115 for await (const chunk of llm.streamFim(
116 "Hi",
117 "name is ChatGPT.",
118 new AbortController().signal,
119 )) {
120 total += chunk;
121 }
122
123 expect(total.length).toBeGreaterThan(0);
124 return;
125 }),
126 timeout,
127 );
128 }
129
130 if (testToolCall) {
131 test(
132 "Tool Call works",
133 retryOnce(async () => {
134 let args = "";
135 let isFirstChunk = true;
136 for await (const chunk of llm.streamChat(
137 [{ role: "user", content: "Hi, my name is Nate." }],
138 new AbortController().signal,
139 {
140 tools: [
141 {
142 displayTitle: "Say Hello",
143 function: {
144 name: "say_hello",
145 description: "Say Hello",
146 parameters: {
147 type: "object",
148 properties: {
149 name: {
150 type: "string",
151 description: "The name of the person to greet",
152 },
153 },
154 },
155 },
156 type: "function",
157 wouldLikeTo: "say hello",
158 isCurrently: "saying hello",
159 hasAlready: "said hello",
160 readonly: true,
161 group: "Hello",
162 },
163 ],
164 toolChoice: {
165 type: "function",
166 function: {
167 name: "say_hello",
168 },
169 },
170 },
171 )) {
172 const typedChunk = chunk as AssistantChatMessage;
173 if (!typedChunk.toolCalls) {
174 continue;
175 }
176 const toolCall = typedChunk.toolCalls[0];
177 args += toolCall.function?.arguments ?? "";
178
179 expect(chunk.role).toBe("assistant");
180 expect(chunk.content).toBe("");
181 expect(typedChunk.toolCalls).toHaveLength(1);
182
183 if (isFirstChunk) {
184 isFirstChunk = false;
185 expect(toolCall.id).toBeDefined();
186 expect(toolCall.function!.name).toBe("say_hello");
187 }
188 }
189
190 const parsedArgs = JSON.parse(args);
191 expect(parsedArgs.name).toBe("Nate");
192 }),
193 timeout,
194 );
195 }
196 });
197}
198
199describe("LLM", () => {
200 if (process.env.IGNORE_API_KEY_TESTS === "true") {
201 test("Skipping API key tests", () => {
202 console.log(
203 "Skipping API key tests due to IGNORE_API_KEY_TESTS being set",
204 );
205 });
206 return;
207 }
208
209 testLLM(
210 new Anthropic({
211 model: "claude-3-5-sonnet-latest",
212 apiKey: process.env.ANTHROPIC_API_KEY,
213 }),
214 {
215 skip: false,
216 testToolCall: true,
217 },
218 );
219 testLLM(new OpenAI({ apiKey: process.env.OPENAI_API_KEY, model: "gpt-4o" }), {
220 skip: false,
221 testToolCall: true,
222 });
223 testLLM(
224 new OpenAI({ apiKey: process.env.OPENAI_API_KEY, model: "o1-preview" }),
225 { skip: false, timeout: 60000 },
226 );
227 testLLM(new OpenAI({ apiKey: process.env.OPENAI_API_KEY, model: "o1" }), {
228 skip: false,
229 timeout: 60000,
230 });
231 testLLM(
232 new Gemini({
233 model: "gemini-2.0-flash-exp",
234 apiKey: process.env.GEMINI_API_KEY,
235 }),
236 { skip: false },
237 );
238 testLLM(
239 new Mistral({
240 apiKey: process.env.MISTRAL_API_KEY,
241 model: "codestral-latest",
242 }),
243 {
244 testFim: true,
245 skip: false,
246 testToolCall: true,
247 timeout: 60000,
248 },
249 );
250 testLLM(
251 new Azure({
252 apiKey: process.env.AZURE_OPENAI_API_KEY,
253 model: "gpt-4o",
254 apiVersion: "2024-05-01-preview",
255 apiBase: "https://continue-azure-openai-instance.openai.azure.com",
256 deployment: "azure-openai-deployment",
257 apiType: "azure-openai",
258 }),
259 { testFim: false, skip: false, timeout: 20000 },
260 );
261 testLLM(
262 new Azure({
263 apiKey: process.env.AZURE_FOUNDRY_API_KEY,
264 model: "codestral-latest",
265 apiBase:
266 "https://codestral-2501-continue-testing.eastus.models.ai.azure.com",
267 apiType: "azure-foundry",
268 }),
269 { testFim: false, skip: false, timeout: 20000 },
270 );
271});