File size: 4,735 Bytes
9580dce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | import { createHuggingFaceClient, toOpenAiMessages } from "../llm-hf";
function mockFetch(payload: unknown, ok = true, status = 200) {
const calls: { url: string; init: RequestInit }[] = [];
const fn = (async (url: string, init: RequestInit) => {
calls.push({ url, init });
return {
ok,
status,
json: async () => payload,
text: async () => JSON.stringify(payload),
} as Response;
}) as unknown as typeof fetch;
return { fn, calls };
}
const CHAT_PAYLOAD = {
choices: [{ message: { content: "PONG" }, finish_reason: "stop" }],
usage: { prompt_tokens: 5, completion_tokens: 1 },
};
describe("toOpenAiMessages", () => {
it("prepends a system message when system is provided", () => {
const out = toOpenAiMessages({
model: "x",
max_tokens: 10,
system: "be terse",
messages: [{ role: "user", content: "hi" }],
});
expect(out[0]).toEqual({ role: "system", content: "be terse" });
expect(out[1]).toEqual({ role: "user", content: "hi" });
});
it("flattens array content blocks into a string", () => {
const out = toOpenAiMessages({
model: "x",
max_tokens: 10,
messages: [
{ role: "user", content: [{ type: "text", text: "a" }, { type: "text", text: "b" }] },
],
});
expect(out[0]).toEqual({ role: "user", content: "ab" });
});
it("omits a system message when none is given", () => {
const out = toOpenAiMessages({
model: "x",
max_tokens: 10,
messages: [{ role: "user", content: "hi" }],
});
expect(out).toHaveLength(1);
expect(out[0].role).toBe("user");
});
});
describe("createHuggingFaceClient", () => {
it("POSTs to the chat/completions endpoint with bearer auth and the configured model", async () => {
const { fn, calls } = mockFetch(CHAT_PAYLOAD);
const client = createHuggingFaceClient({
apiKey: "hf_test",
model: "meta-llama/Llama-3.1-8B-Instruct",
fetchImpl: fn,
});
await client.messages.create({
model: "claude-haiku-4-5-20251001", // should be ignored
max_tokens: 50,
messages: [{ role: "user", content: "hi" }],
});
expect(calls).toHaveLength(1);
expect(calls[0].url).toBe("https://router.huggingface.co/v1/chat/completions");
const headers = calls[0].init.headers as Record<string, string>;
expect(headers.Authorization).toBe("Bearer hf_test");
const body = JSON.parse(calls[0].init.body as string);
expect(body.model).toBe("meta-llama/Llama-3.1-8B-Instruct");
expect(body.max_tokens).toBe(50);
});
it("returns an Anthropic-shaped result with content[0].text", async () => {
const { fn } = mockFetch(CHAT_PAYLOAD);
const client = createHuggingFaceClient({ apiKey: "k", fetchImpl: fn });
const res = await client.messages.create({
model: "m",
max_tokens: 10,
messages: [{ role: "user", content: "say pong" }],
});
expect(res.content[0].type).toBe("text");
expect(res.content[0].text).toBe("PONG");
expect(res.stop_reason).toBe("stop");
});
it("passes temperature when provided", async () => {
const { fn, calls } = mockFetch(CHAT_PAYLOAD);
const client = createHuggingFaceClient({ apiKey: "k", fetchImpl: fn });
await client.messages.create({
model: "m",
max_tokens: 10,
temperature: 0.2,
messages: [{ role: "user", content: "x" }],
});
const body = JSON.parse(calls[0].init.body as string);
expect(body.temperature).toBe(0.2);
});
it("honors a custom base URL and trims a trailing slash", async () => {
const { fn, calls } = mockFetch(CHAT_PAYLOAD);
const client = createHuggingFaceClient({
apiKey: "k",
baseUrl: "https://example.com/v1/",
fetchImpl: fn,
});
await client.messages.create({ model: "m", max_tokens: 10, messages: [{ role: "user", content: "x" }] });
expect(calls[0].url).toBe("https://example.com/v1/chat/completions");
});
it("throws with status and body on a non-ok response", async () => {
const { fn } = mockFetch({ error: "bad token" }, false, 401);
const client = createHuggingFaceClient({ apiKey: "k", fetchImpl: fn });
await expect(
client.messages.create({ model: "m", max_tokens: 10, messages: [{ role: "user", content: "x" }] }),
).rejects.toThrow(/401/);
});
it("defaults to a string even if the provider returns no content", async () => {
const { fn } = mockFetch({ choices: [{ message: {} }] });
const client = createHuggingFaceClient({ apiKey: "k", fetchImpl: fn });
const res = await client.messages.create({ model: "m", max_tokens: 10, messages: [{ role: "user", content: "x" }] });
expect(res.content[0].text).toBe("");
});
});
|