mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
* fix(Google): allow presets to configure expected maxOutputTokens * refactor: standardize client-facing errors * refactor(checkUserKeyExpiry): pass endpoint instead of custom message * feat(UserService): JSDocs and getUserKeyValues * refactor: add NO_BASE_URL error type, make use of getUserKeyValues, throw user-specific errors * ci: update tests with recent changes
36 lines
1 KiB
JavaScript
36 lines
1 KiB
JavaScript
const { EModelEndpoint } = require('librechat-data-provider');
|
|
const { getUserKey, checkUserKeyExpiry } = require('~/server/services/UserService');
|
|
const { AnthropicClient } = require('~/app');
|
|
|
|
const initializeClient = async ({ req, res, endpointOption }) => {
|
|
const { ANTHROPIC_API_KEY, ANTHROPIC_REVERSE_PROXY, PROXY } = process.env;
|
|
const expiresAt = req.body.key;
|
|
const isUserProvided = ANTHROPIC_API_KEY === 'user_provided';
|
|
|
|
const anthropicApiKey = isUserProvided
|
|
? await getUserKey({ userId: req.user.id, name: EModelEndpoint.anthropic })
|
|
: ANTHROPIC_API_KEY;
|
|
|
|
if (!anthropicApiKey) {
|
|
throw new Error('Anthropic API key not provided. Please provide it again.');
|
|
}
|
|
|
|
if (expiresAt && isUserProvided) {
|
|
checkUserKeyExpiry(expiresAt, EModelEndpoint.anthropic);
|
|
}
|
|
|
|
const client = new AnthropicClient(anthropicApiKey, {
|
|
req,
|
|
res,
|
|
reverseProxyUrl: ANTHROPIC_REVERSE_PROXY ?? null,
|
|
proxy: PROXY ?? null,
|
|
...endpointOption,
|
|
});
|
|
|
|
return {
|
|
client,
|
|
anthropicApiKey,
|
|
};
|
|
};
|
|
|
|
module.exports = initializeClient;
|