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>
This commit is contained in:
madonchik123 2023-11-21 04:12:53 +03:00 committed by GitHub
parent b7dcc4264d
commit d043a849a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 10 deletions

View file

@ -318,6 +318,11 @@ PALM_KEY=user_provided
ANTHROPIC_API_KEY=user_provided ANTHROPIC_API_KEY=user_provided
ANTHROPIC_MODELS=claude-1,claude-instant-1,claude-2 ANTHROPIC_MODELS=claude-1,claude-instant-1,claude-2
# In case if you have reverse proxy for ANTHROPIC_REVERSE_PROXY you can add it here
# leave blank to use default base url
# ANTHROPIC_REVERSE_PROXY=
########################## ##########################
# Proxy: To be Used by all endpoints # Proxy: To be Used by all endpoints
########################## ##########################

View file

@ -9,10 +9,13 @@ const AI_PROMPT = '\n\nAssistant:';
const tokenizersCache = {}; const tokenizersCache = {};
class AnthropicClient extends BaseClient { class AnthropicClient extends BaseClient {
constructor(apiKey, options = {}, cacheOptions = {}) { constructor(apiKey, options = {}, cacheOptions = {}, baseURL) {
super(apiKey, options, cacheOptions); super(apiKey, options, cacheOptions);
this.apiKey = apiKey || process.env.ANTHROPIC_API_KEY; this.apiKey = apiKey || process.env.ANTHROPIC_API_KEY;
this.sender = 'Anthropic'; this.sender = 'Anthropic';
if (baseURL) {
this.baseURL = baseURL;
}
this.userLabel = HUMAN_PROMPT; this.userLabel = HUMAN_PROMPT;
this.assistantLabel = AI_PROMPT; this.assistantLabel = AI_PROMPT;
this.setOptions(options); this.setOptions(options);
@ -78,10 +81,10 @@ class AnthropicClient extends BaseClient {
} }
getClient() { getClient() {
if (this.options.reverseProxyUrl) { if (this.baseURL) {
return new Anthropic({ return new Anthropic({
apiKey: this.apiKey, apiKey: this.apiKey,
baseURL: this.options.reverseProxyUrl, baseURL: this.baseURL,
}); });
} else { } else {
return new Anthropic({ return new Anthropic({

View file

@ -2,25 +2,31 @@ const { AnthropicClient } = require('../../../../app');
const { getUserKey, checkUserKeyExpiry } = require('../../../services/UserService'); const { getUserKey, checkUserKeyExpiry } = require('../../../services/UserService');
const initializeClient = async ({ req, res }) => { const initializeClient = async ({ req, res }) => {
const { ANTHROPIC_API_KEY } = process.env; const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
const { key: expiresAt } = req.body; const expiresAt = req.body.key;
const isUserProvided = ANTHROPIC_API_KEY === 'user_provided'; const isUserProvided = ANTHROPIC_API_KEY === 'user_provided';
let key = null; 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) { if (expiresAt && isUserProvided) {
checkUserKeyExpiry( checkUserKeyExpiry(
expiresAt, expiresAt,
'Your ANTHROPIC_API_KEY has expired. Please provide your API key again.', 'Your ANTHROPIC_API_KEY has expired. Please provide your API key again.',
); );
key = await getUserKey({ userId: req.user.id, name: 'anthropic' });
} }
let anthropicApiKey = isUserProvided ? key : ANTHROPIC_API_KEY;
const client = new AnthropicClient(anthropicApiKey, { req, res }); const client = new AnthropicClient(anthropicApiKey, { req, res }, {}, reverseProxy);
return { return {
client, client,
anthropicApiKey, anthropicApiKey,
}; };
}; };
const getAnthropicUserKey = async (userId) => {
return await getUserKey({ userId, name: 'anthropic' });
};
module.exports = initializeClient; module.exports = initializeClient;