Add ANTHROPIC_BASE_URL support for Claude account subscription
- AnthropicClient now uses base_url param or ANTHROPIC_BASE_URL env var to connect to custom endpoints (subscription proxies, self-hosted, etc.) - Add ANTHROPIC_BASE_URL to .env.example - Document custom base URL usage in README https://claude.ai/code/session_01AbNbJYL7gHy47BQ9BJJTAv
This commit is contained in:
parent
eb2e82305c
commit
b3123122ae
|
|
@ -1,5 +1,6 @@
|
||||||
# LLM Providers (set the one you use)
|
# LLM Providers (set the one you use)
|
||||||
ANTHROPIC_API_KEY=
|
ANTHROPIC_API_KEY=
|
||||||
|
ANTHROPIC_BASE_URL=
|
||||||
OPENAI_API_KEY=
|
OPENAI_API_KEY=
|
||||||
GOOGLE_API_KEY=
|
GOOGLE_API_KEY=
|
||||||
XAI_API_KEY=
|
XAI_API_KEY=
|
||||||
|
|
|
||||||
|
|
@ -130,6 +130,11 @@ export OPENROUTER_API_KEY=... # OpenRouter
|
||||||
export ALPHA_VANTAGE_API_KEY=... # Alpha Vantage
|
export ALPHA_VANTAGE_API_KEY=... # Alpha Vantage
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To use a Claude account subscription (or proxy) instead of a direct API key, set a custom base URL:
|
||||||
|
```bash
|
||||||
|
export ANTHROPIC_BASE_URL=https://your-custom-endpoint.example.com
|
||||||
|
```
|
||||||
|
|
||||||
For local models, configure Ollama with `llm_provider: "ollama"` in your config.
|
For local models, configure Ollama with `llm_provider: "ollama"` in your config.
|
||||||
|
|
||||||
Alternatively, copy `.env.example` to `.env` and fill in your keys:
|
Alternatively, copy `.env.example` to `.env` and fill in your keys:
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import os
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
from langchain_anthropic import ChatAnthropic
|
from langchain_anthropic import ChatAnthropic
|
||||||
|
|
@ -24,7 +25,12 @@ class NormalizedChatAnthropic(ChatAnthropic):
|
||||||
|
|
||||||
|
|
||||||
class AnthropicClient(BaseLLMClient):
|
class AnthropicClient(BaseLLMClient):
|
||||||
"""Client for Anthropic Claude models."""
|
"""Client for Anthropic Claude models.
|
||||||
|
|
||||||
|
Supports custom base URLs via the base_url parameter or ANTHROPIC_BASE_URL
|
||||||
|
environment variable, enabling use with Claude account subscriptions
|
||||||
|
or proxy endpoints instead of direct API key access.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, model: str, base_url: Optional[str] = None, **kwargs):
|
def __init__(self, model: str, base_url: Optional[str] = None, **kwargs):
|
||||||
super().__init__(model, base_url, **kwargs)
|
super().__init__(model, base_url, **kwargs)
|
||||||
|
|
@ -33,6 +39,11 @@ class AnthropicClient(BaseLLMClient):
|
||||||
"""Return configured ChatAnthropic instance."""
|
"""Return configured ChatAnthropic instance."""
|
||||||
llm_kwargs = {"model": self.model}
|
llm_kwargs = {"model": self.model}
|
||||||
|
|
||||||
|
# Resolve base URL: explicit param > env var > default Anthropic API
|
||||||
|
base_url = self.base_url or os.environ.get("ANTHROPIC_BASE_URL")
|
||||||
|
if base_url:
|
||||||
|
llm_kwargs["anthropic_api_url"] = base_url
|
||||||
|
|
||||||
for key in _PASSTHROUGH_KWARGS:
|
for key in _PASSTHROUGH_KWARGS:
|
||||||
if key in self.kwargs:
|
if key in self.kwargs:
|
||||||
llm_kwargs[key] = self.kwargs[key]
|
llm_kwargs[key] = self.kwargs[key]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue