Skip to content

Commit

Permalink
feat(ui): allow model providers selected in agent to use the default …
Browse files Browse the repository at this point in the history
…model_name
  • Loading branch information
BroKun committed Sep 15, 2024
1 parent 10d94c8 commit 92e92fa
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 99 deletions.
11 changes: 5 additions & 6 deletions web-apps/ui/src/modules/model/llm-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ export class LLMManager {
@inject(LLMProviderManager) protected providerManager: LLMProviderManager;

get default(): LLMModel | undefined {
const llm = this.providerManager.models.find((item) => item.models.length > 0);
if (!llm) {
const defaultProvider = this.providerManager.models[0];
if (!defaultProvider) {
return undefined;
}
const name = llm.models[0];
const meta = llm.toSingleMeta(name);
if (!meta) {
const llm = defaultProvider.toSingleMeta(defaultProvider.model_name[0]);
if (!llm) {
return undefined;
}
return this.factory(meta);
return this.factory(llm);
}

updateFromProvider = () => {
Expand Down
15 changes: 13 additions & 2 deletions web-apps/ui/src/modules/model/llm-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ export class LLMProvider implements LLMProviderMeta {
this.id = meta.id;
this.nickname = meta.nickname;
this.model_name = meta.model_name;
this.temperature = meta.temperature;
}

toSingleMeta = (name: string): LLMMeta | undefined => {
toSingleMeta = (name?: string): LLMMeta | undefined => {
if (!name) {
if (this.models.length === 0) {
const meta = this.toMeta();
return { ...meta, model_name: [] };
}
return undefined;
}
if (!this.models.includes(name)) {
return undefined;
}
Expand All @@ -58,6 +66,9 @@ export class LLMProvider implements LLMProviderMeta {
};

toSingleMetas = () => {
if (this.models.length === 0) {
return [this.toSingleMeta()];
}
return this.models.map(this.toSingleMeta);
};
}
Expand All @@ -70,7 +81,7 @@ export class LLMModel implements LLMMeta {
nickname: string;

@prop()
model_name: [string];
model_name: [string] | [];

get name(): string {
return this.nickname;
Expand Down
12 changes: 11 additions & 1 deletion web-apps/ui/src/modules/model/llm-provider-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ export class LLMProviderManager {

updateProviders = async () => {
const metas = await this.getProviderssMeta();
this.models = metas.map((item) => this.getOrCreate(item));
this.models = metas
.map((item) => this.getOrCreate(item))
.sort((a, b) => {
if (b.model_name.length === 0) {
return 1;
}
if (a.model_name.length === 0) {
return -1;
}
return 0;
});
};

getOrCreate = (option: LLMProviderMeta): LLMProvider => {
Expand Down
152 changes: 63 additions & 89 deletions web-apps/ui/src/modules/model/model-selector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const ModelSelectorOption = (props: { model: LLMMeta; flat?: boolean }) => {
src={<LLMIcon data={model} />}
/>
{flat && <span className={`${clsPrefix}-option-series`}>{model.nickname}</span>}
<span className={`${clsPrefix}-option-label`}>{model.model_name[0]}</span>
<span className={`${clsPrefix}-option-label`}>
{model.model_name[0] || model.nickname}
</span>
</div>
);
};
Expand Down Expand Up @@ -102,51 +104,66 @@ export const ModelSelector = forwardRef<HTMLDivElement, ModelSelectorProps>(
modelProvider.updateProviders();
}, [modelProvider]);

if (!popoverMode) {
return (
<Select
onSelect={(v) => {
const meta = llms.find((i) => toKey(i) === v);
if (!meta) {
setStateValue(meta);
props.onChange?.(meta);
return;
}
let value = meta;
if (currentModel instanceof LLMModel) {
currentModel.updateMeta(meta);
value = currentModel;
} else {
value = {
...currentModel,
temperature:
currentModel?.temperature === undefined
? meta.temperature
: currentModel.temperature,
id: meta.id,
nickname: meta.nickname,
model_name: meta.model_name,
};
const select = (
<Select
onSelect={(v) => {
const meta = llms.find((i) => toKey(i) === v);
if (!meta) {
setStateValue(meta);
props.onChange?.(meta);
return;
}
let value = meta;
if (currentModel instanceof LLMModel) {
currentModel.updateMeta(meta);
value = currentModel;
} else {
value = {
...currentModel,
temperature:
currentModel?.temperature === undefined
? meta.temperature
: currentModel.temperature,
id: meta.id,
nickname: meta.nickname,
model_name: meta.model_name,
};
}
setStateValue(value);
props.onChange?.(value);
}}
value={toKey(currentModel)}
>
{models.map((model) => {
const metas = model.toSingleMetas().filter((item): item is LLMMeta => !!item);
if (model.models.length > 0) {
return (
<Select.OptGroup key={model.id} label={model.name}>
{metas
.filter((i) => !!i)
.map((item) => (
<Select.Option key={toKey(item)} value={toKey(item)}>
<ModelSelectorOption model={item!} />
</Select.Option>
))}
</Select.OptGroup>
);
} else {
const item = metas[0];
if (!item) {
return null;
}
setStateValue(value);
props.onChange?.(value);
}}
value={toKey(currentModel)}
>
{models.map((model) => (
<Select.OptGroup key={model.id} label={model.name}>
{model
.toSingleMetas()
.filter((i) => !!i)
.map((item) => (
<Select.Option key={toKey(item)} value={toKey(item)}>
<ModelSelectorOption model={item!} />
</Select.Option>
))}
</Select.OptGroup>
))}
</Select>
);
return (
<Select.Option key={item.id} value={toKey(item)}>
<ModelSelectorOption model={item!} />
</Select.Option>
);
}
})}
</Select>
);
if (!popoverMode) {
return select;
}

return (
Expand All @@ -160,50 +177,7 @@ export const ModelSelector = forwardRef<HTMLDivElement, ModelSelectorProps>(
content={
<div className={`${clsPrefix}-popover-content`}>
<Form layout="vertical">
<Form.Item label="模型">
<Select
onSelect={(v) => {
const meta = llms.find((i) => toKey(i) === v);
if (!meta) {
setStateValue(meta);
props.onChange?.(meta);
return;
}
let value = meta;
if (currentModel instanceof LLMModel) {
currentModel.updateMeta(meta);
value = currentModel;
} else {
value = {
...currentModel,
temperature:
currentModel?.temperature === undefined
? meta.temperature
: currentModel.temperature,
id: meta.id,
nickname: meta.nickname,
model_name: meta.model_name,
};
}
setStateValue(value);
props.onChange?.(value);
}}
value={toKey(currentModel)}
>
{models.map((model) => (
<Select.OptGroup key={model.id} label={model.name}>
{model
.toSingleMetas()
.filter((i) => !!i)
.map((item) => (
<Select.Option key={toKey(item)} value={toKey(item)}>
<ModelSelectorOption model={item!} />
</Select.Option>
))}
</Select.OptGroup>
))}
</Select>
</Form.Item>
<Form.Item label="模型">{select}</Form.Item>
{shouldShowConfig && (
<Form.Item label="temperature">
<TemperatureSlider
Expand Down
2 changes: 1 addition & 1 deletion web-apps/ui/src/modules/model/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface LLMMeta {
id: string;
nickname: string;
temperature: number;
model_name: [string];
model_name: [string] | [];
}

export type LLMModelFactory = (option: LLMMeta) => LLMModel;
Expand Down

0 comments on commit 92e92fa

Please sign in to comment.