langsmith-sdk 0.1.1
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 +7 -0
- data/.rspec +4 -0
- data/.rubocop.yml +120 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +48 -0
- data/LICENSE +22 -0
- data/README.md +224 -0
- data/Rakefile +8 -0
- data/examples/LLM_TRACING.md +439 -0
- data/examples/complex_agent.rb +472 -0
- data/examples/llm_tracing.rb +304 -0
- data/examples/openai_integration.rb +751 -0
- data/langsmith.gemspec +38 -0
- data/lib/langsmith/batch_processor.rb +237 -0
- data/lib/langsmith/client.rb +181 -0
- data/lib/langsmith/configuration.rb +96 -0
- data/lib/langsmith/context.rb +73 -0
- data/lib/langsmith/errors.rb +13 -0
- data/lib/langsmith/railtie.rb +86 -0
- data/lib/langsmith/run.rb +320 -0
- data/lib/langsmith/run_tree.rb +154 -0
- data/lib/langsmith/traceable.rb +120 -0
- data/lib/langsmith/version.rb +5 -0
- data/lib/langsmith.rb +144 -0
- metadata +134 -0
data/lib/langsmith.rb
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "langsmith/version"
|
|
4
|
+
require_relative "langsmith/errors"
|
|
5
|
+
require_relative "langsmith/configuration"
|
|
6
|
+
require_relative "langsmith/run"
|
|
7
|
+
require_relative "langsmith/context"
|
|
8
|
+
require_relative "langsmith/client"
|
|
9
|
+
require_relative "langsmith/batch_processor"
|
|
10
|
+
require_relative "langsmith/run_tree"
|
|
11
|
+
require_relative "langsmith/traceable"
|
|
12
|
+
|
|
13
|
+
module Langsmith
|
|
14
|
+
class << self
|
|
15
|
+
# Returns the current configuration.
|
|
16
|
+
# @return [Configuration]
|
|
17
|
+
def configuration
|
|
18
|
+
@configuration ||= Configuration.new
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Configure Langsmith with a block.
|
|
22
|
+
#
|
|
23
|
+
# @example
|
|
24
|
+
# Langsmith.configure do |config|
|
|
25
|
+
# config.api_key = "ls_..."
|
|
26
|
+
# config.tracing_enabled = true
|
|
27
|
+
# config.project = "my-project"
|
|
28
|
+
# end
|
|
29
|
+
#
|
|
30
|
+
# @yield [Configuration] the configuration instance
|
|
31
|
+
# @return [void]
|
|
32
|
+
def configure
|
|
33
|
+
yield(configuration)
|
|
34
|
+
configuration.validate!
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Reset configuration (useful for testing).
|
|
38
|
+
# @return [void]
|
|
39
|
+
def reset_configuration!
|
|
40
|
+
@configuration = Configuration.new
|
|
41
|
+
@batch_processor = nil
|
|
42
|
+
@client = nil
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Check if tracing is enabled and possible (has API key).
|
|
46
|
+
# @return [Boolean]
|
|
47
|
+
def tracing_enabled?
|
|
48
|
+
configuration.tracing_possible?
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Returns the batch processor (lazily initialized).
|
|
52
|
+
# @return [BatchProcessor]
|
|
53
|
+
def batch_processor
|
|
54
|
+
@batch_processor ||= BatchProcessor.new
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Returns the HTTP client (lazily initialized).
|
|
58
|
+
# @return [Client]
|
|
59
|
+
def client
|
|
60
|
+
@client ||= Client.new
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Main tracing API - execute a block within a traced context.
|
|
64
|
+
#
|
|
65
|
+
# @param name [String] Name of the operation being traced
|
|
66
|
+
# @param run_type [String] Type of run ("chain", "llm", "tool", etc.)
|
|
67
|
+
# @param inputs [Hash] Input data for the trace
|
|
68
|
+
# @param metadata [Hash] Additional metadata
|
|
69
|
+
# @param tags [Array<String>] Tags for filtering
|
|
70
|
+
# @param extra [Hash] Extra data (e.g., token usage)
|
|
71
|
+
# @param tenant_id [String] Tenant ID for multi-tenant scenarios (overrides global config)
|
|
72
|
+
# @param project [String] Project name for this trace (overrides global config)
|
|
73
|
+
#
|
|
74
|
+
# @example Basic tracing
|
|
75
|
+
# Langsmith.trace("my_operation", run_type: "chain") do |run|
|
|
76
|
+
# run.add_metadata(user_id: "123")
|
|
77
|
+
# result = do_something()
|
|
78
|
+
# result
|
|
79
|
+
# end
|
|
80
|
+
#
|
|
81
|
+
# @example Nested traces
|
|
82
|
+
# Langsmith.trace("parent", run_type: "chain") do
|
|
83
|
+
# Langsmith.trace("child", run_type: "llm") do
|
|
84
|
+
# call_llm()
|
|
85
|
+
# end
|
|
86
|
+
# end
|
|
87
|
+
#
|
|
88
|
+
# @example Multi-tenant tracing
|
|
89
|
+
# Langsmith.trace("operation", tenant_id: "tenant-123") do |run|
|
|
90
|
+
# # This trace goes to tenant-123
|
|
91
|
+
# end
|
|
92
|
+
#
|
|
93
|
+
# @example Project-specific tracing
|
|
94
|
+
# Langsmith.trace("operation", project: "my-special-project") do |run|
|
|
95
|
+
# # This trace goes to my-special-project
|
|
96
|
+
# end
|
|
97
|
+
#
|
|
98
|
+
# @yield [Run] the run object for adding metadata, events, etc.
|
|
99
|
+
# @return [Object] the return value of the block
|
|
100
|
+
def trace(name, run_type: "chain", inputs: nil, metadata: nil, tags: nil, extra: nil, tenant_id: nil, project: nil,
|
|
101
|
+
&block)
|
|
102
|
+
run_tree = RunTree.new(
|
|
103
|
+
name: name,
|
|
104
|
+
run_type: run_type,
|
|
105
|
+
inputs: inputs,
|
|
106
|
+
metadata: metadata,
|
|
107
|
+
tags: tags,
|
|
108
|
+
extra: extra,
|
|
109
|
+
tenant_id: tenant_id,
|
|
110
|
+
project: project
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
run_tree.execute(&block)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Flush all pending traces (blocking).
|
|
117
|
+
# Useful before application shutdown or in tests.
|
|
118
|
+
# @return [void]
|
|
119
|
+
def flush
|
|
120
|
+
batch_processor.flush
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Shutdown the batch processor gracefully.
|
|
124
|
+
# @return [void]
|
|
125
|
+
def shutdown
|
|
126
|
+
batch_processor.shutdown
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Get the current run from context (if any).
|
|
130
|
+
# @return [Run, nil]
|
|
131
|
+
def current_run
|
|
132
|
+
Context.current_run
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Check if we're currently inside a trace.
|
|
136
|
+
# @return [Boolean]
|
|
137
|
+
def tracing?
|
|
138
|
+
Context.active?
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Load Rails integration if Rails is available
|
|
144
|
+
require_relative "langsmith/railtie" if defined?(Rails::Railtie)
|
metadata
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: langsmith-sdk
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Felipe Cabezudo
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-12-21 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: concurrent-ruby
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.1'
|
|
20
|
+
- - "<"
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '3.0'
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - ">="
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '1.1'
|
|
30
|
+
- - "<"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '3.0'
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: faraday
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '2.0'
|
|
40
|
+
type: :runtime
|
|
41
|
+
prerelease: false
|
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '2.0'
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: faraday-net_http_persistent
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '2.0'
|
|
54
|
+
type: :runtime
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '2.0'
|
|
61
|
+
- !ruby/object:Gem::Dependency
|
|
62
|
+
name: faraday-retry
|
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '2.0'
|
|
68
|
+
type: :runtime
|
|
69
|
+
prerelease: false
|
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - "~>"
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '2.0'
|
|
75
|
+
description: A Ruby client for LangSmith, providing tracing and observability for
|
|
76
|
+
LLM applications
|
|
77
|
+
email:
|
|
78
|
+
- felipecabedilo@gmail.com
|
|
79
|
+
executables: []
|
|
80
|
+
extensions: []
|
|
81
|
+
extra_rdoc_files: []
|
|
82
|
+
files:
|
|
83
|
+
- ".rspec"
|
|
84
|
+
- ".rubocop.yml"
|
|
85
|
+
- ".ruby-version"
|
|
86
|
+
- CHANGELOG.md
|
|
87
|
+
- LICENSE
|
|
88
|
+
- README.md
|
|
89
|
+
- Rakefile
|
|
90
|
+
- examples/LLM_TRACING.md
|
|
91
|
+
- examples/complex_agent.rb
|
|
92
|
+
- examples/llm_tracing.rb
|
|
93
|
+
- examples/openai_integration.rb
|
|
94
|
+
- langsmith.gemspec
|
|
95
|
+
- lib/langsmith.rb
|
|
96
|
+
- lib/langsmith/batch_processor.rb
|
|
97
|
+
- lib/langsmith/client.rb
|
|
98
|
+
- lib/langsmith/configuration.rb
|
|
99
|
+
- lib/langsmith/context.rb
|
|
100
|
+
- lib/langsmith/errors.rb
|
|
101
|
+
- lib/langsmith/railtie.rb
|
|
102
|
+
- lib/langsmith/run.rb
|
|
103
|
+
- lib/langsmith/run_tree.rb
|
|
104
|
+
- lib/langsmith/traceable.rb
|
|
105
|
+
- lib/langsmith/version.rb
|
|
106
|
+
homepage: https://github.com/felipekb/langsmith-ruby-sdk
|
|
107
|
+
licenses:
|
|
108
|
+
- MIT
|
|
109
|
+
metadata:
|
|
110
|
+
allowed_push_host: https://rubygems.org
|
|
111
|
+
homepage_uri: https://github.com/felipekb/langsmith-ruby-sdk
|
|
112
|
+
source_code_uri: https://github.com/felipekb/langsmith-ruby-sdk
|
|
113
|
+
changelog_uri: https://github.com/felipekb/langsmith-ruby-sdk/blob/main/CHANGELOG.md
|
|
114
|
+
rubygems_mfa_required: 'true'
|
|
115
|
+
post_install_message:
|
|
116
|
+
rdoc_options: []
|
|
117
|
+
require_paths:
|
|
118
|
+
- lib
|
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
120
|
+
requirements:
|
|
121
|
+
- - ">="
|
|
122
|
+
- !ruby/object:Gem::Version
|
|
123
|
+
version: 3.1.0
|
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
|
+
requirements:
|
|
126
|
+
- - ">="
|
|
127
|
+
- !ruby/object:Gem::Version
|
|
128
|
+
version: '0'
|
|
129
|
+
requirements: []
|
|
130
|
+
rubygems_version: 3.5.17
|
|
131
|
+
signing_key:
|
|
132
|
+
specification_version: 4
|
|
133
|
+
summary: Ruby SDK for LangSmith tracing and observability
|
|
134
|
+
test_files: []
|