mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-06-16 23:08:41 +00:00
feat(handlers): use new custom openai
This commit is contained in:
@ -4,6 +4,12 @@ import { loadOpenAIChatModels, loadOpenAIEmbeddingsModels } from './openai';
|
|||||||
import { loadAnthropicChatModels } from './anthropic';
|
import { loadAnthropicChatModels } from './anthropic';
|
||||||
import { loadTransformersEmbeddingsModels } from './transformers';
|
import { loadTransformersEmbeddingsModels } from './transformers';
|
||||||
import { loadGeminiChatModels, loadGeminiEmbeddingsModels } from './gemini';
|
import { loadGeminiChatModels, loadGeminiEmbeddingsModels } from './gemini';
|
||||||
|
import {
|
||||||
|
getCustomOpenaiApiKey,
|
||||||
|
getCustomOpenaiApiUrl,
|
||||||
|
getCustomOpenaiModelName,
|
||||||
|
} from '../../config';
|
||||||
|
import { ChatOpenAI } from '@langchain/openai';
|
||||||
|
|
||||||
const chatModelProviders = {
|
const chatModelProviders = {
|
||||||
openai: loadOpenAIChatModels,
|
openai: loadOpenAIChatModels,
|
||||||
@ -30,7 +36,27 @@ export const getAvailableChatModelProviders = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
models['custom_openai'] = {};
|
const customOpenAiApiKey = getCustomOpenaiApiKey();
|
||||||
|
const customOpenAiApiUrl = getCustomOpenaiApiUrl();
|
||||||
|
const customOpenAiModelName = getCustomOpenaiModelName();
|
||||||
|
|
||||||
|
models['custom_openai'] = {
|
||||||
|
...(customOpenAiApiKey && customOpenAiApiUrl && customOpenAiModelName
|
||||||
|
? {
|
||||||
|
[customOpenAiModelName]: {
|
||||||
|
displayName: customOpenAiModelName,
|
||||||
|
model: new ChatOpenAI({
|
||||||
|
openAIApiKey: customOpenAiApiKey,
|
||||||
|
modelName: customOpenAiModelName,
|
||||||
|
temperature: 0.7,
|
||||||
|
configuration: {
|
||||||
|
baseURL: customOpenAiApiUrl,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
};
|
||||||
|
|
||||||
return models;
|
return models;
|
||||||
};
|
};
|
||||||
|
@ -10,6 +10,9 @@ import {
|
|||||||
getGeminiApiKey,
|
getGeminiApiKey,
|
||||||
getOpenaiApiKey,
|
getOpenaiApiKey,
|
||||||
updateConfig,
|
updateConfig,
|
||||||
|
getCustomOpenaiApiUrl,
|
||||||
|
getCustomOpenaiApiKey,
|
||||||
|
getCustomOpenaiModelName,
|
||||||
} from '../config';
|
} from '../config';
|
||||||
import logger from '../utils/logger';
|
import logger from '../utils/logger';
|
||||||
|
|
||||||
@ -54,6 +57,9 @@ router.get('/', async (_, res) => {
|
|||||||
config['anthropicApiKey'] = getAnthropicApiKey();
|
config['anthropicApiKey'] = getAnthropicApiKey();
|
||||||
config['groqApiKey'] = getGroqApiKey();
|
config['groqApiKey'] = getGroqApiKey();
|
||||||
config['geminiApiKey'] = getGeminiApiKey();
|
config['geminiApiKey'] = getGeminiApiKey();
|
||||||
|
config['customOpenaiApiUrl'] = getCustomOpenaiApiUrl();
|
||||||
|
config['customOpenaiApiKey'] = getCustomOpenaiApiKey();
|
||||||
|
config['customOpenaiModelName'] = getCustomOpenaiModelName();
|
||||||
|
|
||||||
res.status(200).json(config);
|
res.status(200).json(config);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
@ -66,14 +72,27 @@ router.post('/', async (req, res) => {
|
|||||||
const config = req.body;
|
const config = req.body;
|
||||||
|
|
||||||
const updatedConfig = {
|
const updatedConfig = {
|
||||||
API_KEYS: {
|
MODELS: {
|
||||||
OPENAI: config.openaiApiKey,
|
OPENAI: {
|
||||||
GROQ: config.groqApiKey,
|
API_KEY: config.openaiApiKey,
|
||||||
ANTHROPIC: config.anthropicApiKey,
|
},
|
||||||
GEMINI: config.geminiApiKey,
|
GROQ: {
|
||||||
},
|
API_KEY: config.groqApiKey,
|
||||||
API_ENDPOINTS: {
|
},
|
||||||
OLLAMA: config.ollamaApiUrl,
|
ANTHROPIC: {
|
||||||
|
API_KEY: config.anthropicApiKey,
|
||||||
|
},
|
||||||
|
GEMINI: {
|
||||||
|
API_KEY: config.geminiApiKey,
|
||||||
|
},
|
||||||
|
OLLAMA: {
|
||||||
|
API_URL: config.ollamaApiUrl,
|
||||||
|
},
|
||||||
|
CUSTOM_OPENAI: {
|
||||||
|
API_URL: config.customOpenaiApiUrl,
|
||||||
|
API_KEY: config.customOpenaiApiKey,
|
||||||
|
MODEL_NAME: config.customOpenaiModelName,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -5,14 +5,17 @@ import { getAvailableChatModelProviders } from '../lib/providers';
|
|||||||
import { HumanMessage, AIMessage } from '@langchain/core/messages';
|
import { HumanMessage, AIMessage } from '@langchain/core/messages';
|
||||||
import logger from '../utils/logger';
|
import logger from '../utils/logger';
|
||||||
import { ChatOpenAI } from '@langchain/openai';
|
import { ChatOpenAI } from '@langchain/openai';
|
||||||
|
import {
|
||||||
|
getCustomOpenaiApiKey,
|
||||||
|
getCustomOpenaiApiUrl,
|
||||||
|
getCustomOpenaiModelName,
|
||||||
|
} from '../config';
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
interface ChatModel {
|
interface ChatModel {
|
||||||
provider: string;
|
provider: string;
|
||||||
model: string;
|
model: string;
|
||||||
customOpenAIBaseURL?: string;
|
|
||||||
customOpenAIKey?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ImageSearchBody {
|
interface ImageSearchBody {
|
||||||
@ -44,21 +47,12 @@ router.post('/', async (req, res) => {
|
|||||||
let llm: BaseChatModel | undefined;
|
let llm: BaseChatModel | undefined;
|
||||||
|
|
||||||
if (body.chatModel?.provider === 'custom_openai') {
|
if (body.chatModel?.provider === 'custom_openai') {
|
||||||
if (
|
|
||||||
!body.chatModel?.customOpenAIBaseURL ||
|
|
||||||
!body.chatModel?.customOpenAIKey
|
|
||||||
) {
|
|
||||||
return res
|
|
||||||
.status(400)
|
|
||||||
.json({ message: 'Missing custom OpenAI base URL or key' });
|
|
||||||
}
|
|
||||||
|
|
||||||
llm = new ChatOpenAI({
|
llm = new ChatOpenAI({
|
||||||
modelName: body.chatModel.model,
|
modelName: getCustomOpenaiModelName(),
|
||||||
openAIApiKey: body.chatModel.customOpenAIKey,
|
openAIApiKey: getCustomOpenaiApiKey(),
|
||||||
temperature: 0.7,
|
temperature: 0.7,
|
||||||
configuration: {
|
configuration: {
|
||||||
baseURL: body.chatModel.customOpenAIBaseURL,
|
baseURL: getCustomOpenaiApiUrl(),
|
||||||
},
|
},
|
||||||
}) as unknown as BaseChatModel;
|
}) as unknown as BaseChatModel;
|
||||||
} else if (
|
} else if (
|
||||||
|
@ -10,14 +10,19 @@ import {
|
|||||||
import { searchHandlers } from '../websocket/messageHandler';
|
import { searchHandlers } from '../websocket/messageHandler';
|
||||||
import { AIMessage, BaseMessage, HumanMessage } from '@langchain/core/messages';
|
import { AIMessage, BaseMessage, HumanMessage } from '@langchain/core/messages';
|
||||||
import { MetaSearchAgentType } from '../search/metaSearchAgent';
|
import { MetaSearchAgentType } from '../search/metaSearchAgent';
|
||||||
|
import {
|
||||||
|
getCustomOpenaiApiKey,
|
||||||
|
getCustomOpenaiApiUrl,
|
||||||
|
getCustomOpenaiModelName,
|
||||||
|
} from '../config';
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
interface chatModel {
|
interface chatModel {
|
||||||
provider: string;
|
provider: string;
|
||||||
model: string;
|
model: string;
|
||||||
customOpenAIBaseURL?: string;
|
|
||||||
customOpenAIKey?: string;
|
customOpenAIKey?: string;
|
||||||
|
customOpenAIBaseURL?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface embeddingModel {
|
interface embeddingModel {
|
||||||
@ -78,21 +83,12 @@ router.post('/', async (req, res) => {
|
|||||||
let embeddings: Embeddings | undefined;
|
let embeddings: Embeddings | undefined;
|
||||||
|
|
||||||
if (body.chatModel?.provider === 'custom_openai') {
|
if (body.chatModel?.provider === 'custom_openai') {
|
||||||
if (
|
|
||||||
!body.chatModel?.customOpenAIBaseURL ||
|
|
||||||
!body.chatModel?.customOpenAIKey
|
|
||||||
) {
|
|
||||||
return res
|
|
||||||
.status(400)
|
|
||||||
.json({ message: 'Missing custom OpenAI base URL or key' });
|
|
||||||
}
|
|
||||||
|
|
||||||
llm = new ChatOpenAI({
|
llm = new ChatOpenAI({
|
||||||
modelName: body.chatModel.model,
|
modelName: body.chatModel?.model || getCustomOpenaiModelName(),
|
||||||
openAIApiKey: body.chatModel.customOpenAIKey,
|
openAIApiKey: body.chatModel?.customOpenAIKey || getCustomOpenaiApiKey(),
|
||||||
temperature: 0.7,
|
temperature: 0.7,
|
||||||
configuration: {
|
configuration: {
|
||||||
baseURL: body.chatModel.customOpenAIBaseURL,
|
baseURL: body.chatModel?.customOpenAIBaseURL || getCustomOpenaiApiUrl(),
|
||||||
},
|
},
|
||||||
}) as unknown as BaseChatModel;
|
}) as unknown as BaseChatModel;
|
||||||
} else if (
|
} else if (
|
||||||
|
@ -5,14 +5,17 @@ import { getAvailableChatModelProviders } from '../lib/providers';
|
|||||||
import { HumanMessage, AIMessage } from '@langchain/core/messages';
|
import { HumanMessage, AIMessage } from '@langchain/core/messages';
|
||||||
import logger from '../utils/logger';
|
import logger from '../utils/logger';
|
||||||
import { ChatOpenAI } from '@langchain/openai';
|
import { ChatOpenAI } from '@langchain/openai';
|
||||||
|
import {
|
||||||
|
getCustomOpenaiApiKey,
|
||||||
|
getCustomOpenaiApiUrl,
|
||||||
|
getCustomOpenaiModelName,
|
||||||
|
} from '../config';
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
interface ChatModel {
|
interface ChatModel {
|
||||||
provider: string;
|
provider: string;
|
||||||
model: string;
|
model: string;
|
||||||
customOpenAIBaseURL?: string;
|
|
||||||
customOpenAIKey?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SuggestionsBody {
|
interface SuggestionsBody {
|
||||||
@ -43,21 +46,12 @@ router.post('/', async (req, res) => {
|
|||||||
let llm: BaseChatModel | undefined;
|
let llm: BaseChatModel | undefined;
|
||||||
|
|
||||||
if (body.chatModel?.provider === 'custom_openai') {
|
if (body.chatModel?.provider === 'custom_openai') {
|
||||||
if (
|
|
||||||
!body.chatModel?.customOpenAIBaseURL ||
|
|
||||||
!body.chatModel?.customOpenAIKey
|
|
||||||
) {
|
|
||||||
return res
|
|
||||||
.status(400)
|
|
||||||
.json({ message: 'Missing custom OpenAI base URL or key' });
|
|
||||||
}
|
|
||||||
|
|
||||||
llm = new ChatOpenAI({
|
llm = new ChatOpenAI({
|
||||||
modelName: body.chatModel.model,
|
modelName: getCustomOpenaiModelName(),
|
||||||
openAIApiKey: body.chatModel.customOpenAIKey,
|
openAIApiKey: getCustomOpenaiApiKey(),
|
||||||
temperature: 0.7,
|
temperature: 0.7,
|
||||||
configuration: {
|
configuration: {
|
||||||
baseURL: body.chatModel.customOpenAIBaseURL,
|
baseURL: getCustomOpenaiApiUrl(),
|
||||||
},
|
},
|
||||||
}) as unknown as BaseChatModel;
|
}) as unknown as BaseChatModel;
|
||||||
} else if (
|
} else if (
|
||||||
|
@ -5,14 +5,17 @@ import { HumanMessage, AIMessage } from '@langchain/core/messages';
|
|||||||
import logger from '../utils/logger';
|
import logger from '../utils/logger';
|
||||||
import handleVideoSearch from '../chains/videoSearchAgent';
|
import handleVideoSearch from '../chains/videoSearchAgent';
|
||||||
import { ChatOpenAI } from '@langchain/openai';
|
import { ChatOpenAI } from '@langchain/openai';
|
||||||
|
import {
|
||||||
|
getCustomOpenaiApiKey,
|
||||||
|
getCustomOpenaiApiUrl,
|
||||||
|
getCustomOpenaiModelName,
|
||||||
|
} from '../config';
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
interface ChatModel {
|
interface ChatModel {
|
||||||
provider: string;
|
provider: string;
|
||||||
model: string;
|
model: string;
|
||||||
customOpenAIBaseURL?: string;
|
|
||||||
customOpenAIKey?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface VideoSearchBody {
|
interface VideoSearchBody {
|
||||||
@ -44,21 +47,12 @@ router.post('/', async (req, res) => {
|
|||||||
let llm: BaseChatModel | undefined;
|
let llm: BaseChatModel | undefined;
|
||||||
|
|
||||||
if (body.chatModel?.provider === 'custom_openai') {
|
if (body.chatModel?.provider === 'custom_openai') {
|
||||||
if (
|
|
||||||
!body.chatModel?.customOpenAIBaseURL ||
|
|
||||||
!body.chatModel?.customOpenAIKey
|
|
||||||
) {
|
|
||||||
return res
|
|
||||||
.status(400)
|
|
||||||
.json({ message: 'Missing custom OpenAI base URL or key' });
|
|
||||||
}
|
|
||||||
|
|
||||||
llm = new ChatOpenAI({
|
llm = new ChatOpenAI({
|
||||||
modelName: body.chatModel.model,
|
modelName: getCustomOpenaiModelName(),
|
||||||
openAIApiKey: body.chatModel.customOpenAIKey,
|
openAIApiKey: getCustomOpenaiApiKey(),
|
||||||
temperature: 0.7,
|
temperature: 0.7,
|
||||||
configuration: {
|
configuration: {
|
||||||
baseURL: body.chatModel.customOpenAIBaseURL,
|
baseURL: getCustomOpenaiApiUrl(),
|
||||||
},
|
},
|
||||||
}) as unknown as BaseChatModel;
|
}) as unknown as BaseChatModel;
|
||||||
} else if (
|
} else if (
|
||||||
|
@ -9,6 +9,11 @@ import type { Embeddings } from '@langchain/core/embeddings';
|
|||||||
import type { IncomingMessage } from 'http';
|
import type { IncomingMessage } from 'http';
|
||||||
import logger from '../utils/logger';
|
import logger from '../utils/logger';
|
||||||
import { ChatOpenAI } from '@langchain/openai';
|
import { ChatOpenAI } from '@langchain/openai';
|
||||||
|
import {
|
||||||
|
getCustomOpenaiApiKey,
|
||||||
|
getCustomOpenaiApiUrl,
|
||||||
|
getCustomOpenaiModelName,
|
||||||
|
} from '../config';
|
||||||
|
|
||||||
export const handleConnection = async (
|
export const handleConnection = async (
|
||||||
ws: WebSocket,
|
ws: WebSocket,
|
||||||
@ -48,14 +53,20 @@ export const handleConnection = async (
|
|||||||
llm = chatModelProviders[chatModelProvider][chatModel]
|
llm = chatModelProviders[chatModelProvider][chatModel]
|
||||||
.model as unknown as BaseChatModel | undefined;
|
.model as unknown as BaseChatModel | undefined;
|
||||||
} else if (chatModelProvider == 'custom_openai') {
|
} else if (chatModelProvider == 'custom_openai') {
|
||||||
llm = new ChatOpenAI({
|
const customOpenaiApiKey = getCustomOpenaiApiKey();
|
||||||
modelName: chatModel,
|
const customOpenaiApiUrl = getCustomOpenaiApiUrl();
|
||||||
openAIApiKey: searchParams.get('openAIApiKey'),
|
const customOpenaiModelName = getCustomOpenaiModelName();
|
||||||
temperature: 0.7,
|
|
||||||
configuration: {
|
if (customOpenaiApiKey && customOpenaiApiUrl && customOpenaiModelName) {
|
||||||
baseURL: searchParams.get('openAIBaseURL'),
|
llm = new ChatOpenAI({
|
||||||
},
|
modelName: customOpenaiModelName,
|
||||||
}) as unknown as BaseChatModel;
|
openAIApiKey: customOpenaiApiKey,
|
||||||
|
temperature: 0.7,
|
||||||
|
configuration: {
|
||||||
|
baseURL: customOpenaiApiUrl,
|
||||||
|
},
|
||||||
|
}) as unknown as BaseChatModel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
Reference in New Issue
Block a user