nitro_intelligence 4.1.0 → 4.2.0
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 +21 -2
- data/docs/README.md +69 -1
- data/lib/nitro_intelligence/client/client.rb +2 -2
- data/lib/nitro_intelligence/client/handlers/audio_transcription_handler.rb +2 -4
- data/lib/nitro_intelligence/client/handlers/base_handler.rb +9 -0
- data/lib/nitro_intelligence/client/handlers/chat_handler.rb +2 -4
- data/lib/nitro_intelligence/client/handlers/decision_handler.rb +2 -9
- data/lib/nitro_intelligence/client/handlers/image_handler.rb +3 -4
- data/lib/nitro_intelligence/client/handlers/observed/audio_transcription_handler.rb +2 -10
- data/lib/nitro_intelligence/client/handlers/observed/base_handler.rb +23 -0
- data/lib/nitro_intelligence/client/handlers/observed/chat_handler.rb +2 -10
- data/lib/nitro_intelligence/client/handlers/observed/decision_handler.rb +3 -10
- data/lib/nitro_intelligence/client/handlers/observed/image_handler.rb +2 -10
- data/lib/nitro_intelligence/client/handlers/observed/text_to_speech_handler.rb +2 -10
- data/lib/nitro_intelligence/client/handlers/text_to_speech_handler.rb +2 -5
- data/lib/nitro_intelligence/client/observers/langfuse_observer.rb +30 -26
- data/lib/nitro_intelligence/langfuse_extension.rb +52 -0
- data/lib/nitro_intelligence/langfuse_tracer_provider.rb +11 -2
- data/lib/nitro_intelligence/media/image_generation.rb +7 -3
- data/lib/nitro_intelligence/models/model.rb +5 -0
- data/lib/nitro_intelligence/models/model_catalog.rb +15 -0
- data/lib/nitro_intelligence/observability/batch_span_processor.rb +200 -0
- data/lib/nitro_intelligence/observability/project_client.rb +23 -0
- data/lib/nitro_intelligence/observability/project_client_registry.rb +6 -2
- data/lib/nitro_intelligence/observation.rb +132 -0
- data/lib/nitro_intelligence/reporter.rb +16 -4
- data/lib/nitro_intelligence/tracer.rb +87 -0
- data/lib/nitro_intelligence/version.rb +1 -1
- data/lib/nitro_intelligence.rb +9 -4
- metadata +25 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4f03c055275bc0a35eda96d094085e42cefcedf07a9b02e621ecf1ba88bb32f7
|
|
4
|
+
data.tar.gz: f11cf2e9e924eff3fd859f9d43a99eaf9993aa11f83308df4354ce717db32627
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bbd1c842b1ff8fadc8d86d2090cd8945ebf71cb7354bb815866d0082a383bb4c07113fd3323db420f28db9f7761b2e588cac2c7dd9fd4f294357c86bcc40b53e
|
|
7
|
+
data.tar.gz: 8f0a724e2c3d44c0370e333e9dd65842dd4a318c47aa5039feb7c8070b062410ec91337fb305b7fb5133afbafac510f4e6293c59e7c2b47dc9a305357eeacd32
|
data/docs/CHANGELOG.md
CHANGED
|
@@ -5,7 +5,27 @@ 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
|
-
## [
|
|
8
|
+
## [4.2.0](https://github.com/powerhome/nitro-intelligence.rb/compare/v4.1.1...v4.2.0) (2026-11-10)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
* let applications trace their own steps alongside the gem's requests ([#198](https://github.com/powerhome/nitro-intelligence.rb/issues/198)) ([dd45726](https://github.com/powerhome/nitro-intelligence.rb/commit/dd457262545752064030755591cd40b265b1cae3))
|
|
14
|
+
* let Reporter#score take observation_id, comment and metadata to ([dd45726](https://github.com/powerhome/nitro-intelligence.rb/commit/dd457262545752064030755591cd40b265b1cae3))
|
|
15
|
+
* **observability:** add project client flush ([#206](https://github.com/powerhome/nitro-intelligence.rb/issues/206)) ([0303034](https://github.com/powerhome/nitro-intelligence.rb/commit/0303034e5586e3fdfa0704be1899b93cbf8b90ce))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Fixed
|
|
19
|
+
|
|
20
|
+
* **observability:** share one project client registry and one client per ([0303034](https://github.com/powerhome/nitro-intelligence.rb/commit/0303034e5586e3fdfa0704be1899b93cbf8b90ce))
|
|
21
|
+
* use the final image when Gemini also returns thought images ([#205](https://github.com/powerhome/nitro-intelligence.rb/issues/205)) ([1ce6b50](https://github.com/powerhome/nitro-intelligence.rb/commit/1ce6b501e7d374de6b39a1d7cabe3314de69c01c))
|
|
22
|
+
|
|
23
|
+
## [4.1.1](https://github.com/powerhome/nitro-intelligence.rb/compare/v4.1.0...v4.1.1) (2026-11-05)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### Fixed
|
|
27
|
+
|
|
28
|
+
* reject a model of the wrong type before sending a request ([#181](https://github.com/powerhome/nitro-intelligence.rb/issues/181)) ([c84955d](https://github.com/powerhome/nitro-intelligence.rb/commit/c84955d9b3751f1c456ff1e03df48fa962204b7e))
|
|
9
29
|
|
|
10
30
|
## [4.1.0] - 2026-11-04
|
|
11
31
|
|
|
@@ -223,7 +243,6 @@ Each warns through `NitroIntelligence.deprecator`, and is removed in 5.0. See [U
|
|
|
223
243
|
- Require Ruby 3.3 or later (#10)
|
|
224
244
|
- Upgrade langfuse-rb to 0.7.0. (#12)
|
|
225
245
|
|
|
226
|
-
[Unreleased]: https://github.com/powerhome/nitro-intelligence.rb/compare/v4.1.0-nitro_intelligence...HEAD
|
|
227
246
|
[4.1.0]: https://github.com/powerhome/nitro-intelligence.rb/compare/v4.0.0-nitro_intelligence...v4.1.0-nitro_intelligence
|
|
228
247
|
[4.0.0]: https://github.com/powerhome/nitro-intelligence.rb/compare/v3.1.0-nitro_intelligence...v4.0.0-nitro_intelligence
|
|
229
248
|
[3.1.0]: https://github.com/powerhome/nitro-intelligence.rb/compare/v3.0.2-nitro_intelligence...v3.1.0-nitro_intelligence
|
data/docs/README.md
CHANGED
|
@@ -275,7 +275,7 @@ client = NitroIntelligence::Client.new
|
|
|
275
275
|
result = client.generate_image(message: "Create an image of a bear installing a window.")
|
|
276
276
|
```
|
|
277
277
|
|
|
278
|
-
`result` is a `NitroIntelligence::ImageGeneration` object, and the generated image can be accessed via `result.generated_image` which returns a `NitroIntelligence::Image`.
|
|
278
|
+
`result` is a `NitroIntelligence::ImageGeneration` object, and the generated image can be accessed via `result.generated_image` which returns a `NitroIntelligence::Image`. Gemini image models that think can return their interim drafts alongside the final render; `generated_image` is always the final one.
|
|
279
279
|
|
|
280
280
|
With observability enabled, `result.trace_id` and `result.observation_id` identify the trace and generation observation. Both are `nil` without observability.
|
|
281
281
|
|
|
@@ -343,6 +343,25 @@ client.chat(
|
|
|
343
343
|
|
|
344
344
|
If no `prompt_label` or `prompt_version` is provided, the 'production' prompt is used by default.
|
|
345
345
|
|
|
346
|
+
### Exporting Completed Observations
|
|
347
|
+
|
|
348
|
+
Observations are exported in the background. Use the same shared project client for tracing and flushing when a job needs to export completed observations before submitting their IDs for human review:
|
|
349
|
+
|
|
350
|
+
```ruby
|
|
351
|
+
project_client = NitroIntelligence.project_client_registry.fetch("my-feature-project")
|
|
352
|
+
raise "Observation export failed or timed out" unless project_client.flush(timeout: 30)
|
|
353
|
+
|
|
354
|
+
# Submit the completed observations' IDs to the annotation queue here.
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
`flush` waits only for this project's observations queued or in flight when called. Observations completed later do not extend the wait. The client remains usable, and active observations are not ended.
|
|
358
|
+
|
|
359
|
+
It returns `true` when those exports succeed or nothing needs exporting. It returns `false` if exporting fails or cannot start, an observation being flushed is dropped, the wait times out, or shutdown prevents completion. Calls made during or after shutdown return `false`. Earlier export failures are not reported.
|
|
360
|
+
|
|
361
|
+
The timeout defaults to 30 seconds; exports continue after a caller times out. With `timeout: 0`, a running client succeeds only if nothing is queued or in flight.
|
|
362
|
+
|
|
363
|
+
It does not flush queued scores or wait for evaluators, and successful export does not guarantee immediate visibility in Cerebro or annotation queues. Reserve `NitroIntelligence.project_client_registry.shutdown_all` for process teardown. See the [ProjectClient#flush API reference](https://www.rubydoc.info/gems/nitro_intelligence/NitroIntelligence/Observability/ProjectClient#flush-instance_method) for details.
|
|
364
|
+
|
|
346
365
|
### Prompt Fallbacks
|
|
347
366
|
|
|
348
367
|
Pass `prompt_fallback_name` to name a second prompt to use when the one you asked for isn't there. The prompt in `prompt_name` is tried first; if it doesn't exist, or its lookup fails, the fallback is used instead:
|
|
@@ -521,6 +540,46 @@ Two things this does not cover:
|
|
|
521
540
|
* **The gateway's redaction is by field, and misses some.** It replaces the fields it knows about, such as `content` and `reasoning_content`, rather than the whole response, so a field a provider adds beyond those is still logged. Reasoning that a provider returns under `provider_specific_fields` is one such field today; see [powerhome/nitro-intelligence#704](https://github.com/powerhome/nitro-intelligence/issues/704).
|
|
522
541
|
* **Prompts you fetch from Cerebro are unaffected.** Masking applies to traces, not to prompt management.
|
|
523
542
|
|
|
543
|
+
### Tracing Application Steps
|
|
544
|
+
|
|
545
|
+
Each observed request is a trace of its own by default. A feature whose work is more than one model call, such as a search that embeds a question, queries an index, reranks the results and filters them by permission, can record that work as one trace with `NitroIntelligence::Tracer`: its own steps as observations, and the gem's requests nested among them.
|
|
546
|
+
|
|
547
|
+
```ruby
|
|
548
|
+
tracer = NitroIntelligence::Tracer.new(observability_project_slug: "page-finder")
|
|
549
|
+
client = NitroIntelligence::Client.new(observability_project_slug: "page-finder")
|
|
550
|
+
|
|
551
|
+
pages = tracer.trace("Page Finder search", input: { question: }, user_id: current_user.id.to_s, session_id:) do |root_observation|
|
|
552
|
+
query = client.chat(message: question) # recorded inside the trace
|
|
553
|
+
|
|
554
|
+
hits = root_observation.observe("Elasticsearch", as_type: :retriever, input: { k: 50, boost: 2 }) do |step|
|
|
555
|
+
search(query).tap { |hits| step.output = hits.map { |hit| { id: hit.page_id, score: hit.score } } }
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
allowed = root_observation.observe("Permission filter", input: { candidates: hits.size }) do |step|
|
|
559
|
+
permitted(hits).tap { |pages| step.output = { allowed: pages.size } }
|
|
560
|
+
end
|
|
561
|
+
|
|
562
|
+
root_observation.output = allowed.map(&:page_id)
|
|
563
|
+
@trace_id = root_observation.trace_id # to score the search when the user clicks a result
|
|
564
|
+
allowed
|
|
565
|
+
end
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
`Tracer#trace` records its block as a trace with the given name, user, session, tags and metadata, and the environment and release the gem's own requests record. `user_id` defaults to `observability_user_id`, as it does for a request. It yields the trace's root observation: the input and output set on it are the trace's input and output too, while metadata set on it is recorded on that observation only. `trace_seed:` gives the trace the ID `NitroIntelligence::Trace.create_id` gives for the seed; otherwise the ID is random, and `root_observation.trace_id` reads it.
|
|
569
|
+
|
|
570
|
+
`Observation#observe` records a step inside the trace, or inside another step, around a block. `as_type` is a Langfuse [observation type](https://langfuse.com/docs/observability/features/observation-types): `:span` by default, or `:retriever`, `:tool`, `:agent`, `:chain`, `:evaluator`, `:guardrail`, `:embedding` or `:generation`. `Observation#event` records a point in time, such as the results a user was shown. An exception raised inside a block is recorded on its step at level `ERROR`, and re-raised.
|
|
571
|
+
|
|
572
|
+
The gem's requests made inside the block are recorded as observations under the step that is open, not as traces of their own, when:
|
|
573
|
+
|
|
574
|
+
* the client records in the same Cerebro project as the tracer. A request recorded in another project is a trace of its own there, as it would be outside the block.
|
|
575
|
+
* the request passes no `trace_seed`. A seeded request is recorded in the seed's trace, as it is anywhere.
|
|
576
|
+
|
|
577
|
+
Such a request records its own observation only: the trace's name, input, output, user, session and tags are the ones the tracer was given, and the request's `trace_name`, `user_id`, `session_id` and `tags` are not recorded. Its `metadata` is still recorded on its observation and sent to the inference gateway.
|
|
578
|
+
|
|
579
|
+
A trace of its own started inside the block (a request to another project, a seeded request, or another `Tracer#trace`) neither takes the open trace's user, session, tags and metadata nor changes them. It takes only what the application propagated before opening the trace, such as with `Langfuse.propagate_attributes`, as it would outside the block.
|
|
580
|
+
|
|
581
|
+
A project that masks content records the input and output of every step and event as `[masked]`, and a failure's error class without its message, as it does for the gem's requests. Metadata, names, user, session and tags are not masked.
|
|
582
|
+
|
|
524
583
|
### Scoring
|
|
525
584
|
|
|
526
585
|
You can use `NitroIntelligence::Reporter` to evaluate existing traces. Calling `NitroIntelligence::Reporter#score` lets you attach metrics to a trace in the observability platform.
|
|
@@ -543,6 +602,15 @@ trace_id = NitroIntelligence::Trace.create_id(seed: document_id)
|
|
|
543
602
|
reporter.score(name: "precision", value: 0.5, trace_id:)
|
|
544
603
|
```
|
|
545
604
|
|
|
605
|
+
#### An Observation, Later
|
|
606
|
+
|
|
607
|
+
A score can be recorded from a later request than the one that made the trace, such as a click on a search result, given the trace's ID. Pass `observation_id:` to score one observation in the trace, as `Observation#id` gives it, rather than the trace as a whole. `comment:` and `metadata:` are recorded with the score.
|
|
608
|
+
|
|
609
|
+
```ruby
|
|
610
|
+
reporter = NitroIntelligence::Reporter.new(observability_project_slug: "page-finder")
|
|
611
|
+
reporter.score(trace_id: params[:trace_id], name: "clicked", value: true, data_type: :boolean, metadata: { position: 3 })
|
|
612
|
+
```
|
|
613
|
+
|
|
546
614
|
#### Using different data types for scoring
|
|
547
615
|
|
|
548
616
|
By default, scores submitted on a trace are numeric, but you can use the `data_type` keyword to change it to `:boolean` or `:categorical`. For example:
|
|
@@ -26,8 +26,8 @@ module NitroIntelligence
|
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
# @api private
|
|
29
|
-
def self.validate_model(model)
|
|
30
|
-
|
|
29
|
+
def self.validate_model(model, type: Model)
|
|
30
|
+
NitroIntelligence.model_catalog.fetch(model, type:)
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
33
|
end
|
|
@@ -14,9 +14,7 @@ module NitroIntelligence
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def perform_request(audio_file:, message: "", parameters: {}, correlation_trace_id: nil, mask_content: false)
|
|
17
|
-
|
|
18
|
-
add_correlation_headers(parameters, trace_id: correlation_trace_id)
|
|
19
|
-
add_message_redaction_header(parameters, mask_content:)
|
|
17
|
+
add_gateway_headers(parameters, correlation_trace_id:, mask_content:, modality: "audio")
|
|
20
18
|
@client.audio.transcriptions.create(
|
|
21
19
|
prompt: message,
|
|
22
20
|
file: audio_file,
|
|
@@ -31,7 +29,7 @@ module NitroIntelligence
|
|
|
31
29
|
}
|
|
32
30
|
|
|
33
31
|
parameters.replace(default_parameters.merge(parameters))
|
|
34
|
-
Client.validate_model(parameters[:model])
|
|
32
|
+
Client.validate_model(parameters[:model], type: TextModel)
|
|
35
33
|
end
|
|
36
34
|
end
|
|
37
35
|
end
|
|
@@ -119,6 +119,15 @@ module NitroIntelligence
|
|
|
119
119
|
)
|
|
120
120
|
end
|
|
121
121
|
|
|
122
|
+
# Every request to the gateway names the model asked for, and the modality where the
|
|
123
|
+
# route alone does not, carries the correlation headers and, for a project that masks
|
|
124
|
+
# content, asks the gateway to redact it from its logs.
|
|
125
|
+
def add_gateway_headers(parameters, correlation_trace_id:, mask_content:, modality: nil)
|
|
126
|
+
add_request_headers(parameters, MODALITY_HEADER => modality, REQUESTED_MODEL_HEADER => parameters[:model])
|
|
127
|
+
add_correlation_headers(parameters, trace_id: correlation_trace_id)
|
|
128
|
+
add_message_redaction_header(parameters, mask_content:)
|
|
129
|
+
end
|
|
130
|
+
|
|
122
131
|
# A project that masks content in Cerebro keeps it out of the gateway's logs
|
|
123
132
|
# too, so one setting decides where a request's content is recorded.
|
|
124
133
|
def add_message_redaction_header(parameters, mask_content:)
|
|
@@ -14,9 +14,7 @@ module NitroIntelligence
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def perform_request(parameters: {}, correlation_trace_id: nil, mask_content: false)
|
|
17
|
-
|
|
18
|
-
add_correlation_headers(parameters, trace_id: correlation_trace_id)
|
|
19
|
-
add_message_redaction_header(parameters, mask_content:)
|
|
17
|
+
add_gateway_headers(parameters, correlation_trace_id:, mask_content:)
|
|
20
18
|
@client.chat.completions.create(**parameters.slice(*ALLOWED_EXTRA_PARAMETERS))
|
|
21
19
|
end
|
|
22
20
|
|
|
@@ -33,7 +31,7 @@ module NitroIntelligence
|
|
|
33
31
|
}
|
|
34
32
|
|
|
35
33
|
parameters.replace(default_parameters.merge(parameters))
|
|
36
|
-
Client.validate_model(parameters[:model])
|
|
34
|
+
Client.validate_model(parameters[:model], type: TextModel)
|
|
37
35
|
end
|
|
38
36
|
end
|
|
39
37
|
end
|
|
@@ -34,9 +34,7 @@ module NitroIntelligence
|
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
def perform_request(state:, questions:, parameters: {}, correlation_trace_id: nil, mask_content: false)
|
|
37
|
-
|
|
38
|
-
add_correlation_headers(parameters, trace_id: correlation_trace_id)
|
|
39
|
-
add_message_redaction_header(parameters, mask_content:)
|
|
37
|
+
add_gateway_headers(parameters, correlation_trace_id:, mask_content:)
|
|
40
38
|
response = @client.request(
|
|
41
39
|
method: :post,
|
|
42
40
|
path:,
|
|
@@ -54,12 +52,7 @@ module NitroIntelligence
|
|
|
54
52
|
}
|
|
55
53
|
|
|
56
54
|
parameters.replace(default_parameters.merge(parameters))
|
|
57
|
-
Client.validate_model(parameters[:model])
|
|
58
|
-
return if NitroIntelligence.model_catalog.lookup_by_name(parameters[:model]).is_a?(DecisionModel)
|
|
59
|
-
|
|
60
|
-
# A decision request has nothing in common with a chat one, so a model of another type
|
|
61
|
-
# would fail at the gateway with an error that does not say why.
|
|
62
|
-
raise ArgumentError, "Model '#{parameters[:model]}' is not a decision model"
|
|
55
|
+
Client.validate_model(parameters[:model], type: DecisionModel)
|
|
63
56
|
end
|
|
64
57
|
|
|
65
58
|
private
|
|
@@ -21,12 +21,12 @@ module NitroIntelligence
|
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
def perform_request(parameters: {}, correlation_trace_id: nil, mask_content: false)
|
|
24
|
-
|
|
25
|
-
add_correlation_headers(parameters, trace_id: correlation_trace_id)
|
|
26
|
-
add_message_redaction_header(parameters, mask_content:)
|
|
24
|
+
add_gateway_headers(parameters, correlation_trace_id:, mask_content:, modality: "image")
|
|
27
25
|
@client.chat.completions.create(**parameters.slice(*ALLOWED_EXTRA_PARAMETERS))
|
|
28
26
|
end
|
|
29
27
|
|
|
28
|
+
# The model is not checked here: building the ImageGeneration already checked it, along
|
|
29
|
+
# with the aspect ratio and resolution it supports.
|
|
30
30
|
def validate_and_resolve!(parameters, image_generation)
|
|
31
31
|
default_parameters = {
|
|
32
32
|
image_generation:,
|
|
@@ -45,7 +45,6 @@ module NitroIntelligence
|
|
|
45
45
|
# Deep, so that request options a caller passes (headers, max_retries,
|
|
46
46
|
# timeout) sit alongside the image config rather than replacing it.
|
|
47
47
|
parameters.replace(default_parameters.deep_merge(parameters))
|
|
48
|
-
Client.validate_model(parameters[:model])
|
|
49
48
|
end
|
|
50
49
|
|
|
51
50
|
private
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require "nitro_intelligence/client/handlers/observed/base_handler"
|
|
1
2
|
require "base64"
|
|
2
3
|
require "nitro_intelligence/media/audio"
|
|
3
4
|
require "nitro_intelligence/observability/prompt_resolver"
|
|
@@ -7,18 +8,13 @@ module NitroIntelligence
|
|
|
7
8
|
module Handlers
|
|
8
9
|
module Observed
|
|
9
10
|
# @api private
|
|
10
|
-
class AudioTranscriptionHandler
|
|
11
|
+
class AudioTranscriptionHandler < BaseHandler
|
|
11
12
|
# Raised by {Client::Base#transcribe_audio} on an observed client when the prompt it names
|
|
12
13
|
# is not a text prompt.
|
|
13
14
|
#
|
|
14
15
|
# @api public
|
|
15
16
|
class ObservedAudioTranscriptionPromptError < StandardError; end
|
|
16
17
|
|
|
17
|
-
def initialize(base_handler:, observer:)
|
|
18
|
-
@base_handler = base_handler
|
|
19
|
-
@observer = observer
|
|
20
|
-
end
|
|
21
|
-
|
|
22
18
|
def create(audio_file:, message: +"", parameters: {})
|
|
23
19
|
@base_handler.validate_and_resolve!(parameters)
|
|
24
20
|
|
|
@@ -39,10 +35,6 @@ module NitroIntelligence
|
|
|
39
35
|
|
|
40
36
|
private
|
|
41
37
|
|
|
42
|
-
def project
|
|
43
|
-
@observer.project_client.project
|
|
44
|
-
end
|
|
45
|
-
|
|
46
38
|
def handle_prompt(message:, parameters:)
|
|
47
39
|
prompt = NitroIntelligence::Observability::PromptResolver.for(
|
|
48
40
|
store: project.prompt_store,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module NitroIntelligence
|
|
2
|
+
module Client
|
|
3
|
+
module Handlers
|
|
4
|
+
module Observed
|
|
5
|
+
# Wraps a handler's request in an observation recorded to the observer's Cerebro project.
|
|
6
|
+
#
|
|
7
|
+
# @api private
|
|
8
|
+
class BaseHandler
|
|
9
|
+
def initialize(base_handler:, observer:)
|
|
10
|
+
@base_handler = base_handler
|
|
11
|
+
@observer = observer
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def project
|
|
17
|
+
@observer.project_client.project
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require "nitro_intelligence/client/handlers/observed/base_handler"
|
|
1
2
|
require "nitro_intelligence/observability/prompt_resolver"
|
|
2
3
|
|
|
3
4
|
module NitroIntelligence
|
|
@@ -6,7 +7,7 @@ module NitroIntelligence
|
|
|
6
7
|
# @api private
|
|
7
8
|
module Observed
|
|
8
9
|
# @api private
|
|
9
|
-
class ChatHandler
|
|
10
|
+
class ChatHandler < BaseHandler
|
|
10
11
|
# Raised by {Client::Base#chat} on an observed client when the request carries no user
|
|
11
12
|
# message for the model to answer: the prompt supplies only a system message, and the
|
|
12
13
|
# caller passed no `message`.
|
|
@@ -14,11 +15,6 @@ module NitroIntelligence
|
|
|
14
15
|
# @api public
|
|
15
16
|
class ObservedChatPromptError < StandardError; end
|
|
16
17
|
|
|
17
|
-
def initialize(base_handler:, observer:)
|
|
18
|
-
@base_handler = base_handler
|
|
19
|
-
@observer = observer
|
|
20
|
-
end
|
|
21
|
-
|
|
22
18
|
def create(message: "", parameters: {})
|
|
23
19
|
@base_handler.validate_and_resolve!(parameters, message)
|
|
24
20
|
|
|
@@ -41,10 +37,6 @@ module NitroIntelligence
|
|
|
41
37
|
|
|
42
38
|
private
|
|
43
39
|
|
|
44
|
-
def project
|
|
45
|
-
@observer.project_client.project
|
|
46
|
-
end
|
|
47
|
-
|
|
48
40
|
# A chat completion needs a turn for the model to answer, and it is the model's own
|
|
49
41
|
# chat template that insists on one: Qwen's raises "No user query found in
|
|
50
42
|
# messages." Left to the gateway that costs a round trip and comes back as a 400
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require "nitro_intelligence/client/handlers/observed/base_handler"
|
|
2
|
+
|
|
1
3
|
module NitroIntelligence
|
|
2
4
|
module Client
|
|
3
5
|
module Handlers
|
|
@@ -7,12 +9,7 @@ module NitroIntelligence
|
|
|
7
9
|
# powerhome/cerebro#128.
|
|
8
10
|
#
|
|
9
11
|
# @api private
|
|
10
|
-
class DecisionHandler
|
|
11
|
-
def initialize(base_handler:, observer:)
|
|
12
|
-
@base_handler = base_handler
|
|
13
|
-
@observer = observer
|
|
14
|
-
end
|
|
15
|
-
|
|
12
|
+
class DecisionHandler < BaseHandler
|
|
16
13
|
def create(state:, questions:, parameters: {})
|
|
17
14
|
@base_handler.validate_and_resolve!(parameters)
|
|
18
15
|
|
|
@@ -33,10 +30,6 @@ module NitroIntelligence
|
|
|
33
30
|
|
|
34
31
|
private
|
|
35
32
|
|
|
36
|
-
def project
|
|
37
|
-
@observer.project_client.project
|
|
38
|
-
end
|
|
39
|
-
|
|
40
33
|
def workflow(generation:, state:, questions:, parameters:)
|
|
41
34
|
decision = @base_handler.perform_request(
|
|
42
35
|
state:, questions:, parameters:, correlation_trace_id: generation.trace_id,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require "nitro_intelligence/client/handlers/observed/base_handler"
|
|
1
2
|
require "nitro_intelligence/media/image_generation"
|
|
2
3
|
require "nitro_intelligence/observability/prompt_resolver"
|
|
3
4
|
|
|
@@ -6,12 +7,7 @@ module NitroIntelligence
|
|
|
6
7
|
module Handlers
|
|
7
8
|
module Observed
|
|
8
9
|
# @api private
|
|
9
|
-
class ImageHandler
|
|
10
|
-
def initialize(base_handler:, observer:)
|
|
11
|
-
@base_handler = base_handler
|
|
12
|
-
@observer = observer
|
|
13
|
-
end
|
|
14
|
-
|
|
10
|
+
class ImageHandler < BaseHandler
|
|
15
11
|
def create(message: "", target_image: nil, reference_images: [], parameters: {})
|
|
16
12
|
image_generation = build_image_generation(message:, target_image:, reference_images:, parameters:)
|
|
17
13
|
|
|
@@ -36,10 +32,6 @@ module NitroIntelligence
|
|
|
36
32
|
|
|
37
33
|
private
|
|
38
34
|
|
|
39
|
-
def project
|
|
40
|
-
@observer.project_client.project
|
|
41
|
-
end
|
|
42
|
-
|
|
43
35
|
def build_image_generation(message:, target_image:, reference_images:, parameters:)
|
|
44
36
|
NitroIntelligence::ImageGeneration.new(message:, target_image:, reference_images:) do |config|
|
|
45
37
|
config.aspect_ratio = parameters[:aspect_ratio] if parameters.key?(:aspect_ratio)
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require "nitro_intelligence/client/handlers/observed/base_handler"
|
|
1
2
|
require "nitro_intelligence/media/audio"
|
|
2
3
|
require "nitro_intelligence/observability/prompt_resolver"
|
|
3
4
|
|
|
@@ -6,18 +7,13 @@ module NitroIntelligence
|
|
|
6
7
|
module Handlers
|
|
7
8
|
module Observed
|
|
8
9
|
# @api private
|
|
9
|
-
class TextToSpeechHandler
|
|
10
|
+
class TextToSpeechHandler < BaseHandler
|
|
10
11
|
# Raised by {Client::Base#text_to_speech} on an observed client when the prompt it names
|
|
11
12
|
# is not a text prompt.
|
|
12
13
|
#
|
|
13
14
|
# @api public
|
|
14
15
|
class ObservedTextToSpeechPromptError < StandardError; end
|
|
15
16
|
|
|
16
|
-
def initialize(base_handler:, observer:)
|
|
17
|
-
@base_handler = base_handler
|
|
18
|
-
@observer = observer
|
|
19
|
-
end
|
|
20
|
-
|
|
21
17
|
def create(message: "", parameters: {})
|
|
22
18
|
prompt = handle_prompt(parameters:)
|
|
23
19
|
|
|
@@ -39,10 +35,6 @@ module NitroIntelligence
|
|
|
39
35
|
|
|
40
36
|
private
|
|
41
37
|
|
|
42
|
-
def project
|
|
43
|
-
@observer.project_client.project
|
|
44
|
-
end
|
|
45
|
-
|
|
46
38
|
def handle_prompt(parameters:)
|
|
47
39
|
prompt = NitroIntelligence::Observability::PromptResolver.for(
|
|
48
40
|
store: project.prompt_store,
|
|
@@ -14,9 +14,7 @@ module NitroIntelligence
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def perform_request(message: "", parameters: {}, correlation_trace_id: nil, mask_content: false)
|
|
17
|
-
|
|
18
|
-
add_correlation_headers(parameters, trace_id: correlation_trace_id)
|
|
19
|
-
add_message_redaction_header(parameters, mask_content:)
|
|
17
|
+
add_gateway_headers(parameters, correlation_trace_id:, mask_content:, modality: "audio")
|
|
20
18
|
@client.audio.speech.create(
|
|
21
19
|
input: message,
|
|
22
20
|
**parameters.slice(*ALLOWED_EXTRA_PARAMETERS)
|
|
@@ -25,8 +23,7 @@ module NitroIntelligence
|
|
|
25
23
|
|
|
26
24
|
def validate_and_resolve!(parameters)
|
|
27
25
|
model_name = parameters[:model] || NitroIntelligence.model_catalog.default_text_to_speech_model&.name
|
|
28
|
-
model =
|
|
29
|
-
raise ArgumentError, "Unsupported model: '#{model_name}'" unless model
|
|
26
|
+
model = Client.validate_model(model_name, type: TextToSpeechModel)
|
|
30
27
|
|
|
31
28
|
default_parameters = {
|
|
32
29
|
metadata: {},
|
|
@@ -22,7 +22,7 @@ module NitroIntelligence
|
|
|
22
22
|
@project_client = project_client
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
def observe(operation_name, type:, parameters:, trace_name:, prompt: nil, input: nil)
|
|
25
|
+
def observe(operation_name, type:, parameters:, trace_name:, prompt: nil, input: nil, &)
|
|
26
26
|
metadata = parameters[:metadata]
|
|
27
27
|
seed = parameters[:trace_seed]
|
|
28
28
|
trace_id = NitroIntelligence::Trace.create_id(seed:) if seed.present?
|
|
@@ -34,25 +34,19 @@ module NitroIntelligence
|
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
metadata = metadata.transform_values(&:to_s)
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
result, trace_attributes = observe_failures(generation) { yield(generation) }
|
|
52
|
-
record_result(generation, trace_attributes)
|
|
53
|
-
|
|
54
|
-
result
|
|
55
|
-
end
|
|
37
|
+
# Inside a trace the application opened, the trace's name, input, output, user, session and
|
|
38
|
+
# tags are the application's, so the request records only its own observation.
|
|
39
|
+
nested = trace_id.nil? && @project_client.observability_client.observing?
|
|
40
|
+
request = { operation_name:, type:, trace_id:, trace_name:, prompt:, input:, metadata:, nested: }
|
|
41
|
+
request[:model] = parameters[:model]
|
|
42
|
+
|
|
43
|
+
# Propagating sets the attributes on the active span too, which inside an application's
|
|
44
|
+
# trace is the application's own observation.
|
|
45
|
+
return record_request(**request, &) if nested
|
|
46
|
+
|
|
47
|
+
propagated = propagated_attributes(parameters, metadata)
|
|
48
|
+
LangfuseExtension.in_new_trace do
|
|
49
|
+
Langfuse.propagate_attributes(**propagated) { record_request(**request, &) }
|
|
56
50
|
end
|
|
57
51
|
end
|
|
58
52
|
|
|
@@ -62,6 +56,21 @@ module NitroIntelligence
|
|
|
62
56
|
@project_client.project
|
|
63
57
|
end
|
|
64
58
|
|
|
59
|
+
def record_request(operation_name:, type:, trace_id:, trace_name:, prompt:, input:, model:, metadata:, nested:) # rubocop:disable Metrics/ParameterLists
|
|
60
|
+
@project_client.observability_client.observe(
|
|
61
|
+
operation_name, as_type: type, trace_id:, environment: NitroIntelligence.environment.to_s, model:, metadata:
|
|
62
|
+
) do |generation|
|
|
63
|
+
generation.update_trace(name: trace_name) unless nested
|
|
64
|
+
generation.update({ prompt: { name: prompt.name, version: prompt.version } }) if prompt
|
|
65
|
+
record_input(generation, input)
|
|
66
|
+
|
|
67
|
+
result, trace_attributes = observe_failures(generation) { yield(generation) }
|
|
68
|
+
record_result(generation, trace_attributes)
|
|
69
|
+
|
|
70
|
+
result
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
65
74
|
# Applied once the response is in hand, so the observation reflects what came
|
|
66
75
|
# back rather than what was asked for. Each attribute is set only when the
|
|
67
76
|
# handler supplied it: an observation that records nothing is better than one
|
|
@@ -85,8 +94,6 @@ module NitroIntelligence
|
|
|
85
94
|
end
|
|
86
95
|
|
|
87
96
|
record_status(generation, trace_attributes)
|
|
88
|
-
|
|
89
|
-
generation.update_trace(input: trace_attributes[:input], output: trace_attributes[:output])
|
|
90
97
|
end
|
|
91
98
|
|
|
92
99
|
def masked(trace_attributes)
|
|
@@ -119,10 +126,7 @@ module NitroIntelligence
|
|
|
119
126
|
def record_input(generation, input)
|
|
120
127
|
return if input.blank?
|
|
121
128
|
|
|
122
|
-
input = project.mask(input)
|
|
123
|
-
|
|
124
|
-
generation.input = input
|
|
125
|
-
generation.update_trace(input:)
|
|
129
|
+
generation.input = project.mask(input)
|
|
126
130
|
end
|
|
127
131
|
|
|
128
132
|
# `session_id` and `tags` are only forwarded when set so that callers who
|
|
@@ -13,6 +13,33 @@ require "nitro_intelligence/langfuse_tracer_provider"
|
|
|
13
13
|
module NitroIntelligence
|
|
14
14
|
# @api private
|
|
15
15
|
class LangfuseExtension
|
|
16
|
+
# Holds the client recording the {Observation} application code has open, so that observations
|
|
17
|
+
# started inside it nest under it only when they are recorded in the same project.
|
|
18
|
+
ACTIVE_OBSERVATION_KEY = OpenTelemetry::Context.create_key("nitro-intelligence-active-observation")
|
|
19
|
+
|
|
20
|
+
# Holds the context the application had before it opened the trace it has open now.
|
|
21
|
+
TRACE_BASE_KEY = OpenTelemetry::Context.create_key("nitro-intelligence-trace-base")
|
|
22
|
+
|
|
23
|
+
# What a trace the application opens adds to the context, besides its active span.
|
|
24
|
+
TRACE_KEYS = [*Langfuse::Propagation::CONTEXT_KEYS.values, ACTIVE_OBSERVATION_KEY, TRACE_BASE_KEY].freeze
|
|
25
|
+
|
|
26
|
+
# Runs the block for a trace of its own. Inside a trace the application opened, the block runs
|
|
27
|
+
# without that trace's active span and propagated attributes, as it would outside the trace:
|
|
28
|
+
# `Langfuse.propagate_attributes` writes onto the active span and merges into what the context
|
|
29
|
+
# already propagates, so a trace started inside another would otherwise change it and inherit
|
|
30
|
+
# from it.
|
|
31
|
+
def self.in_new_trace(&)
|
|
32
|
+
context = OpenTelemetry::Context.current
|
|
33
|
+
base = context.value(TRACE_BASE_KEY)
|
|
34
|
+
unless base.nil?
|
|
35
|
+
context = OpenTelemetry::Trace.context_with_span(OpenTelemetry::Trace.current_span(base),
|
|
36
|
+
parent_context: context)
|
|
37
|
+
context = TRACE_KEYS.reduce(context) { |restored, key| restored.set_value(key, base.value(key)) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
OpenTelemetry::Context.with_current(context.set_value(TRACE_BASE_KEY, context), &)
|
|
41
|
+
end
|
|
42
|
+
|
|
16
43
|
attr_reader :config
|
|
17
44
|
|
|
18
45
|
def initialize
|
|
@@ -24,6 +51,11 @@ module NitroIntelligence
|
|
|
24
51
|
@tracer_provider = NitroIntelligence::LangfuseTracerProvider.new(config)
|
|
25
52
|
end
|
|
26
53
|
|
|
54
|
+
# @api private
|
|
55
|
+
def flush(timeout: 30)
|
|
56
|
+
@tracer_provider.force_flush(timeout:)
|
|
57
|
+
end
|
|
58
|
+
|
|
27
59
|
def shutdown(timeout: 30)
|
|
28
60
|
@client.shutdown
|
|
29
61
|
@tracer_provider.shutdown(timeout:)
|
|
@@ -47,6 +79,20 @@ module NitroIntelligence
|
|
|
47
79
|
)
|
|
48
80
|
end
|
|
49
81
|
|
|
82
|
+
# Whether application code has an observation open in this client, under which an observation
|
|
83
|
+
# started without a trace ID or parent nests.
|
|
84
|
+
def observing?
|
|
85
|
+
active_observation_client.equal?(self)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Runs the block with `observation` as the active span, so that observations this client starts
|
|
89
|
+
# inside it, the gem's own included, nest under it.
|
|
90
|
+
def within(observation, &)
|
|
91
|
+
context = OpenTelemetry::Trace.context_with_span(observation.otel_span)
|
|
92
|
+
context = context.set_value(ACTIVE_OBSERVATION_KEY, self)
|
|
93
|
+
OpenTelemetry::Context.with_current(context, &)
|
|
94
|
+
end
|
|
95
|
+
|
|
50
96
|
def start_observation(name, attrs = {}, as_type: :span, trace_id: nil, parent_span_context: nil, start_time: nil, # rubocop:disable Metrics/ParameterLists
|
|
51
97
|
skip_validation: false)
|
|
52
98
|
parent_span_context = Langfuse.send(:resolve_trace_context, trace_id, parent_span_context)
|
|
@@ -87,5 +133,11 @@ module NitroIntelligence
|
|
|
87
133
|
|
|
88
134
|
observation.send(:run_in_context, &block)
|
|
89
135
|
end
|
|
136
|
+
|
|
137
|
+
private
|
|
138
|
+
|
|
139
|
+
def active_observation_client
|
|
140
|
+
OpenTelemetry::Context.value(ACTIVE_OBSERVATION_KEY)
|
|
141
|
+
end
|
|
90
142
|
end
|
|
91
143
|
end
|