mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-10-18 21:38:14 +00:00
feat(config-route): use new config manager & model registry
This commit is contained in:
@@ -1,134 +1,33 @@
|
|||||||
import {
|
import configManager from '@/lib/config';
|
||||||
getAnthropicApiKey,
|
import ModelRegistry from '@/lib/models/registry';
|
||||||
getCustomOpenaiApiKey,
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
getCustomOpenaiApiUrl,
|
|
||||||
getCustomOpenaiModelName,
|
|
||||||
getGeminiApiKey,
|
|
||||||
getGroqApiKey,
|
|
||||||
getOllamaApiEndpoint,
|
|
||||||
getOpenaiApiKey,
|
|
||||||
getDeepseekApiKey,
|
|
||||||
getAimlApiKey,
|
|
||||||
getLMStudioApiEndpoint,
|
|
||||||
getLemonadeApiEndpoint,
|
|
||||||
getLemonadeApiKey,
|
|
||||||
updateConfig,
|
|
||||||
getOllamaApiKey,
|
|
||||||
} from '@/lib/config';
|
|
||||||
import {
|
|
||||||
getAvailableChatModelProviders,
|
|
||||||
getAvailableEmbeddingModelProviders,
|
|
||||||
} from '@/lib/providers';
|
|
||||||
|
|
||||||
export const GET = async (req: Request) => {
|
export const GET = async (req: NextRequest) => {
|
||||||
try {
|
try {
|
||||||
const config: Record<string, any> = {};
|
const values = configManager.currentConfig;
|
||||||
|
const fields = configManager.getUIConfigSections();
|
||||||
|
|
||||||
const [chatModelProviders, embeddingModelProviders] = await Promise.all([
|
const modelRegistry = new ModelRegistry();
|
||||||
getAvailableChatModelProviders(),
|
const modelProviders = await modelRegistry.getActiveProviders();
|
||||||
getAvailableEmbeddingModelProviders(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
config['chatModelProviders'] = {};
|
values.modelProviders = values.modelProviders.map((mp) => {
|
||||||
config['embeddingModelProviders'] = {};
|
const activeProvider = modelProviders.find((p) => p.id === mp.id)
|
||||||
|
|
||||||
for (const provider in chatModelProviders) {
|
|
||||||
config['chatModelProviders'][provider] = Object.keys(
|
|
||||||
chatModelProviders[provider],
|
|
||||||
).map((model) => {
|
|
||||||
return {
|
return {
|
||||||
name: model,
|
...mp,
|
||||||
displayName: chatModelProviders[provider][model].displayName,
|
chatModels: activeProvider?.chatModels ?? mp.chatModels,
|
||||||
};
|
embeddingModels: activeProvider?.embeddingModels ?? mp.embeddingModels
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
for (const provider in embeddingModelProviders) {
|
return NextResponse.json({
|
||||||
config['embeddingModelProviders'][provider] = Object.keys(
|
values,
|
||||||
embeddingModelProviders[provider],
|
fields,
|
||||||
).map((model) => {
|
})
|
||||||
return {
|
|
||||||
name: model,
|
|
||||||
displayName: embeddingModelProviders[provider][model].displayName,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
config['openaiApiKey'] = getOpenaiApiKey();
|
|
||||||
config['ollamaApiUrl'] = getOllamaApiEndpoint();
|
|
||||||
config['ollamaApiKey'] = getOllamaApiKey();
|
|
||||||
config['lmStudioApiUrl'] = getLMStudioApiEndpoint();
|
|
||||||
config['lemonadeApiUrl'] = getLemonadeApiEndpoint();
|
|
||||||
config['lemonadeApiKey'] = getLemonadeApiKey();
|
|
||||||
config['anthropicApiKey'] = getAnthropicApiKey();
|
|
||||||
config['groqApiKey'] = getGroqApiKey();
|
|
||||||
config['geminiApiKey'] = getGeminiApiKey();
|
|
||||||
config['deepseekApiKey'] = getDeepseekApiKey();
|
|
||||||
config['aimlApiKey'] = getAimlApiKey();
|
|
||||||
config['customOpenaiApiUrl'] = getCustomOpenaiApiUrl();
|
|
||||||
config['customOpenaiApiKey'] = getCustomOpenaiApiKey();
|
|
||||||
config['customOpenaiModelName'] = getCustomOpenaiModelName();
|
|
||||||
|
|
||||||
return Response.json({ ...config }, { status: 200 });
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('An error occurred while getting config:', err);
|
console.error('Error in getting config: ', err);
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{ message: 'An error occurred while getting config' },
|
{ message: 'An error has occurred.' },
|
||||||
{ status: 500 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const POST = async (req: Request) => {
|
|
||||||
try {
|
|
||||||
const config = await req.json();
|
|
||||||
|
|
||||||
const updatedConfig = {
|
|
||||||
MODELS: {
|
|
||||||
OPENAI: {
|
|
||||||
API_KEY: config.openaiApiKey,
|
|
||||||
},
|
|
||||||
GROQ: {
|
|
||||||
API_KEY: config.groqApiKey,
|
|
||||||
},
|
|
||||||
ANTHROPIC: {
|
|
||||||
API_KEY: config.anthropicApiKey,
|
|
||||||
},
|
|
||||||
GEMINI: {
|
|
||||||
API_KEY: config.geminiApiKey,
|
|
||||||
},
|
|
||||||
OLLAMA: {
|
|
||||||
API_URL: config.ollamaApiUrl,
|
|
||||||
API_KEY: config.ollamaApiKey,
|
|
||||||
},
|
|
||||||
DEEPSEEK: {
|
|
||||||
API_KEY: config.deepseekApiKey,
|
|
||||||
},
|
|
||||||
AIMLAPI: {
|
|
||||||
API_KEY: config.aimlApiKey,
|
|
||||||
},
|
|
||||||
LM_STUDIO: {
|
|
||||||
API_URL: config.lmStudioApiUrl,
|
|
||||||
},
|
|
||||||
LEMONADE: {
|
|
||||||
API_URL: config.lemonadeApiUrl,
|
|
||||||
API_KEY: config.lemonadeApiKey,
|
|
||||||
},
|
|
||||||
CUSTOM_OPENAI: {
|
|
||||||
API_URL: config.customOpenaiApiUrl,
|
|
||||||
API_KEY: config.customOpenaiApiKey,
|
|
||||||
MODEL_NAME: config.customOpenaiModelName,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
updateConfig(updatedConfig);
|
|
||||||
|
|
||||||
return Response.json({ message: 'Config updated' }, { status: 200 });
|
|
||||||
} catch (err) {
|
|
||||||
console.error('An error occurred while updating config:', err);
|
|
||||||
return Response.json(
|
|
||||||
{ message: 'An error occurred while updating config' },
|
|
||||||
{ status: 500 },
|
{ status: 500 },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user