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/chat/llm-msg-summarize.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2024 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Button, Collapse, Switch } from "antd";67import { useLanguageModelSetting } from "@cocalc/frontend/account/useLanguageModelSetting";8import { useAsyncEffect, useState } from "@cocalc/frontend/app-framework";9import { Icon, Paragraph, RawPrompt } from "@cocalc/frontend/components";10import AIAvatar from "@cocalc/frontend/components/ai-avatar";11import PopconfirmKeyboard from "@cocalc/frontend/components/popconfirm-keyboard";12import LLMSelector, {13modelToName,14} from "@cocalc/frontend/frame-editors/llm/llm-selector";15import { LLMCostEstimation } from "@cocalc/frontend/misc/llm-cost-estimation";16import { useProjectContext } from "@cocalc/frontend/project/context";17import { COLORS } from "@cocalc/util/theme";18import { ChatActions } from "./actions";19import { ChatMessageTyped } from "./types";2021export function SummarizeThread({22message,23actions,24}: {25message: ChatMessageTyped;26actions?: ChatActions;27}) {28const reply_to = message.get("reply_to");29const { project_id } = useProjectContext();30const [model, setModel] = useLanguageModelSetting(project_id);31const [visible, setVisible] = useState(false);32const [tokens, setTokens] = useState(0);33const [truncated, setTruncated] = useState(false);34const [short, setShort] = useState(true);35const [prompt, setPrompt] = useState<string>("");3637useAsyncEffect(async () => {38// we do no do all the processing if the popconfirm is not visible39if (!visible) return;4041const info = await actions?.summarizeThread({42model,43reply_to,44returnInfo: true,45short,46});4748if (!info) return;49const { tokens, truncated, prompt } = info;50setTokens(tokens);51setTruncated(truncated);52setPrompt(prompt);53}, [visible, model, message, short]);5455return (56<PopconfirmKeyboard57onVisibilityChange={setVisible}58icon={<AIAvatar size={16} />}59title={<>Summarize this thread</>}60description={() => (61<div style={{ maxWidth: "500px" }}>62<Paragraph>63<LLMSelector model={model} setModel={setModel} />64</Paragraph>65<Paragraph>66The conversation in this thread will be sent to the language model{" "}67{modelToName(model)}. It will then start a new thread and reply with68a {short ? "short" : "detailed"} summary of the conversation.69</Paragraph>70<Paragraph>71Summary length:{" "}72<Switch73checked={!short}74onChange={(v) => setShort(!v)}75unCheckedChildren={"short"}76checkedChildren={"detailed"}77/>78</Paragraph>79{truncated ? (80<Paragraph type="warning">81The conversion will be truncated. Consider selecting another82language model with a larger context window.83</Paragraph>84) : null}85<Collapse86items={[87{88key: "1",89label: (90<>Click to see what will be sent to {modelToName(model)}.</>91),92children: (93<RawPrompt94input={prompt}95style={{ border: "none", padding: "0", margin: "0" }}96/>97),98},99]}100/>101<LLMCostEstimation102model={model}103tokens={tokens}104paragraph={true}105type="secondary"106maxOutputTokens={short ? 200 : undefined}107/>108</div>109)}110onConfirm={() => actions?.summarizeThread({ model, reply_to, short })}111okText="Summarize"112>113<Button type="text" style={{ color: COLORS.GRAY_M }}>114<Icon name="vertical-align-middle" /> Summarize…115</Button>116</PopconfirmKeyboard>117);118}119120121