Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/backend/conat/test/llm.test.ts
1450 views
1
/*
2
Test LLM NATS streaming.
3
4
DEVELOPMENT:
5
6
pnpm test llm.test.ts
7
8
*/
9
10
// this sets client
11
import "@cocalc/backend/conat";
12
13
import { init, close } from "@cocalc/conat/llm/server";
14
import { llm } from "@cocalc/conat/llm/client";
15
import { before, after } from "@cocalc/backend/conat/test/setup";
16
17
beforeAll(before);
18
19
describe("create an llm server, client, and stub evaluator, and run an evaluation", () => {
20
// define trivial evaluate
21
const OUTPUT = "Thanks for asing about ";
22
async function evaluate({ input, stream }) {
23
stream(OUTPUT);
24
stream(input);
25
stream();
26
}
27
28
it("creates the server", async () => {
29
await init(evaluate);
30
});
31
32
it("calls the llm", async () => {
33
const v: (string | null)[] = [];
34
const input = "cocalc";
35
const all = await llm({
36
account_id: "00000000-0000-4000-8000-000000000000",
37
system: "in cocalc",
38
input,
39
stream: (text) => {
40
v.push(text);
41
},
42
});
43
expect(all).toBe(OUTPUT + input);
44
expect(v[0]).toBe(OUTPUT);
45
expect(v[1]).toBe(input);
46
});
47
48
it("closes the server", async () => {
49
await close();
50
});
51
});
52
53
describe("test an evaluate that throws an error half way through", () => {
54
// define trivial evaluate
55
const OUTPUT = "Thanks for asing about ";
56
const ERROR = "I give up";
57
async function evaluate({ stream }) {
58
stream(OUTPUT);
59
throw Error(ERROR);
60
}
61
62
it("creates the server", async () => {
63
await init(evaluate);
64
});
65
66
it("calls the llm", async () => {
67
const v: (string | null)[] = [];
68
const input = "cocalc";
69
await expect(
70
async () =>
71
await llm({
72
account_id: "00000000-0000-4000-8000-000000000000",
73
system: "in cocalc",
74
input,
75
stream: (text) => {
76
v.push(text);
77
},
78
}),
79
).rejects.toThrow(ERROR);
80
expect(v[0]).toBe(OUTPUT);
81
expect(v.length).toBe(1);
82
});
83
84
it("closes the server", async () => {
85
await close();
86
});
87
});
88
89
afterAll(after);
90
91