braintrust 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.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -0
  3. data/lib/braintrust/api/functions.rb +11 -0
  4. data/lib/braintrust/contrib/anthropic/instrumentation/beta_messages.rb +242 -0
  5. data/lib/braintrust/contrib/anthropic/integration.rb +2 -2
  6. data/lib/braintrust/contrib/anthropic/patcher.rb +83 -0
  7. data/lib/braintrust/contrib/openai/instrumentation/moderations.rb +93 -0
  8. data/lib/braintrust/contrib/openai/integration.rb +1 -1
  9. data/lib/braintrust/contrib/openai/patcher.rb +43 -0
  10. data/lib/braintrust/contrib/ruby_openai/instrumentation/moderations.rb +94 -0
  11. data/lib/braintrust/contrib/ruby_openai/integration.rb +1 -1
  12. data/lib/braintrust/contrib/ruby_openai/patcher.rb +35 -0
  13. data/lib/braintrust/internal/env.rb +6 -0
  14. data/lib/braintrust/internal/template.rb +91 -0
  15. data/lib/braintrust/prompt.rb +143 -0
  16. data/lib/braintrust/state.rb +1 -1
  17. data/lib/braintrust/trace.rb +41 -0
  18. data/lib/braintrust/vendor/mustache/context.rb +180 -0
  19. data/lib/braintrust/vendor/mustache/context_miss.rb +22 -0
  20. data/lib/braintrust/vendor/mustache/enumerable.rb +14 -0
  21. data/lib/braintrust/vendor/mustache/generator.rb +188 -0
  22. data/lib/braintrust/vendor/mustache/mustache.rb +260 -0
  23. data/lib/braintrust/vendor/mustache/parser.rb +364 -0
  24. data/lib/braintrust/vendor/mustache/settings.rb +252 -0
  25. data/lib/braintrust/vendor/mustache/template.rb +138 -0
  26. data/lib/braintrust/vendor/mustache/utils.rb +42 -0
  27. data/lib/braintrust/vendor/mustache.rb +16 -0
  28. data/lib/braintrust/version.rb +1 -1
  29. data/lib/braintrust.rb +1 -0
  30. metadata +16 -1
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Vendored from mustache gem v1.1.1
4
+ # https://github.com/mustache/mustache
5
+ # License: MIT
6
+ # Modifications: Namespaced under Braintrust::Vendor
7
+
8
+ require "cgi"
9
+
10
+ require_relative "parser"
11
+ require_relative "generator"
12
+
13
+ module Braintrust
14
+ module Vendor
15
+ class Mustache
16
+ # A Template represents a Mustache template. It compiles and caches
17
+ # a raw string template into something usable.
18
+ #
19
+ # The idea is this: when handed a Mustache template, convert it into
20
+ # a Ruby string by transforming Mustache tags into interpolated
21
+ # Ruby.
22
+ #
23
+ # You shouldn't use this class directly, instead:
24
+ #
25
+ # >> Braintrust::Vendor::Mustache.render(template, hash)
26
+ class Template
27
+ attr_reader :source
28
+
29
+ # Expects a Mustache template as a string along with a template
30
+ # path, which it uses to find partials. Options may be passed.
31
+ def initialize(source, options = {})
32
+ @source = source
33
+ @options = options
34
+ end
35
+
36
+ # Renders the `@source` Mustache template using the given
37
+ # `context`, which should be a simple hash keyed with symbols.
38
+ #
39
+ # The first time a template is rendered, this method is overriden
40
+ # and from then on it is "compiled". Subsequent calls will skip
41
+ # the compilation step and run the Ruby version of the template
42
+ # directly.
43
+ def render(context)
44
+ # Compile our Mustache template into a Ruby string
45
+ compiled = "def render(ctx) #{compile} end"
46
+
47
+ # Here we rewrite ourself with the interpolated Ruby version of
48
+ # our Mustache template so subsequent calls are very fast and
49
+ # can skip the compilation stage.
50
+ instance_eval(compiled, __FILE__, __LINE__ - 1)
51
+
52
+ # Call the newly rewritten version of #render
53
+ render(context)
54
+ end
55
+
56
+ # Does the dirty work of transforming a Mustache template into an
57
+ # interpolation-friendly Ruby string.
58
+ def compile(src = @source)
59
+ Generator.new(@options).compile(tokens(src))
60
+ end
61
+ alias_method :to_s, :compile
62
+
63
+ # Returns an array of tokens for a given template.
64
+ #
65
+ # @return [Array] Array of tokens.
66
+ #
67
+ def tokens(src = @source)
68
+ Parser.new(@options).compile(src)
69
+ end
70
+
71
+ # Returns an array of tags.
72
+ #
73
+ # Tags that belong to sections will be of the form `section1.tag`.
74
+ #
75
+ # @return [Array] Returns an array of tags.
76
+ #
77
+ def tags
78
+ Template.recursor(tokens, []) do |token, section|
79
+ if [:etag, :utag].include?(token[1])
80
+ [new_token = nil, new_section = nil, result = ((section + [token[2][2][0]]).join(".")), stop = true]
81
+ elsif [:section, :inverted_section].include?(token[1])
82
+ [new_token = token[4], new_section = (section + [token[2][2][0]]), result = nil, stop = false]
83
+ else
84
+ [new_token = token, new_section = section, result = nil, stop = false]
85
+ end
86
+ end.flatten.reject(&:nil?).uniq
87
+ end
88
+
89
+ # Returns an array of sections.
90
+ #
91
+ # Sections that belong to other sections will be of the form `section1.childsection`
92
+ #
93
+ # @return [Array] Returns an array of section.
94
+ #
95
+ def sections
96
+ Template.recursor(tokens, []) do |token, section|
97
+ if [:section, :inverted_section].include?(token[1])
98
+ new_section = (section + [token[2][2][0]])
99
+ [new_token = token[4], new_section, result = new_section.join("."), stop = false]
100
+ else
101
+ [new_token = token, new_section = section, result = nil, stop = false]
102
+ end
103
+ end.flatten.reject(&:nil?).uniq
104
+ end
105
+
106
+ # Returns an array of partials.
107
+ #
108
+ # Partials that belong to sections are included, but the section name is not preserved
109
+ #
110
+ # @return [Array] Returns an array of partials.
111
+ #
112
+ def partials
113
+ Template.recursor(tokens, []) do |token, section|
114
+ if token[1] == :partial
115
+ [new_token = token, new_section = section, result = token[2], stop = true]
116
+ else
117
+ [new_token = token, new_section = section, result = nil, stop = false]
118
+ end
119
+ end.flatten.reject(&:nil?).uniq
120
+ end
121
+
122
+ # Simple recursive iterator for tokens
123
+ def self.recursor(toks, section, &block)
124
+ toks.map do |token|
125
+ next unless token.is_a? Array
126
+
127
+ if token.first == :mustache
128
+ new_token, new_section, result, stop = yield(token, section)
129
+ [result] + (stop ? [] : recursor(new_token, new_section, &block))
130
+ else
131
+ recursor(token, section, &block)
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Vendored from mustache gem v1.1.1
4
+ # https://github.com/mustache/mustache
5
+ # License: MIT
6
+ # Modifications: Namespaced under Braintrust::Vendor
7
+
8
+ module Braintrust
9
+ module Vendor
10
+ class Mustache
11
+ module Utils
12
+ class String
13
+ def initialize(string)
14
+ @string = string
15
+ end
16
+
17
+ def classify
18
+ @string.split("/").map do |namespace|
19
+ namespace.split(/[-_]/).map do |part|
20
+ part[0] = part.chars.first.upcase
21
+ part
22
+ end.join
23
+ end.join("::")
24
+ end
25
+
26
+ def underscore(view_namespace)
27
+ @string
28
+ .dup
29
+ .split("#{view_namespace}::")
30
+ .last
31
+ .split("::")
32
+ .map do |part|
33
+ part[0] = part[0].downcase
34
+ part.gsub(/[A-Z]/) { |s| "_" << s.downcase }
35
+ end
36
+ .join("/")
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Vendored Mustache template engine
4
+ # From mustache gem v1.1.1 - https://github.com/mustache/mustache
5
+ # License: MIT
6
+ #
7
+ # Modifications from original:
8
+ # - Namespaced under Braintrust::Vendor to avoid conflicts
9
+ # - Disabled HTML escaping (LLM prompts don't need HTML entity encoding)
10
+ #
11
+ # This vendored version ensures:
12
+ # - No external dependency required
13
+ # - Consistent behavior across all SDK users
14
+ # - No HTML escaping that would corrupt prompts containing < > & characters
15
+
16
+ require_relative "mustache/mustache"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Braintrust
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/braintrust.rb CHANGED
@@ -5,6 +5,7 @@ require_relative "braintrust/config"
5
5
  require_relative "braintrust/state"
6
6
  require_relative "braintrust/trace"
7
7
  require_relative "braintrust/api"
8
+ require_relative "braintrust/prompt"
8
9
  require_relative "braintrust/internal/experiments"
9
10
  require_relative "braintrust/internal/env"
10
11
  require_relative "braintrust/eval"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: braintrust
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Braintrust
@@ -196,6 +196,7 @@ files:
196
196
  - lib/braintrust/config.rb
197
197
  - lib/braintrust/contrib.rb
198
198
  - lib/braintrust/contrib/anthropic/deprecated.rb
199
+ - lib/braintrust/contrib/anthropic/instrumentation/beta_messages.rb
199
200
  - lib/braintrust/contrib/anthropic/instrumentation/common.rb
200
201
  - lib/braintrust/contrib/anthropic/instrumentation/messages.rb
201
202
  - lib/braintrust/contrib/anthropic/integration.rb
@@ -205,6 +206,7 @@ files:
205
206
  - lib/braintrust/contrib/openai/deprecated.rb
206
207
  - lib/braintrust/contrib/openai/instrumentation/chat.rb
207
208
  - lib/braintrust/contrib/openai/instrumentation/common.rb
209
+ - lib/braintrust/contrib/openai/instrumentation/moderations.rb
208
210
  - lib/braintrust/contrib/openai/instrumentation/responses.rb
209
211
  - lib/braintrust/contrib/openai/integration.rb
210
212
  - lib/braintrust/contrib/openai/patcher.rb
@@ -219,6 +221,7 @@ files:
219
221
  - lib/braintrust/contrib/ruby_openai/deprecated.rb
220
222
  - lib/braintrust/contrib/ruby_openai/instrumentation/chat.rb
221
223
  - lib/braintrust/contrib/ruby_openai/instrumentation/common.rb
224
+ - lib/braintrust/contrib/ruby_openai/instrumentation/moderations.rb
222
225
  - lib/braintrust/contrib/ruby_openai/instrumentation/responses.rb
223
226
  - lib/braintrust/contrib/ruby_openai/integration.rb
224
227
  - lib/braintrust/contrib/ruby_openai/patcher.rb
@@ -237,15 +240,27 @@ files:
237
240
  - lib/braintrust/internal/encoding.rb
238
241
  - lib/braintrust/internal/env.rb
239
242
  - lib/braintrust/internal/experiments.rb
243
+ - lib/braintrust/internal/template.rb
240
244
  - lib/braintrust/internal/thread_pool.rb
241
245
  - lib/braintrust/internal/time.rb
242
246
  - lib/braintrust/logger.rb
247
+ - lib/braintrust/prompt.rb
243
248
  - lib/braintrust/setup.rb
244
249
  - lib/braintrust/state.rb
245
250
  - lib/braintrust/trace.rb
246
251
  - lib/braintrust/trace/attachment.rb
247
252
  - lib/braintrust/trace/span_filter.rb
248
253
  - lib/braintrust/trace/span_processor.rb
254
+ - lib/braintrust/vendor/mustache.rb
255
+ - lib/braintrust/vendor/mustache/context.rb
256
+ - lib/braintrust/vendor/mustache/context_miss.rb
257
+ - lib/braintrust/vendor/mustache/enumerable.rb
258
+ - lib/braintrust/vendor/mustache/generator.rb
259
+ - lib/braintrust/vendor/mustache/mustache.rb
260
+ - lib/braintrust/vendor/mustache/parser.rb
261
+ - lib/braintrust/vendor/mustache/settings.rb
262
+ - lib/braintrust/vendor/mustache/template.rb
263
+ - lib/braintrust/vendor/mustache/utils.rb
249
264
  - lib/braintrust/version.rb
250
265
  homepage: https://github.com/braintrustdata/braintrust-sdk-ruby
251
266
  licenses: