Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ GEMINI_API_KEY="your-google-ai-studio-key"
# Set to "anthropic" for "just an Anthropic proxy" mode (no remapping)
PREFERRED_PROVIDER="openai"
OPENAI_BASE_URL="https://api.openai.com/v1"
ANTHROPIC_BASE_URL="https://api.anthropic.com"

# Optional: Specify the exact models to map haiku/sonnet to.
# If PREFERRED_PROVIDER=google, these MUST be valid Gemini model names known to the server.
Expand Down
14 changes: 13 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def format(self, record):
# Get OpenAI base URL from environment (if set)
OPENAI_BASE_URL = os.environ.get("OPENAI_BASE_URL")

# Get Anthropic base URL from environment (if set)
ANTHROPIC_BASE_URL = os.environ.get("ANTHROPIC_BASE_URL")

# Get preferred provider (default to openai)
PREFERRED_PROVIDER = os.environ.get("PREFERRED_PROVIDER", "openai").lower()

Expand Down Expand Up @@ -1142,7 +1145,12 @@ async def create_message(
logger.debug(f"Using Gemini API key for model: {request.model}")
else:
litellm_request["api_key"] = ANTHROPIC_API_KEY
logger.debug(f"Using Anthropic API key for model: {request.model}")
# Use custom Anthropic base URL if configured
if ANTHROPIC_BASE_URL:
litellm_request["api_base"] = ANTHROPIC_BASE_URL
logger.debug(f"Using Anthropic API key and custom base URL {ANTHROPIC_BASE_URL} for model: {request.model}")
else:
logger.debug(f"Using Anthropic API key for model: {request.model}")

# For OpenAI models - modify request format to work with limitations
if "openai" in litellm_request["model"] and "messages" in litellm_request:
Expand Down Expand Up @@ -1444,6 +1452,10 @@ async def count_tokens(
# Add custom base URL for OpenAI models if configured
if request.model.startswith("openai/") and OPENAI_BASE_URL:
token_counter_args["api_base"] = OPENAI_BASE_URL

# Add custom base URL for Anthropic models if configured
elif request.model.startswith("anthropic/") and ANTHROPIC_BASE_URL:
token_counter_args["api_base"] = ANTHROPIC_BASE_URL

# Count tokens
token_count = token_counter(**token_counter_args)
Expand Down