Class: Langfuse::Client
- Inherits:
-
Object
- Object
- Langfuse::Client
- Defined in:
- lib/langfuse/client.rb
Overview
Main client for Langfuse SDK
Provides a unified interface for interacting with the Langfuse API. Handles prompt fetching and returns the appropriate prompt client (TextPromptClient or ChatPromptClient) based on the prompt type.
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#api_client ⇒ Object
readonly
Returns the value of attribute api_client.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#compile_prompt(name, variables: {}, version: nil, label: nil, fallback: nil, type: nil) ⇒ String, Array<Hash>
Convenience method: fetch and compile a prompt in one call.
-
#create_prompt(name:, prompt:, type:, config: {}, labels: [], tags: [], commit_message: nil) ⇒ TextPromptClient, ChatPromptClient
Create a new prompt (or new version if name already exists).
-
#create_score(name:, value:, trace_id: nil, observation_id: nil, comment: nil, metadata: nil, data_type: :numeric) ⇒ void
Create a score event and queue it for batching.
-
#flush_scores ⇒ void
Force flush all queued score events.
-
#get_prompt(name, version: nil, label: nil, fallback: nil, type: nil) ⇒ TextPromptClient, ChatPromptClient
Fetch a prompt and return the appropriate client.
-
#initialize(config) ⇒ Client
constructor
Initialize a new Langfuse client.
-
#list_prompts(page: nil, limit: nil) ⇒ Array<Hash>
List all prompts in the Langfuse project.
-
#score_active_observation(name:, value:, comment: nil, metadata: nil, data_type: :numeric) ⇒ void
Create a score for the currently active observation (from OTel span).
-
#score_active_trace(name:, value:, comment: nil, metadata: nil, data_type: :numeric) ⇒ void
Create a score for the currently active trace (from OTel span).
-
#shutdown ⇒ void
Shutdown the client and flush any pending scores.
-
#trace_url(trace_id) ⇒ String
Generate URL for viewing a trace in Langfuse UI.
-
#update_prompt(name:, version:, labels:) ⇒ TextPromptClient, ChatPromptClient
Update an existing prompt version’s metadata.
Constructor Details
#initialize(config) ⇒ Client
Initialize a new Langfuse client
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/langfuse/client.rb', line 27 def initialize(config) @config = config @config.validate! # Create cache if enabled cache = create_cache if cache_enabled? # Create API client with cache @api_client = ApiClient.new( public_key: config.public_key, secret_key: config.secret_key, base_url: config.base_url, timeout: config.timeout, logger: config.logger, cache: cache ) # Initialize score client for batching score events @score_client = ScoreClient.new(api_client: @api_client, config: config) end |
Instance Attribute Details
#api_client ⇒ Object (readonly)
Returns the value of attribute api_client.
22 23 24 |
# File 'lib/langfuse/client.rb', line 22 def api_client @api_client end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
22 23 24 |
# File 'lib/langfuse/client.rb', line 22 def config @config end |
Instance Method Details
#compile_prompt(name, variables: {}, version: nil, label: nil, fallback: nil, type: nil) ⇒ String, Array<Hash>
Convenience method: fetch and compile a prompt in one call
This is a shorthand for calling get_prompt followed by compile. Returns the compiled prompt ready to use with your LLM.
138 139 140 141 |
# File 'lib/langfuse/client.rb', line 138 def compile_prompt(name, variables: {}, version: nil, label: nil, fallback: nil, type: nil) prompt = get_prompt(name, version: version, label: label, fallback: fallback, type: type) prompt.compile(**variables) end |
#create_prompt(name:, prompt:, type:, config: {}, labels: [], tags: [], commit_message: nil) ⇒ TextPromptClient, ChatPromptClient
Create a new prompt (or new version if name already exists)
Creates a new prompt in Langfuse. If a prompt with the same name already exists, this creates a new version of that prompt.
rubocop:disable Metrics/ParameterLists
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/langfuse/client.rb', line 182 def create_prompt(name:, prompt:, type:, config: {}, labels: [], tags: [], commit_message: nil) validate_prompt_type!(type) validate_prompt_content!(prompt, type) prompt_data = api_client.create_prompt( name: name, prompt: normalize_prompt_content(prompt, type), type: type.to_s, config: config, labels: labels, tags: , commit_message: ) build_prompt_client(prompt_data) end |
#create_score(name:, value:, trace_id: nil, observation_id: nil, comment: nil, metadata: nil, data_type: :numeric) ⇒ void
This method returns an undefined value.
Create a score event and queue it for batching
rubocop:disable Metrics/ParameterLists
263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/langfuse/client.rb', line 263 def create_score(name:, value:, trace_id: nil, observation_id: nil, comment: nil, metadata: nil, data_type: :numeric) @score_client.create( name: name, value: value, trace_id: trace_id, observation_id: observation_id, comment: comment, metadata: , data_type: data_type ) end |
#flush_scores ⇒ void
This method returns an undefined value.
Force flush all queued score events
Sends all queued score events to the API immediately.
337 338 339 |
# File 'lib/langfuse/client.rb', line 337 def flush_scores @score_client.flush end |
#get_prompt(name, version: nil, label: nil, fallback: nil, type: nil) ⇒ TextPromptClient, ChatPromptClient
Fetch a prompt and return the appropriate client
Fetches the prompt from the Langfuse API and returns either a TextPromptClient or ChatPromptClient based on the prompt type.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/langfuse/client.rb', line 67 def get_prompt(name, version: nil, label: nil, fallback: nil, type: nil) # Validate fallback usage if fallback && !type raise ArgumentError, "type parameter is required when fallback is provided (use :text or :chat)" end # Try to fetch from API prompt_data = api_client.get_prompt(name, version: version, label: label) build_prompt_client(prompt_data) rescue ApiError, NotFoundError, UnauthorizedError => e # If no fallback, re-raise the error raise e unless fallback # Log warning and return fallback config.logger.warn("Langfuse API error for prompt '#{name}': #{e.}. Using fallback.") build_fallback_prompt_client(name, fallback, type) end |
#list_prompts(page: nil, limit: nil) ⇒ Array<Hash>
List all prompts in the Langfuse project
Fetches a list of all prompt names available in your project. Returns metadata only, not full prompt content.
101 102 103 |
# File 'lib/langfuse/client.rb', line 101 def list_prompts(page: nil, limit: nil) api_client.list_prompts(page: page, limit: limit) end |
#score_active_observation(name:, value:, comment: nil, metadata: nil, data_type: :numeric) ⇒ void
This method returns an undefined value.
Create a score for the currently active observation (from OTel span)
Extracts observation_id and trace_id from the active OpenTelemetry span.
293 294 295 296 297 298 299 300 301 |
# File 'lib/langfuse/client.rb', line 293 def score_active_observation(name:, value:, comment: nil, metadata: nil, data_type: :numeric) @score_client.score_active_observation( name: name, value: value, comment: comment, metadata: , data_type: data_type ) end |
#score_active_trace(name:, value:, comment: nil, metadata: nil, data_type: :numeric) ⇒ void
This method returns an undefined value.
Create a score for the currently active trace (from OTel span)
Extracts trace_id from the active OpenTelemetry span.
319 320 321 322 323 324 325 326 327 |
# File 'lib/langfuse/client.rb', line 319 def score_active_trace(name:, value:, comment: nil, metadata: nil, data_type: :numeric) @score_client.score_active_trace( name: name, value: value, comment: comment, metadata: , data_type: data_type ) end |
#shutdown ⇒ void
This method returns an undefined value.
Shutdown the client and flush any pending scores
Also shuts down the cache if it supports shutdown (e.g., SWR thread pool).
346 347 348 349 |
# File 'lib/langfuse/client.rb', line 346 def shutdown @score_client.shutdown @api_client.shutdown end |
#trace_url(trace_id) ⇒ String
Generate URL for viewing a trace in Langfuse UI
238 239 240 |
# File 'lib/langfuse/client.rb', line 238 def trace_url(trace_id) "#{config.base_url}/traces/#{trace_id}" end |
#update_prompt(name:, version:, labels:) ⇒ TextPromptClient, ChatPromptClient
Update an existing prompt version’s metadata
Updates the labels of an existing prompt version. Note: The prompt content itself cannot be changed after creation.
220 221 222 223 224 225 226 227 228 |
# File 'lib/langfuse/client.rb', line 220 def update_prompt(name:, version:, labels:) prompt_data = api_client.update_prompt( name: name, version: version, labels: labels ) build_prompt_client(prompt_data) end |