File size: 1,263 Bytes
837e3ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { afterEach, describe, expect, it, vi } from "vitest";
import { generateLlmText } from "./llm-client";

afterEach(() => {
  vi.restoreAllMocks();
});

describe("generateLlmText", () => {
  it("requests JSON object responses from OpenAI-compatible providers when asked", async () => {
    const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(
      new Response(
        JSON.stringify({
          choices: [
            {
              message: {
                content: '{"assistantMessage":"Connected","boardAction":{"type":"none","workPackageId":null}}',
              },
            },
          ],
        }),
        {
          status: 200,
          headers: { "content-type": "application/json" },
        },
      ),
    );

    await generateLlmText({
      config: {
        apiKey: "key",
        baseUrl: "https://api.example.com/v1",
        model: "gpt-test",
      },
      systemPrompt: "Return JSON only.",
      userPrompt: "Say hello.",
      responseFormat: "json_object",
    });

    const [, init] = fetchMock.mock.calls[0] ?? [];
    const body = JSON.parse(String(init?.body ?? "{}")) as {
      response_format?: { type?: string };
    };

    expect(body.response_format?.type).toBe("json_object");
  });
});