nitro_intelligence 4.1.1 → 4.3.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 +24 -0
- data/docs/README.md +152 -3
- data/lib/nitro_intelligence/client/base.rb +86 -0
- data/lib/nitro_intelligence/client/client.rb +2 -2
- data/lib/nitro_intelligence/client/handlers/embedding_handler.rb +95 -0
- data/lib/nitro_intelligence/client/handlers/observed/embedding_handler.rb +73 -0
- data/lib/nitro_intelligence/client/handlers/observed/rerank_handler.rb +78 -0
- data/lib/nitro_intelligence/client/handlers/rerank_handler.rb +82 -0
- data/lib/nitro_intelligence/client/observed.rb +19 -0
- data/lib/nitro_intelligence/client/observers/langfuse_observer.rb +41 -26
- data/lib/nitro_intelligence/embeddings.rb +39 -0
- 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 +6 -1
- data/lib/nitro_intelligence/models/model.rb +21 -0
- data/lib/nitro_intelligence/models/model_catalog.rb +4 -0
- data/lib/nitro_intelligence/models/model_factory.rb +2 -0
- data/lib/nitro_intelligence/observability/batch_span_processor.rb +200 -0
- data/lib/nitro_intelligence/observability/project.rb +26 -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/observability/prompt.rb +36 -5
- data/lib/nitro_intelligence/observation.rb +132 -0
- data/lib/nitro_intelligence/ranking.rb +47 -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 +30 -1
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
require "nitro_intelligence/client/handlers/observed/base_handler"
|
|
2
|
+
|
|
3
|
+
module NitroIntelligence
|
|
4
|
+
module Client
|
|
5
|
+
module Handlers
|
|
6
|
+
module Observed
|
|
7
|
+
# Recorded as a generation: Langfuse has no reranker observation type, and a generation
|
|
8
|
+
# carries the model, usage and cost, as it does for decisions.
|
|
9
|
+
#
|
|
10
|
+
# @api private
|
|
11
|
+
class RerankHandler < BaseHandler
|
|
12
|
+
def create(query:, documents:, parameters: {})
|
|
13
|
+
@base_handler.validate_and_resolve!(query, documents, parameters)
|
|
14
|
+
|
|
15
|
+
trace_name = parameters[:trace_name] || project.slug
|
|
16
|
+
|
|
17
|
+
@observer.observe(
|
|
18
|
+
"rerank",
|
|
19
|
+
type: :generation,
|
|
20
|
+
parameters:,
|
|
21
|
+
trace_name:,
|
|
22
|
+
input: input(query, documents, parameters)
|
|
23
|
+
) do |generation|
|
|
24
|
+
workflow(generation:, query:, documents:, parameters:)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
# A caller whose trace already records the candidates, as a retrieval step of its own,
|
|
31
|
+
# can pass `trace_documents: false` to record only how many there were.
|
|
32
|
+
def input(query, documents, parameters)
|
|
33
|
+
if parameters[:trace_documents] == false
|
|
34
|
+
{ query:, document_count: documents.size }
|
|
35
|
+
else
|
|
36
|
+
{ query:, documents: }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def workflow(generation:, query:, documents:, parameters:)
|
|
41
|
+
ranking = @base_handler.perform_request(
|
|
42
|
+
query:, documents:, parameters:, correlation_trace_id: generation.trace_id,
|
|
43
|
+
mask_content: project.mask_content?
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
search_units = count(ranking.meta, :billed_units, :search_units)
|
|
47
|
+
generation.metadata = { search_units: } if search_units
|
|
48
|
+
|
|
49
|
+
trace_attributes = {
|
|
50
|
+
model: parameters[:model],
|
|
51
|
+
output: ranking.results.map(&:to_h),
|
|
52
|
+
usage_details: usage_details(ranking.meta),
|
|
53
|
+
cost_details: @base_handler.cost_details(ranking),
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
[ranking, trace_attributes]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Providers report a rerank's size as tokens, search units, or both. The tokens are recorded
|
|
60
|
+
# under Langfuse's own `input` and `total` keys: OpenAI's names are only recognised alongside
|
|
61
|
+
# `completion_tokens`, which a rerank has none of, and Langfuse would otherwise total every
|
|
62
|
+
# counter it was given, adding input tokens to themselves. Search units are not tokens, so
|
|
63
|
+
# they go in the metadata instead. A count the provider sent in some other shape is left
|
|
64
|
+
# out, rather than failing a request already paid for.
|
|
65
|
+
def usage_details(meta)
|
|
66
|
+
input_tokens = count(meta, :tokens, :input_tokens) || count(meta, :billed_units, :total_tokens)
|
|
67
|
+
{ input: input_tokens, total: input_tokens } if input_tokens
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def count(meta, section, key)
|
|
71
|
+
value = meta[section][key] if meta[section].is_a?(Hash)
|
|
72
|
+
value if value.is_a?(Numeric)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
require "openai"
|
|
2
|
+
require "nitro_intelligence/client/handlers/base_handler"
|
|
3
|
+
require "nitro_intelligence/ranking"
|
|
4
|
+
|
|
5
|
+
module NitroIntelligence
|
|
6
|
+
module Client
|
|
7
|
+
module Handlers
|
|
8
|
+
# Ranks documents by their relevance to a query over the gateway's Cohere-compatible rerank
|
|
9
|
+
# route, `POST /v1/rerank`. openai-ruby has no resource for it, so the request goes through
|
|
10
|
+
# the client's generic `request`, which keeps its credentials, retries, timeouts and errors.
|
|
11
|
+
#
|
|
12
|
+
# @api private
|
|
13
|
+
class RerankHandler < BaseHandler
|
|
14
|
+
PATH = "v1/rerank".freeze
|
|
15
|
+
# What is left of that route to add to an `inference_base_url` that already ends in `/v1`.
|
|
16
|
+
PATH_AFTER_V1 = "rerank".freeze
|
|
17
|
+
|
|
18
|
+
# Rerank request fields other than the model, query and documents, taken from `parameters`.
|
|
19
|
+
BODY_PARAMETERS = %i[top_n].freeze
|
|
20
|
+
|
|
21
|
+
# Declaring a model, rather than taking the decoded hash, is what gets the response its
|
|
22
|
+
# `last_response`, and with it the gateway's cost headers. `Ranking` reads `results` and
|
|
23
|
+
# `meta` raw through `BaseModel#[]`.
|
|
24
|
+
class Response < OpenAI::BaseModel
|
|
25
|
+
required :id, String, nil?: true
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def create(query:, documents:, parameters: {})
|
|
29
|
+
validate_and_resolve!(query, documents, parameters)
|
|
30
|
+
perform_request(query:, documents:, parameters:)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def perform_request(query:, documents:, parameters: {}, correlation_trace_id: nil, mask_content: false)
|
|
34
|
+
add_gateway_headers(parameters, correlation_trace_id:, mask_content:)
|
|
35
|
+
response = @client.request(
|
|
36
|
+
method: :post,
|
|
37
|
+
path:,
|
|
38
|
+
body: { model: parameters[:model], query:, documents:, **parameters.slice(*BODY_PARAMETERS).compact },
|
|
39
|
+
model: Response,
|
|
40
|
+
options: parameters[:request_options]
|
|
41
|
+
)
|
|
42
|
+
Ranking.new(response, top_n: parameters[:top_n])
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def validate_and_resolve!(query, documents, parameters)
|
|
46
|
+
default_parameters = {
|
|
47
|
+
metadata: {},
|
|
48
|
+
model: NitroIntelligence.model_catalog.default_rerank_model&.name,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
parameters.replace(default_parameters.merge(parameters))
|
|
52
|
+
Client.validate_model(parameters[:model], type: RerankModel)
|
|
53
|
+
raise ArgumentError, "query must be a non-empty string" unless query.is_a?(String) && query.present?
|
|
54
|
+
|
|
55
|
+
validate_documents!(documents)
|
|
56
|
+
validate_top_n!(parameters[:top_n])
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def validate_documents!(documents)
|
|
62
|
+
texts = documents.is_a?(Array) && documents.any? && documents.all?(String)
|
|
63
|
+
return if texts && documents.all?(&:present?)
|
|
64
|
+
|
|
65
|
+
raise ArgumentError, "documents must be a non-empty array of non-empty strings"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def validate_top_n!(top_n)
|
|
69
|
+
return if top_n.nil? || (top_n.is_a?(Integer) && top_n.positive?)
|
|
70
|
+
|
|
71
|
+
raise ArgumentError, "top_n must be a positive integer, not #{top_n.inspect}"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Both reach `/v1/rerank`: the whole route on a base URL without a version, or only the
|
|
75
|
+
# rest of it on one that already ends in `/v1`.
|
|
76
|
+
def path
|
|
77
|
+
@client.base_url.path.chomp("/").end_with?("/v1") ? PATH_AFTER_V1 : PATH
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
require "nitro_intelligence/client/handlers/observed/audio_transcription_handler"
|
|
2
2
|
require "nitro_intelligence/client/handlers/observed/chat_handler"
|
|
3
3
|
require "nitro_intelligence/client/handlers/observed/decision_handler"
|
|
4
|
+
require "nitro_intelligence/client/handlers/observed/embedding_handler"
|
|
4
5
|
require "nitro_intelligence/client/handlers/observed/image_handler"
|
|
6
|
+
require "nitro_intelligence/client/handlers/observed/rerank_handler"
|
|
5
7
|
require "nitro_intelligence/client/handlers/observed/text_to_speech_handler"
|
|
6
8
|
require "nitro_intelligence/observability/prompt_store"
|
|
7
9
|
require "nitro_intelligence/observability/upload_handler"
|
|
@@ -16,6 +18,11 @@ module NitroIntelligence
|
|
|
16
18
|
@observer = observer
|
|
17
19
|
end
|
|
18
20
|
|
|
21
|
+
# @api private
|
|
22
|
+
def prompt(name, label: nil, version: nil)
|
|
23
|
+
@observer.project_client.project.prompt(name, label:, version:)
|
|
24
|
+
end
|
|
25
|
+
|
|
19
26
|
private
|
|
20
27
|
|
|
21
28
|
def chat_handler
|
|
@@ -37,12 +44,24 @@ module NitroIntelligence
|
|
|
37
44
|
)
|
|
38
45
|
end
|
|
39
46
|
|
|
47
|
+
def embedding_handler
|
|
48
|
+
@embedding_handler ||= Handlers::Observed::EmbeddingHandler.new(
|
|
49
|
+
base_handler: Handlers::EmbeddingHandler.new(client: @client), observer: @observer
|
|
50
|
+
)
|
|
51
|
+
end
|
|
52
|
+
|
|
40
53
|
def image_handler
|
|
41
54
|
@image_handler ||= Handlers::Observed::ImageHandler.new(
|
|
42
55
|
base_handler: Handlers::ImageHandler.new(client: @client), observer: @observer
|
|
43
56
|
)
|
|
44
57
|
end
|
|
45
58
|
|
|
59
|
+
def rerank_handler
|
|
60
|
+
@rerank_handler ||= Handlers::Observed::RerankHandler.new(
|
|
61
|
+
base_handler: Handlers::RerankHandler.new(client: @client), observer: @observer
|
|
62
|
+
)
|
|
63
|
+
end
|
|
64
|
+
|
|
46
65
|
def text_to_speech_handler
|
|
47
66
|
@text_to_speech_handler ||= Handlers::Observed::TextToSpeechHandler.new(
|
|
48
67
|
base_handler: Handlers::TextToSpeechHandler.new(client: @client), observer: @observer
|
|
@@ -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
|
|
@@ -81,12 +90,21 @@ module NitroIntelligence
|
|
|
81
90
|
|
|
82
91
|
RECORDED_ATTRIBUTES.each do |name|
|
|
83
92
|
value = trace_attributes[name]
|
|
84
|
-
generation
|
|
93
|
+
record_attribute(generation, name, value) if value
|
|
85
94
|
end
|
|
86
95
|
|
|
87
96
|
record_status(generation, trace_attributes)
|
|
97
|
+
end
|
|
88
98
|
|
|
89
|
-
|
|
99
|
+
# langfuse-rb's embedding observation takes its cost only through #update: unlike a
|
|
100
|
+
# generation, it has no setter for it.
|
|
101
|
+
def record_attribute(observation, name, value)
|
|
102
|
+
setter = :"#{name}="
|
|
103
|
+
if observation.respond_to?(setter)
|
|
104
|
+
observation.public_send(setter, value)
|
|
105
|
+
else
|
|
106
|
+
observation.update(name => value)
|
|
107
|
+
end
|
|
90
108
|
end
|
|
91
109
|
|
|
92
110
|
def masked(trace_attributes)
|
|
@@ -119,10 +137,7 @@ module NitroIntelligence
|
|
|
119
137
|
def record_input(generation, input)
|
|
120
138
|
return if input.blank?
|
|
121
139
|
|
|
122
|
-
input = project.mask(input)
|
|
123
|
-
|
|
124
|
-
generation.input = input
|
|
125
|
-
generation.update_trace(input:)
|
|
140
|
+
generation.input = project.mask(input)
|
|
126
141
|
end
|
|
127
142
|
|
|
128
143
|
# `session_id` and `tags` are only forwarded when set so that callers who
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module NitroIntelligence
|
|
2
|
+
# What {Client::Base#embed_query} and {Client::Base#embed_documents} return: one vector for each
|
|
3
|
+
# input, in the order the inputs were given, however many requests it took to embed them.
|
|
4
|
+
class Embeddings
|
|
5
|
+
# @return [String, nil] the model that embedded the input, as the gateway named it
|
|
6
|
+
attr_reader :model
|
|
7
|
+
# @return [Array<Array<Float>>] one vector for each input, in input order
|
|
8
|
+
attr_reader :vectors
|
|
9
|
+
# @return [ActiveSupport::HashWithIndifferentAccess] `prompt_tokens` and `total_tokens`,
|
|
10
|
+
# summed over every request, each left out when a response did not report it
|
|
11
|
+
attr_reader :usage
|
|
12
|
+
# @return [Array<OpenAI::Models::CreateEmbeddingResponse>] the gateway's responses, one for each
|
|
13
|
+
# batch of input, each with its `last_response` and the gateway's headers
|
|
14
|
+
attr_reader :responses
|
|
15
|
+
|
|
16
|
+
# @api private
|
|
17
|
+
def initialize(responses)
|
|
18
|
+
@responses = responses
|
|
19
|
+
@model = responses.first&.model
|
|
20
|
+
@vectors = responses.flat_map { |response| response.data.sort_by(&:index).map(&:embedding) }
|
|
21
|
+
@usage = %i[prompt_tokens total_tokens].index_with { |count| total(count) }.compact.with_indifferent_access
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# The vector of the first input: the only one when the input was a single string.
|
|
25
|
+
#
|
|
26
|
+
# @return [Array<Float>, nil]
|
|
27
|
+
def vector
|
|
28
|
+
vectors.first
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
# Left out when any response did not report it, rather than undercounting.
|
|
34
|
+
def total(count)
|
|
35
|
+
counts = responses.map { |response| response.usage&.public_send(count) }
|
|
36
|
+
counts.sum if counts.any? && counts.none?(&:nil?)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -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
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require "nitro_intelligence/observability/batch_span_processor"
|
|
2
|
+
|
|
1
3
|
module NitroIntelligence
|
|
2
4
|
# This is an adaptation of https://github.com/simplepractice/langfuse-rb/blob/main/lib/langfuse/otel_setup.rb
|
|
3
5
|
# that lets users create multiple Langfuse clients in the same process.
|
|
@@ -16,6 +18,13 @@ module NitroIntelligence
|
|
|
16
18
|
@tracer_provider.tracer("langfuse-rb", Langfuse::VERSION)
|
|
17
19
|
end
|
|
18
20
|
|
|
21
|
+
# @api private
|
|
22
|
+
def force_flush(timeout: 30)
|
|
23
|
+
# Langfuse's other span processor only adds attributes; its flush is a no-op.
|
|
24
|
+
# Calling our processor directly also avoids the provider's unbounded flush mutex.
|
|
25
|
+
@batch_processor.force_flush(timeout:)
|
|
26
|
+
end
|
|
27
|
+
|
|
19
28
|
def shutdown(timeout: 30)
|
|
20
29
|
@tracer_provider.shutdown(timeout:)
|
|
21
30
|
end
|
|
@@ -50,7 +59,7 @@ module NitroIntelligence
|
|
|
50
59
|
compression: "gzip"
|
|
51
60
|
)
|
|
52
61
|
|
|
53
|
-
|
|
62
|
+
@batch_processor = Observability::BatchSpanProcessor.new(
|
|
54
63
|
exporter,
|
|
55
64
|
max_queue_size: config.batch_size * 2,
|
|
56
65
|
schedule_delay: config.flush_interval * 1000,
|
|
@@ -58,7 +67,7 @@ module NitroIntelligence
|
|
|
58
67
|
)
|
|
59
68
|
|
|
60
69
|
tracer_provider = OpenTelemetry::SDK::Trace::TracerProvider.new(id_generator: self)
|
|
61
|
-
tracer_provider.add_span_processor(
|
|
70
|
+
tracer_provider.add_span_processor(@batch_processor)
|
|
62
71
|
span_processor = Langfuse::SpanProcessor.new(config:)
|
|
63
72
|
tracer_provider.add_span_processor(span_processor)
|
|
64
73
|
|
|
@@ -64,8 +64,13 @@ module NitroIntelligence
|
|
|
64
64
|
end
|
|
65
65
|
|
|
66
66
|
# @api private
|
|
67
|
+
#
|
|
68
|
+
# Takes the last entry in `message.images`. Gemini image models that think put their interim
|
|
69
|
+
# thought images ahead of the final render in the same list, and Google documents the last image
|
|
70
|
+
# as the final one.
|
|
67
71
|
def parse_file(chat_completion)
|
|
68
|
-
|
|
72
|
+
images = chat_completion.choices.first&.message.to_h[:images]
|
|
73
|
+
base64_string = Array(images).last&.dig(:image_url, :url)
|
|
69
74
|
|
|
70
75
|
return unless base64_string
|
|
71
76
|
|
|
@@ -28,6 +28,27 @@ module NitroIntelligence
|
|
|
28
28
|
# @api private
|
|
29
29
|
class DecisionModel < Model; end
|
|
30
30
|
|
|
31
|
+
# @api private
|
|
32
|
+
class EmbeddingModel < Model
|
|
33
|
+
# What the model expects before a query's text and before a document's, for a model trained
|
|
34
|
+
# to embed the two differently. Empty for a model that embeds both the same way.
|
|
35
|
+
attr_reader :query_prefix, :document_prefix
|
|
36
|
+
|
|
37
|
+
def initialize(name:, omit_output_fields: [], query_prefix: "", document_prefix: "", **)
|
|
38
|
+
super
|
|
39
|
+
@query_prefix = query_prefix.to_s
|
|
40
|
+
@document_prefix = document_prefix.to_s
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @param input_type [Symbol] `:query` or `:document`
|
|
44
|
+
def prefix_for(input_type)
|
|
45
|
+
input_type == :query ? query_prefix : document_prefix
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @api private
|
|
50
|
+
class RerankModel < Model; end
|
|
51
|
+
|
|
31
52
|
# @api private
|
|
32
53
|
class TextModel < Model; end
|
|
33
54
|
|
|
@@ -6,7 +6,9 @@ module NitroIntelligence
|
|
|
6
6
|
attr_reader :models,
|
|
7
7
|
:default_audio_transcription_model,
|
|
8
8
|
:default_decision_model,
|
|
9
|
+
:default_embedding_model,
|
|
9
10
|
:default_image_model,
|
|
11
|
+
:default_rerank_model,
|
|
10
12
|
:default_text_model,
|
|
11
13
|
:default_text_to_speech_model
|
|
12
14
|
|
|
@@ -15,7 +17,9 @@ module NitroIntelligence
|
|
|
15
17
|
@models = (model_config[:models] || []).map { |model_metadata| ModelFactory.build(model_metadata) }
|
|
16
18
|
@default_audio_transcription_model = lookup_by_name(model_config[:default_audio_transcription_model])
|
|
17
19
|
@default_decision_model = lookup_by_name(model_config[:default_decision_model])
|
|
20
|
+
@default_embedding_model = lookup_by_name(model_config[:default_embedding_model])
|
|
18
21
|
@default_image_model = lookup_by_name(model_config[:default_image_model])
|
|
22
|
+
@default_rerank_model = lookup_by_name(model_config[:default_rerank_model])
|
|
19
23
|
@default_text_model = lookup_by_name(model_config[:default_text_model])
|
|
20
24
|
@default_text_to_speech_model = lookup_by_name(model_config[:default_text_to_speech_model])
|
|
21
25
|
end
|