diff --git a/bin/console b/bin/console index 513061a92..e51e6947d 100755 --- a/bin/console +++ b/bin/console @@ -8,6 +8,7 @@ require 'dotenv/load' require 'irb' RubyLLM.configure do |config| + config.anthropic_api_base = ENV.fetch('ANTHROPIC_BASE_URL', nil) config.anthropic_api_key = ENV.fetch('ANTHROPIC_API_KEY', nil) config.azure_api_base = ENV.fetch('AZURE_API_BASE', nil) config.azure_api_key = ENV.fetch('AZURE_API_KEY', nil) diff --git a/lib/ruby_llm/configuration.rb b/lib/ruby_llm/configuration.rb index 4b0c46d64..c346b75e0 100644 --- a/lib/ruby_llm/configuration.rb +++ b/lib/ruby_llm/configuration.rb @@ -11,6 +11,7 @@ class Configuration :azure_api_base, :azure_api_key, :azure_ai_auth_token, + :anthropic_api_base, :anthropic_api_key, :gemini_api_key, :gemini_api_base, diff --git a/lib/ruby_llm/providers/anthropic.rb b/lib/ruby_llm/providers/anthropic.rb index cd7d38055..78fd33ba4 100644 --- a/lib/ruby_llm/providers/anthropic.rb +++ b/lib/ruby_llm/providers/anthropic.rb @@ -12,7 +12,7 @@ class Anthropic < Provider include Anthropic::Tools def api_base - 'https://api.anthropic.com' + @config.anthropic_api_base || 'https://api.anthropic.com' end def headers diff --git a/lib/ruby_llm/providers/anthropic/chat.rb b/lib/ruby_llm/providers/anthropic/chat.rb index a7af82df3..60aa702ec 100644 --- a/lib/ruby_llm/providers/anthropic/chat.rb +++ b/lib/ruby_llm/providers/anthropic/chat.rb @@ -8,7 +8,7 @@ module Chat module_function def completion_url - '/v1/messages' + 'v1/messages' end def render_payload(messages, tools:, temperature:, model:, stream: false, schema: nil, thinking: nil) # rubocop:disable Metrics/ParameterLists,Lint/UnusedMethodArgument diff --git a/lib/ruby_llm/providers/anthropic/models.rb b/lib/ruby_llm/providers/anthropic/models.rb index 72d392b7a..a6cd2a17f 100644 --- a/lib/ruby_llm/providers/anthropic/models.rb +++ b/lib/ruby_llm/providers/anthropic/models.rb @@ -8,7 +8,7 @@ module Models module_function def models_url - '/v1/models' + 'v1/models' end def parse_list_models_response(response, slug, capabilities) diff --git a/spec/ruby_llm/providers/anthropic_spec.rb b/spec/ruby_llm/providers/anthropic_spec.rb new file mode 100644 index 000000000..ca35d1f9c --- /dev/null +++ b/spec/ruby_llm/providers/anthropic_spec.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe RubyLLM::Providers::Anthropic do + subject(:provider) { described_class.new(config) } + + let(:config) do + instance_double( + RubyLLM::Configuration, + request_timeout: 300, + max_retries: 3, + retry_interval: 0.1, + retry_interval_randomness: 0.5, + retry_backoff_factor: 2, + http_proxy: nil, + anthropic_api_key: 'test-key', + anthropic_api_base: anthropic_api_base + ) + end + + describe '#api_base' do + context 'when anthropic_api_base is not set' do + let(:anthropic_api_base) { nil } + + it 'returns the default Anthropic API URL' do + expect(provider.api_base).to eq('https://api.anthropic.com') + end + end + + context 'when anthropic_api_base is set' do + let(:anthropic_api_base) { 'https://custom-anthropic-endpoint.example.com' } + + it 'returns the custom API URL' do + expect(provider.api_base).to eq('https://custom-anthropic-endpoint.example.com') + end + end + end +end