openlit 1.33.10__py3-none-any.whl → 1.33.12__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 (113) hide show
  1. openlit/__helpers.py +125 -88
  2. openlit/__init__.py +38 -11
  3. openlit/instrumentation/ag2/__init__.py +19 -20
  4. openlit/instrumentation/ag2/ag2.py +134 -69
  5. openlit/instrumentation/ai21/__init__.py +22 -21
  6. openlit/instrumentation/ai21/ai21.py +82 -546
  7. openlit/instrumentation/ai21/async_ai21.py +82 -546
  8. openlit/instrumentation/ai21/utils.py +409 -0
  9. openlit/instrumentation/anthropic/__init__.py +16 -16
  10. openlit/instrumentation/anthropic/anthropic.py +61 -353
  11. openlit/instrumentation/anthropic/async_anthropic.py +62 -354
  12. openlit/instrumentation/anthropic/utils.py +251 -0
  13. openlit/instrumentation/assemblyai/__init__.py +2 -2
  14. openlit/instrumentation/assemblyai/assemblyai.py +3 -3
  15. openlit/instrumentation/astra/__init__.py +25 -25
  16. openlit/instrumentation/astra/astra.py +2 -2
  17. openlit/instrumentation/astra/async_astra.py +2 -2
  18. openlit/instrumentation/azure_ai_inference/__init__.py +5 -5
  19. openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py +8 -8
  20. openlit/instrumentation/azure_ai_inference/azure_ai_inference.py +8 -8
  21. openlit/instrumentation/bedrock/__init__.py +2 -2
  22. openlit/instrumentation/bedrock/bedrock.py +3 -3
  23. openlit/instrumentation/chroma/__init__.py +9 -9
  24. openlit/instrumentation/chroma/chroma.py +2 -2
  25. openlit/instrumentation/cohere/__init__.py +7 -7
  26. openlit/instrumentation/cohere/async_cohere.py +9 -9
  27. openlit/instrumentation/cohere/cohere.py +9 -9
  28. openlit/instrumentation/controlflow/__init__.py +4 -4
  29. openlit/instrumentation/controlflow/controlflow.py +2 -2
  30. openlit/instrumentation/crawl4ai/__init__.py +3 -3
  31. openlit/instrumentation/crawl4ai/async_crawl4ai.py +2 -2
  32. openlit/instrumentation/crawl4ai/crawl4ai.py +2 -2
  33. openlit/instrumentation/crewai/__init__.py +3 -3
  34. openlit/instrumentation/crewai/crewai.py +2 -2
  35. openlit/instrumentation/dynamiq/__init__.py +5 -5
  36. openlit/instrumentation/dynamiq/dynamiq.py +2 -2
  37. openlit/instrumentation/elevenlabs/__init__.py +5 -5
  38. openlit/instrumentation/elevenlabs/async_elevenlabs.py +3 -3
  39. openlit/instrumentation/elevenlabs/elevenlabs.py +3 -3
  40. openlit/instrumentation/embedchain/__init__.py +2 -2
  41. openlit/instrumentation/embedchain/embedchain.py +4 -4
  42. openlit/instrumentation/firecrawl/__init__.py +3 -3
  43. openlit/instrumentation/firecrawl/firecrawl.py +2 -2
  44. openlit/instrumentation/google_ai_studio/__init__.py +3 -3
  45. openlit/instrumentation/google_ai_studio/async_google_ai_studio.py +3 -3
  46. openlit/instrumentation/google_ai_studio/google_ai_studio.py +3 -3
  47. openlit/instrumentation/gpt4all/__init__.py +3 -3
  48. openlit/instrumentation/gpt4all/gpt4all.py +7 -7
  49. openlit/instrumentation/groq/__init__.py +3 -3
  50. openlit/instrumentation/groq/async_groq.py +5 -5
  51. openlit/instrumentation/groq/groq.py +5 -5
  52. openlit/instrumentation/haystack/__init__.py +2 -2
  53. openlit/instrumentation/haystack/haystack.py +2 -2
  54. openlit/instrumentation/julep/__init__.py +7 -7
  55. openlit/instrumentation/julep/async_julep.py +3 -3
  56. openlit/instrumentation/julep/julep.py +3 -3
  57. openlit/instrumentation/langchain/__init__.py +2 -2
  58. openlit/instrumentation/langchain/async_langchain.py +13 -9
  59. openlit/instrumentation/langchain/langchain.py +13 -8
  60. openlit/instrumentation/letta/__init__.py +7 -7
  61. openlit/instrumentation/letta/letta.py +5 -5
  62. openlit/instrumentation/litellm/__init__.py +5 -5
  63. openlit/instrumentation/litellm/async_litellm.py +8 -8
  64. openlit/instrumentation/litellm/litellm.py +8 -8
  65. openlit/instrumentation/llamaindex/__init__.py +2 -2
  66. openlit/instrumentation/llamaindex/llamaindex.py +2 -2
  67. openlit/instrumentation/mem0/__init__.py +2 -2
  68. openlit/instrumentation/mem0/mem0.py +2 -2
  69. openlit/instrumentation/milvus/__init__.py +2 -2
  70. openlit/instrumentation/milvus/milvus.py +2 -2
  71. openlit/instrumentation/mistral/__init__.py +7 -7
  72. openlit/instrumentation/mistral/async_mistral.py +10 -10
  73. openlit/instrumentation/mistral/mistral.py +10 -10
  74. openlit/instrumentation/multion/__init__.py +7 -7
  75. openlit/instrumentation/multion/async_multion.py +5 -5
  76. openlit/instrumentation/multion/multion.py +5 -5
  77. openlit/instrumentation/ollama/__init__.py +11 -9
  78. openlit/instrumentation/ollama/async_ollama.py +71 -465
  79. openlit/instrumentation/ollama/ollama.py +71 -465
  80. openlit/instrumentation/ollama/utils.py +332 -0
  81. openlit/instrumentation/openai/__init__.py +11 -11
  82. openlit/instrumentation/openai/async_openai.py +18 -18
  83. openlit/instrumentation/openai/openai.py +18 -18
  84. openlit/instrumentation/phidata/__init__.py +2 -2
  85. openlit/instrumentation/phidata/phidata.py +2 -2
  86. openlit/instrumentation/pinecone/__init__.py +6 -6
  87. openlit/instrumentation/pinecone/pinecone.py +2 -2
  88. openlit/instrumentation/premai/__init__.py +3 -3
  89. openlit/instrumentation/premai/premai.py +7 -7
  90. openlit/instrumentation/qdrant/__init__.py +2 -2
  91. openlit/instrumentation/qdrant/async_qdrant.py +2 -2
  92. openlit/instrumentation/qdrant/qdrant.py +2 -2
  93. openlit/instrumentation/reka/__init__.py +3 -3
  94. openlit/instrumentation/reka/async_reka.py +3 -3
  95. openlit/instrumentation/reka/reka.py +3 -3
  96. openlit/instrumentation/together/__init__.py +5 -5
  97. openlit/instrumentation/together/async_together.py +8 -8
  98. openlit/instrumentation/together/together.py +8 -8
  99. openlit/instrumentation/transformers/__init__.py +2 -2
  100. openlit/instrumentation/transformers/transformers.py +4 -4
  101. openlit/instrumentation/vertexai/__init__.py +9 -9
  102. openlit/instrumentation/vertexai/async_vertexai.py +4 -4
  103. openlit/instrumentation/vertexai/vertexai.py +4 -4
  104. openlit/instrumentation/vllm/__init__.py +2 -2
  105. openlit/instrumentation/vllm/vllm.py +3 -3
  106. openlit/otel/events.py +85 -0
  107. openlit/otel/tracing.py +3 -13
  108. openlit/semcov/__init__.py +13 -1
  109. {openlit-1.33.10.dist-info → openlit-1.33.12.dist-info}/METADATA +2 -2
  110. openlit-1.33.12.dist-info/RECORD +126 -0
  111. openlit-1.33.10.dist-info/RECORD +0 -122
  112. {openlit-1.33.10.dist-info → openlit-1.33.12.dist-info}/LICENSE +0 -0
  113. {openlit-1.33.10.dist-info → openlit-1.33.12.dist-info}/WHEEL +0 -0
@@ -1,98 +1,163 @@
1
- # pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument
2
1
  """
3
- Module for monitoring AG2.
2
+ Module for monitoring AG2 API calls.
4
3
  """
5
4
 
6
5
  import logging
6
+ import time
7
7
  from opentelemetry.trace import SpanKind, Status, StatusCode
8
8
  from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
9
- from openlit.__helpers import handle_exception
9
+ from openlit.__helpers import (
10
+ handle_exception,
11
+ get_chat_model_cost,
12
+ otel_event,
13
+ )
10
14
  from openlit.semcov import SemanticConvetion
11
15
 
12
16
  # Initialize logger for logging potential issues and operations
13
17
  logger = logging.getLogger(__name__)
14
18
 
15
- def wrap_ag2(gen_ai_endpoint, version, environment, application_name,
16
- tracer, pricing_info, trace_content, metrics, disable_metrics):
19
+ AGENT_NAME = ''
20
+ REQUEST_MODEL = ''
21
+ SYSTEM_MESSAGE = ''
22
+ MODEL_AND_NAME_SET = False
23
+
24
+ def set_span_attributes(span, version, operation_name, environment,
25
+ application_name, server_address, server_port, request_model):
26
+ """
27
+ Set common attributes for the span.
28
+ """
29
+
30
+ # Set Span attributes (OTel Semconv)
31
+ span.set_attribute(TELEMETRY_SDK_NAME, 'openlit')
32
+ span.set_attribute(SemanticConvetion.GEN_AI_OPERATION, operation_name)
33
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM, SemanticConvetion.GEN_AI_SYSTEM_AG2)
34
+ span.set_attribute(SemanticConvetion.GEN_AI_AGENT_NAME, AGENT_NAME)
35
+ span.set_attribute(SemanticConvetion.SERVER_ADDRESS, server_address)
36
+ span.set_attribute(SemanticConvetion.SERVER_PORT, server_port)
37
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL, request_model)
38
+
39
+ # Set Span attributes (Extras)
40
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
41
+ span.set_attribute(SERVICE_NAME, application_name)
42
+ span.set_attribute(SemanticConvetion.GEN_AI_SDK_VERSION, version)
43
+
44
+ def calculate_tokens_and_cost(response, request_model, pricing_info):
17
45
  """
18
- Creates a wrapper around a function call to trace and log its execution metrics.
19
-
20
- This function wraps any given function to measure its execution time,
21
- log its operation, and trace its execution using OpenTelemetry.
22
-
23
- Parameters:
24
- - gen_ai_endpoint (str): A descriptor or name for the endpoint being traced.
25
- - version (str): The version of the Langchain application.
26
- - environment (str): The deployment environment (e.g., 'production', 'development').
27
- - application_name (str): Name of the Langchain application.
28
- - tracer (opentelemetry.trace.Tracer): The tracer object used for OpenTelemetry tracing.
29
- - pricing_info (dict): Information about the pricing for internal metrics (currently not used).
30
- - trace_content (bool): Flag indicating whether to trace the content of the response.
31
-
32
- Returns:
33
- - function: A higher-order function that takes a function 'wrapped' and returns
34
- a new function that wraps 'wrapped' with additional tracing and logging.
46
+ Calculate the input, output tokens, and their respective costs.
35
47
  """
48
+ input_tokens = 0
49
+ output_tokens = 0
36
50
 
51
+ for usage_data in response.cost.values():
52
+ if isinstance(usage_data, dict):
53
+ for model_data in usage_data.values():
54
+ if isinstance(model_data, dict):
55
+ input_tokens += model_data.get('prompt_tokens', 0)
56
+ output_tokens += model_data.get('completion_tokens', 0)
57
+
58
+ cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
59
+ return input_tokens, output_tokens, cost
60
+
61
+ def emit_events(response, event_provider, capture_message_content):
62
+ """
63
+ Emit OpenTelemetry events for each chat history entry.
64
+ """
65
+ for chat in response.chat_history:
66
+ event_type = (
67
+ SemanticConvetion.GEN_AI_CHOICE if chat['role'] == 'user'
68
+ else SemanticConvetion.GEN_AI_USER_MESSAGE
69
+ )
70
+ choice_event = otel_event(
71
+ name=event_type,
72
+ attributes={
73
+ SemanticConvetion.GEN_AI_SYSTEM: SemanticConvetion.GEN_AI_SYSTEM_AG2
74
+ },
75
+ body={
76
+ 'index': response.chat_history.index(chat),
77
+ 'message': {
78
+ **({'content': chat['content']} if capture_message_content else {}),
79
+ 'role': 'assistant' if chat['role'] == 'user' else 'user'
80
+ }
81
+ }
82
+ )
83
+ event_provider.emit(choice_event)
84
+
85
+ def conversable_agent(version, environment, application_name,
86
+ tracer, event_provider, pricing_info, capture_message_content, metrics, disable_metrics):
87
+ """
88
+ Generates a telemetry wrapper for GenAI function call
89
+ """
37
90
  def wrapper(wrapped, instance, args, kwargs):
38
- """
39
- An inner wrapper function that executes the wrapped function, measures execution
40
- time, and records trace data using OpenTelemetry.
41
-
42
- Parameters:
43
- - wrapped (Callable): The original function that this wrapper will execute.
44
- - instance (object): The instance to which the wrapped function belongs. This
45
- is used for instance methods. For static and classmethods,
46
- this may be None.
47
- - args (tuple): Positional arguments passed to the wrapped function.
48
- - kwargs (dict): Keyword arguments passed to the wrapped function.
49
-
50
- Returns:
51
- - The result of the wrapped function call.
52
-
53
- The wrapper initiates a span with the provided tracer, sets various attributes
54
- on the span based on the function's execution and response, and ensures
55
- errors are handled and logged appropriately.
56
- """
57
-
58
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
59
- response = wrapped(*args, **kwargs)
60
-
61
- if isinstance(instance.__dict__.get('llm_config'), dict):
62
- llm_model = instance.__dict__['llm_config'].get('model', 'gpt-4')
63
- else:
64
- # Fallback to default if 'llm_config' is not a dictionary
65
- llm_model = None
91
+ server_address, server_port = '127.0.0.1', 80
92
+ global AGENT_NAME, MODEL_AND_NAME_SET, REQUEST_MODEL, SYSTEM_MESSAGE
93
+
94
+ if not MODEL_AND_NAME_SET:
95
+ AGENT_NAME = kwargs.get('name', 'NOT_FOUND')
96
+ REQUEST_MODEL = kwargs.get('llm_config', {}).get('model', 'gpt-4o')
97
+ SYSTEM_MESSAGE = kwargs.get('system_message', '')
98
+ MODEL_AND_NAME_SET = True
66
99
 
100
+ span_name = f'{SemanticConvetion.GEN_AI_OPERATION_TYPE_CREATE_AGENT} {AGENT_NAME}'
101
+
102
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
67
103
  try:
68
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
69
- span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
70
- gen_ai_endpoint)
71
- span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
72
- SemanticConvetion.GEN_AI_SYSTEM_AG2)
73
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
74
- environment)
75
- span.set_attribute(SERVICE_NAME,
76
- application_name)
77
- span.set_attribute(SemanticConvetion.GEN_AI_OPERATION,
78
- SemanticConvetion.GEN_AI_OPERATION_TYPE_AGENT)
79
- span.set_attribute(SemanticConvetion.GEN_AI_AGENT_ROLE,
80
- instance.name)
81
- if llm_model:
82
- span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
83
- llm_model)
104
+ start_time = time.time()
105
+ response = wrapped(*args, **kwargs)
106
+ end_time = time.time()
84
107
 
108
+ set_span_attributes(span, version, SemanticConvetion.GEN_AI_OPERATION_TYPE_CREATE_AGENT,
109
+ environment, application_name, server_address, server_port, REQUEST_MODEL)
110
+ span.set_attribute(SemanticConvetion.GEN_AI_AGENT_DESCRIPTION, SYSTEM_MESSAGE)
111
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_MODEL, REQUEST_MODEL)
112
+ span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TTFT, end_time - start_time)
85
113
 
86
114
  span.set_status(Status(StatusCode.OK))
87
115
 
88
- # Return original response
89
116
  return response
90
117
 
91
118
  except Exception as e:
92
119
  handle_exception(span, e)
93
- logger.error("Error in trace creation: %s", e)
120
+ logger.error('Error in trace creation: %s', e)
121
+ return response
94
122
 
95
- # Return original response
123
+ return wrapper
124
+
125
+ def agent_run(version, environment, application_name,
126
+ tracer, event_provider, pricing_info, capture_message_content, metrics, disable_metrics):
127
+ """
128
+ Generates a telemetry wrapper for GenAI function call
129
+ """
130
+ def wrapper(wrapped, instance, args, kwargs):
131
+ server_address, server_port = '127.0.0.1', 80
132
+
133
+ span_name = f'{SemanticConvetion.GEN_AI_OPERATION_TYPE_EXECUTE_AGENT_TASK} {AGENT_NAME}'
134
+
135
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
136
+ try:
137
+ start_time = time.time()
138
+ response = wrapped(*args, **kwargs)
139
+ end_time = time.time()
140
+
141
+ input_tokens, output_tokens, cost = calculate_tokens_and_cost(response, REQUEST_MODEL, pricing_info)
142
+ response_model = list(response.cost.get('usage_including_cached_inference', {}).keys())[1]
143
+
144
+ set_span_attributes(span, version, SemanticConvetion.GEN_AI_OPERATION_TYPE_EXECUTE_AGENT_TASK,
145
+ environment, application_name, server_address, server_port, REQUEST_MODEL)
146
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_MODEL, response_model)
147
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
148
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)
149
+ span.set_attribute(SemanticConvetion.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens)
150
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST, cost)
151
+ span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TTFT, end_time - start_time)
152
+
153
+ emit_events(response, event_provider, capture_message_content)
154
+ span.set_status(Status(StatusCode.OK))
155
+
156
+ return response
157
+
158
+ except Exception as e:
159
+ handle_exception(span, e)
160
+ logger.error('Error in trace creation: %s', e)
96
161
  return response
97
162
 
98
163
  return wrapper
@@ -13,7 +13,7 @@ from openlit.instrumentation.ai21.async_ai21 import (
13
13
  async_chat, async_chat_rag
14
14
  )
15
15
 
16
- _instruments = ("ai21 >= 3.0.0",)
16
+ _instruments = ('ai21 >= 3.0.0',)
17
17
 
18
18
  class AI21Instrumentor(BaseInstrumentor):
19
19
  """
@@ -24,41 +24,42 @@ class AI21Instrumentor(BaseInstrumentor):
24
24
  return _instruments
25
25
 
26
26
  def _instrument(self, **kwargs):
27
- application_name = kwargs.get("application_name", "default_application")
28
- environment = kwargs.get("environment", "default_environment")
29
- tracer = kwargs.get("tracer")
30
- metrics = kwargs.get("metrics_dict")
31
- pricing_info = kwargs.get("pricing_info", {})
32
- trace_content = kwargs.get("trace_content", False)
33
- disable_metrics = kwargs.get("disable_metrics")
34
- version = importlib.metadata.version("ai21")
27
+ application_name = kwargs.get('application_name', 'default')
28
+ environment = kwargs.get('environment', 'default')
29
+ tracer = kwargs.get('tracer')
30
+ event_provider = kwargs.get('event_provider')
31
+ metrics = kwargs.get('metrics_dict')
32
+ pricing_info = kwargs.get('pricing_info', {})
33
+ capture_message_content = kwargs.get('capture_message_content', False)
34
+ disable_metrics = kwargs.get('disable_metrics')
35
+ version = importlib.metadata.version('ai21')
35
36
 
36
37
  #sync
37
38
  wrap_function_wrapper(
38
- "ai21.clients.studio.resources.chat.chat_completions",
39
- "ChatCompletions.create",
39
+ 'ai21.clients.studio.resources.chat.chat_completions',
40
+ 'ChatCompletions.create',
40
41
  chat(version, environment, application_name,
41
- tracer, pricing_info, trace_content, metrics, disable_metrics),
42
+ tracer, event_provider, pricing_info, capture_message_content, metrics, disable_metrics),
42
43
  )
43
44
  wrap_function_wrapper(
44
- "ai21.clients.studio.resources.studio_conversational_rag",
45
- "StudioConversationalRag.create",
45
+ 'ai21.clients.studio.resources.studio_conversational_rag',
46
+ 'StudioConversationalRag.create',
46
47
  chat_rag(version, environment, application_name,
47
- tracer, pricing_info, trace_content, metrics, disable_metrics),
48
+ tracer, event_provider, pricing_info, capture_message_content, metrics, disable_metrics),
48
49
  )
49
50
 
50
51
  #Async
51
52
  wrap_function_wrapper(
52
- "ai21.clients.studio.resources.chat.async_chat_completions",
53
- "AsyncChatCompletions.create",
53
+ 'ai21.clients.studio.resources.chat.async_chat_completions',
54
+ 'AsyncChatCompletions.create',
54
55
  async_chat(version, environment, application_name,
55
- tracer, pricing_info, trace_content, metrics, disable_metrics),
56
+ tracer, event_provider, pricing_info, capture_message_content, metrics, disable_metrics),
56
57
  )
57
58
  wrap_function_wrapper(
58
- "ai21.clients.studio.resources.studio_conversational_rag",
59
- "AsyncStudioConversationalRag.create",
59
+ 'ai21.clients.studio.resources.studio_conversational_rag',
60
+ 'AsyncStudioConversationalRag.create',
60
61
  async_chat_rag(version, environment, application_name,
61
- tracer, pricing_info, trace_content, metrics, disable_metrics),
62
+ tracer, event_provider, pricing_info, capture_message_content, metrics, disable_metrics),
62
63
  )
63
64
 
64
65
  def _uninstrument(self, **kwargs):