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/components/language-model-icon.tsx
Views: 687
1
import { CSS, useTypedRedux } from "@cocalc/frontend/app-framework";
2
import {
3
LLMServiceName,
4
LanguageModel,
5
SERVICES,
6
fromCustomOpenAIModel,
7
fromOllamaModel,
8
fromUserDefinedLLMModel,
9
isGoogleModel,
10
isLanguageModel,
11
isUserDefinedModel,
12
model2vendor,
13
} from "@cocalc/util/db-schema/llm-utils";
14
import { unreachable } from "@cocalc/util/misc";
15
import AIAvatar from "./ai-avatar";
16
import AnthropicAvatar from "./anthropic-avatar";
17
import GoogleGeminiLogo from "./google-gemini-avatar";
18
import GooglePalmLogo from "./google-palm-avatar";
19
import MistralAvatar from "./mistral-avatar";
20
import OllamaAvatar from "./ollama-avatar";
21
import OpenAIAvatar from "./openai-avatar";
22
23
export function LanguageModelVendorAvatar(
24
props: Readonly<{
25
model?: LanguageModel;
26
size?: number;
27
style?: CSS;
28
}>,
29
) {
30
const { model, size = 20 } = props;
31
32
const ollama = useTypedRedux("customize", "ollama");
33
const custom_openai = useTypedRedux("customize", "custom_openai");
34
35
const style: CSS = {
36
marginRight: "5px",
37
...props.style,
38
} as const;
39
40
function fallback() {
41
return <AIAvatar size={size} style={style} />;
42
}
43
44
if (model == null) {
45
return fallback();
46
}
47
48
function renderImgIcon(icon: string) {
49
return (
50
<img
51
width={size}
52
height={size}
53
src={icon}
54
style={{ display: "inline-block", ...style }}
55
/>
56
);
57
}
58
59
function renderModel(model: string, vendor?: LLMServiceName) {
60
const useIcon = vendor == null;
61
const vendorName = vendor ?? model2vendor(model).name;
62
switch (vendorName) {
63
case "openai":
64
return <OpenAIAvatar size={size} style={style} />;
65
66
case "custom_openai": {
67
const icon = custom_openai?.getIn([
68
fromCustomOpenAIModel(model),
69
"icon",
70
]);
71
if (useIcon && typeof icon === "string") {
72
return renderImgIcon(icon);
73
} else {
74
return <OpenAIAvatar size={size} style={style} />;
75
}
76
}
77
78
case "google": {
79
if (model === "chat-bison-001") {
80
// Palm2, no longer supported, just for backwards compatibility
81
return <GooglePalmLogo size={size} style={style} />;
82
} else if (!useIcon || isGoogleModel(model)) {
83
return <GoogleGeminiLogo size={size} style={style} />;
84
} else {
85
return fallback();
86
}
87
}
88
89
case "mistralai":
90
return <MistralAvatar size={size} style={style} />;
91
92
case "ollama": {
93
const icon = ollama?.getIn([fromOllamaModel(model), "icon"]);
94
if (useIcon && typeof icon === "string") {
95
return renderImgIcon(icon);
96
} else {
97
return <OllamaAvatar size={size} style={style} />;
98
}
99
}
100
101
case "anthropic":
102
return <AnthropicAvatar size={size} style={style} />;
103
104
case "user":
105
// should never happen, because it is unpacked below
106
return fallback();
107
108
default:
109
unreachable(vendorName);
110
return fallback();
111
}
112
}
113
114
if (isUserDefinedModel(model)) {
115
const udm = fromUserDefinedLLMModel(model);
116
if (!udm) {
117
return fallback();
118
} else {
119
// TODO: support a customizable icon for user defined LLMs
120
for (const vendor of SERVICES) {
121
if (udm.startsWith(`${vendor}-`)) {
122
return renderModel(udm, vendor);
123
}
124
}
125
}
126
} else if (isLanguageModel(model)) {
127
return renderModel(model);
128
}
129
130
return fallback();
131
}
132
133