|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import type { ChatCompletionStreamOutput } from "@huggingface/tasks"; |
| 3 | +import { InferenceClient, chatCompletion, chatCompletionStream } from "../src/index.js"; |
| 4 | +import { HARDCODED_MODEL_INFERENCE_MAPPING } from "../src/providers/consts.js"; |
| 5 | + |
| 6 | +const TIMEOUT = 60000 * 3; |
| 7 | +const env = import.meta.env; |
| 8 | + |
| 9 | +if (!env.HF_TOKEN) { |
| 10 | + console.warn("Set HF_TOKEN in the env to run the tests for better rate limits"); |
| 11 | +} |
| 12 | + |
| 13 | +describe.skip.concurrent( |
| 14 | + "Avian", |
| 15 | + () => { |
| 16 | + const client = new InferenceClient(env.HF_AVIAN_KEY ?? "dummy"); |
| 17 | + |
| 18 | + HARDCODED_MODEL_INFERENCE_MAPPING["avian"] = { |
| 19 | + "zai-org/GLM-4.6": { |
| 20 | + provider: "avian", |
| 21 | + hfModelId: "zai-org/GLM-4.6", |
| 22 | + providerId: "glm-4.6", |
| 23 | + status: "live", |
| 24 | + task: "conversational", |
| 25 | + }, |
| 26 | + }; |
| 27 | + |
| 28 | + it("chatCompletion", async () => { |
| 29 | + const res = await client.chatCompletion({ |
| 30 | + model: "zai-org/GLM-4.6", |
| 31 | + provider: "avian", |
| 32 | + messages: [{ role: "user", content: "Complete this sentence with words, one plus one is equal " }], |
| 33 | + }); |
| 34 | + if (res.choices && res.choices.length > 0) { |
| 35 | + const completion = res.choices[0].message?.content; |
| 36 | + expect(completion).toContain("two"); |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + it("chatCompletion stream", async () => { |
| 41 | + const stream = client.chatCompletionStream({ |
| 42 | + model: "zai-org/GLM-4.6", |
| 43 | + provider: "avian", |
| 44 | + messages: [{ role: "user", content: "Complete the equation 1 + 1 = , just the answer" }], |
| 45 | + stream: true, |
| 46 | + }) as AsyncGenerator<ChatCompletionStreamOutput>; |
| 47 | + |
| 48 | + let fullResponse = ""; |
| 49 | + for await (const chunk of stream) { |
| 50 | + if (chunk.choices && chunk.choices.length > 0) { |
| 51 | + const content = chunk.choices[0].delta?.content; |
| 52 | + if (content) { |
| 53 | + fullResponse += content; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + expect(fullResponse).toBeTruthy(); |
| 59 | + expect(fullResponse.length).toBeGreaterThan(0); |
| 60 | + expect(fullResponse).toMatch(/(two|2)/i); |
| 61 | + }); |
| 62 | + |
| 63 | + it("chatCompletion - using chatCompletion function", async () => { |
| 64 | + const res = await chatCompletion({ |
| 65 | + accessToken: env.HF_AVIAN_KEY ?? "dummy", |
| 66 | + model: "zai-org/GLM-4.6", |
| 67 | + provider: "avian", |
| 68 | + messages: [{ role: "user", content: "Complete this sentence with words, one plus one is equal " }], |
| 69 | + temperature: 0.1, |
| 70 | + }); |
| 71 | + |
| 72 | + expect(res).toBeDefined(); |
| 73 | + expect(res.choices).toBeDefined(); |
| 74 | + expect(res.choices?.length).toBeGreaterThan(0); |
| 75 | + |
| 76 | + if (res.choices && res.choices.length > 0) { |
| 77 | + const completion = res.choices[0].message?.content; |
| 78 | + expect(completion).toBeDefined(); |
| 79 | + expect(typeof completion).toBe("string"); |
| 80 | + expect(completion).toMatch(/(two|2)/i); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + it("chatCompletion stream - using chatCompletionStream function", async () => { |
| 85 | + const stream = chatCompletionStream({ |
| 86 | + accessToken: env.HF_AVIAN_KEY ?? "dummy", |
| 87 | + model: "zai-org/GLM-4.6", |
| 88 | + provider: "avian", |
| 89 | + messages: [{ role: "user", content: "Complete the equation 1 + 1 = , just the answer" }], |
| 90 | + }) as AsyncGenerator<ChatCompletionStreamOutput>; |
| 91 | + |
| 92 | + let out = ""; |
| 93 | + for await (const chunk of stream) { |
| 94 | + if (chunk.choices && chunk.choices.length > 0) { |
| 95 | + out += chunk.choices[0].delta.content; |
| 96 | + } |
| 97 | + } |
| 98 | + expect(out).toMatch(/(two|2)/i); |
| 99 | + }); |
| 100 | + }, |
| 101 | + TIMEOUT |
| 102 | +); |
0 commit comments