openlit 1.34.29__py3-none-any.whl → 1.34.31__py3-none-any.whl

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 (168) hide show
  1. openlit/__helpers.py +235 -86
  2. openlit/__init__.py +16 -13
  3. openlit/_instrumentors.py +2 -1
  4. openlit/evals/all.py +50 -21
  5. openlit/evals/bias_detection.py +47 -20
  6. openlit/evals/hallucination.py +53 -22
  7. openlit/evals/toxicity.py +50 -21
  8. openlit/evals/utils.py +54 -30
  9. openlit/guard/all.py +61 -19
  10. openlit/guard/prompt_injection.py +34 -14
  11. openlit/guard/restrict_topic.py +46 -15
  12. openlit/guard/sensitive_topic.py +34 -14
  13. openlit/guard/utils.py +58 -22
  14. openlit/instrumentation/ag2/__init__.py +24 -8
  15. openlit/instrumentation/ag2/ag2.py +34 -13
  16. openlit/instrumentation/ag2/async_ag2.py +34 -13
  17. openlit/instrumentation/ag2/utils.py +133 -30
  18. openlit/instrumentation/ai21/__init__.py +43 -14
  19. openlit/instrumentation/ai21/ai21.py +47 -21
  20. openlit/instrumentation/ai21/async_ai21.py +47 -21
  21. openlit/instrumentation/ai21/utils.py +299 -78
  22. openlit/instrumentation/anthropic/__init__.py +21 -4
  23. openlit/instrumentation/anthropic/anthropic.py +28 -17
  24. openlit/instrumentation/anthropic/async_anthropic.py +28 -17
  25. openlit/instrumentation/anthropic/utils.py +145 -35
  26. openlit/instrumentation/assemblyai/__init__.py +11 -2
  27. openlit/instrumentation/assemblyai/assemblyai.py +15 -4
  28. openlit/instrumentation/assemblyai/utils.py +120 -25
  29. openlit/instrumentation/astra/__init__.py +43 -10
  30. openlit/instrumentation/astra/astra.py +28 -5
  31. openlit/instrumentation/astra/async_astra.py +28 -5
  32. openlit/instrumentation/astra/utils.py +151 -55
  33. openlit/instrumentation/azure_ai_inference/__init__.py +43 -10
  34. openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py +53 -21
  35. openlit/instrumentation/azure_ai_inference/azure_ai_inference.py +53 -21
  36. openlit/instrumentation/azure_ai_inference/utils.py +307 -83
  37. openlit/instrumentation/bedrock/__init__.py +21 -4
  38. openlit/instrumentation/bedrock/bedrock.py +63 -25
  39. openlit/instrumentation/bedrock/utils.py +139 -30
  40. openlit/instrumentation/chroma/__init__.py +89 -16
  41. openlit/instrumentation/chroma/chroma.py +28 -6
  42. openlit/instrumentation/chroma/utils.py +167 -51
  43. openlit/instrumentation/cohere/__init__.py +63 -18
  44. openlit/instrumentation/cohere/async_cohere.py +63 -24
  45. openlit/instrumentation/cohere/cohere.py +63 -24
  46. openlit/instrumentation/cohere/utils.py +286 -73
  47. openlit/instrumentation/controlflow/__init__.py +35 -9
  48. openlit/instrumentation/controlflow/controlflow.py +66 -33
  49. openlit/instrumentation/crawl4ai/__init__.py +25 -10
  50. openlit/instrumentation/crawl4ai/async_crawl4ai.py +78 -31
  51. openlit/instrumentation/crawl4ai/crawl4ai.py +78 -31
  52. openlit/instrumentation/crewai/__init__.py +111 -24
  53. openlit/instrumentation/crewai/async_crewai.py +114 -0
  54. openlit/instrumentation/crewai/crewai.py +104 -131
  55. openlit/instrumentation/crewai/utils.py +615 -0
  56. openlit/instrumentation/dynamiq/__init__.py +46 -12
  57. openlit/instrumentation/dynamiq/dynamiq.py +74 -33
  58. openlit/instrumentation/elevenlabs/__init__.py +23 -4
  59. openlit/instrumentation/elevenlabs/async_elevenlabs.py +16 -4
  60. openlit/instrumentation/elevenlabs/elevenlabs.py +16 -4
  61. openlit/instrumentation/elevenlabs/utils.py +128 -25
  62. openlit/instrumentation/embedchain/__init__.py +11 -2
  63. openlit/instrumentation/embedchain/embedchain.py +68 -35
  64. openlit/instrumentation/firecrawl/__init__.py +24 -7
  65. openlit/instrumentation/firecrawl/firecrawl.py +46 -20
  66. openlit/instrumentation/google_ai_studio/__init__.py +45 -10
  67. openlit/instrumentation/google_ai_studio/async_google_ai_studio.py +67 -44
  68. openlit/instrumentation/google_ai_studio/google_ai_studio.py +67 -44
  69. openlit/instrumentation/google_ai_studio/utils.py +180 -67
  70. openlit/instrumentation/gpt4all/__init__.py +22 -7
  71. openlit/instrumentation/gpt4all/gpt4all.py +67 -29
  72. openlit/instrumentation/gpt4all/utils.py +285 -61
  73. openlit/instrumentation/gpu/__init__.py +128 -47
  74. openlit/instrumentation/groq/__init__.py +21 -4
  75. openlit/instrumentation/groq/async_groq.py +33 -21
  76. openlit/instrumentation/groq/groq.py +33 -21
  77. openlit/instrumentation/groq/utils.py +192 -55
  78. openlit/instrumentation/haystack/__init__.py +70 -24
  79. openlit/instrumentation/haystack/async_haystack.py +28 -6
  80. openlit/instrumentation/haystack/haystack.py +28 -6
  81. openlit/instrumentation/haystack/utils.py +196 -74
  82. openlit/instrumentation/julep/__init__.py +69 -19
  83. openlit/instrumentation/julep/async_julep.py +53 -27
  84. openlit/instrumentation/julep/julep.py +53 -28
  85. openlit/instrumentation/langchain/__init__.py +74 -63
  86. openlit/instrumentation/langchain/callback_handler.py +1100 -0
  87. openlit/instrumentation/langchain_community/__init__.py +13 -2
  88. openlit/instrumentation/langchain_community/async_langchain_community.py +23 -5
  89. openlit/instrumentation/langchain_community/langchain_community.py +23 -5
  90. openlit/instrumentation/langchain_community/utils.py +35 -9
  91. openlit/instrumentation/letta/__init__.py +68 -15
  92. openlit/instrumentation/letta/letta.py +99 -54
  93. openlit/instrumentation/litellm/__init__.py +43 -14
  94. openlit/instrumentation/litellm/async_litellm.py +51 -26
  95. openlit/instrumentation/litellm/litellm.py +51 -26
  96. openlit/instrumentation/litellm/utils.py +312 -101
  97. openlit/instrumentation/llamaindex/__init__.py +267 -90
  98. openlit/instrumentation/llamaindex/async_llamaindex.py +28 -6
  99. openlit/instrumentation/llamaindex/llamaindex.py +28 -6
  100. openlit/instrumentation/llamaindex/utils.py +204 -91
  101. openlit/instrumentation/mem0/__init__.py +11 -2
  102. openlit/instrumentation/mem0/mem0.py +50 -29
  103. openlit/instrumentation/milvus/__init__.py +10 -2
  104. openlit/instrumentation/milvus/milvus.py +31 -6
  105. openlit/instrumentation/milvus/utils.py +166 -67
  106. openlit/instrumentation/mistral/__init__.py +63 -18
  107. openlit/instrumentation/mistral/async_mistral.py +63 -24
  108. openlit/instrumentation/mistral/mistral.py +63 -24
  109. openlit/instrumentation/mistral/utils.py +277 -69
  110. openlit/instrumentation/multion/__init__.py +69 -19
  111. openlit/instrumentation/multion/async_multion.py +57 -26
  112. openlit/instrumentation/multion/multion.py +57 -26
  113. openlit/instrumentation/ollama/__init__.py +39 -18
  114. openlit/instrumentation/ollama/async_ollama.py +57 -26
  115. openlit/instrumentation/ollama/ollama.py +57 -26
  116. openlit/instrumentation/ollama/utils.py +226 -50
  117. openlit/instrumentation/openai/__init__.py +156 -32
  118. openlit/instrumentation/openai/async_openai.py +147 -67
  119. openlit/instrumentation/openai/openai.py +150 -67
  120. openlit/instrumentation/openai/utils.py +660 -186
  121. openlit/instrumentation/openai_agents/__init__.py +6 -2
  122. openlit/instrumentation/openai_agents/processor.py +409 -537
  123. openlit/instrumentation/phidata/__init__.py +13 -5
  124. openlit/instrumentation/phidata/phidata.py +67 -32
  125. openlit/instrumentation/pinecone/__init__.py +48 -9
  126. openlit/instrumentation/pinecone/async_pinecone.py +27 -5
  127. openlit/instrumentation/pinecone/pinecone.py +27 -5
  128. openlit/instrumentation/pinecone/utils.py +153 -47
  129. openlit/instrumentation/premai/__init__.py +22 -7
  130. openlit/instrumentation/premai/premai.py +51 -26
  131. openlit/instrumentation/premai/utils.py +246 -59
  132. openlit/instrumentation/pydantic_ai/__init__.py +49 -22
  133. openlit/instrumentation/pydantic_ai/pydantic_ai.py +69 -16
  134. openlit/instrumentation/pydantic_ai/utils.py +89 -24
  135. openlit/instrumentation/qdrant/__init__.py +19 -4
  136. openlit/instrumentation/qdrant/async_qdrant.py +33 -7
  137. openlit/instrumentation/qdrant/qdrant.py +33 -7
  138. openlit/instrumentation/qdrant/utils.py +228 -93
  139. openlit/instrumentation/reka/__init__.py +23 -10
  140. openlit/instrumentation/reka/async_reka.py +17 -11
  141. openlit/instrumentation/reka/reka.py +17 -11
  142. openlit/instrumentation/reka/utils.py +138 -36
  143. openlit/instrumentation/together/__init__.py +44 -12
  144. openlit/instrumentation/together/async_together.py +50 -27
  145. openlit/instrumentation/together/together.py +50 -27
  146. openlit/instrumentation/together/utils.py +301 -71
  147. openlit/instrumentation/transformers/__init__.py +2 -1
  148. openlit/instrumentation/transformers/transformers.py +13 -3
  149. openlit/instrumentation/transformers/utils.py +139 -36
  150. openlit/instrumentation/vertexai/__init__.py +81 -16
  151. openlit/instrumentation/vertexai/async_vertexai.py +33 -15
  152. openlit/instrumentation/vertexai/utils.py +123 -27
  153. openlit/instrumentation/vertexai/vertexai.py +33 -15
  154. openlit/instrumentation/vllm/__init__.py +12 -5
  155. openlit/instrumentation/vllm/utils.py +121 -31
  156. openlit/instrumentation/vllm/vllm.py +16 -10
  157. openlit/otel/events.py +35 -10
  158. openlit/otel/metrics.py +32 -24
  159. openlit/otel/tracing.py +24 -9
  160. openlit/semcov/__init__.py +101 -7
  161. {openlit-1.34.29.dist-info → openlit-1.34.31.dist-info}/METADATA +2 -1
  162. openlit-1.34.31.dist-info/RECORD +166 -0
  163. openlit/instrumentation/langchain/async_langchain.py +0 -102
  164. openlit/instrumentation/langchain/langchain.py +0 -102
  165. openlit/instrumentation/langchain/utils.py +0 -252
  166. openlit-1.34.29.dist-info/RECORD +0 -166
  167. {openlit-1.34.29.dist-info → openlit-1.34.31.dist-info}/LICENSE +0 -0
  168. {openlit-1.34.29.dist-info → openlit-1.34.31.dist-info}/WHEEL +0 -0
@@ -4,10 +4,7 @@ Module for monitoring Amazon Bedrock API calls.
4
4
 
5
5
  import time
6
6
  from opentelemetry.trace import SpanKind
7
- from openlit.__helpers import (
8
- handle_exception,
9
- set_server_address_and_port
10
- )
7
+ from openlit.__helpers import handle_exception, set_server_address_and_port
11
8
  from openlit.instrumentation.bedrock.utils import (
12
9
  process_chunk,
13
10
  process_chat_response,
@@ -15,7 +12,17 @@ from openlit.instrumentation.bedrock.utils import (
15
12
  )
16
13
  from openlit.semcov import SemanticConvention
17
14
 
18
- def converse(version, environment, application_name, tracer, pricing_info, capture_message_content, metrics, disable_metrics):
15
+
16
+ def converse(
17
+ version,
18
+ environment,
19
+ application_name,
20
+ tracer,
21
+ pricing_info,
22
+ capture_message_content,
23
+ metrics,
24
+ disable_metrics,
25
+ ):
19
26
  """
20
27
  Generates a telemetry wrapper for AWS Bedrock converse calls.
21
28
  """
@@ -30,10 +37,14 @@ def converse(version, environment, application_name, tracer, pricing_info, captu
30
37
  Wraps the individual converse method call.
31
38
  """
32
39
 
33
- server_address, server_port = set_server_address_and_port(instance, "aws.amazon.com", 443)
40
+ server_address, server_port = set_server_address_and_port(
41
+ instance, "aws.amazon.com", 443
42
+ )
34
43
  request_model = method_kwargs.get("modelId", "amazon.titan-text-express-v1")
35
44
 
36
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
45
+ span_name = (
46
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
47
+ )
37
48
 
38
49
  with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
39
50
  start_time = time.time()
@@ -56,7 +67,7 @@ def converse(version, environment, application_name, tracer, pricing_info, captu
56
67
  disable_metrics=disable_metrics,
57
68
  version=version,
58
69
  llm_config=llm_config,
59
- **method_kwargs
70
+ **method_kwargs,
60
71
  )
61
72
 
62
73
  except Exception as e:
@@ -70,13 +81,25 @@ def converse(version, environment, application_name, tracer, pricing_info, captu
70
81
  # Replace the original method with the instrumented one
71
82
  if kwargs.get("service_name") == "bedrock-runtime":
72
83
  original_invoke_model = client.converse
73
- client.converse = lambda *args, **kwargs: converse_wrapper(original_invoke_model, *args, **kwargs)
84
+ client.converse = lambda *args, **kwargs: converse_wrapper(
85
+ original_invoke_model, *args, **kwargs
86
+ )
74
87
 
75
88
  return client
76
89
 
77
90
  return wrapper
78
91
 
79
- def converse_stream(version, environment, application_name, tracer, pricing_info, capture_message_content, metrics, disable_metrics):
92
+
93
+ def converse_stream(
94
+ version,
95
+ environment,
96
+ application_name,
97
+ tracer,
98
+ pricing_info,
99
+ capture_message_content,
100
+ metrics,
101
+ disable_metrics,
102
+ ):
80
103
  """
81
104
  Generates a telemetry wrapper for AWS Bedrock converse_stream calls.
82
105
  """
@@ -87,15 +110,15 @@ def converse_stream(version, environment, application_name, tracer, pricing_info
87
110
  """
88
111
 
89
112
  def __init__(
90
- self,
91
- wrapped_response,
92
- span,
93
- span_name,
94
- kwargs,
95
- server_address,
96
- server_port,
97
- **args,
98
- ):
113
+ self,
114
+ wrapped_response,
115
+ span,
116
+ span_name,
117
+ kwargs,
118
+ server_address,
119
+ server_port,
120
+ **args,
121
+ ):
99
122
  self.__wrapped_response = wrapped_response
100
123
  # Extract the actual stream iterator from the response
101
124
  if isinstance(wrapped_response, dict) and "stream" in wrapped_response:
@@ -159,7 +182,9 @@ def converse_stream(version, environment, application_name, tracer, pricing_info
159
182
  except StopIteration:
160
183
  try:
161
184
  llm_config = self._kwargs.get("inferenceConfig", {})
162
- with tracer.start_as_current_span(self._span_name, kind=SpanKind.CLIENT) as self._span:
185
+ with tracer.start_as_current_span(
186
+ self._span_name, kind=SpanKind.CLIENT
187
+ ) as self._span:
163
188
  process_streaming_chat_response(
164
189
  self,
165
190
  pricing_info=pricing_info,
@@ -169,7 +194,7 @@ def converse_stream(version, environment, application_name, tracer, pricing_info
169
194
  capture_message_content=capture_message_content,
170
195
  disable_metrics=disable_metrics,
171
196
  version=version,
172
- llm_config=llm_config
197
+ llm_config=llm_config,
173
198
  )
174
199
 
175
200
  except Exception as e:
@@ -187,16 +212,27 @@ def converse_stream(version, environment, application_name, tracer, pricing_info
187
212
  Wraps the individual converse_stream method call.
188
213
  """
189
214
 
190
- server_address, server_port = set_server_address_and_port(instance, "aws.amazon.com", 443)
215
+ server_address, server_port = set_server_address_and_port(
216
+ instance, "aws.amazon.com", 443
217
+ )
191
218
  request_model = method_kwargs.get("modelId", "amazon.titan-text-express-v1")
192
219
 
193
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
220
+ span_name = (
221
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
222
+ )
194
223
 
195
224
  # Get the streaming response
196
225
  stream_response = original_method(*method_args, **method_kwargs)
197
226
  span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
198
227
 
199
- return TracedSyncStream(stream_response, span, span_name, method_kwargs, server_address, server_port)
228
+ return TracedSyncStream(
229
+ stream_response,
230
+ span,
231
+ span_name,
232
+ method_kwargs,
233
+ server_address,
234
+ server_port,
235
+ )
200
236
 
201
237
  # Get the original client instance from the wrapper
202
238
  client = wrapped(*args, **kwargs)
@@ -204,7 +240,9 @@ def converse_stream(version, environment, application_name, tracer, pricing_info
204
240
  # Replace the original method with the instrumented one
205
241
  if kwargs.get("service_name") == "bedrock-runtime":
206
242
  original_stream_model = client.converse_stream
207
- client.converse_stream = lambda *args, **kwargs: converse_stream_wrapper(original_stream_model, *args, **kwargs)
243
+ client.converse_stream = lambda *args, **kwargs: converse_stream_wrapper(
244
+ original_stream_model, *args, **kwargs
245
+ )
208
246
 
209
247
  return client
210
248
 
@@ -1,6 +1,7 @@
1
1
  """
2
2
  AWS Bedrock OpenTelemetry instrumentation utility functions
3
3
  """
4
+
4
5
  import time
5
6
 
6
7
  from opentelemetry.trace import Status, StatusCode
@@ -12,10 +13,11 @@ from openlit.__helpers import (
12
13
  get_chat_model_cost,
13
14
  record_completion_metrics,
14
15
  common_span_attributes,
15
- handle_exception
16
+ handle_exception,
16
17
  )
17
18
  from openlit.semcov import SemanticConvention
18
19
 
20
+
19
21
  def format_content(messages):
20
22
  """
21
23
  Format the messages into a string for span events.
@@ -52,6 +54,7 @@ def format_content(messages):
52
54
 
53
55
  return "\n".join(formatted_messages)
54
56
 
57
+
55
58
  def process_chunk(self, chunk):
56
59
  """
57
60
  Process a chunk of response data and update state.
@@ -92,8 +95,19 @@ def process_chunk(self, chunk):
92
95
  self._output_tokens = usage.get("outputTokens", 0)
93
96
  self._end_time = end_time
94
97
 
95
- def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
96
- capture_message_content, disable_metrics, version, llm_config, is_stream):
98
+
99
+ def common_chat_logic(
100
+ scope,
101
+ pricing_info,
102
+ environment,
103
+ application_name,
104
+ metrics,
105
+ capture_message_content,
106
+ disable_metrics,
107
+ version,
108
+ llm_config,
109
+ is_stream,
110
+ ):
97
111
  """
98
112
  Process chat request and generate Telemetry
99
113
  """
@@ -105,13 +119,26 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
105
119
  formatted_messages = format_content(scope._kwargs.get("messages", []))
106
120
  request_model = scope._kwargs.get("modelId", "amazon.titan-text-express-v1")
107
121
 
108
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
122
+ cost = get_chat_model_cost(
123
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
124
+ )
109
125
 
110
126
  # Common Span Attributes
111
- common_span_attributes(scope,
112
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_AWS_BEDROCK,
113
- scope._server_address, scope._server_port, request_model, scope._response_model,
114
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
127
+ common_span_attributes(
128
+ scope,
129
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
130
+ SemanticConvention.GEN_AI_SYSTEM_AWS_BEDROCK,
131
+ scope._server_address,
132
+ scope._server_port,
133
+ request_model,
134
+ scope._response_model,
135
+ environment,
136
+ application_name,
137
+ is_stream,
138
+ scope._tbt,
139
+ scope._ttft,
140
+ version,
141
+ )
115
142
 
116
143
  # Bedrock-specific attributes from llm_config
117
144
  bedrock_attributes = [
@@ -132,19 +159,35 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
132
159
 
133
160
  # Span Attributes for Response parameters
134
161
  scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
135
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
136
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
162
+ scope._span.set_attribute(
163
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
164
+ )
165
+ scope._span.set_attribute(
166
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
167
+ "text" if isinstance(scope._llmresponse, str) else "json",
168
+ )
137
169
 
138
170
  # Span Attributes for Cost and Tokens
139
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
140
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
141
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
171
+ scope._span.set_attribute(
172
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
173
+ )
174
+ scope._span.set_attribute(
175
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
176
+ )
177
+ scope._span.set_attribute(
178
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
179
+ scope._input_tokens + scope._output_tokens,
180
+ )
142
181
  scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
143
182
 
144
183
  # Span Attributes for Content
145
184
  if capture_message_content:
146
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, formatted_messages)
147
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
185
+ scope._span.set_attribute(
186
+ SemanticConvention.GEN_AI_CONTENT_PROMPT, formatted_messages
187
+ )
188
+ scope._span.set_attribute(
189
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
190
+ )
148
191
 
149
192
  # To be removed once the change to span_attributes (from span events) is complete
150
193
  scope._span.add_event(
@@ -164,13 +207,37 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
164
207
 
165
208
  # Record metrics
166
209
  if not disable_metrics:
167
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_AWS_BEDROCK,
168
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
169
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
170
- cost, scope._tbt, scope._ttft)
210
+ record_completion_metrics(
211
+ metrics,
212
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
213
+ SemanticConvention.GEN_AI_SYSTEM_AWS_BEDROCK,
214
+ scope._server_address,
215
+ scope._server_port,
216
+ request_model,
217
+ scope._response_model,
218
+ environment,
219
+ application_name,
220
+ scope._start_time,
221
+ scope._end_time,
222
+ scope._input_tokens,
223
+ scope._output_tokens,
224
+ cost,
225
+ scope._tbt,
226
+ scope._ttft,
227
+ )
171
228
 
172
- def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
173
- capture_message_content=False, disable_metrics=False, version="", llm_config=None):
229
+
230
+ def process_streaming_chat_response(
231
+ scope,
232
+ pricing_info,
233
+ environment,
234
+ application_name,
235
+ metrics,
236
+ capture_message_content=False,
237
+ disable_metrics=False,
238
+ version="",
239
+ llm_config=None,
240
+ ):
174
241
  """
175
242
  Process streaming chat response and generate telemetry.
176
243
  """
@@ -179,15 +246,40 @@ def process_streaming_chat_response(scope, pricing_info, environment, applicatio
179
246
  if llm_config is None:
180
247
  llm_config = {}
181
248
 
182
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
183
- capture_message_content, disable_metrics, version, llm_config, is_stream=True)
249
+ common_chat_logic(
250
+ scope,
251
+ pricing_info,
252
+ environment,
253
+ application_name,
254
+ metrics,
255
+ capture_message_content,
256
+ disable_metrics,
257
+ version,
258
+ llm_config,
259
+ is_stream=True,
260
+ )
184
261
  except Exception as e:
185
262
  handle_exception(scope._span, e)
186
263
  raise
187
264
 
188
- def process_chat_response(response, request_model, pricing_info, server_port, server_address, environment,
189
- application_name, metrics, start_time, span, capture_message_content=False,
190
- disable_metrics=False, version="1.0.0", llm_config=None, **kwargs):
265
+
266
+ def process_chat_response(
267
+ response,
268
+ request_model,
269
+ pricing_info,
270
+ server_port,
271
+ server_address,
272
+ environment,
273
+ application_name,
274
+ metrics,
275
+ start_time,
276
+ span,
277
+ capture_message_content=False,
278
+ disable_metrics=False,
279
+ version="1.0.0",
280
+ llm_config=None,
281
+ **kwargs,
282
+ ):
191
283
  """
192
284
  Process non-streaming chat response and generate telemetry.
193
285
  """
@@ -202,8 +294,15 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
202
294
  scope._start_time = start_time
203
295
  scope._end_time = time.time()
204
296
  scope._span = span
205
- scope._llmresponse = response_dict.get("output", {}).get("message", {}).get("content", [{}])[0].get("text", "")
206
- scope._response_role = response_dict.get("output", {}).get("message", {}).get("role", "assistant")
297
+ scope._llmresponse = (
298
+ response_dict.get("output", {})
299
+ .get("message", {})
300
+ .get("content", [{}])[0]
301
+ .get("text", "")
302
+ )
303
+ scope._response_role = (
304
+ response_dict.get("output", {}).get("message", {}).get("role", "assistant")
305
+ )
207
306
  scope._input_tokens = response_dict.get("usage", {}).get("inputTokens", 0)
208
307
  scope._output_tokens = response_dict.get("usage", {}).get("outputTokens", 0)
209
308
  scope._response_model = request_model
@@ -214,8 +313,18 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
214
313
  scope._server_address, scope._server_port = server_address, server_port
215
314
  scope._kwargs = kwargs
216
315
 
217
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
218
- capture_message_content, disable_metrics, version, llm_config, is_stream=False)
316
+ common_chat_logic(
317
+ scope,
318
+ pricing_info,
319
+ environment,
320
+ application_name,
321
+ metrics,
322
+ capture_message_content,
323
+ disable_metrics,
324
+ version,
325
+ llm_config,
326
+ is_stream=False,
327
+ )
219
328
 
220
329
  return response
221
330
  except Exception as e:
@@ -11,6 +11,7 @@ from openlit.instrumentation.chroma.chroma import general_wrap
11
11
 
12
12
  _instruments = ("chromadb >= 0.4.0",)
13
13
 
14
+
14
15
  class ChromaInstrumentor(BaseInstrumentor):
15
16
  """
16
17
  An instrumentor for ChromaDB client library.
@@ -33,57 +34,129 @@ class ChromaInstrumentor(BaseInstrumentor):
33
34
  wrap_function_wrapper(
34
35
  "chromadb.db",
35
36
  "DB.create_collection",
36
- general_wrap("chroma.create_collection", version, environment, application_name,
37
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
37
+ general_wrap(
38
+ "chroma.create_collection",
39
+ version,
40
+ environment,
41
+ application_name,
42
+ tracer,
43
+ pricing_info,
44
+ capture_message_content,
45
+ metrics,
46
+ disable_metrics,
47
+ ),
38
48
  )
39
49
 
40
50
  wrap_function_wrapper(
41
51
  "chromadb",
42
52
  "Collection.add",
43
- general_wrap("chroma.add", version, environment, application_name,
44
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
53
+ general_wrap(
54
+ "chroma.add",
55
+ version,
56
+ environment,
57
+ application_name,
58
+ tracer,
59
+ pricing_info,
60
+ capture_message_content,
61
+ metrics,
62
+ disable_metrics,
63
+ ),
45
64
  )
46
65
 
47
66
  wrap_function_wrapper(
48
67
  "chromadb",
49
68
  "Collection.get",
50
- general_wrap("chroma.get", version, environment, application_name,
51
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
69
+ general_wrap(
70
+ "chroma.get",
71
+ version,
72
+ environment,
73
+ application_name,
74
+ tracer,
75
+ pricing_info,
76
+ capture_message_content,
77
+ metrics,
78
+ disable_metrics,
79
+ ),
52
80
  )
53
81
 
54
82
  wrap_function_wrapper(
55
83
  "chromadb",
56
84
  "Collection.peek",
57
- general_wrap("chroma.peek", version, environment, application_name,
58
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
85
+ general_wrap(
86
+ "chroma.peek",
87
+ version,
88
+ environment,
89
+ application_name,
90
+ tracer,
91
+ pricing_info,
92
+ capture_message_content,
93
+ metrics,
94
+ disable_metrics,
95
+ ),
59
96
  )
60
97
 
61
98
  wrap_function_wrapper(
62
99
  "chromadb",
63
100
  "Collection.query",
64
- general_wrap("chroma.query", version, environment, application_name,
65
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
101
+ general_wrap(
102
+ "chroma.query",
103
+ version,
104
+ environment,
105
+ application_name,
106
+ tracer,
107
+ pricing_info,
108
+ capture_message_content,
109
+ metrics,
110
+ disable_metrics,
111
+ ),
66
112
  )
67
113
 
68
114
  wrap_function_wrapper(
69
115
  "chromadb",
70
116
  "Collection.update",
71
- general_wrap("chroma.update", version, environment, application_name,
72
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
117
+ general_wrap(
118
+ "chroma.update",
119
+ version,
120
+ environment,
121
+ application_name,
122
+ tracer,
123
+ pricing_info,
124
+ capture_message_content,
125
+ metrics,
126
+ disable_metrics,
127
+ ),
73
128
  )
74
129
 
75
130
  wrap_function_wrapper(
76
131
  "chromadb",
77
132
  "Collection.upsert",
78
- general_wrap("chroma.upsert", version, environment, application_name,
79
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
133
+ general_wrap(
134
+ "chroma.upsert",
135
+ version,
136
+ environment,
137
+ application_name,
138
+ tracer,
139
+ pricing_info,
140
+ capture_message_content,
141
+ metrics,
142
+ disable_metrics,
143
+ ),
80
144
  )
81
145
 
82
146
  wrap_function_wrapper(
83
147
  "chromadb",
84
148
  "Collection.delete",
85
- general_wrap("chroma.delete", version, environment, application_name,
86
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
149
+ general_wrap(
150
+ "chroma.delete",
151
+ version,
152
+ environment,
153
+ application_name,
154
+ tracer,
155
+ pricing_info,
156
+ capture_message_content,
157
+ metrics,
158
+ disable_metrics,
159
+ ),
87
160
  )
88
161
 
89
162
  def _uninstrument(self, **kwargs):
@@ -12,8 +12,18 @@ from openlit.instrumentation.chroma.utils import (
12
12
  set_server_address_and_port,
13
13
  )
14
14
 
15
- def general_wrap(gen_ai_endpoint, version, environment, application_name,
16
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
15
+
16
+ def general_wrap(
17
+ gen_ai_endpoint,
18
+ version,
19
+ environment,
20
+ application_name,
21
+ tracer,
22
+ pricing_info,
23
+ capture_message_content,
24
+ metrics,
25
+ disable_metrics,
26
+ ):
17
27
  """
18
28
  Generates a telemetry wrapper for ChromaDB function calls.
19
29
  """
@@ -43,9 +53,22 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,
43
53
  try:
44
54
  # Process response and generate telemetry
45
55
  response = process_vectordb_response(
46
- response, db_operation, server_address, server_port,
47
- environment, application_name, metrics, start_time, span,
48
- capture_message_content, disable_metrics, version, instance, args, endpoint=gen_ai_endpoint, **kwargs
56
+ response,
57
+ db_operation,
58
+ server_address,
59
+ server_port,
60
+ environment,
61
+ application_name,
62
+ metrics,
63
+ start_time,
64
+ span,
65
+ capture_message_content,
66
+ disable_metrics,
67
+ version,
68
+ instance,
69
+ args,
70
+ endpoint=gen_ai_endpoint,
71
+ **kwargs,
49
72
  )
50
73
 
51
74
  except Exception as e:
@@ -53,5 +76,4 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,
53
76
 
54
77
  return response
55
78
 
56
-
57
79
  return wrapper