Class: RubyLLM::Providers::Azure

Inherits:
RubyLLM::Provider show all
Defined in:
lib/ruby_llm/providers/azure.rb,
lib/ruby_llm/providers/azure/chat.rb,
lib/ruby_llm/providers/azure/audio.rb,
lib/ruby_llm/providers/azure/media.rb,
lib/ruby_llm/providers/azure/cohere.rb,
lib/ruby_llm/providers/azure/images.rb,
lib/ruby_llm/providers/azure/models.rb,
lib/ruby_llm/providers/azure/videos.rb,
lib/ruby_llm/providers/azure/responses.rb,
lib/ruby_llm/providers/azure/embeddings.rb,
lib/ruby_llm/providers/azure/capabilities.rb,
lib/ruby_llm/providers/azure/chat_completions.rb,
lib/ruby_llm/providers/azure/chat_completions/batches.rb

Overview

Azure AI Foundry / OpenAI-compatible API integration.

Defined Under Namespace

Modules: Audio, Capabilities, Chat, Embeddings, Images, Media, Models, Videos Classes: ChatCompletions, Cohere, Responses

Constant Summary collapse

DEFAULT_CHAT_API_VERSION =
'2024-05-01-preview'
DEFAULT_EMBEDDINGS_API_VERSION =
'2024-05-01-preview'
DEFAULT_MODELS_API_VERSION =
'preview'

Constants included from Support::Inspectable

Support::Inspectable::TRUNCATE_AT

Instance Attribute Summary

Attributes inherited from RubyLLM::Provider

#config, #connection

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RubyLLM::Provider

#animate_later, #assume_models_exist?, #batch_cost, #batch_cost_amounts, #batch_protocol_name, #batch_results, #batch_status, #batches?, #cache_content, #cancel_batch, #capabilities, #compact, #complete, #configuration_requirements, #configured?, configured_providers, configured_remote_providers, #count_tokens, #create_batch, #delete_cache, display_name, #download_file, #embed, #extend_cache, #files?, #find_batch, #find_cache, #find_file, #find_research_job, #initialize, #list_file_uris, #list_models, local?, #local?, local_providers, #long_context_pricing?, model_registry_files, model_required?, models_dev_alias, models_dev_model_id, #moderate, #name, #ocr, #paint, #parse_error, #preprocess_message, protocol, protocols, #protocols, providers, register, remote?, remote_providers, #render, #render_embedding, #rerank, #research_later, resolve, resolve!, resolve_registry_id, #retry_delay, #slug, #speak, #tokenize, #tool_approval_response, #transcribe, #upload_file

Methods included from Support::Inspectable

#full_inspect, #inspect, #pretty_print

Constructor Details

This class inherits a constructor from RubyLLM::Provider

Class Method Details

.assume_models_exist?Boolean

Azure works with deployment names, instead of model names

Returns:

  • (Boolean)


123
124
125
# File 'lib/ruby_llm/providers/azure.rb', line 123

def assume_models_exist?
  true
end

.capabilitiesObject



106
107
108
# File 'lib/ruby_llm/providers/azure.rb', line 106

def capabilities
  Azure::Capabilities
end

.configuration_optionsObject



110
111
112
# File 'lib/ruby_llm/providers/azure.rb', line 110

def configuration_options
  %i[azure_api_base azure_api_key azure_ai_auth_token]
end

.configuration_requirementsObject



114
115
116
# File 'lib/ruby_llm/providers/azure.rb', line 114

def configuration_requirements
  %i[azure_api_base]
end

.configured?(config) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/ruby_llm/providers/azure.rb', line 118

def configured?(config)
  config.azure_api_base && (config.azure_api_key || config.azure_ai_auth_token)
end

Instance Method Details

#api_baseObject



16
17
18
# File 'lib/ruby_llm/providers/azure.rb', line 16

def api_base
  @config.azure_api_base
end

#azure_base_partsObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ruby_llm/providers/azure.rb', line 80

def azure_base_parts
  @azure_base_parts ||= begin
    raw_base = api_base.to_s.sub(%r{/+\z}, '')
    version = raw_base[/[?&]api-version=([^&]+)/i, 1]
    path_base = raw_base.sub(/\?.*\z/, '')

    mode = if path_base.include?('/chat/completions')
             :chat_endpoint
           elsif path_base.include?('/openai/deployments/')
             :deployment_base
           elsif path_base.include?('/openai/v1')
             :openai_v1_base
           else
             :resource_base
           end

    {
      path_base: path_base,
      root: azure_host_root(path_base),
      mode: mode,
      version: version
    }
  end
end

#azure_cohere_url(operation) ⇒ Object

:nodoc:



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_llm/providers/azure.rb', line 46

def azure_cohere_url(operation) # :nodoc:
  base = azure_base_parts[:path_base]
  base = if base.include?('/providers/cohere')
           base.sub(%r{/providers/cohere.*\z}, '/providers/cohere')
         elsif URI.parse(base).host&.end_with?('.models.ai.azure.com')
           base.sub(%r{/v2(?:/.*)?\z}, '')
         else
           "#{azure_base_parts[:root]}/providers/cohere"
         end
  "#{base}/v2/#{operation}"
end

#azure_media_url(path) ⇒ Object

:nodoc:



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby_llm/providers/azure.rb', line 67

def azure_media_url(path) # :nodoc:
  parts = azure_base_parts
  if parts[:path_base].include?('/openai/deployments/')
    base = parts[:path_base].sub(%r{/chat/completions/?\z}, '')
    version = parts[:version] || '2025-04-01-preview'
  else
    base = azure_openai_v1_base
    version = (parts[:version] if parts[:mode] == :openai_v1_base) || 'preview'
  end

  "#{base.sub(%r{/+\z}, '')}/#{path}?api-version=#{version}"
end

#azure_openai_v1_baseObject



58
59
60
61
62
63
64
65
# File 'lib/ruby_llm/providers/azure.rb', line 58

def azure_openai_v1_base
  parts = azure_base_parts
  if parts[:mode] == :openai_v1_base
    parts[:path_base]
  else
    "#{parts[:root]}/openai/v1"
  end
end

#batch_cost_multiplierObject



44
# File 'lib/ruby_llm/providers/azure.rb', line 44

def batch_cost_multiplier(**) = 0.5

#ensure_configured!Object

Raises:



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ruby_llm/providers/azure.rb', line 128

def ensure_configured!
  missing = []
  missing << :azure_api_base unless @config.azure_api_base
  if @config.azure_api_key.nil? && @config.azure_ai_auth_token.nil?
    missing << 'azure_api_key or azure_ai_auth_token'
  end
  return if missing.empty?

  raise ConfigurationError,
        "Missing configuration for Azure: #{missing.join(', ')}"
end

#headersObject



34
35
36
37
38
39
40
41
42
# File 'lib/ruby_llm/providers/azure.rb', line 34

def headers
  if @config.azure_api_key && URI.parse(api_base).host&.end_with?('.models.ai.azure.com')
    { 'Authorization' => "Bearer #{@config.azure_api_key}" }
  elsif @config.azure_api_key
    { 'api-key' => @config.azure_api_key }
  else
    { 'Authorization' => "Bearer #{@config.azure_ai_auth_token}" }
  end
end

#protocol_for(model, operation: nil) ⇒ Object

Deployments named after gpt-5.4+ models need the Responses API for tool use, so they route there automatically. Deployment names often differ from model ids, so the routing stays conservative; an explicit protocol: or the azure_protocol configuration option overrides it.



24
25
26
27
28
29
30
31
32
# File 'lib/ruby_llm/providers/azure.rb', line 24

def protocol_for(model, operation: nil, **)
  return protocols[:cohere] if operation == :rerank
  if operation == :embed &&
     %w[Cohere-embed-v3-english Cohere-embed-v3-multilingual embed-v-4-0].include?(model.id)
    return protocols[:cohere]
  end

  model.id.match?(/gpt-5\.[4-9]|gpt-5\d/) ? protocols[:responses] : super
end