Path: blob/master/src/packages/frontend/account/chatbot.ts
5806 views
/*1We abuse the account_id field in some cases, especially chat, to also2be a string (not a uuid) to refer to various chatbots. Any code that3displays or detects this *should* go through the functions below.45When new models are added, e.g., Claude soon (!), they will go here.67*/89import { redux } from "@cocalc/frontend/app-framework";10import { getUserDefinedLLMByModel } from "@cocalc/frontend/frame-editors/llm/use-userdefined-llm";11import {12LANGUAGE_MODELS,13LANGUAGE_MODEL_PREFIXES,14LLM_USERNAMES,15fromAnthropicService,16fromCustomOpenAIModel,17fromMistralService,18fromOllamaModel,19fromXaiService,20isAnthropicService,21isCustomOpenAI,22isMistralService,23isOllamaLLM,24isUserDefinedModel,25isXaiService,26} from "@cocalc/util/db-schema/llm-utils";2728// we either check if the prefix is one of the known ones (used in some circumstances)29// or if the account id is exactly one of the language models (more precise)30export function isChatBot(account_id?: string): boolean {31if (typeof account_id !== "string") return false;32return (33LANGUAGE_MODEL_PREFIXES.some((prefix) => account_id?.startsWith(prefix)) ||34LANGUAGE_MODELS.some((model) => account_id === model) ||35isOllamaLLM(account_id) ||36isCustomOpenAI(account_id) ||37isUserDefinedModel(account_id)38);39}4041export function chatBotName(account_id?: string): string {42if (typeof account_id !== "string") return "ChatBot";43if (account_id.startsWith("chatgpt")) {44return LLM_USERNAMES[account_id] ?? "ChatGPT";45}46if (account_id.startsWith("openai-")) {47return LLM_USERNAMES[account_id.slice("openai-".length)] ?? "ChatGPT";48}49if (account_id.startsWith("google-")) {50return LLM_USERNAMES[account_id.slice("google-".length)] ?? "Gemini";51}52if (isMistralService(account_id)) {53return LLM_USERNAMES[fromMistralService(account_id)] ?? "Mistral";54}55if (isXaiService(account_id)) {56return LLM_USERNAMES[fromXaiService(account_id)] ?? "xAI";57}58if (isAnthropicService(account_id)) {59return LLM_USERNAMES[fromAnthropicService(account_id)] ?? "Anthropic";60}61if (isOllamaLLM(account_id)) {62const ollama = redux.getStore("customize").get("ollama")?.toJS() ?? {};63const key = fromOllamaModel(account_id);64return ollama[key]?.display ?? "Ollama";65}66if (isCustomOpenAI(account_id)) {67const custom_openai =68redux.getStore("customize").get("custom_openai")?.toJS() ?? {};69const key = fromCustomOpenAIModel(account_id);70return custom_openai[key]?.display ?? "OpenAI (custom)";71}72if (isUserDefinedModel(account_id)) {73const um = getUserDefinedLLMByModel(account_id);74return um?.display ?? "ChatBot";75}76return "ChatBot";77}787980