LibreChat/api/server/routes/endpoints/anthropic/initializeClient.js
madonchik123 d043a849a9
Added Reverse Proxy for Anthropic (#1106)
* Update AnthropicClient.js

Added BaseURL

* Update .env.example

Added ANTHROPIC_REVERSE_PROXY ENV

* Update initializeClient.js

Added Reverse_Proxy

* Update .env.example

* Update initializeClient.js

* Update AnthropicClient.js

* Update .env.example

Request

* Update initializeClient.js

Mae ANTHROPIC_REVERSE_PROXY let instead of const

* fix: lint errors, refactor(initializeClient)

* chore: change casing of reverseProxy

---------

Co-authored-by: Marco Beretta <81851188+Berry-13@users.noreply.github.com>
Co-authored-by: Danny Avila <110412045+danny-avila@users.noreply.github.com>
2023-11-20 20:12:53 -05:00

32 lines
1 KiB
JavaScript

const { AnthropicClient } = require('../../../../app');
const { getUserKey, checkUserKeyExpiry } = require('../../../services/UserService');
const initializeClient = async ({ req, res }) => {
const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
const expiresAt = req.body.key;
const isUserProvided = ANTHROPIC_API_KEY === 'user_provided';
let anthropicApiKey = isUserProvided ? await getAnthropicUserKey(req.user.id) : ANTHROPIC_API_KEY;
let reverseProxy = process.env.ANTHROPIC_REVERSE_PROXY || undefined;
console.log('ANTHROPIC_REVERSE_PROXY', reverseProxy);
if (expiresAt && isUserProvided) {
checkUserKeyExpiry(
expiresAt,
'Your ANTHROPIC_API_KEY has expired. Please provide your API key again.',
);
}
const client = new AnthropicClient(anthropicApiKey, { req, res }, {}, reverseProxy);
return {
client,
anthropicApiKey,
};
};
const getAnthropicUserKey = async (userId) => {
return await getUserKey({ userId, name: 'anthropic' });
};
module.exports = initializeClient;