Class: Langfuse::Client
- Inherits:
-
Object
- Object
- Langfuse::Client
- Defined in:
- lib/langfuse/client.rb
Instance Attribute Summary collapse
-
#auto_flush ⇒ Object
readonly
Returns the value of attribute auto_flush.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#flush_interval ⇒ Object
readonly
Returns the value of attribute flush_interval.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#public_key ⇒ Object
readonly
Returns the value of attribute public_key.
-
#retries ⇒ Object
readonly
Returns the value of attribute retries.
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #create_prompt(name:, prompt:, labels: [], config: {}, **kwargs) ⇒ Object
-
#enqueue_event(type, body) ⇒ Object
Event queue management.
-
#event(trace_id:, name:, start_time: nil, input: nil, output: nil, metadata: nil, level: nil, status_message: nil, parent_observation_id: nil, version: nil, **kwargs) ⇒ Object
Event operations.
- #flush ⇒ Object
-
#generation(trace_id:, name: nil, start_time: nil, end_time: nil, completion_start_time: nil, model: nil, model_parameters: nil, input: nil, output: nil, usage: nil, metadata: nil, level: nil, status_message: nil, parent_observation_id: nil, version: nil, **kwargs) ⇒ Object
Generation operations.
-
#get_prompt(name, version: nil, label: nil, cache_ttl_seconds: 60) ⇒ Object
Prompt operations.
-
#initialize(public_key: nil, secret_key: nil, host: nil, debug: false, timeout: 30, retries: 3, flush_interval: nil, auto_flush: nil) ⇒ Client
constructor
A new instance of Client.
-
#score(name:, value:, trace_id: nil, observation_id: nil, data_type: nil, comment: nil, **kwargs) ⇒ Object
Score/Evaluation operations.
- #shutdown ⇒ Object
-
#span(trace_id:, name: nil, start_time: nil, end_time: nil, input: nil, output: nil, metadata: nil, level: nil, status_message: nil, parent_observation_id: nil, version: nil, **kwargs) ⇒ Object
Span operations.
-
#trace(id: nil, name: nil, user_id: nil, session_id: nil, version: nil, release: nil, input: nil, output: nil, metadata: nil, tags: nil, timestamp: nil, **kwargs) ⇒ Object
Trace operations.
Constructor Details
#initialize(public_key: nil, secret_key: nil, host: nil, debug: false, timeout: 30, retries: 3, flush_interval: nil, auto_flush: nil) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/langfuse/client.rb', line 11 def initialize(public_key: nil, secret_key: nil, host: nil, debug: false, timeout: 30, retries: 3, flush_interval: nil, auto_flush: nil) @public_key = public_key || ENV['LANGFUSE_PUBLIC_KEY'] || Langfuse.configuration.public_key @secret_key = secret_key || ENV['LANGFUSE_SECRET_KEY'] || Langfuse.configuration.secret_key @host = host || ENV['LANGFUSE_HOST'] || Langfuse.configuration.host @debug = debug || Langfuse.configuration.debug @timeout = timeout || Langfuse.configuration.timeout @retries = retries || Langfuse.configuration.retries @flush_interval = flush_interval || ENV['LANGFUSE_FLUSH_INTERVAL']&.to_i || Langfuse.configuration.flush_interval @auto_flush = if auto_flush.nil? ENV['LANGFUSE_AUTO_FLUSH'] == 'false' ? false : Langfuse.configuration.auto_flush else auto_flush end raise AuthenticationError, 'Public key is required' unless @public_key raise AuthenticationError, 'Secret key is required' unless @secret_key @connection = build_connection @event_queue = Concurrent::Array.new @flush_thread = start_flush_thread if @auto_flush end |
Instance Attribute Details
#auto_flush ⇒ Object (readonly)
Returns the value of attribute auto_flush.
9 10 11 |
# File 'lib/langfuse/client.rb', line 9 def auto_flush @auto_flush end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
9 10 11 |
# File 'lib/langfuse/client.rb', line 9 def debug @debug end |
#flush_interval ⇒ Object (readonly)
Returns the value of attribute flush_interval.
9 10 11 |
# File 'lib/langfuse/client.rb', line 9 def flush_interval @flush_interval end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
9 10 11 |
# File 'lib/langfuse/client.rb', line 9 def host @host end |
#public_key ⇒ Object (readonly)
Returns the value of attribute public_key.
9 10 11 |
# File 'lib/langfuse/client.rb', line 9 def public_key @public_key end |
#retries ⇒ Object (readonly)
Returns the value of attribute retries.
9 10 11 |
# File 'lib/langfuse/client.rb', line 9 def retries @retries end |
#secret_key ⇒ Object (readonly)
Returns the value of attribute secret_key.
9 10 11 |
# File 'lib/langfuse/client.rb', line 9 def secret_key @secret_key end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
9 10 11 |
# File 'lib/langfuse/client.rb', line 9 def timeout @timeout end |
Instance Method Details
#create_prompt(name:, prompt:, labels: [], config: {}, **kwargs) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/langfuse/client.rb', line 158 def create_prompt(name:, prompt:, labels: [], config: {}, **kwargs) data = { name: name, prompt: prompt, labels: labels, config: config, **kwargs } response = post('/api/public/v2/prompts', data) Prompt.new(response.body) end |
#enqueue_event(type, body) ⇒ Object
Event queue management
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/langfuse/client.rb', line 188 def enqueue_event(type, body) # 验证事件类型是否有效 valid_types = %w[ trace-create trace-update generation-create generation-update span-create span-update event-create score-create ] unless valid_types.include?(type) puts "Warning: Invalid event type '#{type}'. Skipping event." if @debug return end event = { id: Utils.generate_id, type: type, timestamp: Utils., body: Utils.deep_stringify_keys(body) } if type == 'trace-update' # 查找对应的 trace-create 事件并更新 trace_id = body['id'] || body[:id] if trace_id existing_event_index = @event_queue.find_index do |existing_event| existing_event[:type] == 'trace-create' && (existing_event[:body]['id'] == trace_id || existing_event[:body][:id] == trace_id) end if existing_event_index # 更新现有的 trace-create 事件 @event_queue[existing_event_index][:body].merge!(event[:body]) @event_queue[existing_event_index][:timestamp] = event[:timestamp] puts "Updated existing trace-create event for trace_id: #{trace_id}" if @debug else # 如果没找到对应的 trace-create 事件,将 trace-update 转换为 trace-create event[:type] = 'trace-create' @event_queue << event puts "Converted trace-update to trace-create for trace_id: #{trace_id}" if @debug end elsif @debug puts 'Warning: trace-update event missing trace_id, skipping' end else @event_queue << event end puts "Enqueued event: #{type}" if @debug end |
#event(trace_id:, name:, start_time: nil, input: nil, output: nil, metadata: nil, level: nil, status_message: nil, parent_observation_id: nil, version: nil, **kwargs) ⇒ Object
Event operations
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/langfuse/client.rb', line 102 def event(trace_id:, name:, start_time: nil, input: nil, output: nil, metadata: nil, level: nil, status_message: nil, parent_observation_id: nil, version: nil, **kwargs) Event.new( client: self, trace_id: trace_id, name: name, start_time: start_time, input: input, output: output, metadata: , level: level, status_message: , parent_observation_id: parent_observation_id, version: version, **kwargs ) end |
#flush ⇒ Object
239 240 241 242 243 244 245 246 |
# File 'lib/langfuse/client.rb', line 239 def flush return if @event_queue.empty? events = @event_queue.shift(@event_queue.length) return if events.empty? send_batch(events) end |
#generation(trace_id:, name: nil, start_time: nil, end_time: nil, completion_start_time: nil, model: nil, model_parameters: nil, input: nil, output: nil, usage: nil, metadata: nil, level: nil, status_message: nil, parent_observation_id: nil, version: nil, **kwargs) ⇒ Object
Generation operations
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/langfuse/client.rb', line 76 def generation(trace_id:, name: nil, start_time: nil, end_time: nil, completion_start_time: nil, model: nil, model_parameters: nil, input: nil, output: nil, usage: nil, metadata: nil, level: nil, status_message: nil, parent_observation_id: nil, version: nil, **kwargs) Generation.new( client: self, trace_id: trace_id, name: name, start_time: start_time || Utils., end_time: end_time, completion_start_time: completion_start_time, model: model, model_parameters: model_parameters, input: input, output: output, usage: usage, metadata: , level: level, status_message: , parent_observation_id: parent_observation_id, version: version, **kwargs ) end |
#get_prompt(name, version: nil, label: nil, cache_ttl_seconds: 60) ⇒ Object
Prompt operations
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/langfuse/client.rb', line 121 def get_prompt(name, version: nil, label: nil, cache_ttl_seconds: 60) cache_key = "prompt:#{name}:#{version}:#{label}" if (cached_prompt = @prompt_cache&.dig(cache_key)) && (Time.now - cached_prompt[:cached_at] < cache_ttl_seconds) return cached_prompt[:prompt] end path = "/api/public/v2/prompts/#{name}" params = {} params[:version] = version if version params[:label] = label if label puts "Making request to: #{@host}#{path} with params: #{params}" if @debug response = get(path, params) puts "Response status: #{response.status}" if @debug puts "Response headers: #{response.headers}" if @debug puts "Response body type: #{response.body.class}" if @debug # Check if response body is a string (HTML) instead of parsed JSON if response.body.is_a?(String) && response.body.include?('<!DOCTYPE html>') puts 'Received HTML response instead of JSON:' if @debug puts response.body[0..200] if @debug raise APIError, 'Received HTML response instead of JSON. This usually indicates a 404 error or incorrect API endpoint.' end prompt = Prompt.new(response.body) # Cache the prompt @prompt_cache ||= {} @prompt_cache[cache_key] = { prompt: prompt, cached_at: Time.now } prompt end |
#score(name:, value:, trace_id: nil, observation_id: nil, data_type: nil, comment: nil, **kwargs) ⇒ Object
Score/Evaluation operations
172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/langfuse/client.rb', line 172 def score(name:, value:, trace_id: nil, observation_id: nil, data_type: nil, comment: nil, **kwargs) data = { name: name, value: value, data_type: data_type, comment: comment, **kwargs } data[:trace_id] = trace_id if trace_id data[:observation_id] = observation_id if observation_id enqueue_event('score-create', data) end |
#shutdown ⇒ Object
248 249 250 251 |
# File 'lib/langfuse/client.rb', line 248 def shutdown @flush_thread&.kill if @auto_flush flush unless @event_queue.empty? end |
#span(trace_id:, name: nil, start_time: nil, end_time: nil, input: nil, output: nil, metadata: nil, level: nil, status_message: nil, parent_observation_id: nil, version: nil, **kwargs) ⇒ Object
Span operations
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/langfuse/client.rb', line 55 def span(trace_id:, name: nil, start_time: nil, end_time: nil, input: nil, output: nil, metadata: nil, level: nil, status_message: nil, parent_observation_id: nil, version: nil, **kwargs) Span.new( client: self, trace_id: trace_id, name: name, start_time: start_time || Utils., end_time: end_time, input: input, output: output, metadata: , level: level, status_message: , parent_observation_id: parent_observation_id, version: version, **kwargs ) end |
#trace(id: nil, name: nil, user_id: nil, session_id: nil, version: nil, release: nil, input: nil, output: nil, metadata: nil, tags: nil, timestamp: nil, **kwargs) ⇒ Object
Trace operations
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/langfuse/client.rb', line 35 def trace(id: nil, name: nil, user_id: nil, session_id: nil, version: nil, release: nil, input: nil, output: nil, metadata: nil, tags: nil, timestamp: nil, **kwargs) Trace.new( client: self, id: id || Utils.generate_id, name: name, user_id: user_id, session_id: session_id, version: version, release: release, input: input, output: output, metadata: , tags: , timestamp: || Utils., **kwargs ) end |