langfuse-ruby 0.1.0 → 0.1.2
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 +29 -34
- data/Gemfile +5 -5
- data/Gemfile.lock +1 -1
- data/README.md +3 -3
- data/{PUBLISH_GUIDE.md → docs/PUBLISH_GUIDE.md} +7 -7
- data/docs/README.md +29 -0
- data/examples/connection_config_demo.rb +127 -0
- data/examples/prompt_management.rb +62 -71
- data/langfuse-ruby.gemspec +12 -7
- data/lib/langfuse/client.rb +57 -18
- data/lib/langfuse/evaluation.rb +9 -8
- data/lib/langfuse/prompt.rb +2 -2
- data/lib/langfuse/version.rb +1 -1
- data/lib/langfuse.rb +14 -10
- data/scripts/release.sh +2 -2
- data/scripts/verify_release.rb +2 -2
- metadata +14 -12
- data/PROJECT_SUMMARY.md +0 -263
- data/test_basic.rb +0 -183
- /data/{FINAL_SUMMARY.md → docs/FINAL_SUMMARY.md} +0 -0
- /data/{RELEASE_CHECKLIST.md → docs/RELEASE_CHECKLIST.md} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0bb8cc1f797c0b240830c5f909665dde61bb8a5540f1db8a04393ae0d741ee44
|
4
|
+
data.tar.gz: bc5b9e1dc24e2861ac66955bec4dccd25bbadaf1a3c5f59ddbacd784a50b2180
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5486239447652d3b97eb6200bc2f24ee829bef24da86409747f6a250fcc1126ce68841673b0e67da56a75e08ff4ec1bc9edf4b241c3431b855abdf2befdd7b9
|
7
|
+
data.tar.gz: 88b61075d0a394629dec59bbec509a7f4c80b0269a887dcef866b3a8a250fd75b362fe84fc6247e72b5305935702a7175c1db825461bb05270145912d6e8f79e
|
data/CHANGELOG.md
CHANGED
@@ -5,45 +5,40 @@ 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
|
-
## [
|
8
|
+
## [0.1.1] - 2025-01-12
|
9
9
|
|
10
|
-
|
10
|
+
### Fixed
|
11
|
+
- Improved error handling for `get_prompt` method when prompt doesn't exist
|
12
|
+
- Better error messages for 404 responses that return HTML instead of JSON
|
13
|
+
- Enhanced debugging capabilities with detailed request/response logging
|
14
|
+
|
15
|
+
### Added
|
16
|
+
- Comprehensive troubleshooting guide for prompt management issues
|
17
|
+
- Better detection of HTML responses vs JSON responses
|
18
|
+
- More specific error types for different failure scenarios
|
19
|
+
|
20
|
+
### Changed
|
21
|
+
- Updated gemspec metadata to avoid RubyGems warnings
|
22
|
+
- Improved documentation with clearer error handling examples
|
23
|
+
|
24
|
+
## [0.1.0] - 2025-01-12
|
11
25
|
|
12
26
|
### Added
|
13
27
|
- Initial release of Langfuse Ruby SDK
|
14
|
-
- Complete tracing
|
15
|
-
- Prompt management with
|
16
|
-
- Built-in
|
28
|
+
- Complete tracing capabilities with traces, spans, and generations
|
29
|
+
- Prompt management with versioning and caching
|
30
|
+
- Built-in evaluators (exact match, similarity, length, contains, regex)
|
31
|
+
- Custom scoring and evaluation pipeline support
|
32
|
+
- Async event processing with automatic batching
|
17
33
|
- 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
34
|
- Framework integration examples (Rails, Sidekiq)
|
35
|
+
- Full test suite with RSpec
|
36
|
+
- Documentation and examples
|
23
37
|
|
24
38
|
### Features
|
25
|
-
- **Tracing**:
|
26
|
-
- **Prompt Management**:
|
27
|
-
- **Evaluation**:
|
28
|
-
- **
|
29
|
-
- **
|
30
|
-
- **
|
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/your-username/langfuse-ruby/compare/v0.1.0...HEAD
|
49
|
-
[0.1.0]: https://github.com/your-username/langfuse-ruby/releases/tag/v0.1.0
|
39
|
+
- **Tracing**: Full observability for LLM applications
|
40
|
+
- **Prompt Management**: Version control and deployment of prompts
|
41
|
+
- **Evaluation**: Multiple built-in evaluators and custom scoring
|
42
|
+
- **Async Processing**: Background event processing with batching
|
43
|
+
- **Type Safety**: Comprehensive error handling
|
44
|
+
- **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
@@ -1,7 +1,7 @@
|
|
1
1
|
# Langfuse Ruby SDK
|
2
2
|
|
3
3
|
[](https://badge.fury.io/rb/langfuse-ruby)
|
4
|
-
[](https://github.com/ai-firstly/langfuse-ruby/actions)
|
5
5
|
|
6
6
|
Ruby SDK for [Langfuse](https://langfuse.com) - the open-source LLM engineering platform. This SDK provides comprehensive tracing, prompt management, and evaluation capabilities for LLM applications.
|
7
7
|
|
@@ -422,7 +422,7 @@ For an interactive prompt that will allow you to experiment.
|
|
422
422
|
|
423
423
|
## Contributing
|
424
424
|
|
425
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
425
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ai-firstly/langfuse-ruby.
|
426
426
|
|
427
427
|
## License
|
428
428
|
|
@@ -433,4 +433,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
433
433
|
- [Langfuse Documentation](https://langfuse.com/docs)
|
434
434
|
- [Langfuse GitHub](https://github.com/langfuse/langfuse)
|
435
435
|
- [API Reference](https://api.reference.langfuse.com)
|
436
|
-
- [Ruby SDK Documentation](https://rubydoc.info/gems/langfuse-ruby)
|
436
|
+
- [Ruby SDK Documentation](https://rubydoc.info/gems/langfuse-ruby)
|
@@ -30,10 +30,10 @@ git add .
|
|
30
30
|
git commit -m "Initial commit: Langfuse Ruby SDK v0.1.0"
|
31
31
|
|
32
32
|
# 添加远程仓库(替换为您的 GitHub 仓库)
|
33
|
-
git remote add origin https://github.com/
|
33
|
+
git remote add origin https://github.com/ai-firstly/langfuse-ruby.git
|
34
34
|
|
35
35
|
# 推送到 GitHub
|
36
|
-
git push -u origin
|
36
|
+
git push -u origin master
|
37
37
|
```
|
38
38
|
|
39
39
|
### 3. 创建 GitHub 仓库
|
@@ -50,7 +50,7 @@ git push -u origin main
|
|
50
50
|
|
51
51
|
```bash
|
52
52
|
# 构建 gem
|
53
|
-
gem build langfuse.gemspec
|
53
|
+
gem build langfuse-ruby.gemspec
|
54
54
|
|
55
55
|
# 检查构建结果
|
56
56
|
ls -la *.gem
|
@@ -60,13 +60,13 @@ ls -la *.gem
|
|
60
60
|
|
61
61
|
```bash
|
62
62
|
# 本地安装构建的 gem
|
63
|
-
gem install ./langfuse-0.1.0.gem
|
63
|
+
gem install ./langfuse-ruby-0.1.0.gem
|
64
64
|
|
65
65
|
# 测试安装是否成功
|
66
66
|
ruby -e "require 'langfuse'; puts 'Langfuse loaded successfully'"
|
67
67
|
|
68
68
|
# 卸载本地测试版本
|
69
|
-
gem uninstall langfuse
|
69
|
+
gem uninstall langfuse-ruby
|
70
70
|
```
|
71
71
|
|
72
72
|
### 3. 运行完整测试
|
@@ -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-0.1.
|
114
|
+
gem push langfuse-ruby-0.1.x.gem
|
115
115
|
```
|
116
116
|
|
117
117
|
## 📊 发布后验证
|
data/docs/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Langfuse Ruby SDK 文档
|
2
|
+
|
3
|
+
本目录包含 Langfuse Ruby SDK 的所有文档。
|
4
|
+
|
5
|
+
## 📚 文档索引
|
6
|
+
|
7
|
+
### 开发和发布
|
8
|
+
- [📋 发布指南](PUBLISH_GUIDE.md) - 详细的 gem 发布流程
|
9
|
+
- [✅ 发布检查清单](RELEASE_CHECKLIST.md) - 发布前的检查项目
|
10
|
+
- [🎯 最终总结](FINAL_SUMMARY.md) - 项目完成情况总结
|
11
|
+
|
12
|
+
## 🔗 相关链接
|
13
|
+
|
14
|
+
- [主要 README](../README.md) - 项目主页和使用说明
|
15
|
+
- [示例代码](../examples/) - 使用示例
|
16
|
+
- [API 文档](../lib/) - 源代码文档
|
17
|
+
- [测试文件](../spec/) - 测试用例
|
18
|
+
|
19
|
+
## 📞 获取帮助
|
20
|
+
|
21
|
+
如果您在使用过程中遇到问题,请:
|
22
|
+
|
23
|
+
1. 查看相关文档
|
24
|
+
2. 检查 [GitHub Issues](https://github.com/ai-firstly/langfuse-ruby/issues)
|
25
|
+
3. 提交新的 Issue 或 Pull Request
|
26
|
+
|
27
|
+
---
|
28
|
+
|
29
|
+
**感谢您使用 Langfuse Ruby SDK!** 🚀
|
@@ -0,0 +1,127 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative '../lib/langfuse'
|
4
|
+
|
5
|
+
puts '🚀 Langfuse Ruby SDK 连接配置演示'
|
6
|
+
puts '=' * 50
|
7
|
+
|
8
|
+
# 显示默认配置
|
9
|
+
puts "\n📋 默认配置信息:"
|
10
|
+
puts " 默认主机: #{Langfuse.configuration.host}"
|
11
|
+
puts " 默认超时: #{Langfuse.configuration.timeout}秒"
|
12
|
+
puts " 默认重试: #{Langfuse.configuration.retries}次"
|
13
|
+
|
14
|
+
# 方法 1: 使用测试密钥创建客户端(仅用于演示)
|
15
|
+
puts "\n📝 方法 1: 直接参数配置"
|
16
|
+
puts '代码示例:'
|
17
|
+
puts 'client = Langfuse.new('
|
18
|
+
puts " public_key: 'pk-lf-your-public-key',"
|
19
|
+
puts " secret_key: 'sk-lf-your-secret-key',"
|
20
|
+
puts " host: 'https://us.cloud.langfuse.com'"
|
21
|
+
puts ')'
|
22
|
+
|
23
|
+
# 使用测试密钥创建客户端
|
24
|
+
test_client = Langfuse.new(
|
25
|
+
public_key: 'test-public-key',
|
26
|
+
secret_key: 'test-secret-key',
|
27
|
+
host: 'https://us.cloud.langfuse.com'
|
28
|
+
)
|
29
|
+
|
30
|
+
puts '✅ 客户端配置成功'
|
31
|
+
puts " 主机: #{test_client.host}"
|
32
|
+
puts " 超时: #{test_client.timeout}秒"
|
33
|
+
puts " 重试: #{test_client.retries}次"
|
34
|
+
|
35
|
+
# 方法 2: 全局配置
|
36
|
+
puts "\n📝 方法 2: 全局配置"
|
37
|
+
puts '代码示例:'
|
38
|
+
puts 'Langfuse.configure do |config|'
|
39
|
+
puts " config.public_key = 'pk-lf-your-public-key'"
|
40
|
+
puts " config.secret_key = 'sk-lf-your-secret-key'"
|
41
|
+
puts " config.host = 'https://us.cloud.langfuse.com'"
|
42
|
+
puts ' config.debug = true'
|
43
|
+
puts 'end'
|
44
|
+
|
45
|
+
Langfuse.configure do |config|
|
46
|
+
config.public_key = 'test-public-key'
|
47
|
+
config.secret_key = 'test-secret-key'
|
48
|
+
config.host = 'https://us.cloud.langfuse.com'
|
49
|
+
config.debug = true
|
50
|
+
config.timeout = 60
|
51
|
+
config.retries = 5
|
52
|
+
end
|
53
|
+
|
54
|
+
global_client = Langfuse.new
|
55
|
+
puts '✅ 全局配置成功'
|
56
|
+
puts " 主机: #{global_client.host}"
|
57
|
+
puts " 调试模式: #{global_client.debug}"
|
58
|
+
puts " 超时: #{global_client.timeout}秒"
|
59
|
+
puts " 重试: #{global_client.retries}次"
|
60
|
+
|
61
|
+
# 方法 3: 环境变量配置
|
62
|
+
puts "\n📝 方法 3: 环境变量配置"
|
63
|
+
puts '设置环境变量:'
|
64
|
+
puts "export LANGFUSE_PUBLIC_KEY='pk-lf-your-public-key'"
|
65
|
+
puts "export LANGFUSE_SECRET_KEY='sk-lf-your-secret-key'"
|
66
|
+
puts "export LANGFUSE_HOST='https://us.cloud.langfuse.com'"
|
67
|
+
puts ''
|
68
|
+
puts '然后使用:'
|
69
|
+
puts 'client = Langfuse.new'
|
70
|
+
|
71
|
+
if ENV['LANGFUSE_PUBLIC_KEY'] && ENV['LANGFUSE_SECRET_KEY']
|
72
|
+
env_client = Langfuse.new
|
73
|
+
puts '✅ 环境变量配置成功'
|
74
|
+
puts " 主机: #{env_client.host}"
|
75
|
+
else
|
76
|
+
puts '⚠️ 环境变量未设置,跳过此示例'
|
77
|
+
end
|
78
|
+
|
79
|
+
# 连接配置详情
|
80
|
+
puts "\n🔧 连接配置详情:"
|
81
|
+
puts '根据 Langfuse 官方文档:'
|
82
|
+
puts '1. 认证方式: HTTP Basic Auth'
|
83
|
+
puts '2. 用户名: Langfuse Public Key (pk-lf-...)'
|
84
|
+
puts '3. 密码: Langfuse Secret Key (sk-lf-...)'
|
85
|
+
puts '4. 默认服务器: https://us.cloud.langfuse.com'
|
86
|
+
puts '5. 内容类型: application/json'
|
87
|
+
puts "6. User-Agent: langfuse-ruby/#{Langfuse::VERSION}"
|
88
|
+
|
89
|
+
# 创建测试 trace(不发送到服务器)
|
90
|
+
puts "\n🧪 创建测试 Trace(离线):"
|
91
|
+
begin
|
92
|
+
trace = test_client.trace(
|
93
|
+
name: 'connection-test',
|
94
|
+
user_id: 'demo-user',
|
95
|
+
input: { message: '测试连接配置' },
|
96
|
+
metadata: { demo: true }
|
97
|
+
)
|
98
|
+
|
99
|
+
puts '✅ Trace 创建成功'
|
100
|
+
puts " ID: #{trace.id}"
|
101
|
+
puts " 名称: #{trace.name}"
|
102
|
+
puts " 用户ID: #{trace.user_id}"
|
103
|
+
|
104
|
+
# 添加 generation
|
105
|
+
generation = trace.generation(
|
106
|
+
name: 'demo-generation',
|
107
|
+
model: 'gpt-3.5-turbo',
|
108
|
+
input: [{ role: 'user', content: 'Hello!' }],
|
109
|
+
output: { content: '你好!' },
|
110
|
+
usage: { prompt_tokens: 5, completion_tokens: 3, total_tokens: 8 }
|
111
|
+
)
|
112
|
+
|
113
|
+
puts '✅ Generation 创建成功'
|
114
|
+
puts " ID: #{generation.id}"
|
115
|
+
puts " 模型: #{generation.model}"
|
116
|
+
rescue StandardError => e
|
117
|
+
puts "❌ 测试失败: #{e.message}"
|
118
|
+
end
|
119
|
+
|
120
|
+
puts "\n📚 使用提示:"
|
121
|
+
puts "1. 替换示例中的 'test-public-key' 和 'test-secret-key' 为您的真实 API 密钥"
|
122
|
+
puts '2. 确保网络连接正常'
|
123
|
+
puts '3. 启用 debug 模式可以查看详细的请求日志'
|
124
|
+
puts '4. 调用 client.flush 来发送事件到服务器'
|
125
|
+
puts '5. 使用 client.shutdown 来优雅地关闭客户端'
|
126
|
+
|
127
|
+
puts "\n🎉 连接配置演示完成!"
|
@@ -9,7 +9,7 @@ client = Langfuse.new(
|
|
9
9
|
host: ENV['LANGFUSE_HOST'] || 'https://cloud.langfuse.com'
|
10
10
|
)
|
11
11
|
|
12
|
-
puts
|
12
|
+
puts '🚀 Starting prompt management example...'
|
13
13
|
|
14
14
|
# Example 1: Create and use text prompts
|
15
15
|
puts "\n📝 Example 1: Create and use text prompts"
|
@@ -17,9 +17,9 @@ puts "\n📝 Example 1: Create and use text prompts"
|
|
17
17
|
begin
|
18
18
|
# Create a text prompt
|
19
19
|
text_prompt = client.create_prompt(
|
20
|
-
name:
|
21
|
-
prompt:
|
22
|
-
labels: [
|
20
|
+
name: 'greeting-prompt',
|
21
|
+
prompt: 'Hello {{user_name}}! Welcome to {{service_name}}. How can I help you with {{topic}} today?',
|
22
|
+
labels: %w[greeting customer-service],
|
23
23
|
config: {
|
24
24
|
temperature: 0.7,
|
25
25
|
max_tokens: 100
|
@@ -27,24 +27,22 @@ begin
|
|
27
27
|
)
|
28
28
|
|
29
29
|
puts "Created text prompt: #{text_prompt.name} (Version: #{text_prompt.version})"
|
30
|
-
|
31
30
|
rescue Langfuse::APIError => e
|
32
31
|
puts "Note: Prompt might already exist - #{e.message}"
|
33
32
|
end
|
34
33
|
|
35
34
|
# Get and use the prompt
|
36
35
|
begin
|
37
|
-
prompt = client.get_prompt(
|
36
|
+
prompt = client.get_prompt('greeting-prompt')
|
38
37
|
|
39
38
|
# Compile prompt with variables
|
40
39
|
compiled_text = prompt.compile(
|
41
|
-
user_name:
|
42
|
-
service_name:
|
43
|
-
topic:
|
40
|
+
user_name: 'Alice',
|
41
|
+
service_name: 'AI Assistant',
|
42
|
+
topic: 'machine learning'
|
44
43
|
)
|
45
44
|
|
46
45
|
puts "Compiled prompt: #{compiled_text}"
|
47
|
-
|
48
46
|
rescue Langfuse::APIError => e
|
49
47
|
puts "Could not retrieve prompt: #{e.message}"
|
50
48
|
end
|
@@ -55,18 +53,18 @@ puts "\n💬 Example 2: Create and use chat prompts"
|
|
55
53
|
begin
|
56
54
|
# Create a chat prompt
|
57
55
|
chat_prompt = client.create_prompt(
|
58
|
-
name:
|
56
|
+
name: 'ai-assistant-chat',
|
59
57
|
prompt: [
|
60
58
|
{
|
61
|
-
role:
|
62
|
-
content:
|
59
|
+
role: 'system',
|
60
|
+
content: 'You are a helpful AI assistant specialized in {{domain}}. Always be {{tone}} and provide {{detail_level}} answers.'
|
63
61
|
},
|
64
62
|
{
|
65
|
-
role:
|
66
|
-
content:
|
63
|
+
role: 'user',
|
64
|
+
content: '{{user_message}}'
|
67
65
|
}
|
68
66
|
],
|
69
|
-
labels: [
|
67
|
+
labels: %w[chat assistant ai],
|
70
68
|
config: {
|
71
69
|
temperature: 0.8,
|
72
70
|
max_tokens: 200
|
@@ -74,28 +72,26 @@ begin
|
|
74
72
|
)
|
75
73
|
|
76
74
|
puts "Created chat prompt: #{chat_prompt.name}"
|
77
|
-
|
78
75
|
rescue Langfuse::APIError => e
|
79
76
|
puts "Note: Chat prompt might already exist - #{e.message}"
|
80
77
|
end
|
81
78
|
|
82
79
|
# Get and use the chat prompt
|
83
80
|
begin
|
84
|
-
chat_prompt = client.get_prompt(
|
81
|
+
chat_prompt = client.get_prompt('ai-assistant-chat')
|
85
82
|
|
86
83
|
# Compile chat prompt with variables
|
87
84
|
compiled_messages = chat_prompt.compile(
|
88
|
-
domain:
|
89
|
-
tone:
|
90
|
-
detail_level:
|
91
|
-
user_message:
|
85
|
+
domain: 'software development',
|
86
|
+
tone: 'friendly and professional',
|
87
|
+
detail_level: 'detailed',
|
88
|
+
user_message: 'How do I implement a REST API in Ruby?'
|
92
89
|
)
|
93
90
|
|
94
|
-
puts
|
91
|
+
puts 'Compiled chat messages:'
|
95
92
|
compiled_messages.each_with_index do |message, index|
|
96
93
|
puts " #{index + 1}. #{message[:role]}: #{message[:content]}"
|
97
94
|
end
|
98
|
-
|
99
95
|
rescue Langfuse::APIError => e
|
100
96
|
puts "Could not retrieve chat prompt: #{e.message}"
|
101
97
|
end
|
@@ -112,34 +108,34 @@ puts "Template variables: #{translation_template.input_variables}"
|
|
112
108
|
|
113
109
|
# Use the template
|
114
110
|
translated_prompt = translation_template.format(
|
115
|
-
source_language:
|
116
|
-
target_language:
|
117
|
-
text:
|
111
|
+
source_language: 'English',
|
112
|
+
target_language: 'Spanish',
|
113
|
+
text: 'Hello, how are you today?'
|
118
114
|
)
|
119
115
|
|
120
116
|
puts "Translation prompt: #{translated_prompt}"
|
121
117
|
|
122
118
|
# Create a reusable chat template
|
123
119
|
coding_template = Langfuse::ChatPromptTemplate.from_messages([
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
])
|
120
|
+
{
|
121
|
+
role: 'system',
|
122
|
+
content: 'You are an expert {{language}} developer. Provide clean, well-commented code examples.'
|
123
|
+
},
|
124
|
+
{
|
125
|
+
role: 'user',
|
126
|
+
content: '{{request}}'
|
127
|
+
}
|
128
|
+
])
|
133
129
|
|
134
130
|
puts "Chat template variables: #{coding_template.input_variables}"
|
135
131
|
|
136
132
|
# Use the chat template
|
137
133
|
coding_messages = coding_template.format(
|
138
|
-
language:
|
139
|
-
request:
|
134
|
+
language: 'Ruby',
|
135
|
+
request: 'Show me how to create a simple HTTP server'
|
140
136
|
)
|
141
137
|
|
142
|
-
puts
|
138
|
+
puts 'Coding chat messages:'
|
143
139
|
coding_messages.each_with_index do |message, index|
|
144
140
|
puts " #{index + 1}. #{message[:role]}: #{message[:content]}"
|
145
141
|
end
|
@@ -149,17 +145,16 @@ puts "\n🔄 Example 4: Prompt versioning and caching"
|
|
149
145
|
|
150
146
|
# Get specific version of a prompt
|
151
147
|
begin
|
152
|
-
versioned_prompt = client.get_prompt(
|
148
|
+
versioned_prompt = client.get_prompt('greeting-prompt', version: 1)
|
153
149
|
puts "Retrieved prompt version: #{versioned_prompt.version}"
|
154
150
|
|
155
151
|
# Get latest version (cached)
|
156
|
-
latest_prompt = client.get_prompt(
|
152
|
+
latest_prompt = client.get_prompt('greeting-prompt')
|
157
153
|
puts "Latest prompt version: #{latest_prompt.version}"
|
158
154
|
|
159
155
|
# Get with label
|
160
|
-
labeled_prompt = client.get_prompt(
|
156
|
+
labeled_prompt = client.get_prompt('greeting-prompt', label: 'production')
|
161
157
|
puts "Labeled prompt: #{labeled_prompt.labels}"
|
162
|
-
|
163
158
|
rescue Langfuse::APIError => e
|
164
159
|
puts "Could not retrieve versioned prompt: #{e.message}"
|
165
160
|
end
|
@@ -169,27 +164,27 @@ puts "\n🔗 Example 5: Using prompts in tracing"
|
|
169
164
|
|
170
165
|
begin
|
171
166
|
# Get a prompt for use in generation
|
172
|
-
system_prompt = client.get_prompt(
|
167
|
+
system_prompt = client.get_prompt('ai-assistant-chat')
|
173
168
|
|
174
169
|
# Create a trace
|
175
170
|
trace = client.trace(
|
176
|
-
name:
|
177
|
-
user_id:
|
178
|
-
input: { message:
|
171
|
+
name: 'prompt-based-chat',
|
172
|
+
user_id: 'user-789',
|
173
|
+
input: { message: 'Explain Ruby blocks' }
|
179
174
|
)
|
180
175
|
|
181
176
|
# Compile the prompt
|
182
177
|
messages = system_prompt.compile(
|
183
|
-
domain:
|
184
|
-
tone:
|
185
|
-
detail_level:
|
186
|
-
user_message:
|
178
|
+
domain: 'Ruby programming',
|
179
|
+
tone: 'educational and clear',
|
180
|
+
detail_level: 'beginner-friendly',
|
181
|
+
user_message: 'Explain Ruby blocks'
|
187
182
|
)
|
188
183
|
|
189
184
|
# Create generation with prompt
|
190
185
|
generation = trace.generation(
|
191
|
-
name:
|
192
|
-
model:
|
186
|
+
name: 'openai-chat-with-prompt',
|
187
|
+
model: 'gpt-3.5-turbo',
|
193
188
|
input: messages,
|
194
189
|
output: {
|
195
190
|
content: "Ruby blocks are pieces of code that can be passed to methods. They're defined using either do...end or curly braces {}. Blocks are commonly used with iterators like .each, .map, and .select."
|
@@ -207,7 +202,6 @@ begin
|
|
207
202
|
|
208
203
|
puts "Created generation with prompt: #{generation.id}"
|
209
204
|
puts "Trace URL: #{trace.get_url}"
|
210
|
-
|
211
205
|
rescue Langfuse::APIError => e
|
212
206
|
puts "Could not use prompt in tracing: #{e.message}"
|
213
207
|
end
|
@@ -218,12 +212,12 @@ puts "\n🎯 Example 6: Advanced prompt features"
|
|
218
212
|
# Create a prompt with complex templating
|
219
213
|
begin
|
220
214
|
complex_prompt = client.create_prompt(
|
221
|
-
name:
|
215
|
+
name: 'code-review-prompt',
|
222
216
|
prompt: {
|
223
|
-
system:
|
217
|
+
system: 'You are a senior {{language}} developer reviewing code. Focus on {{review_aspects}}.',
|
224
218
|
user: "Please review this {{language}} code:\n\n```{{language}}\n{{code}}\n```\n\nProvide feedback on: {{specific_feedback}}"
|
225
219
|
},
|
226
|
-
labels: [
|
220
|
+
labels: %w[code-review development],
|
227
221
|
config: {
|
228
222
|
temperature: 0.3,
|
229
223
|
max_tokens: 500
|
@@ -231,7 +225,6 @@ begin
|
|
231
225
|
)
|
232
226
|
|
233
227
|
puts "Created complex prompt: #{complex_prompt.name}"
|
234
|
-
|
235
228
|
rescue Langfuse::APIError => e
|
236
229
|
puts "Note: Complex prompt might already exist - #{e.message}"
|
237
230
|
end
|
@@ -239,16 +232,14 @@ end
|
|
239
232
|
# Create a prompt with conditional logic (using Ruby)
|
240
233
|
class ConditionalPrompt
|
241
234
|
def self.generate(user_level:, topic:, include_examples: true)
|
242
|
-
base_prompt =
|
235
|
+
base_prompt = 'Explain {{topic}} for a {{user_level}} audience.'
|
243
236
|
|
244
|
-
if include_examples
|
245
|
-
base_prompt += " Include practical examples."
|
246
|
-
end
|
237
|
+
base_prompt += ' Include practical examples.' if include_examples
|
247
238
|
|
248
|
-
if user_level ==
|
249
|
-
base_prompt +=
|
250
|
-
elsif user_level ==
|
251
|
-
base_prompt +=
|
239
|
+
if user_level == 'beginner'
|
240
|
+
base_prompt += ' Use simple language and avoid jargon.'
|
241
|
+
elsif user_level == 'advanced'
|
242
|
+
base_prompt += ' Feel free to use technical terminology.'
|
252
243
|
end
|
253
244
|
|
254
245
|
base_prompt
|
@@ -256,8 +247,8 @@ class ConditionalPrompt
|
|
256
247
|
end
|
257
248
|
|
258
249
|
conditional_prompt_text = ConditionalPrompt.generate(
|
259
|
-
user_level:
|
260
|
-
topic:
|
250
|
+
user_level: 'beginner',
|
251
|
+
topic: 'machine learning',
|
261
252
|
include_examples: true
|
262
253
|
)
|
263
254
|
|
@@ -266,8 +257,8 @@ puts "Conditional prompt: #{conditional_prompt_text}"
|
|
266
257
|
# Use with template
|
267
258
|
conditional_template = Langfuse::PromptTemplate.from_template(conditional_prompt_text)
|
268
259
|
formatted_prompt = conditional_template.format(
|
269
|
-
topic:
|
270
|
-
user_level:
|
260
|
+
topic: 'neural networks',
|
261
|
+
user_level: 'beginner'
|
271
262
|
)
|
272
263
|
|
273
264
|
puts "Formatted conditional prompt: #{formatted_prompt}"
|
@@ -277,7 +268,7 @@ puts "\n🔄 Flushing events..."
|
|
277
268
|
client.flush
|
278
269
|
|
279
270
|
puts "\n✅ Prompt management example completed!"
|
280
|
-
puts
|
271
|
+
puts 'Check your Langfuse dashboard to see the prompts and traces.'
|
281
272
|
|
282
273
|
# Shutdown client
|
283
274
|
client.shutdown
|