Elffuss BraidHub

kikocis/continue public

4db7e4f9992e · 6.4 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: { type: "function", function: { name: "say_hello" } },
165 },
166 )) {
167 const typedChunk = chunk as AssistantChatMessage;
168 if (!typedChunk.toolCalls) {
169 continue;
170 }
171 const toolCall = typedChunk.toolCalls[0];
172 args += toolCall.function?.arguments ?? "";
173
174 expect(chunk.role).toBe("assistant");
175 expect(chunk.content).toBe("");
176 expect(typedChunk.toolCalls).toHaveLength(1);
177
178 if (isFirstChunk) {
179 isFirstChunk = false;
180 expect(toolCall.id).toBeDefined();
181 expect(toolCall.function!.name).toBe("say_hello");
182 }
183 }
184
185 const parsedArgs = JSON.parse(args);
186 expect(parsedArgs.name).toBe("Nate");
187 }),
188 timeout,
189 );
190 }
191 });
192}
193
194describe("LLM", () => {
195 if (process.env.IGNORE_API_KEY_TESTS === "true") {
196 test("Skipping API key tests", () => {
197 console.log(
198 "Skipping API key tests due to IGNORE_API_KEY_TESTS being set",
199 );
200 });
201 return;
202 }
203
204 testLLM(
205 new Anthropic({
206 model: "claude-3-5-sonnet-latest",
207 apiKey: process.env.ANTHROPIC_API_KEY,
208 }),
209 { skip: false, testToolCall: true },
210 );
211 testLLM(new OpenAI({ apiKey: process.env.OPENAI_API_KEY, model: "gpt-4o" }), {
212 skip: false,
213 testToolCall: true,
214 });
215 testLLM(
216 new OpenAI({ apiKey: process.env.OPENAI_API_KEY, model: "o3-mini" }),
217 { skip: false, timeout: 60000 },
218 );
219 testLLM(new OpenAI({ apiKey: process.env.OPENAI_API_KEY, model: "o1" }), {
220 skip: false,
221 timeout: 60000,
222 });
223 testLLM(
224 new Gemini({
225 model: "gemini-2.0-flash-exp",
226 apiKey: process.env.GEMINI_API_KEY,
227 }),
228 { skip: false },
229 );
230 testLLM(
231 new Mistral({
232 apiKey: process.env.MISTRAL_API_KEY,
233 model: "codestral-latest",
234 }),
235 { testFim: true, skip: false, testToolCall: true, timeout: 60000 },
236 );
237 testLLM(
238 new Azure({
239 apiKey: process.env.AZURE_OPENAI_API_KEY,
240 model: "gpt-4o",
241 apiVersion: "2024-05-01-preview",
242 apiBase: "https://continue-azure-openai-instance.openai.azure.com",
243 deployment: "azure-openai-deployment",
244 apiType: "azure-openai",
245 }),
246 { testFim: false, skip: false, timeout: 20000 },
247 );
248 testLLM(
249 new Azure({
250 apiKey: process.env.AZURE_FOUNDRY_CODESTRAL_API_KEY,
251 model: "Codestral-2501",
252 apiBase: "https://continue-foundry-resource.services.ai.azure.com",
253 env: { apiType: "azure-foundry", apiVersion: "2024-05-01-preview" },
254 }),
255 { testFim: false, skip: false, timeout: 20000 },
256 );
257});