Skip to content
2 changes: 2 additions & 0 deletions lib/ruby_llm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'ruby_llm' => 'RubyLLM',
'llm' => 'LLM',
'openai' => 'OpenAI',
'azure_openai' => 'AzureOpenAI',
'api' => 'API',
'deepseek' => 'DeepSeek',
'perplexity' => 'Perplexity',
Expand Down Expand Up @@ -100,6 +101,7 @@ def logger
RubyLLM::Provider.register :openrouter, RubyLLM::Providers::OpenRouter
RubyLLM::Provider.register :perplexity, RubyLLM::Providers::Perplexity
RubyLLM::Provider.register :vertexai, RubyLLM::Providers::VertexAI
RubyLLM::Provider.register :azure_openai, RubyLLM::Providers::AzureOpenAI

if defined?(Rails::Railtie)
require 'ruby_llm/railtie'
Expand Down
4 changes: 4 additions & 0 deletions lib/ruby_llm/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class Configuration
:gpustack_api_base,
:gpustack_api_key,
:mistral_api_key,
# Azure OpenAI Provider configuration
:azure_openai_api_base,
:azure_openai_api_version,
:azure_openai_api_key,
# Default models
:default_model,
:default_embedding_model,
Expand Down
48 changes: 48 additions & 0 deletions lib/ruby_llm/providers/azure_openai.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module RubyLLM
module Providers
# Azure OpenAI API integration. Derived from OpenAI integration to support
# OpenAI capabilities via Microsoft Azure endpoints.
class AzureOpenAI < Provider
# Include OpenAI modules first for base functionality
include OpenAI::Chat
include OpenAI::Embeddings
include OpenAI::Tools
include OpenAI::Media
include OpenAI::Streaming
include OpenAI::Models

# Then include Azure overrides
include AzureOpenAI::Chat
include AzureOpenAI::Embeddings
include AzureOpenAI::Streaming
include AzureOpenAI::Models

def api_base
# https://<ENDPOINT>/openai/deployments/<MODEL>/chat/completions?api-version=<APIVERSION>
"#{@config.azure_openai_api_base}/openai"
end

def headers
{
'Authorization' => "Bearer #{@config.azure_openai_api_key}"
}.compact
end

class << self
def capabilities
OpenAI::Capabilities
end

def slug
'azure_openai'
end

def configuration_requirements
%i[azure_openai_api_key azure_openai_api_base azure_openai_api_version]
end
end
end
end
end
21 changes: 21 additions & 0 deletions lib/ruby_llm/providers/azure_openai/chat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module RubyLLM
module Providers
class AzureOpenAI
# Chat methods of the Azure OpenAI API integration
module Chat
def completion_url
# https://<ENDPOINT>/openai/deployments/<MODEL>/chat/completions?api-version=<APIVERSION>
"deployments/#{@model_id}/chat/completions?api-version=#{@config.azure_openai_api_version}"
end

def render_payload(messages, tools:, temperature:, model:, stream: false, schema: nil)
# Hold model_id in instance variable for use in completion_url and stream_url
@model_id = model.id
super
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/ruby_llm/providers/azure_openai/embeddings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module RubyLLM
module Providers
class AzureOpenAI
# Embeddings methods for the Azure OpenAI API integration
module Embeddings
def embedding_url(model:)
"deployments/#{model}/embeddings?api-version=#{@config.azure_openai_api_version}"
end
end
end
end
end
49 changes: 49 additions & 0 deletions lib/ruby_llm/providers/azure_openai/models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module RubyLLM
module Providers
class AzureOpenAI
# Models methods of the Azure OpenAI API integration
module Models
KNOWN_MODELS = [
# Chat models
'gpt-4o',
'gpt-4o-mini',
'gpt-4.1',
'gpt-4.1-mini',
'gpt-4.1-nano',
'gpt-4',
# Reasoning models (o-series)
'o1',
'o1-mini',
'o3',
'o3-mini',
'o3-pro',
'o4-mini',
# Embedding models
'text-embedding-3-large',
'text-embedding-3-small',
# Image generation
'dall-e-3',
'gpt-image-1',
'gpt-image-1-mini'
].freeze

def models_url
'models?api-version=2024-10-21'
end

def parse_list_models_response(response, slug, capabilities)
# select the known models only since this list from Azure OpenAI is
# very long
response.body['data'].select! do |m|
KNOWN_MODELS.include?(m['id'])
end
# Use the OpenAI processor for the list, keeping in mind that pricing etc
# won't be correct
super
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/ruby_llm/providers/azure_openai/streaming.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module RubyLLM
module Providers
class AzureOpenAI
# Streaming methods of the Azure OpenAI API integration
module Streaming
# Azure OpenAI uses the same streaming implementation as OpenAI.
# The completion_url override in AzureOpenAI::Chat handles the
# different endpoint format for Azure deployments.
end
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading