Skip to content

Commit 2d7de5a

Browse files
committed
Add Azure OpenAI provider
1 parent a35aa11 commit 2d7de5a

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

lib/ruby_llm/configuration.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class Configuration
2424
:bedrock_session_token,
2525
:openrouter_api_key,
2626
:ollama_api_base,
27+
:azure_openai_api_base,
28+
:azure_openai_api_version,
29+
:azure_openai_api_key,
2730
# Default models
2831
:default_model,
2932
:default_embedding_model,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
module RubyLLM
4+
module Providers
5+
# OpenAI API integration. Handles chat completion, function calling,
6+
# and OpenAI's unique streaming format. Supports GPT-4, GPT-3.5,
7+
# and other OpenAI models.
8+
module AzureOpenAI
9+
extend OpenAI
10+
extend AzureOpenAI::Chat
11+
12+
module_function
13+
14+
def api_base(config)
15+
# https://<ENDPOINT>/openai/deployments/<MODEL>/chat/completions?api-version=<APIVERSION>
16+
"#{config.azure_openai_api_base}/openai/deployments"
17+
end
18+
19+
def headers(config)
20+
{
21+
'Authorization' => "Bearer #{config.azure_openai_api_key}"
22+
}.compact
23+
end
24+
25+
def capabilities
26+
OpenAI::Capabilities
27+
end
28+
29+
def slug
30+
'azure_openai'
31+
end
32+
33+
def configuration_requirements
34+
%i[azure_openai_api_key azure_openai_api_base azure_openai_api_version]
35+
end
36+
37+
def local?
38+
false
39+
end
40+
end
41+
end
42+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
module RubyLLM
4+
module Providers
5+
module AzureOpenAI
6+
# Chat methods of the Ollama API integration
7+
module Chat
8+
extend OpenAI::Chat
9+
10+
module_function
11+
12+
def sync_response(connection, payload)
13+
# Hold config in instance variable for use in completion_url and stream_url
14+
@config = connection.config
15+
super
16+
end
17+
18+
def completion_url
19+
# https://<ENDPOINT>/openai/deployments/<MODEL>/chat/completions?api-version=<APIVERSION>
20+
"#{@model_id}/chat/completions?api-version=#{@config.azure_openai_api_version}"
21+
end
22+
23+
def render_payload(messages, tools:, temperature:, model:, stream: false)
24+
# Hold model_id in instance variable for use in completion_url and stream_url
25+
@model_id = model
26+
super
27+
end
28+
end
29+
end
30+
end
31+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module RubyLLM
4+
module Providers
5+
module AzureOpenAI
6+
# Streaming methods of the OpenAI API integration
7+
module Streaming
8+
extend OpenAI::Streaming
9+
10+
module_function
11+
12+
def stream_response(connection, payload, &block)
13+
# Hold config in instance variable for use in completion_url and stream_url
14+
@config = connection.config
15+
super
16+
end
17+
end
18+
end
19+
end
20+
end

0 commit comments

Comments
 (0)