nitro_intelligence 4.3.0 → 4.3.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 +4 -4
- data/docs/CHANGELOG.md +9 -0
- 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/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: 7257a5598a1874c82985d7ad53c510630324d188fb0b8357b9cbd0f2bd2a2a01
|
|
4
|
+
data.tar.gz: 257cf46ad150fdf356ffb18163d632f78e4c1cf83ada368fb821bac28af12e7a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 57af436d4123e3f5ebe01c00e035d41bcc7a6de019c0318d8621b07db6dc25e780548414aaa3ce0975938ac686503db9b2ed942e9ebc2d0c3ef6fb3c41ad49a3
|
|
7
|
+
data.tar.gz: fbe0b679170af183511c157dd59fad3ed5e9fd13cc38bb9dfac19fa6a3fe17b7868a71181ed7dcf8cfe2ad82f38d71e8daecf48f113e96e0971741f32fd0171b
|
data/docs/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,15 @@ 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.1](https://github.com/powerhome/nitro-intelligence.rb/compare/v4.3.0...v4.3.1) (2026-11-11)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
|
|
13
|
+
* 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))
|
|
14
|
+
* 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))
|
|
15
|
+
* 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))
|
|
16
|
+
|
|
8
17
|
## [4.3.0](https://github.com/powerhome/nitro-intelligence.rb/compare/v4.2.0...v4.3.0) (2026-11-11)
|
|
9
18
|
|
|
10
19
|
|
|
@@ -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
|
|
|
@@ -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
|
|