Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embeddings tests #2481

Merged
merged 3 commits into from
Mar 4, 2024
Merged
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
25 changes: 25 additions & 0 deletions test/multiverse/suites/ruby_openai/openai_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ def error_response(return_value: false)
end
end

class EmbeddingsResponse
def body(return_value: false)
{'object' => 'list',
'data' => [{
'object' => 'embedding',
'index' => 0,
'embedding' => [0.002297497, 1, -0.016932933, 0.018126108, -0.014432343, -0.0030051514] # A real embeddings response includes dozens more vector points.
}],
'model' => 'text-embedding-ada-002',
'usage' => {'prompt_tokens' => 8, 'total_tokens' => 8}}
end
end

def client
@client ||= OpenAI::Client.new(access_token: 'FAKE_ACCESS_TOKEN')
end
Expand Down Expand Up @@ -181,4 +194,16 @@ def stub_error_post_request(&blk)
end
end
end

def stub_embeddings_post_request(&blk)
if Gem::Version.new(::OpenAI::VERSION) <= Gem::Version.new('3.4.0')
HTTParty.stub(:post, EmbeddingsResponse.new.body(return_value: true)) do
yield
end
else
connection_client.stub(:conn, faraday_connection) do
yield
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def test_conversation_id_added_to_message_events

def test_openai_embedding_segment_name
txn = in_transaction do
stub_post_request do
stub_embeddings_post_request do
client.embeddings(parameters: embeddings_params)
end
end
Expand All @@ -193,7 +193,7 @@ def test_openai_embedding_segment_name

def test_embedding_has_duration_of_segment
txn = in_transaction do
stub_post_request do
stub_embeddings_post_request do
client.embeddings(parameters: embeddings_params)
end
end
Expand All @@ -205,7 +205,7 @@ def test_embedding_has_duration_of_segment

def test_openai_metric_recorded_for_embeddings_every_time
in_transaction do
stub_post_request do
stub_embeddings_post_request do
client.embeddings(parameters: embeddings_params)
client.embeddings(parameters: embeddings_params)
end
Expand All @@ -226,7 +226,7 @@ def test_embedding_event_sets_error_true_if_raised

def test_set_llm_agent_attribute_on_embedding_transaction
in_transaction do |txn|
stub_post_request do
stub_embeddings_post_request do
client.embeddings(parameters: embeddings_params)
end
end
Expand Down
Loading