langchainrb_datadog 0.1.1 → 0.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/lib/langchain/datadog/assistant.rb +86 -0
- data/lib/langchain/datadog/llm.rb +13 -1
- data/lib/langchain/datadog/version.rb +1 -1
- data/lib/langchain/datadog.rb +5 -0
- metadata +6 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 476c2defa855d2de1247b12f86cfe0cb8257112008cdbe75ab31203d4a7f8008
|
4
|
+
data.tar.gz: d3ab0555d28f069e5d83272128fe8a377d26ae0706f01edfeb621c9f9ab9c544
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e78fb388b5fc76dbb800deeb68abd056defdaf2053c0e938a45546f5e5889f0b7fea6fb99e4d3f679fa0a22813dffe753c6a1a81f699d7beb8a6af9bd9516905
|
7
|
+
data.tar.gz: 7cc14ab026333ab14db8635239cb34cd479e68f73c181ad4ed4ee2db68cbe19249641bd2b49e6486d44dfbc026929a6311f2cbeb22e08183fd6bc4e02d4a71de
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Langchain
|
4
|
+
module Datadog
|
5
|
+
# Implements hooks for Langchain::Assistant class to capture LLM
|
6
|
+
# calls and report them to the Datadog LLM Observability API.
|
7
|
+
module Assistant
|
8
|
+
include Langchain::Datadog::Tracing
|
9
|
+
|
10
|
+
def run(...)
|
11
|
+
return super unless Datadog.enabled?
|
12
|
+
|
13
|
+
span do
|
14
|
+
super
|
15
|
+
extract_content messages.last if state == :completed
|
16
|
+
end
|
17
|
+
|
18
|
+
messages
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def run_tool(tool_call, ...)
|
24
|
+
return super unless Datadog.enabled?
|
25
|
+
|
26
|
+
Tracing.send(:span, tool_call.to_json, kind: 'tool') do
|
27
|
+
super
|
28
|
+
extract_content messages.last
|
29
|
+
end
|
30
|
+
|
31
|
+
messages
|
32
|
+
end
|
33
|
+
|
34
|
+
def span
|
35
|
+
previous_parent_id = Tracing.active_parent_id
|
36
|
+
Tracing.start_span
|
37
|
+
|
38
|
+
input_messages = {
|
39
|
+
messages: messages.map do |message|
|
40
|
+
{ content: extract_content(message), role: message.role }
|
41
|
+
end
|
42
|
+
}
|
43
|
+
|
44
|
+
start_ns = (Time.now.to_r * 1_000_000_000).to_i
|
45
|
+
output = yield
|
46
|
+
duration = (Time.now.to_r * 1_000_000_000).to_i - start_ns
|
47
|
+
|
48
|
+
output_value = { value: output } if output
|
49
|
+
|
50
|
+
trace([{
|
51
|
+
name: caller_locations(1, 1)[0].label,
|
52
|
+
span_id: Tracing.active_span_id.to_s,
|
53
|
+
trace_id: Tracing.active_trace_id.to_s,
|
54
|
+
parent_id: Tracing.active_parent_id&.to_s || 'undefined',
|
55
|
+
start_ns:,
|
56
|
+
duration:,
|
57
|
+
meta: {
|
58
|
+
kind: 'agent',
|
59
|
+
input: input_messages,
|
60
|
+
output: output_value
|
61
|
+
}.compact,
|
62
|
+
metrics:
|
63
|
+
}.compact])
|
64
|
+
|
65
|
+
output
|
66
|
+
ensure
|
67
|
+
Tracing.end_span(parent_id: previous_parent_id)
|
68
|
+
end
|
69
|
+
|
70
|
+
def metrics
|
71
|
+
input_tokens = total_prompt_tokens
|
72
|
+
output_tokens = total_completion_tokens
|
73
|
+
|
74
|
+
metrics = { input_tokens:, output_tokens:, total_tokens: }.compact
|
75
|
+
|
76
|
+
metrics.empty? ? nil : metrics
|
77
|
+
end
|
78
|
+
|
79
|
+
def extract_content(message)
|
80
|
+
return nil unless message.respond_to?(:content) && message.respond_to?(:image_url)
|
81
|
+
|
82
|
+
[message.content, message.image_url].join(' ').strip
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -72,7 +72,8 @@ module Langchain
|
|
72
72
|
|
73
73
|
def input(parameters)
|
74
74
|
case parameters
|
75
|
-
in { messages: messages }
|
75
|
+
in { messages: messages }
|
76
|
+
{ messages: messages.map { |message| transform_content(message) } }
|
76
77
|
in { prompt: value } then { value: }
|
77
78
|
in { text: value } then { value: }
|
78
79
|
else nil
|
@@ -110,6 +111,17 @@ module Langchain
|
|
110
111
|
|
111
112
|
metrics.empty? ? nil : metrics
|
112
113
|
end
|
114
|
+
|
115
|
+
def transform_content(message) = {
|
116
|
+
content: (if (content = message[:content]).is_a?(Array)
|
117
|
+
content.map do |chunk|
|
118
|
+
chunk[:text] || chunk.dig(:image_url, :url)
|
119
|
+
end.compact.join
|
120
|
+
else
|
121
|
+
content
|
122
|
+
end),
|
123
|
+
role: message[:role]
|
124
|
+
}
|
113
125
|
end
|
114
126
|
end
|
115
127
|
end
|
data/lib/langchain/datadog.rb
CHANGED
@@ -7,6 +7,7 @@ require_relative 'datadog/version'
|
|
7
7
|
require_relative 'datadog/tracing'
|
8
8
|
require_relative 'datadog/llm'
|
9
9
|
require_relative 'datadog/vectorsearch'
|
10
|
+
require_relative 'datadog/assistant'
|
10
11
|
|
11
12
|
module Langchain
|
12
13
|
# Datadog LLM Observability integration with Langchain.rb.
|
@@ -87,3 +88,7 @@ Zeitwerk::Loader.eager_load_namespace(Langchain::Vectorsearch) rescue Zeitwerk::
|
|
87
88
|
Langchain::Vectorsearch::Base.subclasses.each do |klass|
|
88
89
|
klass.prepend Langchain::Datadog::Vectorsearch
|
89
90
|
end
|
91
|
+
|
92
|
+
# Prepend the Datadog::Assistant module to the Langchain::Assistant class to
|
93
|
+
# capture LLM calls and report them to the Datadog LLM Observability API.
|
94
|
+
Langchain::Assistant.prepend Langchain::Datadog::Assistant
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: langchainrb_datadog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GaggleAMP
|
8
8
|
- Nikolaos Anastopoulos
|
9
|
-
autorequire:
|
10
9
|
bindir: exe
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2025-02-06 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: faraday
|
@@ -29,14 +28,14 @@ dependencies:
|
|
29
28
|
name: langchainrb
|
30
29
|
requirement: !ruby/object:Gem::Requirement
|
31
30
|
requirements:
|
32
|
-
- - "
|
31
|
+
- - ">="
|
33
32
|
- !ruby/object:Gem::Version
|
34
33
|
version: 0.17.1
|
35
34
|
type: :runtime
|
36
35
|
prerelease: false
|
37
36
|
version_requirements: !ruby/object:Gem::Requirement
|
38
37
|
requirements:
|
39
|
-
- - "
|
38
|
+
- - ">="
|
40
39
|
- !ruby/object:Gem::Version
|
41
40
|
version: 0.17.1
|
42
41
|
description: Hooks into Langchain.rb methods to capture LLM calls and report them
|
@@ -54,6 +53,7 @@ files:
|
|
54
53
|
- README.md
|
55
54
|
- Rakefile
|
56
55
|
- lib/langchain/datadog.rb
|
56
|
+
- lib/langchain/datadog/assistant.rb
|
57
57
|
- lib/langchain/datadog/llm.rb
|
58
58
|
- lib/langchain/datadog/tracing.rb
|
59
59
|
- lib/langchain/datadog/vectorsearch.rb
|
@@ -66,7 +66,6 @@ metadata:
|
|
66
66
|
homepage_uri: https://github.com/GaggleAMP/langchainrb_datadog
|
67
67
|
source_code_uri: https://github.com/GaggleAMP/langchainrb_datadog
|
68
68
|
github_repo: git@github.com:GaggleAMP/langchainrb_datadog.git
|
69
|
-
post_install_message:
|
70
69
|
rdoc_options: []
|
71
70
|
require_paths:
|
72
71
|
- lib
|
@@ -81,8 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
80
|
- !ruby/object:Gem::Version
|
82
81
|
version: '0'
|
83
82
|
requirements: []
|
84
|
-
rubygems_version: 3.
|
85
|
-
signing_key:
|
83
|
+
rubygems_version: 3.6.2
|
86
84
|
specification_version: 4
|
87
85
|
summary: Enables LLM observability with Datadog for Langchain.rb.
|
88
86
|
test_files: []
|