Skip to content

Commit

Permalink
🐛 fix: fix model list issue in client mode (lobehub#6240)
Browse files Browse the repository at this point in the history
* fix model list issue in client mode

* fix tests
  • Loading branch information
arvinxx authored Feb 17, 2025
1 parent 1fde7b2 commit d6c6cda
Show file tree
Hide file tree
Showing 11 changed files with 562 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Page = async (props: PagePropsWithId) => {
if (isServerMode) {
const { userId } = await getUserAuth();

const { aiProvider } = getServerGlobalConfig();
const { aiProvider } = await getServerGlobalConfig();
const aiInfraRepos = new AiInfraRepos(
serverDB,
userId!,
Expand Down
112 changes: 85 additions & 27 deletions src/config/aiModels/azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,84 @@ const azureChatModels: AIChatModelCard[] = [
{
abilities: {
functionCall: true,
reasoning: true,
},
config: {
deploymentName: 'gpt-35-turbo',
deploymentName: 'o3-mini',
},
contextWindowTokens: 16_385,
contextWindowTokens: 200_000,
description:
'GPT 3.5 Turbo,OpenAI提供的高效模型,适用于聊天和文本生成任务,支持并行函数调用。',
displayName: 'GPT 3.5 Turbo',
'o3-mini 是我们最新的小型推理模型,在与 o1-mini 相同的成本和延迟目标下提供高智能。',
displayName: 'OpenAI o3-mini',
id: 'o3-mini',
maxOutput: 100_000,
pricing: {
input: 1.1,
output: 4.4,
},
releasedAt: '2025-01-31',
type: 'chat',
},
{
abilities: {
reasoning: true,
},
config: {
deploymentName: 'o1-mini',
},
contextWindowTokens: 128_000,
description:
'o1-mini是一款针对编程、数学和科学应用场景而设计的快速、经济高效的推理模型。该模型具有128K上下文和2023年10月的知识截止日期。',
displayName: 'OpenAI o1-mini',
enabled: true,
id: 'gpt-3.5-turbo',
maxOutput: 4096,
id: 'o1-mini',
maxOutput: 65_536,
pricing: {
input: 1.1,
output: 4.4,
},
releasedAt: '2024-09-12',
type: 'chat',
},
{
abilities: {
functionCall: true,
reasoning: true,
},
config: {
deploymentName: 'gpt-35-turbo-16k',
deploymentName: 'o1',
},
contextWindowTokens: 16_384,
description: 'GPT 3.5 Turbo 16k,高容量文本生成模型,适合复杂任务。',
displayName: 'GPT 3.5 Turbo',
id: 'gpt-3.5-turbo-16k',
maxOutput: 4096,
contextWindowTokens: 200_000,
description:
'o1是OpenAI新的推理模型,支持图文输入并输出文本,适用于需要广泛通用知识的复杂任务。该模型具有200K上下文和2023年10月的知识截止日期。',
displayName: 'OpenAI o1',
enabled: true,
id: 'o1',
maxOutput: 100_000,
pricing: {
input: 15,
output: 60,
},
releasedAt: '2024-12-17',
type: 'chat',
},
{
abilities: {
reasoning: true,
},
config: {
deploymentName: 'o1-preview',
},
contextWindowTokens: 128_000,
description:
'o1是OpenAI新的推理模型,适用于需要广泛通用知识的复杂任务。该模型具有128K上下文和2023年10月的知识截止日期。',
displayName: 'OpenAI o1-preview',
id: 'o1-preview',
maxOutput: 32_768,
pricing: {
input: 15,
output: 60,
},
releasedAt: '2024-09-12',
type: 'chat',
},
{
Expand All @@ -37,29 +90,34 @@ const azureChatModels: AIChatModelCard[] = [
vision: true,
},
config: {
deploymentName: 'gpt-4-turbo',
deploymentName: 'gpt-4o',
},
contextWindowTokens: 128_000,
description: 'GPT 4 Turbo,多模态模型,提供杰出的语言理解和生成能力,同时支持图像输入。',
displayName: 'GPT 4 Turbo',
description:
'ChatGPT-4o 是一款动态模型,实时更新以保持当前最新版本。它结合了强大的语言理解与生成能力,适合于大规模应用场景,包括客户服务、教育和技术支持。',
displayName: 'GPT-4o',
enabled: true,
id: 'gpt-4',
maxOutput: 4096,
id: 'gpt-4o',
pricing: {
input: 2.5,
output: 10,
},
releasedAt: '2024-05-13',
type: 'chat',
},

{
abilities: {
functionCall: true,
vision: true,
},
config: {
deploymentName: 'gpt-4o-mini',
deploymentName: 'gpt-4-turbo',
},
contextWindowTokens: 128_000,
description: 'GPT-4o Mini,小型高效模型,具备与GPT-4o相似的卓越性能。',
displayName: 'GPT 4o Mini',
enabled: true,
id: 'gpt-4o-mini',
description: 'GPT 4 Turbo,多模态模型,提供杰出的语言理解和生成能力,同时支持图像输入。',
displayName: 'GPT 4 Turbo',
id: 'gpt-4',
maxOutput: 4096,
type: 'chat',
},
Expand All @@ -69,13 +127,13 @@ const azureChatModels: AIChatModelCard[] = [
vision: true,
},
config: {
deploymentName: 'gpt-4o',
deploymentName: 'gpt-4o-mini',
},
contextWindowTokens: 128_000,
description: 'GPT-4o 是最新的多模态模型,结合高级文本和图像处理能力。',
displayName: 'GPT 4o',
description: 'GPT-4o Mini,小型高效模型,具备与GPT-4o相似的卓越性能。',
displayName: 'GPT 4o Mini',
enabled: true,
id: 'gpt-4o',
id: 'gpt-4o-mini',
maxOutput: 4096,
type: 'chat',
},
Expand Down
30 changes: 20 additions & 10 deletions src/database/repositories/aiInfra/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class AiInfraRepos {
private userId: string;
private db: LobeChatDatabase;
aiProviderModel: AiProviderModel;
private providerConfigs: Record<string, ProviderConfig>;
private readonly providerConfigs: Record<string, ProviderConfig>;
aiModelModel: AiModelModel;

constructor(
Expand Down Expand Up @@ -66,6 +66,9 @@ export class AiInfraRepos {
});
};

/**
* used in the chat page. to show the enabled providers
*/
getUserEnabledProviderList = async () => {
const list = await this.getAiProviderList();
return list
Expand All @@ -81,6 +84,9 @@ export class AiInfraRepos {
);
};

/**
* used in the chat page. to show the enabled models
*/
getEnabledModels = async () => {
const providers = await this.getAiProviderList();
const enabledProviders = providers.filter((item) => item.enabled);
Expand Down Expand Up @@ -129,15 +135,6 @@ export class AiInfraRepos {
) as EnabledAiModel[];
};

getAiProviderModelList = async (providerId: string) => {
const aiModels = await this.aiModelModel.getModelListByProviderId(providerId);

const defaultModels: AiProviderModelListItem[] =
(await this.fetchBuiltinModels(providerId)) || [];

return mergeArrayById(defaultModels, aiModels) as AiProviderModelListItem[];
};

getAiProviderRuntimeState = async (
decryptor?: DecryptUserKeyVaults,
): Promise<AiProviderRuntimeState> => {
Expand All @@ -156,6 +153,18 @@ export class AiInfraRepos {
return { enabledAiModels, enabledAiProviders, runtimeConfig };
};

getAiProviderModelList = async (providerId: string) => {
const aiModels = await this.aiModelModel.getModelListByProviderId(providerId);

const defaultModels: AiProviderModelListItem[] =
(await this.fetchBuiltinModels(providerId)) || [];

return mergeArrayById(defaultModels, aiModels) as AiProviderModelListItem[];
};

/**
* use in the `/settings/provider/[id]` page
*/
getAiProviderDetail = async (id: string, decryptor?: DecryptUserKeyVaults) => {
const config = await this.aiProviderModel.getAiProviderById(id, decryptor);

Expand All @@ -171,6 +180,7 @@ export class AiInfraRepos {
try {
const { default: providerModels } = await import(`@/config/aiModels/${providerId}`);

// use the serverModelLists as the defined server model list
const presetList = this.providerConfigs[providerId]?.serverModelLists || providerModels;
return (presetList as AIChatModelCard[]).map<AiProviderModelListItem>((m) => ({
...m,
Expand Down
10 changes: 5 additions & 5 deletions src/libs/agent-runtime/openai/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ exports[`LobeOpenAI > models > should get models 1`] = `
},
{
"contextWindowTokens": 16385,
"displayName": "GPT 3.5 Turbo",
"enabled": true,
"displayName": "GPT-3.5 Turbo",
"enabled": false,
"functionCall": true,
"id": "gpt-3.5-turbo",
"reasoning": false,
Expand All @@ -39,8 +39,8 @@ exports[`LobeOpenAI > models > should get models 1`] = `
"vision": false,
},
{
"contextWindowTokens": 16384,
"displayName": "GPT 3.5 Turbo",
"contextWindowTokens": undefined,
"displayName": undefined,
"enabled": false,
"functionCall": true,
"id": "gpt-3.5-turbo-16k",
Expand Down Expand Up @@ -221,7 +221,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `
{
"contextWindowTokens": 128000,
"displayName": "GPT 4 Turbo",
"enabled": true,
"enabled": false,
"functionCall": true,
"id": "gpt-4",
"reasoning": false,
Expand Down
5 changes: 4 additions & 1 deletion src/server/globalConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { genServerAiProvidersConfig } from './genServerAiProviderConfig';
import { parseAgentConfig } from './parseDefaultAgent';
import { parseFilesConfig } from './parseFilesConfig';

export const getServerGlobalConfig = () => {
export const getServerGlobalConfig = async () => {
const { ACCESS_CODES, DEFAULT_AGENT_CONFIG } = getAppConfig();

const config: GlobalServerConfig = {
Expand All @@ -38,6 +38,9 @@ export const getServerGlobalConfig = () => {
ollama: {
fetchOnClient: !process.env.OLLAMA_PROXY_URL,
},
volcengine: {
withDeploymentName: true,
},
}),
defaultAgent: {
config: parseAgentConfig(DEFAULT_AGENT_CONFIG),
Expand Down
2 changes: 1 addition & 1 deletion src/server/routers/lambda/aiModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const aiModelProcedure = authedProcedure.use(async (opts) => {
const { ctx } = opts;

const gateKeeper = await KeyVaultsGateKeeper.initWithEnvKey();
const { aiProvider } = getServerGlobalConfig();
const { aiProvider } = await getServerGlobalConfig();

return opts.next({
ctx: {
Expand Down
2 changes: 1 addition & 1 deletion src/server/routers/lambda/aiProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ProviderConfig } from '@/types/user/settings';
const aiProviderProcedure = authedProcedure.use(async (opts) => {
const { ctx } = opts;

const { aiProvider } = getServerGlobalConfig();
const { aiProvider } = await getServerGlobalConfig();

const gateKeeper = await KeyVaultsGateKeeper.initWithEnvKey();
return opts.next({
Expand Down
8 changes: 7 additions & 1 deletion src/services/aiModel/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ export class ClientService extends BaseClientService implements IAiModelService
return new AiModelModel(clientDB as any, this.userId);
}
private get aiInfraRepos(): AiInfraRepos {
return new AiInfraRepos(clientDB as any, this.userId, {});
let config = {};

if (typeof window !== 'undefined') {
config = window.global_serverConfigStore.getState().serverConfig.aiProvider || {};
}

return new AiInfraRepos(clientDB as any, this.userId, config);
}

createAiModel: IAiModelService['createAiModel'] = async (params) => {
Expand Down
Loading

0 comments on commit d6c6cda

Please sign in to comment.