Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/active_agent/providers/azure_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ def client
)
end

protected

# Executes an embedding request against the Azure deployment's
# +/embeddings+ endpoint.
#
# @param parameters [Hash]
# @return [Hash] symbolized API response
def api_embed_execute(parameters)
client.embeddings.create(**parameters).as_json.deep_symbolize_keys
end

# Custom OpenAI client for Azure OpenAI Service.
#
# Azure uses different authentication headers (api-key instead of Authorization: Bearer)
Expand Down
60 changes: 60 additions & 0 deletions test/providers/azure/embed_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require "test_helper"

begin
require "openai"
rescue LoadError
puts "OpenAI gem not available, skipping Azure embed tests"
return
end

require_relative "../../../lib/active_agent/providers/azure_provider"

module Providers
module Azure
class EmbedTest < ActiveSupport::TestCase
include WebMock::API

EMBEDDINGS_ENDPOINT = "https://mycompany.openai.azure.com/openai/deployments/embed-deployment/embeddings"

test "sends an embedding request to the Azure deployment embeddings endpoint" do
stub_request(:post, EMBEDDINGS_ENDPOINT)
.with(query: { "api-version" => "2024-10-21" })
.to_return(
status: 200,
headers: { "Content-Type" => "application/json" },
body: embedding_response.to_json
)

response = ActiveAgent::Providers::AzureProvider.new(
service: "AzureOpenAI",
model: "text-embedding-3-small",
input: "hello",
api_key: "azure-test",
azure_resource: "mycompany",
deployment_id: "embed-deployment",
api_version: "2024-10-21"
).embed

assert_equal [ 0.1, 0.2 ], response.data.first[:embedding]
assert_requested(
:post, EMBEDDINGS_ENDPOINT,
query: { "api-version" => "2024-10-21" },
headers: { "api-key" => "azure-test" }
)
end

private

def embedding_response
{
object: "list",
model: "text-embedding-3-small",
data: [ { object: "embedding", index: 0, embedding: [ 0.1, 0.2 ] } ],
usage: { prompt_tokens: 2, total_tokens: 2 }
}
end
end
end
end
Loading