openlit 1.5.0__py3-none-any.whl → 1.7.0__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.
@@ -0,0 +1,564 @@
1
+ # pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, possibly-used-before-assignment
2
+ """
3
+ Module for monitoring Ollama API calls.
4
+ """
5
+
6
+ import logging
7
+ from opentelemetry.trace import SpanKind, Status, StatusCode
8
+ from opentelemetry.sdk.resources import TELEMETRY_SDK_NAME
9
+ from openlit.__helpers import handle_exception, general_tokens
10
+ from openlit.semcov import SemanticConvetion
11
+
12
+ # Initialize logger for logging potential issues and operations
13
+ logger = logging.getLogger(__name__)
14
+
15
+ def chat(gen_ai_endpoint, version, environment, application_name,
16
+ tracer, pricing_info, trace_content, metrics, disable_metrics):
17
+ """
18
+ Generates a telemetry wrapper for chat to collect metrics.
19
+
20
+ Args:
21
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
22
+ version: Version of the monitoring package.
23
+ environment: Deployment environment (e.g., production, staging).
24
+ application_name: Name of the application using the Ollama API.
25
+ tracer: OpenTelemetry tracer for creating spans.
26
+ pricing_info: Information used for calculating the cost of Ollama usage.
27
+ trace_content: Flag indicating whether to trace the actual content.
28
+
29
+ Returns:
30
+ A function that wraps the chat method to add telemetry.
31
+ """
32
+
33
+ def wrapper(wrapped, instance, args, kwargs):
34
+ """
35
+ Wraps the 'chat' API call to add telemetry.
36
+
37
+ This collects metrics such as execution time, cost, and token usage, and handles errors
38
+ gracefully, adding details to the trace for observability.
39
+
40
+ Args:
41
+ wrapped: The original 'chat' method to be wrapped.
42
+ instance: The instance of the class where the original method is defined.
43
+ args: Positional arguments for the 'chat' method.
44
+ kwargs: Keyword arguments for the 'chat' method.
45
+
46
+ Returns:
47
+ The response from the original 'chat' method.
48
+ """
49
+
50
+ # Check if streaming is enabled for the API call
51
+ streaming = kwargs.get("stream", False)
52
+
53
+ # pylint: disable=no-else-return
54
+ if streaming:
55
+ # Special handling for streaming response to accommodate the nature of data flow
56
+ def stream_generator():
57
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
58
+ # Placeholder for aggregating streaming response
59
+ llmresponse = ""
60
+
61
+ # Loop through streaming events capturing relevant details
62
+ for chunk in wrapped(*args, **kwargs):
63
+ # Collect aggregated response from events
64
+ content = chunk['message']['content']
65
+ llmresponse += content
66
+
67
+ if chunk['done'] is True:
68
+ completion_tokens = chunk["eval_count"]
69
+
70
+ yield chunk
71
+
72
+ # Handling exception ensure observability without disrupting operation
73
+ try:
74
+ # Format 'messages' into a single string
75
+ message_prompt = kwargs.get("messages", "")
76
+ formatted_messages = []
77
+ for message in message_prompt:
78
+ role = message["role"]
79
+ content = message["content"]
80
+
81
+ if isinstance(content, list):
82
+ content_str = ", ".join(
83
+ # pylint: disable=line-too-long
84
+ f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
85
+ if "type" in item else f'text: {item["text"]}'
86
+ for item in content
87
+ )
88
+ formatted_messages.append(f"{role}: {content_str}")
89
+ else:
90
+ formatted_messages.append(f"{role}: {content}")
91
+ prompt = "\n".join(formatted_messages)
92
+
93
+ # Calculate cost of the operation
94
+ cost = 0
95
+ prompt_tokens = general_tokens(prompt)
96
+ total_tokens = prompt_tokens + completion_tokens
97
+
98
+ # Set Span attributes
99
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
100
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
101
+ SemanticConvetion.GEN_AI_SYSTEM_OLLAMA)
102
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
103
+ SemanticConvetion.GEN_AI_TYPE_CHAT)
104
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
105
+ gen_ai_endpoint)
106
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
107
+ environment)
108
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
109
+ application_name)
110
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
111
+ kwargs.get("model", "llama3"))
112
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
113
+ True)
114
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
115
+ prompt_tokens)
116
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
117
+ completion_tokens)
118
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
119
+ total_tokens)
120
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
121
+ cost)
122
+ if trace_content:
123
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
124
+ prompt)
125
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,
126
+ llmresponse)
127
+
128
+ span.set_status(Status(StatusCode.OK))
129
+
130
+ if disable_metrics is False:
131
+ attributes = {
132
+ TELEMETRY_SDK_NAME:
133
+ "openlit",
134
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
135
+ application_name,
136
+ SemanticConvetion.GEN_AI_SYSTEM:
137
+ SemanticConvetion.GEN_AI_SYSTEM_OLLAMA,
138
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
139
+ environment,
140
+ SemanticConvetion.GEN_AI_TYPE:
141
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
142
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
143
+ kwargs.get("model", "llama3")
144
+ }
145
+
146
+ metrics["genai_requests"].add(1, attributes)
147
+ metrics["genai_total_tokens"].add(total_tokens, attributes)
148
+ metrics["genai_completion_tokens"].add(completion_tokens, attributes)
149
+ metrics["genai_prompt_tokens"].add(prompt_tokens, attributes)
150
+ metrics["genai_cost"].record(cost, attributes)
151
+
152
+ except Exception as e:
153
+ handle_exception(span, e)
154
+ logger.error("Error in trace creation: %s", e)
155
+
156
+ return stream_generator()
157
+
158
+ # Handling for non-streaming responses
159
+ else:
160
+ # pylint: disable=line-too-long
161
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
162
+ response = wrapped(*args, **kwargs)
163
+
164
+ try:
165
+ # Format 'messages' into a single string
166
+ message_prompt = kwargs.get("messages", "")
167
+ formatted_messages = []
168
+ for message in message_prompt:
169
+ role = message["role"]
170
+ content = message["content"]
171
+
172
+ if isinstance(content, list):
173
+ content_str = ", ".join(
174
+ # pylint: disable=line-too-long
175
+ f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
176
+ if "type" in item else f'text: {item["text"]}'
177
+ for item in content
178
+ )
179
+ formatted_messages.append(f"{role}: {content_str}")
180
+ else:
181
+ formatted_messages.append(f"{role}: {content}")
182
+ prompt = "\n".join(formatted_messages)
183
+
184
+ # Set base span attribues
185
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
186
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
187
+ SemanticConvetion.GEN_AI_SYSTEM_OLLAMA)
188
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
189
+ SemanticConvetion.GEN_AI_TYPE_CHAT)
190
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
191
+ gen_ai_endpoint)
192
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
193
+ environment)
194
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
195
+ application_name)
196
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
197
+ kwargs.get("model", "llama3"))
198
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
199
+ False)
200
+ if trace_content:
201
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
202
+ prompt)
203
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,
204
+ response['message']['content'])
205
+
206
+ # Calculate cost of the operation
207
+ cost = 0
208
+ prompt_tokens = general_tokens(prompt)
209
+ completion_tokens = response["eval_count"]
210
+ total_tokens = prompt_tokens + completion_tokens
211
+
212
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
213
+ prompt_tokens)
214
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
215
+ completion_tokens)
216
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
217
+ total_tokens)
218
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
219
+ response["done_reason"])
220
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
221
+ cost)
222
+
223
+ span.set_status(Status(StatusCode.OK))
224
+
225
+ if disable_metrics is False:
226
+ attributes = {
227
+ TELEMETRY_SDK_NAME:
228
+ "openlit",
229
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
230
+ application_name,
231
+ SemanticConvetion.GEN_AI_SYSTEM:
232
+ SemanticConvetion.GEN_AI_SYSTEM_OLLAMA,
233
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
234
+ environment,
235
+ SemanticConvetion.GEN_AI_TYPE:
236
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
237
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
238
+ kwargs.get("model", "llama3")
239
+ }
240
+
241
+ metrics["genai_requests"].add(1, attributes)
242
+ metrics["genai_total_tokens"].add(total_tokens, attributes)
243
+ metrics["genai_completion_tokens"].add(completion_tokens, attributes)
244
+ metrics["genai_prompt_tokens"].add(prompt_tokens, attributes)
245
+ metrics["genai_cost"].record(cost, attributes)
246
+
247
+ # Return original response
248
+ return response
249
+
250
+ except Exception as e:
251
+ handle_exception(span, e)
252
+ logger.error("Error in trace creation: %s", e)
253
+
254
+ # Return original response
255
+ return response
256
+
257
+ return wrapper
258
+
259
+ def generate(gen_ai_endpoint, version, environment, application_name,
260
+ tracer, pricing_info, trace_content, metrics, disable_metrics):
261
+ """
262
+ Generates a telemetry wrapper for generate to collect metrics.
263
+
264
+ Args:
265
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
266
+ version: Version of the monitoring package.
267
+ environment: Deployment environment (e.g., production, staging).
268
+ application_name: Name of the application using the Ollama API.
269
+ tracer: OpenTelemetry tracer for creating spans.
270
+ pricing_info: Information used for calculating the cost of Ollama usage.
271
+ trace_content: Flag indicating whether to trace the actual content.
272
+
273
+ Returns:
274
+ A function that wraps the generate method to add telemetry.
275
+ """
276
+
277
+ def wrapper(wrapped, instance, args, kwargs):
278
+ """
279
+ Wraps the 'generate' API call to add telemetry.
280
+
281
+ This collects metrics such as execution time, cost, and token usage, and handles errors
282
+ gracefully, adding details to the trace for observability.
283
+
284
+ Args:
285
+ wrapped: The original 'generate' method to be wrapped.
286
+ instance: The instance of the class where the original method is defined.
287
+ args: Positional arguments for the 'generate' method.
288
+ kwargs: Keyword arguments for the 'generate' method.
289
+
290
+ Returns:
291
+ The response from the original 'generate' method.
292
+ """
293
+
294
+ # Check if streaming is enabled for the API call
295
+ streaming = kwargs.get("stream", False)
296
+
297
+ # pylint: disable=no-else-return
298
+ if streaming:
299
+ # Special handling for streaming response to accommodate the nature of data flow
300
+ def stream_generator():
301
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
302
+ # Placeholder for aggregating streaming response
303
+ llmresponse = ""
304
+
305
+ # Loop through streaming events capturing relevant details
306
+ for chunk in wrapped(*args, **kwargs):
307
+ # Collect aggregated response from events
308
+ content = chunk['response']
309
+ llmresponse += content
310
+
311
+ if chunk['done'] is True:
312
+ completion_tokens = chunk["eval_count"]
313
+
314
+ yield chunk
315
+
316
+ # Handling exception ensure observability without disrupting operation
317
+ try:
318
+ # Calculate cost of the operation
319
+ cost = 0
320
+ prompt_tokens = general_tokens(kwargs.get("prompt", ""))
321
+ total_tokens = prompt_tokens + completion_tokens
322
+
323
+ # Set Span attributes
324
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
325
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
326
+ SemanticConvetion.GEN_AI_SYSTEM_OLLAMA)
327
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
328
+ SemanticConvetion.GEN_AI_TYPE_CHAT)
329
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
330
+ gen_ai_endpoint)
331
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
332
+ environment)
333
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
334
+ application_name)
335
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
336
+ kwargs.get("model", "llama3"))
337
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
338
+ True)
339
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
340
+ prompt_tokens)
341
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
342
+ completion_tokens)
343
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
344
+ total_tokens)
345
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
346
+ cost)
347
+ if trace_content:
348
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
349
+ kwargs.get("prompt", ""))
350
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,
351
+ llmresponse)
352
+
353
+ span.set_status(Status(StatusCode.OK))
354
+
355
+ if disable_metrics is False:
356
+ attributes = {
357
+ TELEMETRY_SDK_NAME:
358
+ "openlit",
359
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
360
+ application_name,
361
+ SemanticConvetion.GEN_AI_SYSTEM:
362
+ SemanticConvetion.GEN_AI_SYSTEM_OLLAMA,
363
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
364
+ environment,
365
+ SemanticConvetion.GEN_AI_TYPE:
366
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
367
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
368
+ kwargs.get("model", "llama3")
369
+ }
370
+
371
+ metrics["genai_requests"].add(1, attributes)
372
+ metrics["genai_total_tokens"].add(total_tokens, attributes)
373
+ metrics["genai_completion_tokens"].add(completion_tokens, attributes)
374
+ metrics["genai_prompt_tokens"].add(prompt_tokens, attributes)
375
+ metrics["genai_cost"].record(cost, attributes)
376
+
377
+ except Exception as e:
378
+ handle_exception(span, e)
379
+ logger.error("Error in trace creation: %s", e)
380
+
381
+ return stream_generator()
382
+
383
+ # Handling for non-streaming responses
384
+ else:
385
+ # pylint: disable=line-too-long
386
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
387
+ response = wrapped(*args, **kwargs)
388
+
389
+ try:
390
+ # Set base span attribues
391
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
392
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
393
+ SemanticConvetion.GEN_AI_SYSTEM_OLLAMA)
394
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
395
+ SemanticConvetion.GEN_AI_TYPE_CHAT)
396
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
397
+ gen_ai_endpoint)
398
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
399
+ environment)
400
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
401
+ application_name)
402
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
403
+ kwargs.get("model", "llama3"))
404
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
405
+ False)
406
+ if trace_content:
407
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
408
+ kwargs.get("prompt", ""))
409
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,
410
+ response['response'])
411
+
412
+ # Calculate cost of the operation
413
+ cost = 0
414
+ prompt_tokens = response["prompt_eval_count"]
415
+ completion_tokens = response["eval_count"]
416
+ total_tokens = prompt_tokens + completion_tokens
417
+
418
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
419
+ prompt_tokens)
420
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
421
+ completion_tokens)
422
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
423
+ total_tokens)
424
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
425
+ response["done_reason"])
426
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
427
+ cost)
428
+
429
+ span.set_status(Status(StatusCode.OK))
430
+
431
+ if disable_metrics is False:
432
+ attributes = {
433
+ TELEMETRY_SDK_NAME:
434
+ "openlit",
435
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
436
+ application_name,
437
+ SemanticConvetion.GEN_AI_SYSTEM:
438
+ SemanticConvetion.GEN_AI_SYSTEM_OLLAMA,
439
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
440
+ environment,
441
+ SemanticConvetion.GEN_AI_TYPE:
442
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
443
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
444
+ kwargs.get("model", "llama3")
445
+ }
446
+
447
+ metrics["genai_requests"].add(1, attributes)
448
+ metrics["genai_total_tokens"].add(total_tokens, attributes)
449
+ metrics["genai_completion_tokens"].add(completion_tokens, attributes)
450
+ metrics["genai_prompt_tokens"].add(prompt_tokens, attributes)
451
+ metrics["genai_cost"].record(cost, attributes)
452
+
453
+ # Return original response
454
+ return response
455
+
456
+ except Exception as e:
457
+ handle_exception(span, e)
458
+ logger.error("Error in trace creation: %s", e)
459
+
460
+ # Return original response
461
+ return response
462
+
463
+ return wrapper
464
+
465
+ def embeddings(gen_ai_endpoint, version, environment, application_name,
466
+ tracer, pricing_info, trace_content, metrics, disable_metrics):
467
+ """
468
+ Generates a telemetry wrapper for embeddings to collect metrics.
469
+
470
+ Args:
471
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
472
+ version: Version of the monitoring package.
473
+ environment: Deployment environment (e.g., production, staging).
474
+ application_name: Name of the application using the Ollama API.
475
+ tracer: OpenTelemetry tracer for creating spans.
476
+ pricing_info: Information used for calculating the cost of Ollama usage.
477
+ trace_content: Flag indicating whether to trace the actual content.
478
+
479
+ Returns:
480
+ A function that wraps the embeddings method to add telemetry.
481
+ """
482
+
483
+ def wrapper(wrapped, instance, args, kwargs):
484
+ """
485
+ Wraps the 'embeddings' API call to add telemetry.
486
+
487
+ This collects metrics such as execution time, cost, and token usage, and handles errors
488
+ gracefully, adding details to the trace for observability.
489
+
490
+ Args:
491
+ wrapped: The original 'embeddings' method to be wrapped.
492
+ instance: The instance of the class where the original method is defined.
493
+ args: Positional arguments for the 'embeddings' method.
494
+ kwargs: Keyword arguments for the 'embeddings' method.
495
+
496
+ Returns:
497
+ The response from the original 'embeddings' method.
498
+ """
499
+
500
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
501
+ response = wrapped(*args, **kwargs)
502
+
503
+ try:
504
+ # Calculate cost of the operation
505
+ cost = 0
506
+ prompt_tokens = general_tokens(kwargs.get('prompt', ""))
507
+ # Set Span attributes
508
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
509
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
510
+ SemanticConvetion.GEN_AI_SYSTEM_OLLAMA)
511
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
512
+ SemanticConvetion.GEN_AI_TYPE_EMBEDDING)
513
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
514
+ gen_ai_endpoint)
515
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
516
+ environment)
517
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
518
+ application_name)
519
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
520
+ kwargs.get('model', "llama3"))
521
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
522
+ prompt_tokens)
523
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
524
+ prompt_tokens)
525
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
526
+ cost)
527
+ if trace_content:
528
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
529
+ kwargs.get('prompt', ""))
530
+
531
+ span.set_status(Status(StatusCode.OK))
532
+
533
+ if disable_metrics is False:
534
+ attributes = {
535
+ TELEMETRY_SDK_NAME:
536
+ "openlit",
537
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
538
+ application_name,
539
+ SemanticConvetion.GEN_AI_SYSTEM:
540
+ SemanticConvetion.GEN_AI_SYSTEM_OLLAMA,
541
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
542
+ environment,
543
+ SemanticConvetion.GEN_AI_TYPE:
544
+ SemanticConvetion.GEN_AI_TYPE_EMBEDDING,
545
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
546
+ kwargs.get('model', "llama3")
547
+ }
548
+
549
+ metrics["genai_requests"].add(1, attributes)
550
+ metrics["genai_total_tokens"].add(prompt_tokens, attributes)
551
+ metrics["genai_prompt_tokens"].add(prompt_tokens, attributes)
552
+ metrics["genai_cost"].record(cost, attributes)
553
+
554
+ # Return original response
555
+ return response
556
+
557
+ except Exception as e:
558
+ handle_exception(span, e)
559
+ logger.error("Error in trace creation: %s", e)
560
+
561
+ # Return original response
562
+ return response
563
+
564
+ return wrapper
@@ -88,6 +88,7 @@ class SemanticConvetion:
88
88
  GEN_AI_SYSTEM_BEDROCK = "bedrock"
89
89
  GEN_AI_SYSTEM_VERTEXAI = "vertexai"
90
90
  GEN_AI_SYSTEM_GROQ = "groq"
91
+ GEN_AI_SYSTEM_OLLAMA = "ollama"
91
92
  GEN_AI_SYSTEM_LANGCHAIN = "langchain"
92
93
  GEN_AI_SYSTEM_LLAMAINDEX = "llama_index"
93
94
  GEN_AI_SYSTEM_HAYSTACK = "haystack"
@@ -98,6 +99,7 @@ class SemanticConvetion:
98
99
  DB_COLLECTION_NAME = "db.collection.name"
99
100
  DB_OPERATION = "db.operation"
100
101
  DB_OPERATION_STATUS = "db.operation.status"
102
+ DB_OPERATION_COST = "db.operation.cost"
101
103
  DB_OPERATION_CREATE_INDEX = "create_index"
102
104
  DB_OPERATION_CREATE_COLLECTION = "create_collection"
103
105
  DB_OPERATION_UPDATE_COLLECTION = "update_collection"
@@ -122,7 +124,8 @@ class SemanticConvetion:
122
124
  DB_N_RESULTS = "db.n_results"
123
125
  DB_DELETE_ALL = "db.delete_all"
124
126
  DB_INDEX_NAME = "db.index.name"
125
- DB_INDEX_DIMENSION = "db.create_index.dimensions"
127
+ DB_INDEX_DIMENSION = "db.index.dimension"
128
+ DB_COLLECTION_DIMENSION = "db.collection.dimension"
126
129
  DB_INDEX_METRIC = "db.create_index.metric"
127
130
  DB_INDEX_SPEC = "db.create_index.spec"
128
131
  DB_NAMESPACE = "db.query.namespace"
@@ -133,3 +136,4 @@ class SemanticConvetion:
133
136
  DB_SYSTEM_CHROMA = "chroma"
134
137
  DB_SYSTEM_PINECONE = "pinecone"
135
138
  DB_SYSTEM_QDRANT = "qdrant"
139
+ DB_SYSTEM_MILVUS = "milvus"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: openlit
3
- Version: 1.5.0
3
+ Version: 1.7.0
4
4
  Summary: OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications, facilitating the integration of observability into your GenAI-driven projects
5
5
  Home-page: https://github.com/openlit/openlit/tree/main/openlit/python
6
6
  Keywords: OpenTelemetry,otel,otlp,llm,tracing,openai,anthropic,claude,cohere,llm monitoring,observability,monitoring,gpt,Generative AI,chatGPT
@@ -50,6 +50,7 @@ This project adheres to the [Semantic Conventions](https://github.com/open-telem
50
50
 
51
51
  ### LLMs
52
52
  - [✅ OpenAI](https://docs.openlit.io/latest/integrations/openai)
53
+ - [✅ Ollama](https://docs.openlit.io/latest/integrations/ollama)
53
54
  - [✅ Anthropic](https://docs.openlit.io/latest/integrations/anthropic)
54
55
  - [✅ Cohere](https://docs.openlit.io/latest/integrations/cohere)
55
56
  - [✅ Mistral](https://docs.openlit.io/latest/integrations/mistral)
@@ -63,6 +64,7 @@ This project adheres to the [Semantic Conventions](https://github.com/open-telem
63
64
  - [✅ ChromaDB](https://docs.openlit.io/latest/integrations/chromadb)
64
65
  - [✅ Pinecone](https://docs.openlit.io/latest/integrations/pinecone)
65
66
  - [✅ Qdrant](https://docs.openlit.io/latest/integrations/qdrant)
67
+ - [✅ Milvus](https://docs.openlit.io/latest/integrations/milvus)
66
68
 
67
69
  ### Frameworks
68
70
  - [✅ Langchain](https://docs.openlit.io/latest/integrations/langchain)
@@ -1,5 +1,5 @@
1
1
  openlit/__helpers.py,sha256=EEbLEUKuCiBp0WiieAvUnGcaU5D7grFgNVDCBgMKjQE,4651
2
- openlit/__init__.py,sha256=nm6JVVYnaslW5RQRvj1x0qnCQA_WBhAjHioY3w_IXlk,9647
2
+ openlit/__init__.py,sha256=NzHt2F8iJh5ljM1wD-HH4pkH2yH0e0ivE49U5LNGlnk,9917
3
3
  openlit/instrumentation/anthropic/__init__.py,sha256=oaU53BOPyfUKbEzYvLr1DPymDluurSnwo4Hernf2XdU,1955
4
4
  openlit/instrumentation/anthropic/anthropic.py,sha256=CYBui5eEfWdSfFF0xtCQjh1xO-gCVJc_V9Hli0szVZE,16026
5
5
  openlit/instrumentation/anthropic/async_anthropic.py,sha256=NW84kTQ3BkUx1zZuMRps_J7zTYkmq5BxOrqSjqWInBs,16068
@@ -18,9 +18,14 @@ openlit/instrumentation/langchain/__init__.py,sha256=TW1ZR7I1i9Oig-wDWp3j1gmtQFO
18
18
  openlit/instrumentation/langchain/langchain.py,sha256=G66UytYwWW0DdvChomzkc5_MJ-sjupuDwlxe4KqlGhY,7639
19
19
  openlit/instrumentation/llamaindex/__init__.py,sha256=FwE0iozGbZcd2dWo9uk4EO6qKPAe55byhZBuzuFyxXA,1973
20
20
  openlit/instrumentation/llamaindex/llamaindex.py,sha256=uiIigbwhonSbJWA7LpgOVI1R4kxxPODS1K5wyHIQ4hM,4048
21
+ openlit/instrumentation/milvus/__init__.py,sha256=qi1yfmMrvkDtnrN_6toW8qC9BRL78bq7ayWpObJ8Bq4,2961
22
+ openlit/instrumentation/milvus/milvus.py,sha256=qhKIoggBAJhRctRrBYz69AcvXH-eh7oBn_l9WfxpAjI,9121
21
23
  openlit/instrumentation/mistral/__init__.py,sha256=zJCIpFWRbsYrvooOJYuqwyuKeSOQLWbyXWCObL-Snks,3156
22
24
  openlit/instrumentation/mistral/async_mistral.py,sha256=PXpiLwkonTtAPVOUh9pXMSYeabwH0GFG_HRDWrEKhMM,21361
23
25
  openlit/instrumentation/mistral/mistral.py,sha256=nbAyMlPiuA9hihePkM_nnxAjahZSndT-B-qXRO5VIhk,21212
26
+ openlit/instrumentation/ollama/__init__.py,sha256=cOax8PiypDuo_FC4WvDCYBRo7lH5nV9xU92h7k-eZbg,3812
27
+ openlit/instrumentation/ollama/async_ollama.py,sha256=ESk1zZTj2hPmkWIH5F2owuoo0apleDSSx5VORlO3e3w,28991
28
+ openlit/instrumentation/ollama/ollama.py,sha256=PLGF9RB3TRNZ9GSGqeGVvKFBtgUK8Hc8xwvk-3NPeGI,28901
24
29
  openlit/instrumentation/openai/__init__.py,sha256=AZ2cPr3TMKkgGdMl_yXMeSi7bWhtmMqOW1iHdzHHGHA,16265
25
30
  openlit/instrumentation/openai/async_azure_openai.py,sha256=QE_5KHaCHAndkf7Y2Iq66mZUc0I1qtqIJ6iYPnwiuXA,46297
26
31
  openlit/instrumentation/openai/async_openai.py,sha256=qfJG_gIkSz3Gjau0zQ_G3tvkqkOd-md5LBK6wIjvH0U,45841
@@ -37,8 +42,8 @@ openlit/instrumentation/vertexai/async_vertexai.py,sha256=PMHYyLf1J4gZpC_-KZ_ZVx
37
42
  openlit/instrumentation/vertexai/vertexai.py,sha256=UvpNKBHPoV9idVMfGigZnmWuEQiyqSwZn0zK9-U7Lzw,52125
38
43
  openlit/otel/metrics.py,sha256=O7NoaDz0bY19mqpE4-0PcKwEe-B-iJFRgOCaanAuZAc,4291
39
44
  openlit/otel/tracing.py,sha256=vL1ifMbARPBpqK--yXYsCM6y5dSu5LFIKqkhZXtYmUc,3712
40
- openlit/semcov/__init__.py,sha256=0BHZuWE4strAmc-ilcVf6Nzoe0FEx0v9FStkccxs6mc,6168
41
- openlit-1.5.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
42
- openlit-1.5.0.dist-info/METADATA,sha256=Z0jWHca3OkoU_dcT2STaAvAFDf3xLbPtAXfOxGAK2m0,12146
43
- openlit-1.5.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
44
- openlit-1.5.0.dist-info/RECORD,,
45
+ openlit/semcov/__init__.py,sha256=wcy_1uP6_YxpaHxlo5he91m2GcuD5sfngZbZGgIwNlY,6328
46
+ openlit-1.7.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
47
+ openlit-1.7.0.dist-info/METADATA,sha256=gTob1Bi1ZPwj0i0AxWvnjxdg24xrAspP7_OmOIphCBo,12280
48
+ openlit-1.7.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
49
+ openlit-1.7.0.dist-info/RECORD,,