CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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