Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/admin/llm/test-component.tsx
Views: 687
import { Alert, Space } from "antd";1import { throttle } from "lodash";23import {4useAsyncEffect,5useEffect,6useState,7} from "@cocalc/frontend/app-framework";8import { Icon, Loading } from "@cocalc/frontend/components";9import { Markdown } from "@cocalc/frontend/markdown";10import { webapp_client } from "@cocalc/frontend/webapp-client";11import { LanguageModelCore } from "@cocalc/util/db-schema/llm-utils";12import { PROMPTS } from "./tests";13import { Value } from "./value";1415interface TestLLMProps {16model: LanguageModelCore | string;17test: number | null;18queryState: [boolean | undefined, (val: boolean) => void];19}2021export function TestLLM({ model, test, queryState }: TestLLMProps) {22const [querying, setQuerying] = queryState;23const [output, setOutput] = useState<string>("");24const [error, setError] = useState<string>("");25const [passed, setPassed] = useState<boolean | undefined>();2627const {28prompt,29expected,30system = undefined,31history = undefined,32} = typeof test === "number" ? PROMPTS[test] : { prompt: "", expected: "" };33const expectedRegex = new RegExp(expected, "g");3435const check = throttle(36() => {37if (passed != null && output.trim() === "") {38setPassed(undefined);39} else if (expectedRegex.test(output) && !passed) {40setPassed(true);41}42},43250,44{45leading: false,46trailing: true,47},48);4950useEffect(() => {51if (prompt.trim() === "") {52setOutput("");53setError("");54setPassed(undefined);55}56}, [prompt, test]);5758useEffect(() => {59check();60}, [output]);6162useAsyncEffect(async () => {63if (!querying || prompt.trim() === "") {64querying && setQuerying(false);65setError("");66return;67}6869try {70setPassed(undefined);71const llmStream = webapp_client.openai_client.queryStream({72input: prompt,73project_id: null,74tag: "admin-llm-test",75model,76system,77history,78maxTokens: 20,79});8081let reply = "";82llmStream.on("token", (token) => {83if (token) {84reply += token;85setOutput(reply);86}87});8889llmStream.on("error", (err) => {90setPassed(false);91setError(err?.toString());92setQuerying(false);93});94} catch (err) {95setError(err?.toString());96} finally {97setQuerying(false);98}99}, [querying]);100101function renderPassed() {102if (typeof passed === "boolean") {103return <Value val={passed} />;104} else {105return <Icon unicode={0x2753} />;106}107}108109if (querying) {110return <Loading />;111}112113return (114<>115<Space direction="horizontal" align="start">116{renderPassed()} <Markdown value={output} />117</Space>118{error ? <Alert banner type="error" message={error} /> : undefined}119</>120);121}122123124