langfuse-ruby 0.1.1 → 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 +59 -0
- data/Gemfile +5 -5
- data/Gemfile.lock +1 -1
- data/README.md +111 -20
- data/docs/PUBLISH_GUIDE.md +2 -2
- data/docs/README.md +0 -4
- 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 +141 -0
- data/examples/event_usage.rb +145 -0
- data/examples/prompt_management.rb +62 -71
- data/lib/langfuse/client.rb +176 -58
- 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 +5 -2
- data/test_offline.rb +93 -101
- metadata +8 -7
- data/.github/workflows/ci.yml +0 -47
- data/PROMPT_TROUBLESHOOTING.md +0 -206
- data/docs/CHANGELOG.md +0 -49
- data/docs/PROJECT_SUMMARY.md +0 -263
- data/test_basic.rb +0 -183
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
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
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
|
+
|
23
|
+
## [0.1.1] - 2025-01-12
|
24
|
+
|
25
|
+
### Fixed
|
26
|
+
- Improved error handling for `get_prompt` method when prompt doesn't exist
|
27
|
+
- Better error messages for 404 responses that return HTML instead of JSON
|
28
|
+
- Enhanced debugging capabilities with detailed request/response logging
|
29
|
+
|
30
|
+
### Added
|
31
|
+
- Comprehensive troubleshooting guide for prompt management issues
|
32
|
+
- Better detection of HTML responses vs JSON responses
|
33
|
+
- More specific error types for different failure scenarios
|
34
|
+
|
35
|
+
### Changed
|
36
|
+
- Updated gemspec metadata to avoid RubyGems warnings
|
37
|
+
- Improved documentation with clearer error handling examples
|
38
|
+
|
39
|
+
## [0.1.0] - 2025-01-12
|
40
|
+
|
41
|
+
### Added
|
42
|
+
- Initial release of Langfuse Ruby SDK
|
43
|
+
- Complete tracing capabilities with traces, spans, and generations
|
44
|
+
- Prompt management with versioning and caching
|
45
|
+
- Built-in evaluators (exact match, similarity, length, contains, regex)
|
46
|
+
- Custom scoring and evaluation pipeline support
|
47
|
+
- Async event processing with automatic batching
|
48
|
+
- Comprehensive error handling and validation
|
49
|
+
- Framework integration examples (Rails, Sidekiq)
|
50
|
+
- Full test suite with RSpec
|
51
|
+
- Documentation and examples
|
52
|
+
|
53
|
+
### Features
|
54
|
+
- **Tracing**: Full observability for LLM applications
|
55
|
+
- **Prompt Management**: Version control and deployment of prompts
|
56
|
+
- **Evaluation**: Multiple built-in evaluators and custom scoring
|
57
|
+
- **Async Processing**: Background event processing with batching
|
58
|
+
- **Type Safety**: Comprehensive error handling
|
59
|
+
- **Integration**: Easy integration with Ruby frameworks
|
data/Gemfile
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in langfuse.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
-
gem
|
7
|
-
gem
|
8
|
-
gem
|
9
|
-
gem
|
6
|
+
gem 'rake', '~> 13.0'
|
7
|
+
gem 'rspec', '~> 3.0'
|
8
|
+
gem 'rubocop', '~> 1.21'
|
9
|
+
gem 'yard', '~> 0.9'
|
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
|
data/docs/PUBLISH_GUIDE.md
CHANGED
@@ -108,10 +108,10 @@ chmod 0600 ~/.gem/credentials
|
|
108
108
|
|
109
109
|
```bash
|
110
110
|
# 构建最新版本
|
111
|
-
gem build langfuse.gemspec
|
111
|
+
gem build langfuse-ruby.gemspec
|
112
112
|
|
113
113
|
# 发布到 RubyGems
|
114
|
-
gem push langfuse-ruby-0.1.
|
114
|
+
gem push langfuse-ruby-0.1.x.gem
|
115
115
|
```
|
116
116
|
|
117
117
|
## 📊 发布后验证
|
data/docs/README.md
CHANGED
@@ -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: 初始版本
|