mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
feat(api): add support for user-provided OpenAI API key (#311)
- Add support for user-provided OpenAI API key by setting OPENAI_KEY to "user_provided" in .env.example - Pass oaiApiKey to titleConvo function in titleConvo.js - Pass oaiApiKey to askClient function in askOpenAI.js - Modify openAI object in endpoints.js to include userProvide property based on whether OPENAI_KEY is set to "user_provided" or not.
This commit is contained in:
parent
61a4231feb
commit
26152d7e5f
4 changed files with 12 additions and 8 deletions
|
|
@ -24,7 +24,8 @@ MONGO_URI=mongodb://127.0.0.1:27017/chatgpt-clone
|
||||||
##########################
|
##########################
|
||||||
|
|
||||||
# Access key from OpenAI platform.
|
# Access key from OpenAI platform.
|
||||||
# Leave it blank to disable this feature.
|
# Leave it blank to disable this feature.
|
||||||
|
# Set to "user_provided" to allow the user to provide their API key from the UI.
|
||||||
OPENAI_KEY=
|
OPENAI_KEY=
|
||||||
|
|
||||||
# Identify the available models, separated by commas *without spaces*.
|
# Identify the available models, separated by commas *without spaces*.
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ const proxyEnvToAxiosProxy = (proxyString) => {
|
||||||
return proxyConfig;
|
return proxyConfig;
|
||||||
};
|
};
|
||||||
|
|
||||||
const titleConvo = async ({ endpoint, text, response }) => {
|
const titleConvo = async ({ endpoint, text, response, oaiApiKey }) => {
|
||||||
let title = 'New Chat';
|
let title = 'New Chat';
|
||||||
const ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default;
|
const ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default;
|
||||||
|
|
||||||
|
|
@ -50,7 +50,7 @@ const titleConvo = async ({ endpoint, text, response }) => {
|
||||||
frequency_penalty: 0
|
frequency_penalty: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
let apiKey = process.env.OPENAI_KEY;
|
let apiKey = oaiApiKey || process.env.OPENAI_KEY;
|
||||||
|
|
||||||
if (azure) {
|
if (azure) {
|
||||||
apiKey = process.env.AZURE_OPENAI_API_KEY;
|
apiKey = process.env.AZURE_OPENAI_API_KEY;
|
||||||
|
|
|
||||||
|
|
@ -169,12 +169,13 @@ const ask = async ({
|
||||||
};
|
};
|
||||||
const abortKey = conversationId;
|
const abortKey = conversationId;
|
||||||
abortControllers.set(abortKey, { abortController, ...endpointOption });
|
abortControllers.set(abortKey, { abortController, ...endpointOption });
|
||||||
|
const oaiApiKey = req.body?.token ?? null;
|
||||||
|
|
||||||
let response = await askClient({
|
let response = await askClient({
|
||||||
text,
|
text,
|
||||||
parentMessageId: userParentMessageId,
|
parentMessageId: userParentMessageId,
|
||||||
conversationId,
|
conversationId,
|
||||||
oaiApiKey: req.body?.token ?? null,
|
oaiApiKey,
|
||||||
...endpointOption,
|
...endpointOption,
|
||||||
onProgress: progressCallback.call(null, {
|
onProgress: progressCallback.call(null, {
|
||||||
res,
|
res,
|
||||||
|
|
@ -250,7 +251,7 @@ const ask = async ({
|
||||||
res.end();
|
res.end();
|
||||||
|
|
||||||
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
||||||
const title = await titleConvo({ endpoint: endpointOption?.endpoint, text, response: responseMessage });
|
const title = await titleConvo({ endpoint: endpointOption?.endpoint, text, response: responseMessage, oaiApiKey });
|
||||||
await saveConvo(req.user.id, {
|
await saveConvo(req.user.id, {
|
||||||
conversationId: conversationId,
|
conversationId: conversationId,
|
||||||
title
|
title
|
||||||
|
|
|
||||||
|
|
@ -38,9 +38,11 @@ router.get('/', async function (req, res) {
|
||||||
const google =
|
const google =
|
||||||
key || palmUser ? { userProvide: palmUser, availableModels: ['chat-bison', 'text-bison'] } : false;
|
key || palmUser ? { userProvide: palmUser, availableModels: ['chat-bison', 'text-bison'] } : false;
|
||||||
const azureOpenAI = !!process.env.AZURE_OPENAI_KEY;
|
const azureOpenAI = !!process.env.AZURE_OPENAI_KEY;
|
||||||
|
const apiKey = process.env.OPENAI_KEY || process.env.AZURE_OPENAI_API_KEY;
|
||||||
|
console.log('API KEY', apiKey);
|
||||||
const openAI =
|
const openAI =
|
||||||
process.env.OPENAI_KEY || process.env.AZURE_OPENAI_API_KEY
|
apiKey
|
||||||
? { availableModels: getOpenAIModels(), userProvide: true }
|
? { availableModels: getOpenAIModels(), userProvide: apiKey === 'user_provided' }
|
||||||
: false;
|
: false;
|
||||||
const bingAI = process.env.BINGAI_TOKEN
|
const bingAI = process.env.BINGAI_TOKEN
|
||||||
? { userProvide: process.env.BINGAI_TOKEN == 'user_provided' }
|
? { userProvide: process.env.BINGAI_TOKEN == 'user_provided' }
|
||||||
|
|
@ -55,4 +57,4 @@ router.get('/', async function (req, res) {
|
||||||
res.send(JSON.stringify({ azureOpenAI, openAI, google, bingAI, chatGPTBrowser }));
|
res.send(JSON.stringify({ azureOpenAI, openAI, google, bingAI, chatGPTBrowser }));
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = { router, getOpenAIModels, getChatGPTBrowserModels };
|
module.exports = { router, getOpenAIModels, getChatGPTBrowserModels };
|
||||||
Loading…
Add table
Add a link
Reference in a new issue