-
Notifications
You must be signed in to change notification settings - Fork 5
Adding RubyLLM Support #18
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
Open
aniamisiorek
wants to merge
6
commits into
traceloop:main
Choose a base branch
from
aniamisiorek:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
14bb1e6
adding gen_ai
aniamisiorek 987d252
adding halting
aniamisiorek 6b2bc5f
coderabbit comments
aniamisiorek 3e0b4c5
adding conversation id
aniamisiorek da3f2b4
adding conversation id -- fix
aniamisiorek 6509a5d
guarding rubyLLM usage
aniamisiorek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,16 +6,21 @@ module Traceloop | |
| module SDK | ||
| class Traceloop | ||
| def initialize | ||
| api_key = ENV["TRACELOOP_API_KEY"] | ||
| raise "TRACELOOP_API_KEY environment variable is required" if api_key.nil? || api_key.empty? | ||
|
|
||
| OpenTelemetry::SDK.configure do |c| | ||
| c.add_span_processor( | ||
| OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new( | ||
| OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new( | ||
| OpenTelemetry::Exporter::OTLP::Exporter.new( | ||
| endpoint: "#{ENV.fetch("TRACELOOP_BASE_URL", "https://api.traceloop.com")}/v1/traces", | ||
| headers: { "Authorization" => "Bearer #{ENV.fetch("TRACELOOP_API_KEY")}" } | ||
| headers: { | ||
| "Authorization" => "#{ENV.fetch("TRACELOOP_AUTH_SCHEME", "Bearer")} #{ENV.fetch("TRACELOOP_API_KEY")}" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is that?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. authorization headers may vary in type. i.e. for our dynatrace exporter, they use Api-Token. |
||
| } | ||
| ) | ||
| ) | ||
| ) | ||
| puts "Traceloop exporting traces to #{ENV.fetch("TRACELOOP_BASE", "https://api.traceloop.com")}" | ||
| puts "Traceloop exporting traces to #{ENV.fetch("TRACELOOP_BASE_URL", "https://api.traceloop.com")}" | ||
| end | ||
|
|
||
| @tracer = OpenTelemetry.tracer_provider.tracer("Traceloop") | ||
|
|
@@ -41,25 +46,30 @@ def log_messages(messages) | |
| def log_prompt(system_prompt="", user_prompt) | ||
| unless system_prompt.empty? | ||
| @span.add_attributes({ | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.0.role" => "system", | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.0.content" => system_prompt, | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.1.role" => "user", | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.1.content" => user_prompt | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_PROMPTS}.0.role" => "system", | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_PROMPTS}.0.content" => system_prompt, | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_PROMPTS}.1.role" => "user", | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_PROMPTS}.1.content" => user_prompt | ||
| }) | ||
| else | ||
| @span.add_attributes({ | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.0.role" => "user", | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.0.content" => user_prompt | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_PROMPTS}.0.role" => "user", | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_PROMPTS}.0.content" => user_prompt | ||
| }) | ||
| end | ||
| end | ||
|
|
||
| def log_response(response) | ||
| if response.respond_to?(:body) | ||
| log_bedrock_response(response) | ||
| # Check for RubyLLM::Message objects | ||
| elsif defined?(::RubyLLM::Message) && response.is_a?(::RubyLLM::Message) | ||
| log_ruby_llm_message(response) | ||
| elsif defined?(::RubyLLM::Tool::Halt) && response.is_a?(::RubyLLM::Tool::Halt) | ||
| log_ruby_llm_halt(response) | ||
| # This is Gemini specific, see - | ||
| # https://github.com/gbaptista/gemini-ai?tab=readme-ov-file#generate_content | ||
| elsif response.has_key?("candidates") | ||
| elsif response.respond_to?(:has_key?) && response.has_key?("candidates") | ||
| log_gemini_response(response) | ||
| else | ||
| log_openai_response(response) | ||
|
|
@@ -73,10 +83,29 @@ def log_gemini_response(response) | |
|
|
||
| @span.add_attributes({ | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_COMPLETIONS}.0.role" => "assistant", | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_COMPLETIONS}.0.content" => response.dig("candidates", 0, "content", "parts", 0, "text") | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_COMPLETIONS}.0.content" => response.dig( | ||
| "candidates", 0, "content", "parts", 0, "text") | ||
| }) | ||
| end | ||
|
|
||
| def log_ruby_llm_message(response) | ||
| @span.add_attributes({ | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_RESPONSE_MODEL => response.model_id, | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_USAGE_OUTPUT_TOKENS => response.output_tokens || 0, | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_USAGE_INPUT_TOKENS => response.input_tokens || 0, | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_COMPLETIONS}.0.role" => response.role.to_s, | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_COMPLETIONS}.0.content" => response.content | ||
| }) | ||
| end | ||
|
|
||
| def log_ruby_llm_halt(response) | ||
| @span.add_attributes({ | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_RESPONSE_MODEL => @model, | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_COMPLETIONS}.0.role" => "tool", | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_COMPLETIONS}.0.content" => response.content | ||
| }) | ||
| end | ||
|
|
||
| def log_bedrock_response(response) | ||
| body = JSON.parse(response.body.read()) | ||
|
|
||
|
|
@@ -109,25 +138,38 @@ def log_openai_response(response) | |
| }) | ||
| if response.has_key?("usage") | ||
| @span.add_attributes({ | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_USAGE_TOTAL_TOKENS => response.dig("usage", "total_tokens"), | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_USAGE_COMPLETION_TOKENS => response.dig("usage", "completion_tokens"), | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_USAGE_PROMPT_TOKENS => response.dig("usage", "prompt_tokens"), | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_USAGE_TOTAL_TOKENS => response.dig("usage", | ||
| "total_tokens"), | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_USAGE_COMPLETION_TOKENS => response.dig( | ||
| "usage", "completion_tokens"), | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_USAGE_PROMPT_TOKENS => response.dig("usage", | ||
| "prompt_tokens"), | ||
| }) | ||
| end | ||
| if response.has_key?("choices") | ||
| @span.add_attributes({ | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_COMPLETIONS}.0.role" => response.dig("choices", 0, "message", "role"), | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_COMPLETIONS}.0.content" => response.dig("choices", 0, "message", "content") | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_COMPLETIONS}.0.role" => response.dig( | ||
| "choices", 0, "message", "role"), | ||
| "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_COMPLETIONS}.0.content" => response.dig( | ||
| "choices", 0, "message", "content") | ||
| }) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def llm_call(provider, model) | ||
| def llm_call(provider, model, conversation_id: nil) | ||
| @tracer.in_span("#{provider}.chat") do |span| | ||
| span.add_attributes({ | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_REQUEST_MODEL => model, | ||
| }) | ||
| attributes = { | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_REQUEST_MODEL => model, | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_SYSTEM => provider, | ||
| OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_PROVIDER => provider, | ||
| } | ||
|
|
||
| if conversation_id | ||
| attributes[OpenTelemetry::SemanticConventionsAi::SpanAttributes::GEN_AI_CONVERSATION_ID] = conversation_id | ||
| end | ||
|
|
||
| span.add_attributes(attributes) | ||
| yield Tracer.new(span, provider, model) | ||
| end | ||
| end | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just replace the existing ones
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and remove references to llm semantics?