| 1 | import { BaseLlmApi, constructLlmApi } from "../index.js"; |
| 2 | import { LLMConfig } from "../types.js"; |
| 3 | import { TestConfigOptions } from "./main.test.js"; |
| 4 | |
| 5 | export function getLlmApi(config: LLMConfig) { |
| 6 | const api = constructLlmApi(config); |
| 7 | if (!api) { |
| 8 | throw new Error("Failed to construct LLM"); |
| 9 | } |
| 10 | return api; |
| 11 | } |
| 12 | export function testEmbed(api: BaseLlmApi, model: string) { |
| 13 | test("should successfully embed", async () => { |
| 14 | const response = await api.embed({ |
| 15 | model, |
| 16 | input: ["This is a test", "Hello world!"], |
| 17 | }); |
| 18 | expect(response.model).toBe(model); |
| 19 | expect(response.object).toBe("list"); |
| 20 | expect(response.data.length).toEqual(2); |
| 21 | response.data.forEach((val, index) => { |
| 22 | expect(val.index).toBe(index); |
| 23 | expect(val.object).toBe("embedding"); |
| 24 | expect(val.embedding.some((v) => typeof v !== "number")).toBe(false); |
| 25 | }); |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | export function testRerank(api: BaseLlmApi, model: string) { |
| 30 | test("should successfully rerank", async () => { |
| 31 | const response = await api.rerank({ |
| 32 | model: model, |
| 33 | query: "What is the capital of spain?", |
| 34 | documents: [ |
| 35 | "The capital of spain is Madrid", |
| 36 | "The largest breed of dog is the Great Dane", |
| 37 | ], |
| 38 | }); |
| 39 | expect(response.model).toBe(model); |
| 40 | expect(response.object).toBe("list"); |
| 41 | expect(response.data.length).toEqual(2); |
| 42 | response.data.forEach((val, index) => { |
| 43 | expect(val.index).toBe(index); |
| 44 | expect(typeof val.relevance_score).toBe("number"); |
| 45 | }); |
| 46 | expect(response.data[0].relevance_score).toBeGreaterThan( |
| 47 | response.data[1].relevance_score, |
| 48 | ); |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | export function testFim(api: BaseLlmApi, model: string) { |
| 53 | test("should successfully fim", async () => { |
| 54 | const response = api.fimStream( |
| 55 | { |
| 56 | model: model, |
| 57 | prompt: "This is a ", |
| 58 | suffix: " .", |
| 59 | stream: true, |
| 60 | }, |
| 61 | new AbortController().signal, |
| 62 | ); |
| 63 | |
| 64 | let completion = ""; |
| 65 | for await (const result of response) { |
| 66 | expect(result.choices.length).toBeGreaterThan(0); |
| 67 | expect(typeof result.choices[0].delta.content).toBe("string"); |
| 68 | |
| 69 | completion += result.choices[0].delta.content; |
| 70 | } |
| 71 | |
| 72 | expect(completion.length).toBeGreaterThan(0); |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | export function testChat( |
| 77 | api: BaseLlmApi, |
| 78 | model: string, |
| 79 | options?: TestConfigOptions, |
| 80 | ) { |
| 81 | test("should successfully stream chat", async () => { |
| 82 | const stream = api.chatCompletionStream( |
| 83 | { |
| 84 | model, |
| 85 | messages: [{ role: "user", content: "Hello! Who are you?" }], |
| 86 | stream: true, |
| 87 | }, |
| 88 | new AbortController().signal, |
| 89 | ); |
| 90 | let completion = ""; |
| 91 | for await (const result of stream) { |
| 92 | completion += result.choices[0].delta.content ?? ""; |
| 93 | |
| 94 | expect(result.choices.length).toBeGreaterThan(0); |
| 95 | } |
| 96 | expect(completion.length).toBeGreaterThan(0); |
| 97 | }); |
| 98 | |
| 99 | test("should successfully stream multi-part chat with empty text", async () => { |
| 100 | const stream = api.chatCompletionStream( |
| 101 | { |
| 102 | model, |
| 103 | messages: [ |
| 104 | { |
| 105 | role: "user", |
| 106 | content: [ |
| 107 | { |
| 108 | type: "text", |
| 109 | text: "Hello! Who are you?", |
| 110 | }, |
| 111 | { |
| 112 | // @ts-ignore |
| 113 | type: "text", |
| 114 | text: "", |
| 115 | }, |
| 116 | ], |
| 117 | }, |
| 118 | ], |
| 119 | stream: true, |
| 120 | }, |
| 121 | new AbortController().signal, |
| 122 | ); |
| 123 | let completion = ""; |
| 124 | for await (const result of stream) { |
| 125 | completion += result.choices[0].delta.content ?? ""; |
| 126 | |
| 127 | expect(result.choices.length).toBeGreaterThan(0); |
| 128 | } |
| 129 | expect(completion.length).toBeGreaterThan(0); |
| 130 | }); |
| 131 | |
| 132 | test.skip("should successfully stream multi-part chat with image", async () => { |
| 133 | const stream = api.chatCompletionStream( |
| 134 | { |
| 135 | model, |
| 136 | messages: [ |
| 137 | { |
| 138 | role: "user", |
| 139 | content: [ |
| 140 | { |
| 141 | type: "text", |
| 142 | text: "Hello! Who are you?", |
| 143 | }, |
| 144 | { |
| 145 | // @ts-ignore |
| 146 | type: "image_url", |
| 147 | image_url: { |
| 148 | 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", |
| 149 | detail: "low", |
| 150 | }, |
| 151 | }, |
| 152 | ], |
| 153 | }, |
| 154 | ], |
| 155 | stream: true, |
| 156 | }, |
| 157 | new AbortController().signal, |
| 158 | ); |
| 159 | let completion = ""; |
| 160 | for await (const result of stream) { |
| 161 | completion += result.choices[0].delta.content ?? ""; |
| 162 | expect(result.choices.length).toBeGreaterThan(0); |
| 163 | } |
| 164 | expect(completion.length).toBeGreaterThan(0); |
| 165 | }); |
| 166 | |
| 167 | test("should successfully non-stream chat", async () => { |
| 168 | const response = await api.chatCompletionNonStream( |
| 169 | { |
| 170 | model, |
| 171 | messages: [{ role: "user", content: "Hello! Who are you?" }], |
| 172 | stream: false, |
| 173 | }, |
| 174 | new AbortController().signal, |
| 175 | ); |
| 176 | |
| 177 | expect(response.choices.length).toBeGreaterThan(0); |
| 178 | |
| 179 | const completion = response.choices[0].message.content; |
| 180 | expect(typeof completion).toBe("string"); |
| 181 | expect(completion?.length).toBeGreaterThan(0); |
| 182 | }); |
| 183 | |
| 184 | test("should acknowledge system message in chat", async () => { |
| 185 | const response = await api.chatCompletionNonStream( |
| 186 | { |
| 187 | model, |
| 188 | messages: [ |
| 189 | { |
| 190 | role: "system", |
| 191 | content: |
| 192 | "Regardless of what is asked of you, your answer should start with 'RESPONSE: '.", |
| 193 | }, |
| 194 | { role: "user", content: "Who are you?" }, |
| 195 | ], |
| 196 | stream: false, |
| 197 | }, |
| 198 | new AbortController().signal, |
| 199 | ); |
| 200 | expect(response.choices.length).toBeGreaterThan(0); |
| 201 | const completion = response.choices[0].message.content; |
| 202 | expect(typeof completion).toBe("string"); |
| 203 | expect(completion?.length).toBeGreaterThan(0); |
| 204 | expect(completion?.startsWith("RESPONSE: ")).toBe(true); |
| 205 | }); |
| 206 | |
| 207 | if (options?.skipTools === false) { |
| 208 | test("Tool Call works", async () => { |
| 209 | let args = ""; |
| 210 | let isFirstChunk = true; |
| 211 | for await (const chunk of api.chatCompletionStream( |
| 212 | { |
| 213 | messages: [ |
| 214 | { |
| 215 | role: "user", |
| 216 | content: |
| 217 | "Hi, my name is Nate. Please call the say_hello function to respond to me.", |
| 218 | }, |
| 219 | ], |
| 220 | tools: [ |
| 221 | { |
| 222 | function: { |
| 223 | name: "say_hello", |
| 224 | description: "Say Hello", |
| 225 | parameters: { |
| 226 | type: "object", |
| 227 | properties: { |
| 228 | name: { |
| 229 | type: "string", |
| 230 | description: "The name of the person to greet", |
| 231 | }, |
| 232 | }, |
| 233 | }, |
| 234 | }, |
| 235 | type: "function", |
| 236 | }, |
| 237 | ], |
| 238 | tool_choice: { |
| 239 | type: "function", |
| 240 | function: { |
| 241 | name: "say_hello", |
| 242 | }, |
| 243 | }, |
| 244 | stream: true, |
| 245 | model, |
| 246 | }, |
| 247 | new AbortController().signal, |
| 248 | )) { |
| 249 | const toolCall = chunk.choices[0].delta.tool_calls?.[0]; |
| 250 | if (!toolCall) { |
| 251 | continue; |
| 252 | } |
| 253 | args += toolCall.function?.arguments ?? ""; |
| 254 | |
| 255 | expect(chunk.choices[0].delta.tool_calls).toHaveLength(1); |
| 256 | |
| 257 | if (isFirstChunk) { |
| 258 | isFirstChunk = false; |
| 259 | expect(toolCall.id).toBeDefined(); |
| 260 | expect(toolCall.function!.name).toBe("say_hello"); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | const parsedArgs = JSON.parse(args); |
| 265 | expect(parsedArgs.name).toBe("Nate"); |
| 266 | }); |
| 267 | |
| 268 | test("Tool Call second message works", async () => { |
| 269 | let response = ""; |
| 270 | for await (const chunk of api.chatCompletionStream( |
| 271 | { |
| 272 | messages: [ |
| 273 | { |
| 274 | role: "user", |
| 275 | content: |
| 276 | "Hi, my name is Nate. Please call the say_hello function to respond to me, then respond to me again.", |
| 277 | }, |
| 278 | { |
| 279 | role: "assistant", |
| 280 | tool_calls: [ |
| 281 | { |
| 282 | function: { |
| 283 | arguments: '{"name":"Nate"}', |
| 284 | name: "say_hello", |
| 285 | }, |
| 286 | id: "tool_call_1", |
| 287 | type: "function", |
| 288 | }, |
| 289 | ], |
| 290 | }, |
| 291 | { |
| 292 | role: "tool", |
| 293 | tool_call_id: "tool_call_1", |
| 294 | content: "Nate was greeted", |
| 295 | }, |
| 296 | ], |
| 297 | tools: [ |
| 298 | { |
| 299 | function: { |
| 300 | name: "say_hello", |
| 301 | description: "Say Hello", |
| 302 | parameters: { |
| 303 | type: "object", |
| 304 | properties: { |
| 305 | name: { |
| 306 | type: "string", |
| 307 | description: "The name of the person to greet", |
| 308 | }, |
| 309 | }, |
| 310 | }, |
| 311 | }, |
| 312 | type: "function", |
| 313 | }, |
| 314 | ], |
| 315 | stream: true, |
| 316 | model, |
| 317 | }, |
| 318 | new AbortController().signal, |
| 319 | )) { |
| 320 | response += chunk.choices[0].delta.content ?? ""; |
| 321 | } |
| 322 | |
| 323 | expect(response.length).toBeGreaterThan(0); |
| 324 | }); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | export function testCompletion(api: BaseLlmApi, model: string) { |
| 329 | test("should successfully stream complete", async () => { |
| 330 | const stream = api.completionStream( |
| 331 | { |
| 332 | model: model, |
| 333 | prompt: "Hello! Who are you?", |
| 334 | stream: true, |
| 335 | }, |
| 336 | new AbortController().signal, |
| 337 | ); |
| 338 | let completion = ""; |
| 339 | for await (const result of stream) { |
| 340 | completion += result.choices[0].text ?? ""; |
| 341 | |
| 342 | expect(typeof result.choices[0].text).toBe("string"); |
| 343 | expect(result.choices.length).toBeGreaterThan(0); |
| 344 | } |
| 345 | expect(completion.length).toBeGreaterThan(0); |
| 346 | }); |
| 347 | |
| 348 | test("should successfully non-stream complete", async () => { |
| 349 | const response = await api.completionNonStream( |
| 350 | { |
| 351 | model, |
| 352 | prompt: "Hello! Who are you?", |
| 353 | stream: false, |
| 354 | }, |
| 355 | new AbortController().signal, |
| 356 | ); |
| 357 | |
| 358 | expect(response.choices.length).toBeGreaterThan(0); |
| 359 | |
| 360 | const completion = response.choices[0].text; |
| 361 | expect(typeof completion).toBe("string"); |
| 362 | expect(completion.length).toBeGreaterThan(0); |
| 363 | }); |
| 364 | } |