From 5f180688365921d784acf59a708e6d22904b7ed7 Mon Sep 17 00:00:00 2001 From: Hannah Ramadan Date: Mon, 4 Mar 2024 09:39:44 -0800 Subject: [PATCH 1/3] Add to embeddings tests --- .../suites/ruby_openai/openai_helpers.rb | 25 +++++++++++++++++++ .../ruby_openai_instrumentation_test.rb | 16 +++--------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/test/multiverse/suites/ruby_openai/openai_helpers.rb b/test/multiverse/suites/ruby_openai/openai_helpers.rb index 75375168c4..9967889412 100644 --- a/test/multiverse/suites/ruby_openai/openai_helpers.rb +++ b/test/multiverse/suites/ruby_openai/openai_helpers.rb @@ -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 + def client @client ||= OpenAI::Client.new(access_token: 'FAKE_ACCESS_TOKEN') end @@ -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 diff --git a/test/multiverse/suites/ruby_openai/ruby_openai_instrumentation_test.rb b/test/multiverse/suites/ruby_openai/ruby_openai_instrumentation_test.rb index 5dec72f4a0..d05a752fb2 100644 --- a/test/multiverse/suites/ruby_openai/ruby_openai_instrumentation_test.rb +++ b/test/multiverse/suites/ruby_openai/ruby_openai_instrumentation_test.rb @@ -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 @@ -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 @@ -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 @@ -223,14 +223,4 @@ def test_embedding_event_sets_error_true_if_raised refute_nil segment.llm_event assert_truthy segment.llm_event.error end - - def test_set_llm_agent_attribute_on_embedding_transaction - in_transaction do |txn| - stub_post_request do - client.embeddings(parameters: embeddings_params) - end - end - - assert_truthy harvest_transaction_events![1][0][2][:llm] - end end From 4e6f9b7df7aecf1413300cb72acfe79837f2a7b3 Mon Sep 17 00:00:00 2001 From: Hannah Ramadan Date: Mon, 4 Mar 2024 10:11:57 -0800 Subject: [PATCH 2/3] Rubocop --- .../suites/ruby_openai/openai_helpers.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/multiverse/suites/ruby_openai/openai_helpers.rb b/test/multiverse/suites/ruby_openai/openai_helpers.rb index 9967889412..8bd0e3643b 100644 --- a/test/multiverse/suites/ruby_openai/openai_helpers.rb +++ b/test/multiverse/suites/ruby_openai/openai_helpers.rb @@ -34,15 +34,15 @@ def error_response(return_value: false) 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} - } + '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') From 8b86c5933b03428c3cb5f1f7933072b49c28215d Mon Sep 17 00:00:00 2001 From: Hannah Ramadan Date: Mon, 4 Mar 2024 15:15:14 -0800 Subject: [PATCH 3/3] llm on embedding txn --- .../ruby_openai/ruby_openai_instrumentation_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/multiverse/suites/ruby_openai/ruby_openai_instrumentation_test.rb b/test/multiverse/suites/ruby_openai/ruby_openai_instrumentation_test.rb index d05a752fb2..0dff89aaf9 100644 --- a/test/multiverse/suites/ruby_openai/ruby_openai_instrumentation_test.rb +++ b/test/multiverse/suites/ruby_openai/ruby_openai_instrumentation_test.rb @@ -223,4 +223,14 @@ def test_embedding_event_sets_error_true_if_raised refute_nil segment.llm_event assert_truthy segment.llm_event.error end + + def test_set_llm_agent_attribute_on_embedding_transaction + in_transaction do |txn| + stub_embeddings_post_request do + client.embeddings(parameters: embeddings_params) + end + end + + assert_truthy harvest_transaction_events![1][0][2][:llm] + end end