langfuse-ruby 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/Gemfile.lock +1 -1
- data/README.md +111 -20
- data/docs/TYPE_VALIDATION_TROUBLESHOOTING.md +202 -0
- data/examples/auto_flush_control.rb +205 -0
- data/examples/basic_tracing.rb +0 -12
- data/examples/connection_config_demo.rb +15 -1
- data/examples/event_usage.rb +145 -0
- data/lib/langfuse/client.rb +161 -52
- data/lib/langfuse/event.rb +63 -0
- data/lib/langfuse/generation.rb +19 -40
- data/lib/langfuse/span.rb +17 -0
- data/lib/langfuse/trace.rb +17 -34
- data/lib/langfuse/utils.rb +23 -1
- data/lib/langfuse/version.rb +1 -1
- data/lib/langfuse.rb +4 -1
- data/test_offline.rb +93 -101
- metadata +6 -3
- data/.github/workflows/ci.yml +0 -47
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af3d00eedfb953a5bdc8f1a99d272debe050ed43cf160329c907cd209b28e042
|
4
|
+
data.tar.gz: 3a4c6ac469fa7874c0e310b059029bcaa879e6f048abfd902097dfde1d7ed3fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b74bc9c6d80fe4f09c20f38345bb0244b801acc07b770674b85778c21ad0b66b0fabe07206071c52f8780531dce49799b6db8d67b1cee662432ab8f5ae81258
|
7
|
+
data.tar.gz: 9901d034719eab0f2c082962cf36396591db46b33f54665c3dcb9cfdd9f22c8f96ba1b71b7296d4131e20a43355e57375e70020d734a43f810d05a802e207e3f
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
### Added
|
11
|
+
- Added support for `event-create` event type in Langfuse ingestion API
|
12
|
+
- New `Event` class for creating generic events within traces, spans, and generations
|
13
|
+
- Added `event()` method to `Client`, `Trace`, `Span`, and `Generation` classes
|
14
|
+
- Enhanced event validation to include all supported Langfuse event types
|
15
|
+
- New example file `examples/event_usage.rb` demonstrating event functionality
|
16
|
+
|
17
|
+
## [0.1.2] - 2025-01-13
|
18
|
+
|
19
|
+
### Added
|
20
|
+
- Enhanced event data validation and debugging capabilities
|
21
|
+
- More detailed error messages for event structure validation failures
|
22
|
+
|
8
23
|
## [0.1.1] - 2025-01-12
|
9
24
|
|
10
25
|
### Fixed
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -10,6 +10,7 @@ Ruby SDK for [Langfuse](https://langfuse.com) - the open-source LLM engineering
|
|
10
10
|
- 🔍 **Tracing**: Complete observability for LLM applications with traces, spans, and generations
|
11
11
|
- 📝 **Prompt Management**: Version control and deployment of prompts with caching
|
12
12
|
- 📊 **Evaluation**: Built-in evaluators and custom scoring capabilities
|
13
|
+
- 🎯 **Events**: Generic event tracking for custom application events and logging
|
13
14
|
- 🚀 **Async Processing**: Background event processing with automatic batching
|
14
15
|
- 🔒 **Type Safety**: Comprehensive error handling and validation
|
15
16
|
- 🎯 **Framework Integration**: Easy integration with popular Ruby frameworks
|
@@ -66,7 +67,6 @@ trace = client.trace(
|
|
66
67
|
name: "chat-completion",
|
67
68
|
user_id: "user123",
|
68
69
|
session_id: "session456",
|
69
|
-
input: { message: "Hello, world!" },
|
70
70
|
metadata: { environment: "production" }
|
71
71
|
)
|
72
72
|
|
@@ -75,15 +75,10 @@ generation = trace.generation(
|
|
75
75
|
name: "openai-completion",
|
76
76
|
model: "gpt-3.5-turbo",
|
77
77
|
input: [{ role: "user", content: "Hello, world!" }],
|
78
|
-
output: { content: "Hello! How can I help you today?" },
|
79
|
-
usage: { prompt_tokens: 10, completion_tokens: 15, total_tokens: 25 },
|
80
78
|
model_parameters: { temperature: 0.7, max_tokens: 100 }
|
81
79
|
)
|
82
80
|
|
83
|
-
|
84
|
-
trace.update(
|
85
|
-
output: { response: "Hello! How can I help you today?" }
|
86
|
-
)
|
81
|
+
generation.end(output: 'Hello! How can I help you today?', usage: { prompt_tokens: 10, completion_tokens: 15, total_tokens: 25 })
|
87
82
|
|
88
83
|
# Flush events (optional - happens automatically)
|
89
84
|
client.flush
|
@@ -130,12 +125,40 @@ llm_gen = answer_span.generation(
|
|
130
125
|
input: [
|
131
126
|
{ role: "system", content: "Answer based on context" },
|
132
127
|
{ role: "user", content: "What is machine learning?" }
|
133
|
-
]
|
134
|
-
output: { content: "Machine learning is a subset of AI..." },
|
135
|
-
usage: { prompt_tokens: 50, completion_tokens: 30, total_tokens: 80 }
|
128
|
+
]
|
136
129
|
)
|
137
130
|
|
138
|
-
answer_span.end(output: { answer: "Machine learning is a subset of AI..." })
|
131
|
+
answer_span.end(output: { answer: "Machine learning is a subset of AI..." }, usage: { prompt_tokens: 50, completion_tokens: 30, total_tokens: 80 })
|
132
|
+
```
|
133
|
+
|
134
|
+
## Events
|
135
|
+
|
136
|
+
Create generic events for custom application events and logging:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
# Create events from trace
|
140
|
+
event = trace.event(
|
141
|
+
name: "user_action",
|
142
|
+
input: { action: "login", user_id: "123" },
|
143
|
+
output: { success: true },
|
144
|
+
metadata: { ip: "192.168.1.1" }
|
145
|
+
)
|
146
|
+
|
147
|
+
# Create events from spans or generations
|
148
|
+
validation_event = span.event(
|
149
|
+
name: "validation_check",
|
150
|
+
input: { rules: ["required", "format"] },
|
151
|
+
output: { valid: true, warnings: [] }
|
152
|
+
)
|
153
|
+
|
154
|
+
# Direct event creation
|
155
|
+
event = client.event(
|
156
|
+
trace_id: trace.id,
|
157
|
+
name: "audit_log",
|
158
|
+
input: { operation: "data_export" },
|
159
|
+
output: { status: "completed" },
|
160
|
+
level: "INFO"
|
161
|
+
)
|
139
162
|
```
|
140
163
|
|
141
164
|
## Prompt Management
|
@@ -304,7 +327,9 @@ client = Langfuse.new(
|
|
304
327
|
host: "https://your-instance.langfuse.com",
|
305
328
|
debug: true, # Enable debug logging
|
306
329
|
timeout: 30, # Request timeout in seconds
|
307
|
-
retries: 3
|
330
|
+
retries: 3, # Number of retry attempts
|
331
|
+
flush_interval: 30, # Event flush interval in seconds (default: 5)
|
332
|
+
auto_flush: true # Enable automatic flushing (default: true)
|
308
333
|
)
|
309
334
|
```
|
310
335
|
|
@@ -316,6 +341,76 @@ You can also configure the client using environment variables:
|
|
316
341
|
export LANGFUSE_PUBLIC_KEY="pk-lf-..."
|
317
342
|
export LANGFUSE_SECRET_KEY="sk-lf-..."
|
318
343
|
export LANGFUSE_HOST="https://cloud.langfuse.com"
|
344
|
+
export LANGFUSE_FLUSH_INTERVAL=5
|
345
|
+
export LANGFUSE_AUTO_FLUSH=true
|
346
|
+
```
|
347
|
+
|
348
|
+
### Automatic Flush Control
|
349
|
+
|
350
|
+
By default, the Langfuse client automatically flushes events to the server at regular intervals using a background thread. You can control this behavior:
|
351
|
+
|
352
|
+
#### Enable/Disable Auto Flush
|
353
|
+
|
354
|
+
```ruby
|
355
|
+
# Enable automatic flushing (default)
|
356
|
+
client = Langfuse.new(
|
357
|
+
public_key: "pk-lf-...",
|
358
|
+
secret_key: "sk-lf-...",
|
359
|
+
auto_flush: true,
|
360
|
+
flush_interval: 5 # Flush every 5 seconds
|
361
|
+
)
|
362
|
+
|
363
|
+
# Disable automatic flushing for manual control
|
364
|
+
client = Langfuse.new(
|
365
|
+
public_key: "pk-lf-...",
|
366
|
+
secret_key: "sk-lf-...",
|
367
|
+
auto_flush: false
|
368
|
+
)
|
369
|
+
|
370
|
+
# Manual flush when auto_flush is disabled
|
371
|
+
client.flush
|
372
|
+
```
|
373
|
+
|
374
|
+
#### Global Configuration
|
375
|
+
|
376
|
+
```ruby
|
377
|
+
Langfuse.configure do |config|
|
378
|
+
config.auto_flush = false # Disable auto flush globally
|
379
|
+
config.flush_interval = 10
|
380
|
+
end
|
381
|
+
```
|
382
|
+
|
383
|
+
#### Environment Variable
|
384
|
+
|
385
|
+
```bash
|
386
|
+
export LANGFUSE_AUTO_FLUSH=false
|
387
|
+
```
|
388
|
+
|
389
|
+
#### Use Cases
|
390
|
+
|
391
|
+
**Auto Flush Enabled (Default)**
|
392
|
+
- Best for most applications
|
393
|
+
- Events are sent automatically
|
394
|
+
- No manual management required
|
395
|
+
|
396
|
+
**Auto Flush Disabled**
|
397
|
+
- Better performance for batch operations
|
398
|
+
- More control over when events are sent
|
399
|
+
- Requires manual flush calls
|
400
|
+
- Useful for high-frequency operations
|
401
|
+
|
402
|
+
```ruby
|
403
|
+
# Example: Batch processing with manual flush
|
404
|
+
client = Langfuse.new(auto_flush: false)
|
405
|
+
|
406
|
+
# Process many items
|
407
|
+
1000.times do |i|
|
408
|
+
trace = client.trace(name: "batch-item-#{i}")
|
409
|
+
# ... process item
|
410
|
+
end
|
411
|
+
|
412
|
+
# Flush all events at once
|
413
|
+
client.flush
|
319
414
|
```
|
320
415
|
|
321
416
|
### Shutdown
|
@@ -356,9 +451,7 @@ class ChatController < ApplicationController
|
|
356
451
|
|
357
452
|
# Your LLM logic here
|
358
453
|
response = generate_response(params[:message])
|
359
|
-
|
360
|
-
trace.update(output: { response: response })
|
361
|
-
|
454
|
+
|
362
455
|
render json: { response: response }
|
363
456
|
end
|
364
457
|
end
|
@@ -370,19 +463,17 @@ end
|
|
370
463
|
class LLMProcessingJob < ApplicationJob
|
371
464
|
def perform(user_id, message)
|
372
465
|
client = Langfuse.new
|
373
|
-
|
466
|
+
|
374
467
|
trace = client.trace(
|
375
468
|
name: "background-llm-processing",
|
376
469
|
user_id: user_id,
|
377
470
|
input: { message: message },
|
378
471
|
metadata: { job_class: self.class.name }
|
379
472
|
)
|
380
|
-
|
473
|
+
|
381
474
|
# Process with LLM
|
382
475
|
result = process_with_llm(message)
|
383
|
-
|
384
|
-
trace.update(output: result)
|
385
|
-
|
476
|
+
|
386
477
|
# Ensure events are flushed
|
387
478
|
client.flush
|
388
479
|
end
|
@@ -0,0 +1,202 @@
|
|
1
|
+
# 类型验证错误故障排除指南
|
2
|
+
|
3
|
+
## 问题描述
|
4
|
+
|
5
|
+
当您遇到以下错误时:
|
6
|
+
|
7
|
+
```json
|
8
|
+
{
|
9
|
+
"id": "xxx",
|
10
|
+
"status": 400,
|
11
|
+
"message": "Invalid request data",
|
12
|
+
"error": [
|
13
|
+
{
|
14
|
+
"code": "invalid_union",
|
15
|
+
"errors": [],
|
16
|
+
"note": "No matching discriminator",
|
17
|
+
"path": ["type"],
|
18
|
+
"message": "Invalid input"
|
19
|
+
}
|
20
|
+
]
|
21
|
+
}
|
22
|
+
```
|
23
|
+
|
24
|
+
这表示 Langfuse 服务器端的 API 验证发现事件的 `type` 字段不符合预期的格式。
|
25
|
+
|
26
|
+
## 根本原因
|
27
|
+
|
28
|
+
1. **事件类型无效**: 发送的事件类型不在服务器端支持的列表中
|
29
|
+
2. **事件数据结构错误**: 事件的数据结构不符合对应类型的要求
|
30
|
+
3. **数据序列化问题**: 事件数据在序列化过程中出现问题
|
31
|
+
|
32
|
+
## 常见修复案例
|
33
|
+
|
34
|
+
### 1. 事件类型无效
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
client.trace(name: "my-trace", user_id: "user-123", input: { query: "Hello" })
|
38
|
+
```
|
39
|
+
|
40
|
+
## 解决方案
|
41
|
+
|
42
|
+
### 1. 启用调试模式
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
client = Langfuse.new(
|
46
|
+
public_key: "your-key",
|
47
|
+
secret_key: "your-secret",
|
48
|
+
debug: true # 启用调试模式
|
49
|
+
)
|
50
|
+
```
|
51
|
+
|
52
|
+
调试模式会显示:
|
53
|
+
- 发送的事件类型
|
54
|
+
- 事件数据结构
|
55
|
+
- 详细的错误信息
|
56
|
+
|
57
|
+
### 2. 检查支持的事件类型
|
58
|
+
|
59
|
+
当前支持的事件类型:
|
60
|
+
- `trace-create`
|
61
|
+
- `generation-create`
|
62
|
+
- `generation-update`
|
63
|
+
- `span-create`
|
64
|
+
- `span-update`
|
65
|
+
- `event-create`
|
66
|
+
- `score-create`
|
67
|
+
|
68
|
+
### 3. 验证事件数据
|
69
|
+
|
70
|
+
确保事件数据包含必要的字段:
|
71
|
+
|
72
|
+
#### Trace 事件
|
73
|
+
```ruby
|
74
|
+
{
|
75
|
+
id: "uuid",
|
76
|
+
name: "trace-name",
|
77
|
+
user_id: "user-id",
|
78
|
+
input: { ... },
|
79
|
+
metadata: { ... },
|
80
|
+
tags: [...],
|
81
|
+
timestamp: "2025-01-01T00:00:00.000Z"
|
82
|
+
}
|
83
|
+
```
|
84
|
+
|
85
|
+
#### Generation 事件
|
86
|
+
```ruby
|
87
|
+
{
|
88
|
+
id: "uuid",
|
89
|
+
trace_id: "trace-uuid",
|
90
|
+
name: "generation-name",
|
91
|
+
model: "gpt-3.5-turbo",
|
92
|
+
input: [...],
|
93
|
+
output: { ... },
|
94
|
+
usage: { ... },
|
95
|
+
metadata: { ... }
|
96
|
+
}
|
97
|
+
```
|
98
|
+
|
99
|
+
#### Span 事件
|
100
|
+
```ruby
|
101
|
+
{
|
102
|
+
id: "uuid",
|
103
|
+
trace_id: "trace-uuid",
|
104
|
+
name: "span-name",
|
105
|
+
start_time: "2025-01-01T00:00:00.000Z",
|
106
|
+
end_time: "2025-01-01T00:00:01.000Z",
|
107
|
+
input: { ... },
|
108
|
+
output: { ... },
|
109
|
+
metadata: { ... }
|
110
|
+
}
|
111
|
+
```
|
112
|
+
|
113
|
+
#### Event 事件
|
114
|
+
```ruby
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
|
118
|
+
```
|
119
|
+
|
120
|
+
#### Score 事件
|
121
|
+
```ruby
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
### 4. 检查网络和认证
|
126
|
+
|
127
|
+
确保:
|
128
|
+
- API 密钥正确
|
129
|
+
- 网络连接正常
|
130
|
+
- 服务器端点可访问
|
131
|
+
|
132
|
+
### 5. 使用错误处理
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
begin
|
136
|
+
client.flush
|
137
|
+
rescue Langfuse::ValidationError => e
|
138
|
+
if e.message.include?('Event type validation failed')
|
139
|
+
puts "类型验证错误: #{e.message}"
|
140
|
+
# 检查事件数据格式
|
141
|
+
else
|
142
|
+
puts "其他验证错误: #{e.message}"
|
143
|
+
end
|
144
|
+
rescue Langfuse::APIError => e
|
145
|
+
puts "API 错误: #{e.message}"
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
149
|
+
## 预防措施
|
150
|
+
|
151
|
+
1. **使用官方 SDK 方法**: 避免直接构造事件数据
|
152
|
+
2. **数据验证**: 在发送前验证数据完整性
|
153
|
+
3. **错误监控**: 实施适当的错误处理和监控
|
154
|
+
4. **测试环境**: 在测试环境中验证集成
|
155
|
+
5. **保持更新**: 定期更新 SDK 到最新版本
|
156
|
+
|
157
|
+
## 示例代码
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
# 正确的使用方式
|
161
|
+
client = Langfuse.new(
|
162
|
+
public_key: "pk-lf-xxx",
|
163
|
+
secret_key: "sk-lf-xxx",
|
164
|
+
debug: true
|
165
|
+
)
|
166
|
+
|
167
|
+
# 创建 trace
|
168
|
+
trace = client.trace(
|
169
|
+
name: "my-trace",
|
170
|
+
user_id: "user-123",
|
171
|
+
input: { query: "Hello" }
|
172
|
+
)
|
173
|
+
|
174
|
+
# 创建 generation
|
175
|
+
generation = trace.generation(
|
176
|
+
name: "my-generation",
|
177
|
+
model: "gpt-3.5-turbo",
|
178
|
+
input: [{ role: "user", content: "Hello" }],
|
179
|
+
output: { content: "Hi there!" }
|
180
|
+
)
|
181
|
+
|
182
|
+
# 安全地刷新事件
|
183
|
+
begin
|
184
|
+
client.flush
|
185
|
+
puts "事件发送成功"
|
186
|
+
rescue => e
|
187
|
+
puts "发送失败: #{e.message}"
|
188
|
+
end
|
189
|
+
```
|
190
|
+
|
191
|
+
## 联系支持
|
192
|
+
|
193
|
+
如果问题持续存在,请提供:
|
194
|
+
1. 完整的错误消息
|
195
|
+
2. 调试模式的输出
|
196
|
+
3. 相关的代码片段
|
197
|
+
4. SDK 版本信息
|
198
|
+
|
199
|
+
## 更新日志
|
200
|
+
|
201
|
+
- v0.1.1: 改进了错误消息的可读性
|
202
|
+
- v0.1.0: 初始版本
|
@@ -0,0 +1,205 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative '../lib/langfuse'
|
4
|
+
|
5
|
+
puts '🚀 Langfuse Ruby SDK 自动刷新控制演示'
|
6
|
+
puts '=' * 50
|
7
|
+
|
8
|
+
# 示例 1: 启用自动刷新(默认行为)
|
9
|
+
puts "\n📝 示例 1: 启用自动刷新(默认行为)"
|
10
|
+
puts '代码示例:'
|
11
|
+
puts 'client = Langfuse.new('
|
12
|
+
puts " public_key: 'pk-lf-your-public-key',"
|
13
|
+
puts " secret_key: 'sk-lf-your-secret-key',"
|
14
|
+
puts ' auto_flush: true # 默认为 true,可以省略'
|
15
|
+
puts ')'
|
16
|
+
|
17
|
+
client_auto = Langfuse.new(
|
18
|
+
public_key: 'test-public-key',
|
19
|
+
secret_key: 'test-secret-key',
|
20
|
+
auto_flush: true,
|
21
|
+
flush_interval: 2 # 2秒刷新一次用于演示
|
22
|
+
)
|
23
|
+
|
24
|
+
puts '✅ 自动刷新客户端创建成功'
|
25
|
+
puts " 自动刷新: #{client_auto.auto_flush}"
|
26
|
+
puts " 刷新间隔: #{client_auto.flush_interval}秒"
|
27
|
+
|
28
|
+
# 创建一些事件
|
29
|
+
trace_auto = client_auto.trace(
|
30
|
+
name: 'auto-flush-demo',
|
31
|
+
input: { message: '这是自动刷新演示' },
|
32
|
+
metadata: { demo: true }
|
33
|
+
)
|
34
|
+
|
35
|
+
puts '✅ 创建了 trace,将在后台自动刷新'
|
36
|
+
|
37
|
+
# 示例 2: 禁用自动刷新
|
38
|
+
puts "\n📝 示例 2: 禁用自动刷新"
|
39
|
+
puts '代码示例:'
|
40
|
+
puts 'client = Langfuse.new('
|
41
|
+
puts " public_key: 'pk-lf-your-public-key',"
|
42
|
+
puts " secret_key: 'sk-lf-your-secret-key',"
|
43
|
+
puts ' auto_flush: false # 禁用自动刷新'
|
44
|
+
puts ')'
|
45
|
+
|
46
|
+
client_manual = Langfuse.new(
|
47
|
+
public_key: 'test-public-key',
|
48
|
+
secret_key: 'test-secret-key',
|
49
|
+
auto_flush: false
|
50
|
+
)
|
51
|
+
|
52
|
+
puts '✅ 手动刷新客户端创建成功'
|
53
|
+
puts " 自动刷新: #{client_manual.auto_flush}"
|
54
|
+
puts " 刷新间隔: #{client_manual.flush_interval}秒(不会自动刷新)"
|
55
|
+
|
56
|
+
# 创建一些事件
|
57
|
+
trace_manual = client_manual.trace(
|
58
|
+
name: 'manual-flush-demo',
|
59
|
+
input: { message: '这是手动刷新演示' },
|
60
|
+
metadata: { demo: true }
|
61
|
+
)
|
62
|
+
|
63
|
+
generation_manual = trace_manual.generation(
|
64
|
+
name: 'manual-generation',
|
65
|
+
model: 'gpt-3.5-turbo',
|
66
|
+
input: [{ role: 'user', content: 'Hello!' }],
|
67
|
+
output: { content: '你好!' },
|
68
|
+
usage: { prompt_tokens: 5, completion_tokens: 3, total_tokens: 8 }
|
69
|
+
)
|
70
|
+
|
71
|
+
puts '✅ 创建了 trace 和 generation,需要手动刷新'
|
72
|
+
puts " 事件队列长度: #{client_manual.instance_variable_get(:@event_queue).length}"
|
73
|
+
|
74
|
+
# 手动刷新(离线模式,不实际发送)
|
75
|
+
puts "\n🔄 手动刷新事件(离线模式)..."
|
76
|
+
puts ' 注意:在实际应用中,这里会发送事件到 Langfuse 服务器'
|
77
|
+
puts ' 当前为演示模式,不会实际发送请求'
|
78
|
+
# client_manual.flush # 注释掉以避免网络请求
|
79
|
+
puts " 事件队列长度: #{client_manual.instance_variable_get(:@event_queue).length}"
|
80
|
+
|
81
|
+
# 示例 3: 通过全局配置控制
|
82
|
+
puts "\n📝 示例 3: 通过全局配置控制"
|
83
|
+
puts '代码示例:'
|
84
|
+
puts 'Langfuse.configure do |config|'
|
85
|
+
puts " config.public_key = 'pk-lf-your-public-key'"
|
86
|
+
puts " config.secret_key = 'sk-lf-your-secret-key'"
|
87
|
+
puts ' config.auto_flush = false # 全局禁用自动刷新'
|
88
|
+
puts ' config.flush_interval = 10'
|
89
|
+
puts 'end'
|
90
|
+
|
91
|
+
Langfuse.configure do |config|
|
92
|
+
config.public_key = 'test-public-key'
|
93
|
+
config.secret_key = 'test-secret-key'
|
94
|
+
config.auto_flush = false
|
95
|
+
config.flush_interval = 10
|
96
|
+
end
|
97
|
+
|
98
|
+
client_global = Langfuse.new
|
99
|
+
puts '✅ 全局配置客户端创建成功'
|
100
|
+
puts " 自动刷新: #{client_global.auto_flush}"
|
101
|
+
puts " 刷新间隔: #{client_global.flush_interval}秒"
|
102
|
+
|
103
|
+
# 示例 4: 通过环境变量控制
|
104
|
+
puts "\n📝 示例 4: 通过环境变量控制"
|
105
|
+
puts '设置环境变量:'
|
106
|
+
puts "export LANGFUSE_PUBLIC_KEY='pk-lf-your-public-key'"
|
107
|
+
puts "export LANGFUSE_SECRET_KEY='sk-lf-your-secret-key'"
|
108
|
+
puts 'export LANGFUSE_AUTO_FLUSH=false'
|
109
|
+
puts 'export LANGFUSE_FLUSH_INTERVAL=15'
|
110
|
+
puts ''
|
111
|
+
puts '然后使用:'
|
112
|
+
puts 'client = Langfuse.new'
|
113
|
+
|
114
|
+
# 模拟环境变量
|
115
|
+
ENV['LANGFUSE_AUTO_FLUSH'] = 'false'
|
116
|
+
ENV['LANGFUSE_FLUSH_INTERVAL'] = '15'
|
117
|
+
|
118
|
+
client_env = Langfuse.new(
|
119
|
+
public_key: 'test-public-key',
|
120
|
+
secret_key: 'test-secret-key'
|
121
|
+
)
|
122
|
+
|
123
|
+
puts '✅ 环境变量配置客户端创建成功'
|
124
|
+
puts " 自动刷新: #{client_env.auto_flush}"
|
125
|
+
puts " 刷新间隔: #{client_env.flush_interval}秒"
|
126
|
+
|
127
|
+
# 清理环境变量
|
128
|
+
ENV.delete('LANGFUSE_AUTO_FLUSH')
|
129
|
+
ENV.delete('LANGFUSE_FLUSH_INTERVAL')
|
130
|
+
|
131
|
+
# 示例 5: 混合使用场景
|
132
|
+
puts "\n📝 示例 5: 混合使用场景"
|
133
|
+
puts '在某些情况下,您可能希望:'
|
134
|
+
puts '1. 大部分时间使用自动刷新'
|
135
|
+
puts '2. 在批量操作时临时禁用自动刷新'
|
136
|
+
puts '3. 在操作完成后手动刷新'
|
137
|
+
|
138
|
+
# 批量操作示例
|
139
|
+
puts "\n🔄 批量操作演示..."
|
140
|
+
batch_client = Langfuse.new(
|
141
|
+
public_key: 'test-public-key',
|
142
|
+
secret_key: 'test-secret-key',
|
143
|
+
auto_flush: false # 禁用自动刷新以提高批量操作性能
|
144
|
+
)
|
145
|
+
|
146
|
+
# 批量创建多个 traces
|
147
|
+
traces = []
|
148
|
+
10.times do |i|
|
149
|
+
trace = batch_client.trace(
|
150
|
+
name: "batch-trace-#{i}",
|
151
|
+
input: { batch_id: i },
|
152
|
+
metadata: { batch_operation: true }
|
153
|
+
)
|
154
|
+
traces << trace
|
155
|
+
end
|
156
|
+
|
157
|
+
puts "✅ 批量创建了 #{traces.length} 个 traces"
|
158
|
+
puts " 事件队列长度: #{batch_client.instance_variable_get(:@event_queue).length}"
|
159
|
+
|
160
|
+
# 批量操作完成后手动刷新(离线模式)
|
161
|
+
puts "\n🔄 批量操作完成,执行手动刷新(离线模式)..."
|
162
|
+
puts ' 注意:在实际应用中,这里会发送所有事件到 Langfuse 服务器'
|
163
|
+
# batch_client.flush # 注释掉以避免网络请求
|
164
|
+
puts " 事件队列长度: #{batch_client.instance_variable_get(:@event_queue).length}(未发送)"
|
165
|
+
|
166
|
+
# 使用建议
|
167
|
+
puts "\n💡 使用建议:"
|
168
|
+
puts '1. 默认情况下保持 auto_flush=true,适合大多数应用场景'
|
169
|
+
puts '2. 在批量操作或高频操作时,考虑设置 auto_flush=false'
|
170
|
+
puts '3. 禁用自动刷新时,记得在适当的时机调用 client.flush'
|
171
|
+
puts '4. 应用关闭前,务必调用 client.shutdown 确保所有事件都被发送'
|
172
|
+
puts '5. 可以通过环境变量在不同环境中控制刷新行为'
|
173
|
+
|
174
|
+
# 性能对比
|
175
|
+
puts "\n⚡ 性能对比:"
|
176
|
+
puts '自动刷新模式:'
|
177
|
+
puts ' - 优点:无需手动管理,事件及时发送'
|
178
|
+
puts ' - 缺点:后台线程消耗资源,可能影响高频操作性能'
|
179
|
+
puts ''
|
180
|
+
puts '手动刷新模式:'
|
181
|
+
puts ' - 优点:更好的性能控制,适合批量操作'
|
182
|
+
puts ' - 缺点:需要手动管理刷新时机,容易遗漏'
|
183
|
+
|
184
|
+
# 等待一下让自动刷新客户端工作
|
185
|
+
puts "\n⏳ 等待自动刷新客户端工作..."
|
186
|
+
sleep(3)
|
187
|
+
|
188
|
+
# 关闭所有客户端(离线模式)
|
189
|
+
puts "\n🔒 关闭所有客户端(离线模式)..."
|
190
|
+
puts ' 注意:在实际应用中,shutdown 会确保所有事件都被发送'
|
191
|
+
# 在演示模式下,我们只终止后台线程而不发送请求
|
192
|
+
client_auto.instance_variable_get(:@flush_thread)&.kill
|
193
|
+
client_manual.instance_variable_get(:@flush_thread)&.kill
|
194
|
+
client_global.instance_variable_get(:@flush_thread)&.kill
|
195
|
+
client_env.instance_variable_get(:@flush_thread)&.kill
|
196
|
+
batch_client.instance_variable_get(:@flush_thread)&.kill
|
197
|
+
|
198
|
+
puts "\n🎉 自动刷新控制演示完成!"
|
199
|
+
puts ''
|
200
|
+
puts '📚 总结:'
|
201
|
+
puts '现在您可以在应用层面灵活控制 Langfuse 的自动刷新行为:'
|
202
|
+
puts '- 通过构造函数参数 auto_flush 控制'
|
203
|
+
puts '- 通过全局配置 Langfuse.configure 控制'
|
204
|
+
puts '- 通过环境变量 LANGFUSE_AUTO_FLUSH 控制'
|
205
|
+
puts '- 根据不同场景选择合适的刷新策略'
|
data/examples/basic_tracing.rb
CHANGED
@@ -47,11 +47,6 @@ generation = trace.generation(
|
|
47
47
|
|
48
48
|
puts "Created generation: #{generation.id}"
|
49
49
|
|
50
|
-
# Update trace with final output
|
51
|
-
trace.update(
|
52
|
-
output: { response: "I'm doing well, thank you! How can I help you today?" }
|
53
|
-
)
|
54
|
-
|
55
50
|
puts "Trace URL: #{trace.get_url}"
|
56
51
|
|
57
52
|
# Example 2: Nested spans for complex workflow
|
@@ -130,13 +125,6 @@ answer_span.end(
|
|
130
125
|
}
|
131
126
|
)
|
132
127
|
|
133
|
-
# Update workflow trace
|
134
|
-
workflow_trace.update(
|
135
|
-
output: {
|
136
|
-
answer: 'Machine learning is a subset of artificial intelligence that enables computers to learn and improve from experience without being explicitly programmed. ML algorithms identify patterns in data and use these patterns to make predictions or decisions.'
|
137
|
-
}
|
138
|
-
)
|
139
|
-
|
140
128
|
puts "Workflow trace URL: #{workflow_trace.get_url}"
|
141
129
|
|
142
130
|
# Example 3: Adding scores and evaluations
|