Path: blob/master/src/packages/backend/conat/test/llm.test.ts
1450 views
/*1Test LLM NATS streaming.23DEVELOPMENT:45pnpm test llm.test.ts67*/89// this sets client10import "@cocalc/backend/conat";1112import { init, close } from "@cocalc/conat/llm/server";13import { llm } from "@cocalc/conat/llm/client";14import { before, after } from "@cocalc/backend/conat/test/setup";1516beforeAll(before);1718describe("create an llm server, client, and stub evaluator, and run an evaluation", () => {19// define trivial evaluate20const OUTPUT = "Thanks for asing about ";21async function evaluate({ input, stream }) {22stream(OUTPUT);23stream(input);24stream();25}2627it("creates the server", async () => {28await init(evaluate);29});3031it("calls the llm", async () => {32const v: (string | null)[] = [];33const input = "cocalc";34const all = await llm({35account_id: "00000000-0000-4000-8000-000000000000",36system: "in cocalc",37input,38stream: (text) => {39v.push(text);40},41});42expect(all).toBe(OUTPUT + input);43expect(v[0]).toBe(OUTPUT);44expect(v[1]).toBe(input);45});4647it("closes the server", async () => {48await close();49});50});5152describe("test an evaluate that throws an error half way through", () => {53// define trivial evaluate54const OUTPUT = "Thanks for asing about ";55const ERROR = "I give up";56async function evaluate({ stream }) {57stream(OUTPUT);58throw Error(ERROR);59}6061it("creates the server", async () => {62await init(evaluate);63});6465it("calls the llm", async () => {66const v: (string | null)[] = [];67const input = "cocalc";68await expect(69async () =>70await llm({71account_id: "00000000-0000-4000-8000-000000000000",72system: "in cocalc",73input,74stream: (text) => {75v.push(text);76},77}),78).rejects.toThrow(ERROR);79expect(v[0]).toBe(OUTPUT);80expect(v.length).toBe(1);81});8283it("closes the server", async () => {84await close();85});86});8788afterAll(after);899091