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.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: langfuse-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Sun
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-07-12 00:00:00.000000000 Z
11
+ date: 2025-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -172,28 +172,30 @@ executables: []
172
172
  extensions: []
173
173
  extra_rdoc_files: []
174
174
  files:
175
- - ".github/workflows/ci.yml"
176
175
  - ".github/workflows/release.yml"
177
176
  - ".gitignore"
177
+ - CHANGELOG.md
178
178
  - Gemfile
179
179
  - Gemfile.lock
180
180
  - LICENSE
181
- - PROMPT_TROUBLESHOOTING.md
182
181
  - README.md
183
182
  - Rakefile
184
- - docs/CHANGELOG.md
185
183
  - docs/FINAL_SUMMARY.md
186
- - docs/PROJECT_SUMMARY.md
187
184
  - docs/PUBLISH_GUIDE.md
188
185
  - docs/README.md
189
186
  - docs/RELEASE_CHECKLIST.md
187
+ - docs/TYPE_VALIDATION_TROUBLESHOOTING.md
188
+ - examples/auto_flush_control.rb
190
189
  - examples/basic_tracing.rb
190
+ - examples/connection_config_demo.rb
191
+ - examples/event_usage.rb
191
192
  - examples/prompt_management.rb
192
193
  - langfuse-ruby.gemspec
193
194
  - lib/langfuse.rb
194
195
  - lib/langfuse/client.rb
195
196
  - lib/langfuse/errors.rb
196
197
  - lib/langfuse/evaluation.rb
198
+ - lib/langfuse/event.rb
197
199
  - lib/langfuse/generation.rb
198
200
  - lib/langfuse/prompt.rb
199
201
  - lib/langfuse/span.rb
@@ -202,7 +204,6 @@ files:
202
204
  - lib/langfuse/version.rb
203
205
  - scripts/release.sh
204
206
  - scripts/verify_release.rb
205
- - test_basic.rb
206
207
  - test_offline.rb
207
208
  homepage: https://langfuse.com
208
209
  licenses:
@@ -1,47 +0,0 @@
1
- name: CI
2
-
3
- on:
4
- push:
5
- branches: [ main ]
6
- pull_request:
7
- branches: [ main ]
8
-
9
- jobs:
10
- test:
11
- runs-on: ubuntu-latest
12
- strategy:
13
- matrix:
14
- ruby-version: ['2.7', '3.0', '3.1', '3.2']
15
-
16
- steps:
17
- - uses: actions/checkout@v4
18
-
19
- - name: Set up Ruby ${{ matrix.ruby-version }}
20
- uses: ruby/setup-ruby@v1
21
- with:
22
- ruby-version: ${{ matrix.ruby-version }}
23
- bundler-cache: true
24
-
25
- - name: Run tests
26
- run: bundle exec rspec
27
-
28
- - name: Run offline tests
29
- run: ruby test_offline.rb
30
-
31
- - name: Check gem build
32
- run: gem build langfuse.gemspec
33
-
34
- lint:
35
- runs-on: ubuntu-latest
36
- steps:
37
- - uses: actions/checkout@v4
38
-
39
- - name: Set up Ruby
40
- uses: ruby/setup-ruby@v1
41
- with:
42
- ruby-version: '3.2'
43
- bundler-cache: true
44
-
45
- - name: Run RuboCop
46
- run: bundle exec rubocop
47
- continue-on-error: true
@@ -1,206 +0,0 @@
1
- # Langfuse Ruby SDK - Prompt 获取故障排除指南
2
-
3
- ## 问题描述
4
-
5
- 当调用 `client.get_prompt("prompt-name")` 时,可能会遇到以下错误:
6
-
7
- ```
8
- JSON::ParserError: unexpected token at '<!DOCTYPE html>
9
- ```
10
-
11
- ## 问题原因
12
-
13
- 这个错误通常表示 Langfuse API 返回了 HTML 页面而不是预期的 JSON 响应。最常见的原因是:
14
-
15
- 1. **404 错误**:请求的 prompt 不存在
16
- 2. **认证问题**:API 密钥无效或权限不足
17
- 3. **网络问题**:无法连接到 Langfuse 服务器
18
-
19
- ## 解决方案
20
-
21
- ### 1. 检查 Prompt 是否存在
22
-
23
- 首先确认要获取的 prompt 是否已在 Langfuse 中创建:
24
-
25
- ```ruby
26
- # 尝试获取 prompt
27
- begin
28
- prompt = client.get_prompt("your-prompt-name")
29
- puts "成功获取 prompt: #{prompt.name}"
30
- rescue Langfuse::ValidationError => e
31
- if e.message.include?("404")
32
- puts "Prompt 不存在,请先创建它"
33
- else
34
- puts "其他验证错误: #{e.message}"
35
- end
36
- rescue => e
37
- puts "未知错误: #{e.message}"
38
- end
39
- ```
40
-
41
- ### 2. 创建测试 Prompt
42
-
43
- 如果 prompt 不存在,可以先创建一个:
44
-
45
- ```ruby
46
- # 创建一个测试 prompt
47
- test_prompt = client.create_prompt(
48
- name: "test-prompt",
49
- prompt: "Hello {{name}}! This is a test prompt.",
50
- labels: ["test"],
51
- config: { temperature: 0.7 }
52
- )
53
-
54
- puts "创建了 prompt: #{test_prompt.name}"
55
-
56
- # 然后尝试获取它
57
- retrieved_prompt = client.get_prompt("test-prompt")
58
- puts "成功获取 prompt: #{retrieved_prompt.name}"
59
- ```
60
-
61
- ### 3. 检查 API 密钥
62
-
63
- 确认 API 密钥设置正确:
64
-
65
- ```ruby
66
- # 检查环境变量
67
- puts "Public Key: #{ENV['LANGFUSE_PUBLIC_KEY']}"
68
- puts "Secret Key: #{ENV['LANGFUSE_SECRET_KEY'] ? '已设置' : '未设置'}"
69
- puts "Host: #{ENV['LANGFUSE_HOST'] || 'https://cloud.langfuse.com'}"
70
-
71
- # 或者在代码中直接设置
72
- client = Langfuse.new(
73
- public_key: "your-public-key",
74
- secret_key: "your-secret-key",
75
- host: "https://cloud.langfuse.com"
76
- )
77
- ```
78
-
79
- ### 4. 测试连接
80
-
81
- 测试与 Langfuse 服务器的基本连接:
82
-
83
- ```ruby
84
- begin
85
- response = client.send(:get, "/api/public/health")
86
- puts "连接正常"
87
- rescue => e
88
- puts "连接失败: #{e.message}"
89
- end
90
- ```
91
-
92
- ### 5. 启用调试模式
93
-
94
- 启用调试模式以获取更多信息:
95
-
96
- ```ruby
97
- client = Langfuse.new(
98
- public_key: "your-public-key",
99
- secret_key: "your-secret-key",
100
- host: "https://cloud.langfuse.com",
101
- debug: true # 启用调试模式
102
- )
103
- ```
104
-
105
- ## 改进的错误处理
106
-
107
- Ruby SDK 已经改进了错误处理,现在会提供更清晰的错误信息:
108
-
109
- ```ruby
110
- begin
111
- prompt = client.get_prompt("non-existent-prompt")
112
- rescue Langfuse::ValidationError => e
113
- puts "验证错误: #{e.message}"
114
- # 现在会显示:Resource not found (404). Server returned HTML page instead of JSON API response. This usually means the requested resource does not exist.
115
- rescue Langfuse::AuthenticationError => e
116
- puts "认证错误: #{e.message}"
117
- rescue => e
118
- puts "其他错误: #{e.message}"
119
- end
120
- ```
121
-
122
- ## 常见问题
123
-
124
- ### Q: 为什么会收到 HTML 响应而不是 JSON?
125
-
126
- A: 当 API 端点返回 404 错误时,Langfuse 服务器返回一个 HTML 错误页面而不是 JSON 响应。这是 Web 应用程序的常见行为。
127
-
128
- ### Q: 如何确认 prompt 名称是否正确?
129
-
130
- A: 可以登录 Langfuse Web 界面查看所有可用的 prompt,或者使用 API 列出所有 prompt:
131
-
132
- ```ruby
133
- # 注意:这需要使用底层 API 方法
134
- # prompts = client.api.prompts.list()
135
- ```
136
-
137
- ### Q: 大小写是否重要?
138
-
139
- A: 是的,prompt 名称是区分大小写的。确保使用正确的大小写。
140
-
141
- ### Q: 如何处理网络超时?
142
-
143
- A: 可以调整超时设置:
144
-
145
- ```ruby
146
- client = Langfuse.new(
147
- public_key: "your-public-key",
148
- secret_key: "your-secret-key",
149
- host: "https://cloud.langfuse.com",
150
- timeout: 60 # 60 秒超时
151
- )
152
- ```
153
-
154
- ## 完整示例
155
-
156
- 以下是一个完整的示例,展示如何安全地获取 prompt:
157
-
158
- ```ruby
159
- require 'langfuse'
160
-
161
- # 初始化客户端
162
- client = Langfuse.new(
163
- public_key: ENV['LANGFUSE_PUBLIC_KEY'],
164
- secret_key: ENV['LANGFUSE_SECRET_KEY'],
165
- host: ENV['LANGFUSE_HOST'] || 'https://cloud.langfuse.com',
166
- debug: true
167
- )
168
-
169
- # 安全地获取 prompt
170
- def safe_get_prompt(client, name)
171
- begin
172
- prompt = client.get_prompt(name)
173
- puts "✅ 成功获取 prompt: #{prompt.name}"
174
- return prompt
175
- rescue Langfuse::ValidationError => e
176
- if e.message.include?("404")
177
- puts "❌ Prompt '#{name}' 不存在"
178
- puts "建议:请先在 Langfuse 中创建这个 prompt"
179
- else
180
- puts "❌ 验证错误: #{e.message}"
181
- end
182
- rescue Langfuse::AuthenticationError => e
183
- puts "❌ 认证失败: #{e.message}"
184
- puts "建议:检查 API 密钥是否正确"
185
- rescue => e
186
- puts "❌ 未知错误: #{e.message}"
187
- end
188
- return nil
189
- end
190
-
191
- # 使用示例
192
- prompt = safe_get_prompt(client, "your-prompt-name")
193
- if prompt
194
- compiled = prompt.compile(variable: "value")
195
- puts "编译后的 prompt: #{compiled}"
196
- end
197
- ```
198
-
199
- ## 获取帮助
200
-
201
- 如果问题仍然存在,请:
202
-
203
- 1. 检查 [Langfuse 文档](https://langfuse.com/docs/prompts/get-started)
204
- 2. 访问 [GitHub Issues](https://github.com/langfuse/langfuse-ruby/issues)
205
- 3. 加入 [Discord 社区](https://discord.gg/langfuse)
206
- 4. 查看 [状态页面](https://status.langfuse.com) 了解服务状态
data/docs/CHANGELOG.md DELETED
@@ -1,49 +0,0 @@
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
- ## [0.1.0] - 2025-01-09
11
-
12
- ### Added
13
- - Initial release of Langfuse Ruby SDK
14
- - Complete tracing functionality with traces, spans, and generations
15
- - Prompt management with version control and caching
16
- - Built-in evaluation system with multiple evaluators
17
- - Comprehensive error handling and validation
18
- - Automatic event batching and background processing
19
- - Support for environment variable configuration
20
- - Extensive documentation and examples
21
- - Full test coverage with RSpec
22
- - Framework integration examples (Rails, Sidekiq)
23
-
24
- ### Features
25
- - **Tracing**: Create and manage traces with nested spans and generations
26
- - **Prompt Management**: Create, retrieve, and compile prompts with variable substitution
27
- - **Evaluation**: Built-in evaluators for exact match, similarity, length, regex, and custom scoring
28
- - **Client Management**: Robust HTTP client with retries, timeouts, and authentication
29
- - **Event Processing**: Asynchronous event queue with automatic flushing
30
- - **Error Handling**: Comprehensive error types with detailed messages
31
- - **Utilities**: Helper methods for ID generation, timestamps, and data transformation
32
-
33
- ### Dependencies
34
- - faraday (~> 2.0) - HTTP client library
35
- - faraday-net_http (~> 3.0) - Net::HTTP adapter for Faraday
36
- - json (~> 2.0) - JSON parsing and generation
37
- - concurrent-ruby (~> 1.0) - Thread-safe data structures
38
-
39
- ### Development Dependencies
40
- - bundler (~> 2.0)
41
- - rake (~> 13.0)
42
- - rspec (~> 3.0)
43
- - webmock (~> 3.0)
44
- - vcr (~> 6.0)
45
- - rubocop (~> 1.0)
46
- - yard (~> 0.9)
47
-
48
- [Unreleased]: https://github.com/ai-firstly/langfuse-ruby/compare/v0.1.0...HEAD
49
- [0.1.0]: https://github.com/ai-firstly/langfuse-ruby/releases/tag/v0.1.0
@@ -1,263 +0,0 @@
1
- # Langfuse Ruby SDK - 项目总结
2
-
3
- ## 🎉 项目完成状态
4
-
5
- ✅ **项目已完成** - 所有核心功能已实现并通过测试
6
-
7
- ## 📋 功能清单
8
-
9
- ### ✅ 已实现的功能
10
-
11
- 1. **核心架构**
12
- - [x] 模块化设计
13
- - [x] 完整的错误处理系统
14
- - [x] 配置管理
15
- - [x] 工具类和辅助方法
16
-
17
- 2. **客户端管理**
18
- - [x] HTTP 客户端(基于 Faraday)
19
- - [x] 认证系统(Basic Auth)
20
- - [x] 自动重试机制
21
- - [x] 超时处理
22
- - [x] 错误分类和处理
23
-
24
- 3. **追踪功能**
25
- - [x] Trace 创建和管理
26
- - [x] Span 创建和嵌套
27
- - [x] Generation 追踪(LLM 调用)
28
- - [x] 事件队列和批处理
29
- - [x] 后台线程自动刷新
30
-
31
- 4. **提示管理**
32
- - [x] 提示创建和获取
33
- - [x] 版本控制
34
- - [x] 缓存机制
35
- - [x] 变量编译({{variable}} 格式)
36
- - [x] 文本和聊天提示支持
37
- - [x] 提示模板类
38
-
39
- 5. **评估系统**
40
- - [x] 基础评估器框架
41
- - [x] 精确匹配评估器
42
- - [x] 相似度评估器(Levenshtein 距离)
43
- - [x] 长度评估器
44
- - [x] 包含评估器
45
- - [x] 正则表达式评估器
46
- - [x] 自定义评分系统
47
-
48
- 6. **测试和文档**
49
- - [x] 完整的测试套件
50
- - [x] 详细的 README 文档
51
- - [x] 使用示例
52
- - [x] API 文档
53
- - [x] 变更日志
54
-
55
- ## 🏗️ 项目结构
56
-
57
- ```
58
- langfuse/
59
- ├── lib/
60
- │ └── langfuse/
61
- │ ├── client.rb # 核心客户端
62
- │ ├── trace.rb # 追踪功能
63
- │ ├── span.rb # Span 管理
64
- │ ├── generation.rb # Generation 管理
65
- │ ├── prompt.rb # 提示管理
66
- │ ├── evaluation.rb # 评估系统
67
- │ ├── errors.rb # 错误定义
68
- │ ├── utils.rb # 工具类
69
- │ └── version.rb # 版本信息
70
- ├── spec/ # 测试文件
71
- ├── examples/ # 使用示例
72
- ├── langfuse.gemspec # Gem 规范
73
- ├── README.md # 项目文档
74
- ├── CHANGELOG.md # 变更日志
75
- └── LICENSE # 许可证
76
- ```
77
-
78
- ## 🔧 技术栈
79
-
80
- - **Ruby**: >= 2.7.0
81
- - **HTTP 客户端**: Faraday 2.0+
82
- - **并发处理**: concurrent-ruby 1.0+
83
- - **JSON 处理**: json 2.0+
84
- - **测试框架**: RSpec 3.0+
85
- - **代码质量**: RuboCop 1.0+
86
-
87
- ## 🚀 核心特性
88
-
89
- ### 1. 追踪系统
90
- - 支持嵌套的 traces、spans 和 generations
91
- - 自动 ID 生成和时间戳
92
- - 元数据和标签支持
93
- - 异步事件处理
94
-
95
- ### 2. 提示管理
96
- - 版本控制和缓存
97
- - 变量替换系统
98
- - 多种提示格式支持
99
- - LangChain 兼容性
100
-
101
- ### 3. 评估框架
102
- - 多种内置评估器
103
- - 可扩展的评估系统
104
- - 自定义评分支持
105
- - 详细的评估结果
106
-
107
- ### 4. 企业级特性
108
- - 完整的错误处理
109
- - 配置管理
110
- - 环境变量支持
111
- - 框架集成示例
112
-
113
- ## 📊 测试结果
114
-
115
- ```
116
- 🚀 Testing Langfuse Ruby SDK (Offline Mode)...
117
-
118
- ✅ Configuration successful
119
- ✅ Client initialization successful
120
- ✅ Trace creation successful
121
- ✅ Generation creation successful
122
- ✅ Prompt template successful
123
- ✅ Chat prompt template successful
124
- ✅ Evaluators successful (5/5)
125
- ✅ Utils successful
126
- ✅ Event queue successful
127
- ✅ Complex workflow successful
128
- ✅ Error handling successful
129
-
130
- 🎉 All offline tests completed successfully!
131
- ```
132
-
133
- ## 📚 使用示例
134
-
135
- ### 基本用法
136
- ```ruby
137
- require 'langfuse'
138
-
139
- client = Langfuse.new(
140
- public_key: "pk-lf-...",
141
- secret_key: "sk-lf-..."
142
- )
143
-
144
- trace = client.trace(
145
- name: "chat-completion",
146
- user_id: "user123",
147
- input: { message: "Hello!" }
148
- )
149
-
150
- generation = trace.generation(
151
- name: "openai-chat",
152
- model: "gpt-3.5-turbo",
153
- input: [{ role: "user", content: "Hello!" }],
154
- output: { content: "Hi there!" }
155
- )
156
-
157
- client.flush
158
- ```
159
-
160
- ### 提示管理
161
- ```ruby
162
- # 获取提示
163
- prompt = client.get_prompt("greeting-prompt")
164
-
165
- # 编译提示
166
- compiled = prompt.compile(
167
- user_name: "Alice",
168
- topic: "AI"
169
- )
170
- ```
171
-
172
- ### 评估系统
173
- ```ruby
174
- evaluator = Langfuse::Evaluators::ExactMatchEvaluator.new
175
- result = evaluator.evaluate(
176
- "What is 2+2?",
177
- "4",
178
- expected: "4"
179
- )
180
- ```
181
-
182
- ## 🔄 与官方 SDK 对比
183
-
184
- | 功能 | Python SDK | JS SDK | Ruby SDK |
185
- |------|------------|--------|----------|
186
- | 基础追踪 | ✅ | ✅ | ✅ |
187
- | 提示管理 | ✅ | ✅ | ✅ |
188
- | 评估系统 | ✅ | ✅ | ✅ |
189
- | 异步处理 | ✅ | ✅ | ✅ |
190
- | 错误处理 | ✅ | ✅ | ✅ |
191
- | 框架集成 | ✅ | ✅ | ✅ |
192
-
193
- ## 📦 发布准备
194
-
195
- ### Gem 规范
196
- - 名称: `langfuse`
197
- - 版本: `0.1.0`
198
- - 许可证: MIT
199
- - Ruby 版本: >= 2.7.0
200
-
201
- ### 依赖项
202
- ```ruby
203
- spec.add_dependency "faraday", "~> 2.0"
204
- spec.add_dependency "faraday-net_http", "~> 3.0"
205
- spec.add_dependency "json", "~> 2.0"
206
- spec.add_dependency "concurrent-ruby", "~> 1.0"
207
- ```
208
-
209
- ## 🛠️ 开发指南
210
-
211
- ### 安装依赖
212
- ```bash
213
- bundle install
214
- ```
215
-
216
- ### 运行测试
217
- ```bash
218
- bundle exec rspec
219
- ```
220
-
221
- ### 离线测试
222
- ```bash
223
- ruby test_offline.rb
224
- ```
225
-
226
- ## 🔮 未来扩展
227
-
228
- ### 可能的改进
229
- 1. **性能优化**
230
- - 连接池管理
231
- - 更高效的批处理
232
- - 内存优化
233
-
234
- 2. **功能扩展**
235
- - 更多评估器
236
- - 数据集管理
237
- - 实验功能
238
-
239
- 3. **集成支持**
240
- - Rails 集成 gem
241
- - Sidekiq 中间件
242
- - 更多框架支持
243
-
244
- ## 📄 许可证
245
-
246
- MIT License - 允许商业和开源使用
247
-
248
- ## 🤝 贡献指南
249
-
250
- 1. Fork 项目
251
- 2. 创建功能分支
252
- 3. 提交更改
253
- 4. 创建 Pull Request
254
-
255
- ## 📞 支持
256
-
257
- - GitHub Issues: 报告 bug 和功能请求
258
- - 文档: 详细的使用指南
259
- - 示例: 完整的使用示例
260
-
261
- ---
262
-
263
- **总结**: 这个 Langfuse Ruby SDK 是一个功能完整、测试充分的生产级 SDK,完全兼容 Langfuse API,可以立即用于生产环境。它提供了与官方 Python 和 JavaScript SDK 相同的功能,并且遵循 Ruby 社区的最佳实践。