Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

Added Reverse Proxy for Anthropic (#1106)

madonchik123d043a849a9ba
diff --git a/.env.example b/.env.example
index 14d5aaeb69..86d529fde5 100644
--- a/.env.example
+++ b/.env.example
@@ -318,6 +318,11 @@ PALM_KEY=user_provided
ANTHROPIC_API_KEY=user_provided
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
##########################
diff --git a/api/app/clients/AnthropicClient.js b/api/app/clients/AnthropicClient.js
index 04e5563bf2..cdc105d258 100644
--- a/api/app/clients/AnthropicClient.js
+++ b/api/app/clients/AnthropicClient.js
@@ -9,10 +9,13 @@ const AI_PROMPT = '\n\nAssistant:';
const tokenizersCache = {};
class AnthropicClient extends BaseClient {
- constructor(apiKey, options = {}, cacheOptions = {}) {
+ constructor(apiKey, options = {}, cacheOptions = {}, baseURL) {
super(apiKey, options, cacheOptions);
this.apiKey = apiKey || process.env.ANTHROPIC_API_KEY;
this.sender = 'Anthropic';
+ if (baseURL) {
+ this.baseURL = baseURL;
+ }
this.userLabel = HUMAN_PROMPT;
this.assistantLabel = AI_PROMPT;
this.setOptions(options);
@@ -78,10 +81,10 @@ class AnthropicClient extends BaseClient {
}
getClient() {
- if (this.options.reverseProxyUrl) {
+ if (this.baseURL) {
return new Anthropic({
apiKey: this.apiKey,
- baseURL: this.options.reverseProxyUrl,
+ baseURL: this.baseURL,
});
} else {
return new Anthropic({
diff --git a/api/server/routes/endpoints/anthropic/initializeClient.js b/api/server/routes/endpoints/anthropic/initializeClient.js
index 0b5bc6e0f9..5160611e7e 100644
--- a/api/server/routes/endpoints/anthropic/initializeClient.js
+++ b/api/server/routes/endpoints/anthropic/initializeClient.js
@@ -2,25 +2,31 @@ const { AnthropicClient } = require('../../../../app');
const { getUserKey, checkUserKeyExpiry } = require('../../../services/UserService');
const initializeClient = async ({ req, res }) => {
- const { ANTHROPIC_API_KEY } = process.env;
- const { key: expiresAt } = req.body;
-
+ const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
+ const expiresAt = req.body.key;
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) {
checkUserKeyExpiry(
expiresAt,
'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 {
client,
anthropicApiKey,
};
};
+const getAnthropicUserKey = async (userId) => {
+ return await getUserKey({ userId, name: 'anthropic' });
+};
+
module.exports = initializeClient;