nitro_intelligence 4.3.0 → 4.3.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/docs/CHANGELOG.md +16 -0
- data/docs/README.md +1 -1
- data/lib/nitro_intelligence/client/handlers/base_handler.rb +87 -0
- data/lib/nitro_intelligence/client/handlers/observed/chat_handler.rb +2 -6
- data/lib/nitro_intelligence/client/handlers/observed/embedding_handler.rb +9 -2
- data/lib/nitro_intelligence/client/handlers/observed/image_handler.rb +2 -6
- data/lib/nitro_intelligence/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7cbb853c952d95a2f7daf43fe1b24e6fdc22a86037a521c90c35d6d15a3101a2
|
|
4
|
+
data.tar.gz: a7f86718cdc8408cf2a87011214ce9e4cf4cce904eaac9560f24decec840f89c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 239ee2221ce1120a1ff04b0a68316a8fb762086aebcf57ec2e7fc72302de51ae2b1cf30ea8cca40ab79c2cceb3af8e649cd23584192bc4547eec63b6730d13cb
|
|
7
|
+
data.tar.gz: 38b82841af416d45a904e3e8fbd63b50056dcbbb20e3d33fadb0b02fef7618b201c9c3e302dc217f06eeb8944c2c5a0a1a56fcf4ee9c7aa8023dae887a1e7dbd
|
data/docs/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,22 @@ 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.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [4.3.2](https://github.com/powerhome/nitro-intelligence.rb/compare/v4.3.1...v4.3.2) (2026-11-23)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
|
|
13
|
+
* **embeddings:** stop Cerebro double-counting embedding usage ([#222](https://github.com/powerhome/nitro-intelligence.rb/issues/222)) ([93e076c](https://github.com/powerhome/nitro-intelligence.rb/commit/93e076c681d54e3d1baa789f4d74ada24637a3d1))
|
|
14
|
+
|
|
15
|
+
## [4.3.1](https://github.com/powerhome/nitro-intelligence.rb/compare/v4.3.0...v4.3.1) (2026-11-11)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Fixed
|
|
19
|
+
|
|
20
|
+
* record the cached-input and cache-write tokens a chat generation reports in Cerebro ([#211](https://github.com/powerhome/nitro-intelligence.rb/issues/211)) ([460a10f](https://github.com/powerhome/nitro-intelligence.rb/commit/460a10f2fb3b2a04ad28369eeec71ebadc3318a4))
|
|
21
|
+
* record the reasoning tokens a chat generation spent ([#211](https://github.com/powerhome/nitro-intelligence.rb/issues/211)) ([460a10f](https://github.com/powerhome/nitro-intelligence.rb/commit/460a10f2fb3b2a04ad28369eeec71ebadc3318a4))
|
|
22
|
+
* show a chat generation's reasoning in Cerebro's formatted view ([#212](https://github.com/powerhome/nitro-intelligence.rb/issues/212)) ([d7ffc8d](https://github.com/powerhome/nitro-intelligence.rb/commit/d7ffc8dc42d666af09d4f2ccade997b0345959d1))
|
|
23
|
+
|
|
8
24
|
## [4.3.0](https://github.com/powerhome/nitro-intelligence.rb/compare/v4.2.0...v4.3.0) (2026-11-11)
|
|
9
25
|
|
|
10
26
|
|
data/docs/README.md
CHANGED
|
@@ -297,7 +297,7 @@ client.embed_query(query: "How do I request time off?", parameters: { dimensions
|
|
|
297
297
|
|
|
298
298
|
A model's `query_prefix` and `document_prefix` in `config.model_config` are what it expects before a query's text and before a document's, in the form it was trained on. Qwen3-Embedding takes an instruction before each query and nothing before a document, as in the configuration above; E5 takes `"query: "` and `"passage: "`. Leave both out for a model that embeds queries and documents the same way, such as OpenAI's. Switching to another embeddings model only means changing its `model_config` entry, but vectors from different models cannot be compared, so it also means embedding your stored documents again.
|
|
299
299
|
|
|
300
|
-
`embeddings.usage` holds `prompt_tokens` and `total_tokens` summed over every request, and `embeddings.responses` the gateway's responses, one per batch, each with its `last_response`. With observability enabled, each call is recorded in Cerebro as an embedding observation named `embed-query` or `embed-documents`, with the input as the model received it, prefix included, the gateway's cost added up over every batch, and the number and size of the vectors as output. The vectors themselves are never recorded. If the gateway did not price every batch, the cost is left unset rather than undercounted. If a batch fails after others succeeded, the failed call still records what those cost before the error is raised.
|
|
300
|
+
`embeddings.usage` holds `prompt_tokens` and `total_tokens` summed over every request, and `embeddings.responses` the gateway's responses, one per batch, each with its `last_response`. With observability enabled, each call is recorded in Cerebro as an embedding observation named `embed-query` or `embed-documents`, with the input as the model received it, prefix included, the input tokens as usage, the gateway's cost added up over every batch, and the number and size of the vectors as output. The vectors themselves are never recorded. If the gateway did not price every batch, the cost is left unset rather than undercounted. If a batch fails after others succeeded, the failed call still records what those cost before the error is raised.
|
|
301
301
|
|
|
302
302
|
Embeddings take no managed prompt: `prompt_name` and `prompt_variables` are ignored.
|
|
303
303
|
|
|
@@ -65,8 +65,95 @@ module NitroIntelligence
|
|
|
65
65
|
}.compact
|
|
66
66
|
end
|
|
67
67
|
|
|
68
|
+
# A chat completion's message as Cerebro should record it, with its reasoning moved to
|
|
69
|
+
# where Cerebro's formatted view shows it.
|
|
70
|
+
#
|
|
71
|
+
# The gateway returns reasoning as the message's `reasoning_content`, and LiteLLM repeats
|
|
72
|
+
# it as `provider_specific_fields.reasoning`. Cerebro's formatted view reads neither: it
|
|
73
|
+
# shows a message's reasoning only from its `thinking` list, so the text is otherwise
|
|
74
|
+
# visible in the JSON view alone, and recorded twice. Here it is recorded once, as
|
|
75
|
+
# `thinking`. Only reasoning that is text is moved: anything else a provider sends there,
|
|
76
|
+
# and every other provider field, is kept as the gateway returned it.
|
|
77
|
+
#
|
|
78
|
+
# A copy, because openai-ruby's `to_h` is the message's own hash: changing it, or letting
|
|
79
|
+
# the observer truncate it, would change the response the caller gets back.
|
|
80
|
+
def recorded_message(message)
|
|
81
|
+
recorded = message.to_h.deep_dup
|
|
82
|
+
reasoning = reasoning_text(recorded[:reasoning_content])
|
|
83
|
+
recorded.delete(:reasoning_content) if reasoning
|
|
84
|
+
reasoning = take_provider_reasoning(recorded, reasoning) || reasoning
|
|
85
|
+
|
|
86
|
+
recorded[:thinking] = [{ type: "thinking", content: reasoning }] if reasoning
|
|
87
|
+
recorded
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# The breakdowns of a chat completion's usage recorded in Cerebro, each under the name
|
|
91
|
+
# Cerebro is sent and the names the gateway may report it under, first match winning.
|
|
92
|
+
# Cerebro takes every breakdown it is sent out of the input or output count and records
|
|
93
|
+
# it alongside, so only breakdowns that are part of that count and do not overlap one
|
|
94
|
+
# another are sent. The gateway reports others that are not: it mirrors a cache write
|
|
95
|
+
# as both `cache_write_tokens` and `cache_creation_tokens`, splits tokens by modality
|
|
96
|
+
# (`text_tokens`, `image_tokens` and the like) across the cached and reasoning ones,
|
|
97
|
+
# and adds counts such as `initial_prompt_tokens` that are not part of the input at all.
|
|
98
|
+
PROMPT_TOKEN_DETAILS = {
|
|
99
|
+
cached_tokens: %i[cached_tokens],
|
|
100
|
+
cache_write_tokens: %i[cache_write_tokens cache_creation_tokens],
|
|
101
|
+
}.freeze
|
|
102
|
+
COMPLETION_TOKEN_DETAILS = {
|
|
103
|
+
reasoning_tokens: %i[reasoning_tokens],
|
|
104
|
+
}.freeze
|
|
105
|
+
|
|
106
|
+
# A chat completion's token usage, in the shape the gateway reports it, breakdowns
|
|
107
|
+
# included. Cerebro reads this shape itself: it records each breakdown under a key of
|
|
108
|
+
# its own (reasoning as `output_reasoning_tokens`, cached input as
|
|
109
|
+
# `input_cached_tokens`) and takes it out of the output or input count it belongs to,
|
|
110
|
+
# so the total stays the one the gateway reported. Flattening a breakdown into a key of
|
|
111
|
+
# our own would instead be counted on top of the output it is part of.
|
|
112
|
+
def chat_usage_details(usage)
|
|
113
|
+
{
|
|
114
|
+
prompt_tokens: usage.prompt_tokens,
|
|
115
|
+
completion_tokens: usage.completion_tokens,
|
|
116
|
+
total_tokens: usage.total_tokens,
|
|
117
|
+
prompt_tokens_details: token_counts(usage.prompt_tokens_details, PROMPT_TOKEN_DETAILS),
|
|
118
|
+
completion_tokens_details: token_counts(usage.completion_tokens_details, COMPLETION_TOKEN_DETAILS),
|
|
119
|
+
}.compact
|
|
120
|
+
end
|
|
121
|
+
|
|
68
122
|
private
|
|
69
123
|
|
|
124
|
+
def reasoning_text(value)
|
|
125
|
+
value if value.is_a?(String) && value.present?
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Removes LiteLLM's copy of the reasoning from the provider fields, and returns it, when it
|
|
129
|
+
# is text that repeats the message's own or the message has none. Reasoning that says
|
|
130
|
+
# something else, or is not text, is left where it is.
|
|
131
|
+
def take_provider_reasoning(recorded, reasoning)
|
|
132
|
+
provider_fields = recorded[:provider_specific_fields]
|
|
133
|
+
return nil unless provider_fields.is_a?(Hash)
|
|
134
|
+
|
|
135
|
+
provider_reasoning = reasoning_text(provider_fields[:reasoning])
|
|
136
|
+
return nil unless provider_reasoning && (reasoning.nil? || provider_reasoning == reasoning)
|
|
137
|
+
|
|
138
|
+
remaining = provider_fields.except(:reasoning)
|
|
139
|
+
if remaining.empty?
|
|
140
|
+
recorded.delete(:provider_specific_fields)
|
|
141
|
+
else
|
|
142
|
+
recorded[:provider_specific_fields] = remaining
|
|
143
|
+
end
|
|
144
|
+
provider_fields[:reasoning]
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# The breakdowns `names` lists that `details` reports a count for. Cerebro rejects the
|
|
148
|
+
# whole usage if any breakdown value is not a count, so a value that is not one is left
|
|
149
|
+
# out rather than costing the generation its token counts.
|
|
150
|
+
def token_counts(details, names)
|
|
151
|
+
reported = details.to_h
|
|
152
|
+
names.to_h do |name, aliases|
|
|
153
|
+
[name, aliases.map { |key| reported[key] }.find { |value| value.is_a?(Integer) && value >= 0 }]
|
|
154
|
+
end.compact.presence
|
|
155
|
+
end
|
|
156
|
+
|
|
70
157
|
# `last_response` carries the HTTP metadata of the response a model was built
|
|
71
158
|
# from. The client leaves it unset on nested and locally constructed models,
|
|
72
159
|
# so both the method and its value are optional.
|
|
@@ -98,16 +98,12 @@ module NitroIntelligence
|
|
|
98
98
|
chat_completion = @base_handler.perform_request(
|
|
99
99
|
parameters:, correlation_trace_id: generation.trace_id, mask_content: project.mask_content?
|
|
100
100
|
)
|
|
101
|
-
output = chat_completion.choices.first.message
|
|
101
|
+
output = @base_handler.recorded_message(chat_completion.choices.first.message)
|
|
102
102
|
|
|
103
103
|
trace_attributes = {
|
|
104
104
|
model: chat_completion.model,
|
|
105
105
|
output:,
|
|
106
|
-
usage_details:
|
|
107
|
-
prompt_tokens: chat_completion.usage.prompt_tokens,
|
|
108
|
-
completion_tokens: chat_completion.usage.completion_tokens,
|
|
109
|
-
total_tokens: chat_completion.usage.total_tokens,
|
|
110
|
-
},
|
|
106
|
+
usage_details: @base_handler.chat_usage_details(chat_completion.usage),
|
|
111
107
|
cost_details: @base_handler.cost_details(chat_completion),
|
|
112
108
|
}
|
|
113
109
|
|
|
@@ -44,7 +44,7 @@ module NitroIntelligence
|
|
|
44
44
|
trace_attributes = {
|
|
45
45
|
model: embeddings.model,
|
|
46
46
|
output: { count: embeddings.vectors.size, dimensions: embeddings.vector&.size },
|
|
47
|
-
usage_details: embeddings.usage
|
|
47
|
+
usage_details: usage_details(embeddings.usage),
|
|
48
48
|
cost_details: @base_handler.cost_details(embeddings),
|
|
49
49
|
}
|
|
50
50
|
|
|
@@ -61,11 +61,18 @@ module NitroIntelligence
|
|
|
61
61
|
|
|
62
62
|
billed = Embeddings.new(responses)
|
|
63
63
|
attributes = {
|
|
64
|
-
usage_details: billed.usage
|
|
64
|
+
usage_details: usage_details(billed.usage),
|
|
65
65
|
cost_details: @base_handler.cost_details(billed),
|
|
66
66
|
}.compact
|
|
67
67
|
embedding.update(attributes) if attributes.any?
|
|
68
68
|
end
|
|
69
|
+
|
|
70
|
+
# Recorded under Langfuse's own `input` and `total` keys. OpenAI's names are only recognised
|
|
71
|
+
# alongside `completion_tokens`, which an embedding has none of, so Langfuse would otherwise
|
|
72
|
+
# keep `prompt_tokens` and `total_tokens` as two counters and total them, doubling the count.
|
|
73
|
+
def usage_details(usage)
|
|
74
|
+
{ input: usage[:prompt_tokens], total: usage[:total_tokens] }.compact.presence
|
|
75
|
+
end
|
|
69
76
|
end
|
|
70
77
|
end
|
|
71
78
|
end
|
|
@@ -89,18 +89,14 @@ module NitroIntelligence
|
|
|
89
89
|
# A copy, because recording replaces its image URLs and the caller may send the same
|
|
90
90
|
# messages again.
|
|
91
91
|
input = parameters[:messages].deep_dup
|
|
92
|
-
output = chat_completion.choices.first.message
|
|
92
|
+
output = @base_handler.recorded_message(chat_completion.choices.first.message)
|
|
93
93
|
handle_image_generation_uploads(input, output, image_generation)
|
|
94
94
|
|
|
95
95
|
trace_attributes = {
|
|
96
96
|
model: chat_completion.model,
|
|
97
97
|
input:,
|
|
98
98
|
output:,
|
|
99
|
-
usage_details:
|
|
100
|
-
prompt_tokens: chat_completion.usage.prompt_tokens,
|
|
101
|
-
completion_tokens: chat_completion.usage.completion_tokens,
|
|
102
|
-
total_tokens: chat_completion.usage.total_tokens,
|
|
103
|
-
},
|
|
99
|
+
usage_details: @base_handler.chat_usage_details(chat_completion.usage),
|
|
104
100
|
cost_details: @base_handler.cost_details(chat_completion),
|
|
105
101
|
}
|
|
106
102
|
|