fix: add custom config in get-models, test-model
This commit is contained in:
@@ -54,7 +54,7 @@ const ModelItem: React.FC<ModelItemProps> = ({
|
|||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const form = Form.useFormInstance<FormData>();
|
const form = Form.useFormInstance<FormData>();
|
||||||
const { runTestModel, loading: testLoading } = useTestProviderModel();
|
const { runTestModel, loading: testLoading } = useTestProviderModel();
|
||||||
const { id, action, currentData } = useFormContext();
|
const { id, action, currentData, getCustomConfig } = useFormContext();
|
||||||
const [openTip, setOpenTip] = React.useState(false);
|
const [openTip, setOpenTip] = React.useState(false);
|
||||||
|
|
||||||
const generateCurrentAPIKey = (currentAPIKey: string) => {
|
const generateCurrentAPIKey = (currentAPIKey: string) => {
|
||||||
@@ -76,6 +76,7 @@ const ModelItem: React.FC<ModelItemProps> = ({
|
|||||||
|
|
||||||
const handleTestModel = async () => {
|
const handleTestModel = async () => {
|
||||||
const proxyConfigEnabled = form.getFieldValue('proxy_enabled');
|
const proxyConfigEnabled = form.getFieldValue('proxy_enabled');
|
||||||
|
const customConfig = getCustomConfig?.();
|
||||||
const res = await runTestModel({
|
const res = await runTestModel({
|
||||||
id: generateID(),
|
id: generateID(),
|
||||||
data: {
|
data: {
|
||||||
@@ -87,7 +88,12 @@ const ModelItem: React.FC<ModelItemProps> = ({
|
|||||||
? form.getFieldValue('proxy_url') || null
|
? form.getFieldValue('proxy_url') || null
|
||||||
: null,
|
: null,
|
||||||
config: {
|
config: {
|
||||||
type: form.getFieldValue(['config', 'type']) || ''
|
type: form.getFieldValue(['config', 'type']) || '',
|
||||||
|
openaiCustomUrl:
|
||||||
|
form.getFieldValue(['config', 'openaiCustomUrl']) ||
|
||||||
|
customConfig?.openaiCustomUrl ||
|
||||||
|
null,
|
||||||
|
...customConfig
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import MetadataList from '@/components/metadata-list';
|
|||||||
import { PageAction } from '@/config';
|
import { PageAction } from '@/config';
|
||||||
import { useIntl } from '@umijs/max';
|
import { useIntl } from '@umijs/max';
|
||||||
import { Form } from 'antd';
|
import { Form } from 'antd';
|
||||||
|
import _ from 'lodash';
|
||||||
import { useRef } from 'react';
|
import { useRef } from 'react';
|
||||||
import { useFormContext } from '../config/form-context';
|
import { useFormContext } from '../config/form-context';
|
||||||
import { FormData, ProviderModel } from '../config/types';
|
import { FormData, ProviderModel } from '../config/types';
|
||||||
@@ -17,7 +18,8 @@ const SupportedModels = () => {
|
|||||||
const prevConfigRef = useRef<{
|
const prevConfigRef = useRef<{
|
||||||
type: string;
|
type: string;
|
||||||
api_key: string;
|
api_key: string;
|
||||||
}>({ type: '', api_key: '' });
|
openaiCustomUrl: string;
|
||||||
|
}>({ type: '', api_key: '', openaiCustomUrl: '' });
|
||||||
const { id, action, currentData, getCustomConfig } = useFormContext();
|
const { id, action, currentData, getCustomConfig } = useFormContext();
|
||||||
|
|
||||||
const generateCurrentAPIKey = (currentAPIKey: string) => {
|
const generateCurrentAPIKey = (currentAPIKey: string) => {
|
||||||
@@ -37,10 +39,13 @@ const SupportedModels = () => {
|
|||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
const checkConfigChange = (current: { type: string; api_key: string }) => {
|
const checkConfigChange = (current: {
|
||||||
|
type: string;
|
||||||
|
api_key: string;
|
||||||
|
openaiCustomUrl: string;
|
||||||
|
}) => {
|
||||||
return (
|
return (
|
||||||
(current.type !== prevConfigRef.current.type ||
|
!_.isEqual(current, prevConfigRef.current) &&
|
||||||
current.api_key !== prevConfigRef.current.api_key) &&
|
|
||||||
current.api_key &&
|
current.api_key &&
|
||||||
current.type
|
current.type
|
||||||
);
|
);
|
||||||
@@ -53,16 +58,21 @@ const SupportedModels = () => {
|
|||||||
const proxyConfigEnabled = form.getFieldValue('proxy_enabled');
|
const proxyConfigEnabled = form.getFieldValue('proxy_enabled');
|
||||||
const currentAPIKey = form.getFieldValue('api_key') || '';
|
const currentAPIKey = form.getFieldValue('api_key') || '';
|
||||||
const configType = form.getFieldValue(['config', 'type']);
|
const configType = form.getFieldValue(['config', 'type']);
|
||||||
|
const openaiCustomUrl = form.getFieldValue(['config', 'openaiCustomUrl']);
|
||||||
|
const customConfig = getCustomConfig?.();
|
||||||
|
|
||||||
|
const currentConfig = {
|
||||||
|
type: configType,
|
||||||
|
api_key: currentAPIKey,
|
||||||
|
openaiCustomUrl: customConfig?.openaiCustomUrl || openaiCustomUrl || ''
|
||||||
|
};
|
||||||
|
|
||||||
// Avoid repeated requests with the same API key
|
// Avoid repeated requests with the same API key
|
||||||
if (
|
if (open && checkConfigChange(currentConfig)) {
|
||||||
open &&
|
|
||||||
checkConfigChange({ type: configType, api_key: currentAPIKey })
|
|
||||||
) {
|
|
||||||
prevConfigRef.current = {
|
prevConfigRef.current = {
|
||||||
type: configType,
|
...currentConfig
|
||||||
api_key: currentAPIKey
|
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchProviderModels({
|
fetchProviderModels({
|
||||||
id: generateID(),
|
id: generateID(),
|
||||||
data: {
|
data: {
|
||||||
@@ -73,9 +83,8 @@ const SupportedModels = () => {
|
|||||||
config: {
|
config: {
|
||||||
type: form.getFieldValue(['config', 'type']) || '',
|
type: form.getFieldValue(['config', 'type']) || '',
|
||||||
openaiCustomUrl:
|
openaiCustomUrl:
|
||||||
form.getFieldValue(['config', 'openaiCustomUrl']) ||
|
openaiCustomUrl || customConfig?.openaiCustomUrl || null,
|
||||||
getCustomConfig?.()?.openaiCustomUrl ||
|
...customConfig
|
||||||
null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -83,7 +92,8 @@ const SupportedModels = () => {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
prevConfigRef.current = {
|
prevConfigRef.current = {
|
||||||
type: '',
|
type: '',
|
||||||
api_key: ''
|
api_key: '',
|
||||||
|
openaiCustomUrl: ''
|
||||||
};
|
};
|
||||||
// If validation fails, reset the provider model list to avoid confusion
|
// If validation fails, reset the provider model list to avoid confusion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ export const useTestProviderModel = () => {
|
|||||||
id: number;
|
id: number;
|
||||||
data: {
|
data: {
|
||||||
api_token: string;
|
api_token: string;
|
||||||
config: { type: string };
|
config: { type: string; [key: string]: any };
|
||||||
model_name: string;
|
model_name: string;
|
||||||
proxy_url: string;
|
proxy_url: string;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user