feat: model editing

This commit is contained in:
Junyan Qin
2025-05-10 14:25:44 +08:00
parent 138ddf122a
commit 90b479b9d2
3 changed files with 50 additions and 33 deletions
@@ -19,7 +19,7 @@ class LLMModelsRouterGroup(group.RouterGroup):
return self.success(data={'uuid': model_uuid}) return self.success(data={'uuid': model_uuid})
@self.route('/<model_uuid>', methods=['GET', 'DELETE']) @self.route('/<model_uuid>', methods=['GET', 'PUT', 'DELETE'])
async def _(model_uuid: str) -> str: async def _(model_uuid: str) -> str:
if quart.request.method == 'GET': if quart.request.method == 'GET':
model = await self.ap.model_service.get_llm_model(model_uuid) model = await self.ap.model_service.get_llm_model(model_uuid)
@@ -28,12 +28,12 @@ class LLMModelsRouterGroup(group.RouterGroup):
return self.http_status(404, -1, 'model not found') return self.http_status(404, -1, 'model not found')
return self.success(data={'model': model}) return self.success(data={'model': model})
# elif quart.request.method == 'PUT': elif quart.request.method == 'PUT':
# json_data = await quart.request.json json_data = await quart.request.json
# await self.ap.model_service.update_llm_model(model_uuid, json_data) await self.ap.model_service.update_llm_model(model_uuid, json_data)
# return self.success() return self.success()
elif quart.request.method == 'DELETE': elif quart.request.method == 'DELETE':
await self.ap.model_service.delete_llm_model(model_uuid) await self.ap.model_service.delete_llm_model(model_uuid)
@@ -83,7 +83,7 @@ export default function LLMForm({
}: { }: {
editMode: boolean; editMode: boolean;
initLLMId?: string; initLLMId?: string;
onFormSubmit: (value: z.infer<typeof formSchema>) => void; onFormSubmit: () => void;
onFormCancel: () => void; onFormCancel: () => void;
onLLMDeleted: () => void; onLLMDeleted: () => void;
}) { }) {
@@ -222,20 +222,7 @@ export default function LLMForm({
} }
function handleFormSubmit(value: z.infer<typeof formSchema>) { function handleFormSubmit(value: z.infer<typeof formSchema>) {
if (editMode) { const extraArgsObj: Record<string, string | number | boolean> = {};
// 暂不支持更改模型
// onSaveEdit(value)
} else {
onCreateLLM(value);
}
form.reset();
}
function onCreateLLM(value: z.infer<typeof formSchema>) {
console.log('create llm', value);
// 转换extra_args为对象格式
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const extraArgsObj: Record<string, any> = {};
value.extra_args?.forEach((arg) => { value.extra_args?.forEach((arg) => {
if (arg.type === 'number') { if (arg.type === 'number') {
extraArgsObj[arg.key] = Number(arg.value); extraArgsObj[arg.key] = Number(arg.value);
@@ -246,8 +233,8 @@ export default function LLMForm({
} }
}); });
const requestParam: LLMModel = { const llmModel: LLMModel = {
uuid: UUID.generate(), uuid: editMode ? initLLMId || '' : UUID.generate(),
name: value.name, name: value.name,
description: '', description: '',
requester: value.model_provider, requester: value.model_provider,
@@ -259,15 +246,36 @@ export default function LLMForm({
api_keys: [value.api_key], api_keys: [value.api_key],
abilities: value.abilities, abilities: value.abilities,
}; };
httpClient
.createProviderLLMModel(requestParam) if (editMode) {
.then(() => { onSaveEdit(llmModel).then(() => {
onFormSubmit(value); form.reset();
toast.success('创建成功');
})
.catch((err) => {
toast.error('创建失败:' + err.message);
}); });
} else {
onCreateLLM(llmModel).then(() => {
form.reset();
});
}
}
async function onCreateLLM(llmModel: LLMModel) {
try {
await httpClient.createProviderLLMModel(llmModel);
onFormSubmit();
toast.success('创建成功');
} catch (err) {
toast.error('创建失败:' + (err as Error).message);
}
}
async function onSaveEdit(llmModel: LLMModel) {
try {
await httpClient.updateProviderLLMModel(initLLMId || '', llmModel);
onFormSubmit();
toast.success('保存成功');
} catch (err) {
toast.error('保存失败:' + (err as Error).message);
}
} }
function deleteModel() { function deleteModel() {
@@ -524,7 +532,6 @@ export default function LLMForm({
</FormItem> </FormItem>
</div> </div>
<DialogFooter> <DialogFooter>
{!editMode && <Button type="submit"></Button>}
{editMode && ( {editMode && (
<Button <Button
type="button" type="button"
@@ -534,6 +541,9 @@ export default function LLMForm({
</Button> </Button>
)} )}
<Button type="submit">{editMode ? '保存' : '提交'}</Button>
<Button <Button
type="button" type="button"
variant="outline" variant="outline"
+9 -2
View File
@@ -256,6 +256,13 @@ class HttpClient {
return this.delete(`/api/v1/provider/models/llm/${uuid}`); return this.delete(`/api/v1/provider/models/llm/${uuid}`);
} }
public updateProviderLLMModel(
uuid: string,
model: LLMModel,
): Promise<object> {
return this.put(`/api/v1/provider/models/llm/${uuid}`, model);
}
// ============ Pipeline API ============ // ============ Pipeline API ============
public getGeneralPipelineMetadata(): Promise<GetPipelineMetadataResponseData> { public getGeneralPipelineMetadata(): Promise<GetPipelineMetadataResponseData> {
// as designed, this method will be deprecated, and only for developer to check the prefered config schema // as designed, this method will be deprecated, and only for developer to check the prefered config schema
@@ -431,8 +438,8 @@ class HttpClient {
} }
// export const httpClient = new HttpClient("https://version-4.langbot.dev"); // export const httpClient = new HttpClient("https://version-4.langbot.dev");
// export const httpClient = new HttpClient('http://localhost:5300'); export const httpClient = new HttpClient('http://localhost:5300');
export const httpClient = new HttpClient('/'); // export const httpClient = new HttpClient('/');
// 临时写法,未来两种Client都继承自HttpClient父类,不允许共享方法 // 临时写法,未来两种Client都继承自HttpClient父类,不允许共享方法
export const spaceClient = new HttpClient('https://space.langbot.app'); export const spaceClient = new HttpClient('https://space.langbot.app');