-
Notifications
You must be signed in to change notification settings - Fork 5
Expose trace interface in scorers #99
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
Qard
wants to merge
3
commits into
main
Choose a base branch
from
trace-in-scorer
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
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "net/http" | ||
| require "json" | ||
| require "uri" | ||
| require_relative "../logger" | ||
|
|
||
| module Braintrust | ||
| class API | ||
| # BTQL API namespace | ||
| # Provides methods for querying spans and other data using BTQL | ||
| class BTQL | ||
| def initialize(api) | ||
| @api = api | ||
| @state = api.state | ||
| end | ||
|
|
||
| # Query spans using BTQL | ||
| # POST /btql | ||
| # @param query [Hash] AST-based query filter | ||
| # @param object_type [String] Type of object (e.g., "experiment") | ||
| # @param object_id [String] Object ID | ||
| # @param fmt [String] Response format (default: "jsonl") | ||
| # @return [Hash] Response with :body, :freshness_state | ||
| def query(query:, object_type:, object_id:, fmt: "jsonl") | ||
| payload = { | ||
| query: query, | ||
| object_type: object_type, | ||
| object_id: object_id, | ||
| fmt: fmt | ||
| } | ||
|
|
||
| response = http_post_json_raw("/btql", payload) | ||
|
|
||
| { | ||
| body: response.body, | ||
| freshness_state: response["x-bt-freshness-state"] || "complete" | ||
| } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Core HTTP request method (copied from datasets.rb pattern) | ||
| def http_request(method, path, params: {}, payload: nil, base_url: nil, parse_json: true) | ||
| base = base_url || @state.api_url | ||
| uri = URI("#{base}#{path}") | ||
| uri.query = URI.encode_www_form(params) unless params.empty? | ||
|
|
||
| request = case method | ||
| when :get | ||
| Net::HTTP::Get.new(uri) | ||
| when :post | ||
| req = Net::HTTP::Post.new(uri) | ||
| req["Content-Type"] = "application/json" | ||
| req.body = JSON.dump(payload) if payload | ||
| req | ||
| else | ||
| raise ArgumentError, "Unsupported HTTP method: #{method}" | ||
| end | ||
|
|
||
| request["Authorization"] = "Bearer #{@state.api_key}" | ||
|
|
||
| start_time = Time.now | ||
| Log.debug("[API] #{method.upcase} #{uri}") | ||
|
|
||
| http = Net::HTTP.new(uri.host, uri.port) | ||
| http.use_ssl = (uri.scheme == "https") | ||
| response = http.request(request) | ||
|
|
||
| duration_ms = ((Time.now - start_time) * 1000).round(2) | ||
| Log.debug("[API] #{method.upcase} #{uri} -> #{response.code} (#{duration_ms}ms, #{response.body.bytesize} bytes)") | ||
|
|
||
| unless response.is_a?(Net::HTTPSuccess) | ||
| Log.debug("[API] Error response body: #{response.body}") | ||
| raise Error, "HTTP #{response.code} for #{method.upcase} #{uri}: #{response.body}" | ||
| end | ||
|
|
||
| parse_json ? JSON.parse(response.body) : response | ||
| end | ||
|
|
||
| def http_post_json_raw(path, payload) | ||
| http_request(:post, path, payload: payload, parse_json: false) | ||
| end | ||
| end | ||
| 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
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Braintrust | ||
| module Eval | ||
| # Scoped context for a single evaluation run. | ||
| # Owns lifecycle of eval-specific resources. | ||
| # Created at eval start, disposed at eval end. | ||
| class Context | ||
| attr_reader :experiment_id | ||
|
|
||
| # @param experiment_id [String] The experiment ID | ||
| def initialize(experiment_id:) | ||
| @experiment_id = experiment_id | ||
| end | ||
|
|
||
| # Dispose of resources (for eager cleanup) | ||
| def dispose | ||
| # Currently no resources to dispose | ||
| # This method is kept for future extensibility | ||
| end | ||
| end | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| require_relative "result" | ||
| require_relative "summary" | ||
| require_relative "../internal/thread_pool" | ||
| require_relative "../trace_context" | ||
|
|
||
| require "opentelemetry/sdk" | ||
| require "json" | ||
|
|
@@ -18,7 +19,7 @@ class Runner | |
| MAX_PARALLELISM = Internal::ThreadPool::MAX_PARALLELISM | ||
|
|
||
| def initialize(experiment_id:, experiment_name:, project_id:, project_name:, | ||
| task:, scorers:, api:, tracer_provider: nil) | ||
| task:, scorers:, api:, tracer_provider: nil, eval_context: nil) | ||
| @experiment_id = experiment_id | ||
| @experiment_name = experiment_name | ||
| @project_id = project_id | ||
|
|
@@ -29,6 +30,7 @@ def initialize(experiment_id:, experiment_name:, project_id:, project_name:, | |
| @tracer_provider = tracer_provider || OpenTelemetry.tracer_provider | ||
| @tracer = @tracer_provider.tracer("braintrust-eval") | ||
| @parent_attr = "experiment_id:#{experiment_id}" | ||
| @eval_context = eval_context | ||
|
|
||
| # Mutex for thread-safe score collection | ||
| @score_mutex = Mutex.new | ||
|
|
@@ -103,8 +105,11 @@ def run_case(test_case, errors) | |
| end | ||
|
|
||
| # Run scorers | ||
| # Create TraceContext for scorers (if scorers exist) | ||
| trace = scorers.empty? ? nil : create_trace_context(eval_span) | ||
|
|
||
| begin | ||
| run_scorers(test_case, output) | ||
| run_scorers(test_case, output, trace) | ||
| rescue => e | ||
| # Error already recorded on score span, set eval span status | ||
| eval_span.status = OpenTelemetry::Trace::Status.error(e.message) | ||
|
|
@@ -149,15 +154,16 @@ def run_task(test_case) | |
| # Creates single score span for all scorers | ||
| # @param test_case [Case] The test case | ||
| # @param output [Object] Task output | ||
| def run_scorers(test_case, output) | ||
| # @param trace [TraceContext, nil] Optional trace context for scorers | ||
| def run_scorers(test_case, output, trace = nil) | ||
| tracer.in_span("score") do |score_span| | ||
| score_span.set_attribute("braintrust.parent", parent_attr) | ||
| set_json_attr(score_span, "braintrust.span_attributes", {type: "score"}) | ||
| set_json_attr(score_span, "braintrust.span_attributes", {type: "score", purpose: "scorer"}) | ||
|
|
||
| scores = {} | ||
| scorer_error = nil | ||
| scorers.each do |scorer| | ||
| score_value = scorer.call(test_case.input, test_case.expected, output, test_case.metadata || {}) | ||
| score_value = scorer.call(test_case.input, test_case.expected, output, test_case.metadata || {}, trace) | ||
| scores[scorer.name] = score_value | ||
|
|
||
| # Collect raw score for summary (thread-safe) | ||
|
|
@@ -239,6 +245,25 @@ def collect_score(name, value) | |
| (@scores[name] ||= []) << value | ||
| end | ||
| end | ||
|
|
||
| # Create a TraceContext for scorers to access span data | ||
| # @param eval_span [OpenTelemetry::Trace::Span] The eval span | ||
| # @return [TraceContext, nil] TraceContext if eval_context present, nil otherwise | ||
| def create_trace_context(eval_span) | ||
|
Collaborator
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. I don't think this belongs here: Eval::Runner concerns running evals, not building trace contexts. |
||
| # Skip if no eval_context (e.g., in tests) | ||
| return nil unless @eval_context | ||
|
|
||
| # Extract root_span_id from the eval span's trace_id | ||
| root_span_id = eval_span.context.trace_id.unpack1("H*") | ||
|
|
||
| TraceContext.new( | ||
| object_type: "experiment", | ||
| object_id: experiment_id, | ||
| root_span_id: root_span_id, | ||
| state: @api.state, | ||
| ensure_spans_flushed: -> { @tracer_provider.force_flush } | ||
| ) | ||
| end | ||
| end | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,8 @@ | |
| module Braintrust | ||
| module Eval | ||
| # Scorer wraps a scoring function that evaluates task output against expected values | ||
| # Scorers can accept 3 params (input, expected, output) or 4 params (input, expected, output, metadata) | ||
| # Scorers can accept 3 params (input, expected, output), 4 params (input, expected, output, metadata), | ||
| # or 5 params (input, expected, output, metadata, trace) | ||
| # They can return a float, hash, or array of hashes | ||
| class Scorer | ||
| attr_reader :name | ||
|
|
@@ -43,9 +44,10 @@ def initialize(name_or_callable = nil, callable = nil, &block) | |
| # @param expected [Object] The expected output | ||
| # @param output [Object] The actual output from the task | ||
| # @param metadata [Hash] Optional metadata | ||
| # @param trace [TraceContext, nil] Optional trace context | ||
| # @return [Float, Hash, Array] Score value(s) | ||
| def call(input, expected, output, metadata = {}) | ||
| @wrapped_callable.call(input, expected, output, metadata) | ||
| def call(input, expected, output, metadata = {}, trace = nil) | ||
|
Collaborator
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. Why does a scorer take a trace context? |
||
| @wrapped_callable.call(input, expected, output, metadata, trace) | ||
| end | ||
|
|
||
| private | ||
|
|
@@ -68,25 +70,31 @@ def detect_name(callable) | |
| "scorer" | ||
| end | ||
|
|
||
| # Wrap the callable to always accept 4 parameters | ||
| # Wrap the callable to always accept 5 parameters | ||
| # @param callable [#call] The callable to wrap | ||
| # @return [Proc] Wrapped callable that accepts 4 params | ||
| # @return [Proc] Wrapped callable that accepts 5 params | ||
| def wrap_callable(callable) | ||
| arity = callable_arity(callable) | ||
|
|
||
| case arity | ||
| when 3 | ||
| # Callable takes 3 params - wrap to ignore metadata | ||
| ->(input, expected, output, metadata) { | ||
| # Callable takes 3 params - wrap to ignore metadata and trace | ||
| ->(input, expected, output, metadata, trace) { | ||
| callable.call(input, expected, output) | ||
| } | ||
| when 4, -4, -1 | ||
| # Callable takes 4 params (or variadic with 4+) | ||
| when 4, -4 | ||
| # Callable takes 4 params - wrap to ignore trace | ||
| # -4 means optional 4th param | ||
| ->(input, expected, output, metadata, trace) { | ||
| callable.call(input, expected, output, metadata) | ||
| } | ||
| when 5, -5, -1 | ||
| # Callable takes 5 params (or variadic with 5+) | ||
| # -5 means optional 5th param | ||
| # -1 means variadic (*args) | ||
| callable | ||
| else | ||
| raise ArgumentError, "Scorer must accept 3 or 4 parameters (got arity #{arity})" | ||
| raise ArgumentError, "Scorer must accept 3, 4, or 5 parameters (got arity #{arity})" | ||
| end | ||
| end | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
When would scorers not exist? (I would think that if you run an Eval you would want to score it?)
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.
Might not be using local code scorers though, right? Could just be getting scored in our backend.