Repository navigation
Expand file tree
/
Copy pathbasic.rb
More file actions
49 lines (39 loc) · 1.37 KB
/
Copy pathbasic.rb
File metadata and controls
49 lines (39 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/setup"
require "braintrust"
require "ruby_llm"
require "opentelemetry/sdk"
# Example: Basic RubyLLM chat with Braintrust tracing
#
# Usage:
# OPENAI_API_KEY=your-key bundle exec appraisal ruby_llm ruby examples/contrib/ruby_llm/basic.rb
unless ENV["OPENAI_API_KEY"]
puts "Error: OPENAI_API_KEY environment variable is required"
exit 1
end
# Initialize Braintrust (with blocking login)
#
# NOTE: blocking_login is only necessary for this short-lived example.
# In most production apps, you can omit this.
Braintrust.init(blocking_login: true)
RubyLLM.configure do |config|
config.openai_api_key = ENV["OPENAI_API_KEY"]
end
# Get a tracer and wrap the API call in a span
tracer = OpenTelemetry.tracer_provider.tracer("ruby-llm-example")
root_span = nil
tracer.in_span("examples/contrib/ruby_llm/basic.rb") do |span|
root_span = span
# Make a chat request (automatically traced!)
chat = RubyLLM.chat(model: "gpt-4o-mini")
chat.ask("What is the capital of France?")
end
# Print permalink to view this trace in Braintrust
puts "\nView this trace in Braintrust:"
puts " #{Braintrust::Trace.permalink(root_span)}"
# Shutdown to flush spans to Braintrust
#
# NOTE: shutdown is only necessary for this short-lived example.
# In most production apps, you can omit this.
OpenTelemetry.tracer_provider.shutdown