Skip to content

Commit 280acc7

Browse files
committed
Add configurable Anthropic API base URL
Introduces anthropic_api_base configuration option to allow users to specify custom Anthropic API endpoints. This enables support for proxies, custom deployments, and alternative Anthropic-compatible services. Also fixes URL path formatting in chat and models endpoints by removing redundant leading slashes.
1 parent bd2bb14 commit 280acc7

6 files changed

Lines changed: 44 additions & 3 deletions

File tree

bin/console

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require 'irb'
99

1010
RubyLLM.configure do |config|
1111
config.anthropic_api_key = ENV.fetch('ANTHROPIC_API_KEY', nil)
12+
config.anthropic_api_base = ENV.fetch('ANTHROPIC_BASE_URL', nil)
1213
config.bedrock_api_key = ENV.fetch('AWS_ACCESS_KEY_ID', nil)
1314
config.bedrock_region = ENV.fetch('AWS_REGION', nil)
1415
config.bedrock_secret_key = ENV.fetch('AWS_SECRET_ACCESS_KEY', nil)

lib/ruby_llm/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Configuration
88
:openai_organization_id,
99
:openai_project_id,
1010
:openai_use_system_role,
11+
:anthropic_api_base,
1112
:anthropic_api_key,
1213
:gemini_api_key,
1314
:gemini_api_base,

lib/ruby_llm/providers/anthropic.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Anthropic < Provider
1212
include Anthropic::Tools
1313

1414
def api_base
15-
'https://api.anthropic.com'
15+
@config.anthropic_api_base || 'https://api.anthropic.com'
1616
end
1717

1818
def headers

lib/ruby_llm/providers/anthropic/chat.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Chat
88
module_function
99

1010
def completion_url
11-
'/v1/messages'
11+
'v1/messages'
1212
end
1313

1414
def render_payload(messages, tools:, temperature:, model:, stream: false, schema: nil, thinking: nil) # rubocop:disable Metrics/ParameterLists,Lint/UnusedMethodArgument

lib/ruby_llm/providers/anthropic/models.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Models
88
module_function
99

1010
def models_url
11-
'/v1/models'
11+
'v1/models'
1212
end
1313

1414
def parse_list_models_response(response, slug, capabilities)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe RubyLLM::Providers::Anthropic do
6+
subject(:provider) { described_class.new(config) }
7+
8+
let(:config) do
9+
instance_double(
10+
RubyLLM::Configuration,
11+
request_timeout: 300,
12+
max_retries: 3,
13+
retry_interval: 0.1,
14+
retry_interval_randomness: 0.5,
15+
retry_backoff_factor: 2,
16+
http_proxy: nil,
17+
anthropic_api_key: 'test-key',
18+
anthropic_api_base: anthropic_api_base
19+
)
20+
end
21+
22+
describe '#api_base' do
23+
context 'when anthropic_api_base is not set' do
24+
let(:anthropic_api_base) { nil }
25+
26+
it 'returns the default Anthropic API URL' do
27+
expect(provider.api_base).to eq('https://api.anthropic.com')
28+
end
29+
end
30+
31+
context 'when anthropic_api_base is set' do
32+
let(:anthropic_api_base) { 'https://custom-anthropic-endpoint.example.com' }
33+
34+
it 'returns the custom API URL' do
35+
expect(provider.api_base).to eq('https://custom-anthropic-endpoint.example.com')
36+
end
37+
end
38+
end
39+
end

0 commit comments

Comments
 (0)