traceloop-sdk 0.0.4 → 0.0.6

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/traceloop/sdk.rb +42 -7
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 50c7d784c78f716b3416a3143d25a8dfaa0c406a16bbcd10342082a63d93f074
4
- data.tar.gz: 26543e0a8ad9b85e4746b338fb086aad27963e23d8279e4548beb12d13629c1c
3
+ metadata.gz: 50ee211ca84ca20194daaee350af3ea0eeebe4d112a0f2882da3effa2a65c921
4
+ data.tar.gz: 7b86bc1e6a3acbfa71f04e0574e942ee285348bc03b04f1fc3f7bee2b9bc56e5
5
5
  SHA512:
6
- metadata.gz: 3b4bf642d9f3f412637bafd75cb5a56d6f499a0eb8853113a2c3b37ae3ddb61ab0c92275cc170253df689c305a6e56984008a35be0782bfc122df713a7e7ea66
7
- data.tar.gz: cda39a259b39a44d895373b820bc269da1711f2f7cd6618176f3508d6b4047b0bfc2998492225111651fdca35b799306747a047259a85468ec3288dab0407771
6
+ metadata.gz: 7839bf70345e804b3f4eb0e79bf6d23f49b6947edbfcc89dd9768ee778cce05d2a93786bf9adfab4da9063a97476e4dfd9d6f29ff79f427bb85a3790e86569cb
7
+ data.tar.gz: f505c2e1e2f8b4fa7f91ae2782ddac3c7c641dbb3d98bf8e9d437ef40b77cb5567c9d223aea5471ea43c8117d5959f4cb7f3e1094593765a4e3a07357c1bac8a
data/lib/traceloop/sdk.rb CHANGED
@@ -29,13 +29,20 @@ module Traceloop
29
29
  @model = model
30
30
  end
31
31
 
32
- def log_prompt(model, system_prompt="", user_prompt)
33
- @span.add_attributes({
34
- "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.0.role" => "system",
35
- "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.0.content" => system_prompt,
36
- "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.1.role" => "user",
37
- "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.1.content" => user_prompt
38
- })
32
+ def log_prompt(system_prompt="", user_prompt)
33
+ unless system_prompt.empty?
34
+ @span.add_attributes({
35
+ "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.0.role" => "system",
36
+ "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.0.content" => system_prompt,
37
+ "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.1.role" => "user",
38
+ "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.1.content" => user_prompt
39
+ })
40
+ else
41
+ @span.add_attributes({
42
+ "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.0.role" => "user",
43
+ "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_PROMPTS}.0.content" => user_prompt
44
+ })
45
+ end
39
46
  end
40
47
 
41
48
  def log_response(response)
@@ -43,6 +50,8 @@ module Traceloop
43
50
  # https://github.com/gbaptista/gemini-ai?tab=readme-ov-file#generate_content
44
51
  if response.has_key?("candidates")
45
52
  log_gemini_response(response)
53
+ elsif response.has_key?("body")
54
+ log_bedrock_response(response)
46
55
  else
47
56
  log_openai_response(response)
48
57
  end
@@ -59,6 +68,32 @@ module Traceloop
59
68
  })
60
69
  end
61
70
 
71
+ def log_bedrock_response(response)
72
+ body = JSON.parse(response.body.read())
73
+
74
+ @span.add_attributes({
75
+ OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_RESPONSE_MODEL => body.dig("model"),
76
+ })
77
+ if response.has_key?("usage")
78
+ input_tokens = body.dig("usage", "input_tokens")
79
+ output_tokens = body.dig("usage", "output_tokens")
80
+
81
+ @span.add_attributes({
82
+ OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_USAGE_TOTAL_TOKENS => input_tokens + output_tokens,
83
+ OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_USAGE_COMPLETION_TOKENS => output_tokens,
84
+ OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_USAGE_PROMPT_TOKENS => input_tokens,
85
+ })
86
+ end
87
+ if response.has_key?("content")
88
+ @span.add_attributes({
89
+ "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_COMPLETIONS}.0.role" => body.dig("role"),
90
+ "#{OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_COMPLETIONS}.0.content" => body.dig("content").first.dig("text")
91
+ })
92
+ end
93
+
94
+ response.body.rewind()
95
+ end
96
+
62
97
  def log_openai_response(response)
63
98
  @span.add_attributes({
64
99
  OpenTelemetry::SemanticConventionsAi::SpanAttributes::LLM_RESPONSE_MODEL => response.dig("model"),
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: traceloop-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Traceloop
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-24 00:00:00.000000000 Z
11
+ date: 2024-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opentelemetry-semantic_conventions_ai