Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/account/chatbot.ts
5806 views
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 { getUserDefinedLLMByModel } from "@cocalc/frontend/frame-editors/llm/use-userdefined-llm";
12
import {
13
LANGUAGE_MODELS,
14
LANGUAGE_MODEL_PREFIXES,
15
LLM_USERNAMES,
16
fromAnthropicService,
17
fromCustomOpenAIModel,
18
fromMistralService,
19
fromOllamaModel,
20
fromXaiService,
21
isAnthropicService,
22
isCustomOpenAI,
23
isMistralService,
24
isOllamaLLM,
25
isUserDefinedModel,
26
isXaiService,
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 (isXaiService(account_id)) {
57
return LLM_USERNAMES[fromXaiService(account_id)] ?? "xAI";
58
}
59
if (isAnthropicService(account_id)) {
60
return LLM_USERNAMES[fromAnthropicService(account_id)] ?? "Anthropic";
61
}
62
if (isOllamaLLM(account_id)) {
63
const ollama = redux.getStore("customize").get("ollama")?.toJS() ?? {};
64
const key = fromOllamaModel(account_id);
65
return ollama[key]?.display ?? "Ollama";
66
}
67
if (isCustomOpenAI(account_id)) {
68
const custom_openai =
69
redux.getStore("customize").get("custom_openai")?.toJS() ?? {};
70
const key = fromCustomOpenAIModel(account_id);
71
return custom_openai[key]?.display ?? "OpenAI (custom)";
72
}
73
if (isUserDefinedModel(account_id)) {
74
const um = getUserDefinedLLMByModel(account_id);
75
return um?.display ?? "ChatBot";
76
}
77
return "ChatBot";
78
}
79
80