Skip to content

Commit 2aefa2c

Browse files
authored
Merge pull request #7604 from sagemathinc/fix-llm-openai-config
frontend/llm: refactor and fix OpenAI vs Custom OpenAI
2 parents 8453ad7 + b98e2a7 commit 2aefa2c

File tree

11 files changed

+198
-163
lines changed

11 files changed

+198
-163
lines changed

src/packages/frontend/components/language-model-icon.tsx

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import { CSS } from "@cocalc/frontend/app-framework";
1+
import { CSS, useTypedRedux } from "@cocalc/frontend/app-framework";
22
import {
33
LanguageModel,
4-
isCustomOpenAI,
4+
fromCustomOpenAIModel,
5+
fromOllamaModel,
56
isGoogleModel,
67
isLanguageModel,
7-
isOllamaLLM,
88
model2vendor,
99
} from "@cocalc/util/db-schema/llm-utils";
1010
import { unreachable } from "@cocalc/util/misc";
1111
import AIAvatar from "./ai-avatar";
12+
import AnthropicAvatar from "./anthropic-avatar";
1213
import GoogleGeminiLogo from "./google-gemini-avatar";
1314
import GooglePalmLogo from "./google-palm-avatar";
1415
import MistralAvatar from "./mistral-avatar";
1516
import OllamaAvatar from "./ollama-avatar";
1617
import OpenAIAvatar from "./openai-avatar";
17-
import AnthropicAvatar from "./anthropic-avatar";
1818

1919
export function LanguageModelVendorAvatar(
2020
props: Readonly<{
@@ -25,6 +25,9 @@ export function LanguageModelVendorAvatar(
2525
) {
2626
const { model, size = 20 } = props;
2727

28+
const ollama = useTypedRedux("customize", "ollama");
29+
const custom_openai = useTypedRedux("customize", "custom_openai");
30+
2831
const style: CSS = {
2932
marginRight: "5px",
3033
...props.style,
@@ -38,13 +41,35 @@ export function LanguageModelVendorAvatar(
3841
return fallback();
3942
}
4043

44+
function renderImgIcon(icon: string) {
45+
return (
46+
<img
47+
width={size}
48+
height={size}
49+
src={icon}
50+
style={{ display: "inline-block", ...style }}
51+
/>
52+
);
53+
}
54+
4155
if (isLanguageModel(model)) {
4256
const vendorName = model2vendor(model).name;
4357
switch (vendorName) {
4458
case "openai":
45-
case "custom_openai":
4659
return <OpenAIAvatar size={size} style={style} />;
4760

61+
case "custom_openai": {
62+
const icon = custom_openai?.getIn([
63+
fromCustomOpenAIModel(model),
64+
"icon",
65+
]);
66+
if (typeof icon === "string") {
67+
return renderImgIcon(icon);
68+
} else {
69+
return <OpenAIAvatar size={size} style={style} />;
70+
}
71+
}
72+
4873
case "google": {
4974
if (model === "chat-bison-001") {
5075
// Palm2, no longer supported, just for backwards compatibility
@@ -59,8 +84,14 @@ export function LanguageModelVendorAvatar(
5984
case "mistralai":
6085
return <MistralAvatar size={size} style={style} />;
6186

62-
case "ollama":
63-
return <OllamaAvatar size={size} style={style} />;
87+
case "ollama": {
88+
const icon = ollama?.getIn([fromOllamaModel(model), "icon"]);
89+
if (typeof icon === "string") {
90+
return renderImgIcon(icon);
91+
} else {
92+
return <OllamaAvatar size={size} style={style} />;
93+
}
94+
}
6495

6596
case "anthropic":
6697
return <AnthropicAvatar size={size} style={style} />;
@@ -71,13 +102,5 @@ export function LanguageModelVendorAvatar(
71102
}
72103
}
73104

74-
if (isOllamaLLM(model)) {
75-
return <OllamaAvatar size={size} style={style} />;
76-
}
77-
78-
if (isCustomOpenAI(model)) {
79-
return <OpenAIAvatar size={size} style={style} />;
80-
}
81-
82105
return fallback();
83106
}

src/packages/frontend/editors/markdown-input/mentionable-users.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { redux, useMemo, useTypedRedux } from "@cocalc/frontend/app-framework";
1313
import AnthropicAvatar from "@cocalc/frontend/components/anthropic-avatar";
1414
import GoogleGeminiLogo from "@cocalc/frontend/components/google-gemini-avatar";
1515
import MistralAvatar from "@cocalc/frontend/components/mistral-avatar";
16-
import OllamaAvatar from "@cocalc/frontend/components/ollama-avatar";
1716
import OpenAIAvatar from "@cocalc/frontend/components/openai-avatar";
1817
import { LLMModelPrice } from "@cocalc/frontend/frame-editors/llm/llm-selector";
1918
import { useProjectContext } from "@cocalc/frontend/project/context";
@@ -37,6 +36,7 @@ import {
3736
} from "@cocalc/util/db-schema/llm-utils";
3837
import { cmp, timestamp_cmp, trunc_middle } from "@cocalc/util/misc";
3938
import { CustomLLMPublic } from "@cocalc/util/types/llm";
39+
import { LanguageModelVendorAvatar } from "../../components/language-model-icon";
4040
import { Item as CompleteItem } from "./complete";
4141

4242
// we make the show_llm_main_menu field required, to avoid forgetting to set it ;-)
@@ -259,8 +259,8 @@ function mentionableUsers({
259259
value,
260260
label: (
261261
<span>
262-
<OllamaAvatar size={size} /> {conf.display}{" "}
263-
<LLMModelPrice model={m} floatRight />
262+
<LanguageModelVendorAvatar model={value} size={size} />{" "}
263+
{conf.display} <LLMModelPrice model={m} floatRight />
264264
</span>
265265
),
266266
search: search_term,
@@ -283,8 +283,8 @@ function mentionableUsers({
283283
value,
284284
label: (
285285
<span>
286-
<OpenAIAvatar size={size} /> {conf.display}{" "}
287-
<LLMModelPrice model={m} floatRight />
286+
<LanguageModelVendorAvatar model={value} size={size} />{" "}
287+
{conf.display} <LLMModelPrice model={m} floatRight />
288288
</span>
289289
),
290290
search: search_term,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { redux } from "@cocalc/frontend/app-framework";
2+
import { Text } from "@cocalc/frontend/components";
3+
4+
export function getCustomLLMGroup() {
5+
const customize = redux.getStore("customize");
6+
const site_name = customize.get("site_name");
7+
const organization_name = customize.get("organization_name") ?? "";
8+
return {
9+
title: `Managed by ${organization_name || site_name}`,
10+
label: (
11+
<>
12+
<Text strong>{site_name} language models</Text>
13+
</>
14+
),
15+
};
16+
}

src/packages/frontend/frame-editors/llm/llm-query-dropdown.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { LanguageModelVendorAvatar } from "@cocalc/frontend/components/language-
77
import { modelToName } from "@cocalc/frontend/frame-editors/llm/llm-selector";
88
import { useAvailableLLMs } from "@cocalc/frontend/frame-editors/llm/use-llm-menu-options";
99
import { useProjectContext } from "@cocalc/frontend/project/context";
10-
import { LLM_PROVIDER } from "@cocalc/util/db-schema/llm-utils";
1110
import { LLMTools } from "@cocalc/jupyter/types";
11+
import { LLM_PROVIDER } from "@cocalc/util/db-schema/llm-utils";
1212

1313
interface Props {
1414
llmTools?: Pick<LLMTools, "model" | "setModel">;
@@ -26,7 +26,7 @@ export function LLMQueryDropdownButton({
2626
disabled = false,
2727
}: Props) {
2828
const { project_id } = useProjectContext();
29-
const models = useAvailableLLMs(project_id);
29+
const modelsByService = useAvailableLLMs(project_id);
3030

3131
function renderOkText() {
3232
if (llmTools == null) return <></>;
@@ -40,14 +40,17 @@ export function LLMQueryDropdownButton({
4040
function getItems(): MenuProps["items"] {
4141
const ret: MenuProps["items"] = [];
4242
let first = true;
43-
for (const [service, entry] of Object.entries(models)) {
43+
for (const [service, entry] of Object.entries(modelsByService)) {
4444
const { models } = entry;
4545
if (models.length === 0) continue;
4646

4747
if (!first) ret.push({ type: "divider" });
4848
first = false;
4949

50-
const { name, short } = LLM_PROVIDER[service];
50+
const { name, short } =
51+
service === "custom"
52+
? { name: entry.name, short: entry.desc }
53+
: LLM_PROVIDER[service];
5154
ret.push({
5255
type: "group",
5356
label: (
@@ -57,6 +60,7 @@ export function LLMQueryDropdownButton({
5760
</>
5861
),
5962
});
63+
6064
for (const model of models) {
6165
const { name, title, desc, price } = model;
6266
ret.push({

0 commit comments

Comments
 (0)