openlit 1.3.0__tar.gz → 1.5.0__tar.gz

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 (44) hide show
  1. {openlit-1.3.0 → openlit-1.5.0}/PKG-INFO +3 -1
  2. {openlit-1.3.0 → openlit-1.5.0}/README.md +2 -0
  3. {openlit-1.3.0 → openlit-1.5.0}/pyproject.toml +1 -1
  4. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/__init__.py +6 -0
  5. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/chroma/chroma.py +1 -1
  6. openlit-1.5.0/src/openlit/instrumentation/groq/__init__.py +50 -0
  7. openlit-1.5.0/src/openlit/instrumentation/groq/async_groq.py +331 -0
  8. openlit-1.5.0/src/openlit/instrumentation/groq/groq.py +331 -0
  9. openlit-1.5.0/src/openlit/instrumentation/qdrant/__init__.py +155 -0
  10. openlit-1.5.0/src/openlit/instrumentation/qdrant/qdrant.py +258 -0
  11. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/otel/tracing.py +3 -0
  12. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/semcov/__init__.py +11 -3
  13. {openlit-1.3.0 → openlit-1.5.0}/LICENSE +0 -0
  14. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/__helpers.py +0 -0
  15. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/anthropic/__init__.py +0 -0
  16. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/anthropic/anthropic.py +0 -0
  17. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/anthropic/async_anthropic.py +0 -0
  18. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/bedrock/__init__.py +0 -0
  19. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/bedrock/bedrock.py +0 -0
  20. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/chroma/__init__.py +0 -0
  21. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/cohere/__init__.py +0 -0
  22. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/cohere/cohere.py +0 -0
  23. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/haystack/__init__.py +0 -0
  24. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/haystack/haystack.py +0 -0
  25. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/langchain/__init__.py +0 -0
  26. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/langchain/langchain.py +0 -0
  27. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/llamaindex/__init__.py +0 -0
  28. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/llamaindex/llamaindex.py +0 -0
  29. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/mistral/__init__.py +0 -0
  30. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/mistral/async_mistral.py +0 -0
  31. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/mistral/mistral.py +0 -0
  32. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/openai/__init__.py +0 -0
  33. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/openai/async_azure_openai.py +0 -0
  34. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/openai/async_openai.py +0 -0
  35. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/openai/azure_openai.py +0 -0
  36. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/openai/openai.py +0 -0
  37. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/pinecone/__init__.py +0 -0
  38. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/pinecone/pinecone.py +0 -0
  39. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/transformers/__init__.py +0 -0
  40. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/transformers/transformers.py +0 -0
  41. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/vertexai/__init__.py +0 -0
  42. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/vertexai/async_vertexai.py +0 -0
  43. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/instrumentation/vertexai/vertexai.py +0 -0
  44. {openlit-1.3.0 → openlit-1.5.0}/src/openlit/otel/metrics.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: openlit
3
- Version: 1.3.0
3
+ Version: 1.5.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
@@ -57,10 +57,12 @@ This project adheres to the [Semantic Conventions](https://github.com/open-telem
57
57
  - [✅ HuggingFace Transformers](https://docs.openlit.io/latest/integrations/huggingface)
58
58
  - [✅ Amazon Bedrock](https://docs.openlit.io/latest/integrations/bedrock)
59
59
  - [✅ Vertex AI](https://docs.openlit.io/latest/integrations/vertexai)
60
+ - [✅ Groq](https://docs.openlit.io/latest/integrations/groq)
60
61
 
61
62
  ### Vector DBs
62
63
  - [✅ ChromaDB](https://docs.openlit.io/latest/integrations/chromadb)
63
64
  - [✅ Pinecone](https://docs.openlit.io/latest/integrations/pinecone)
65
+ - [✅ Qdrant](https://docs.openlit.io/latest/integrations/qdrant)
64
66
 
65
67
  ### Frameworks
66
68
  - [✅ Langchain](https://docs.openlit.io/latest/integrations/langchain)
@@ -32,10 +32,12 @@ This project adheres to the [Semantic Conventions](https://github.com/open-telem
32
32
  - [✅ HuggingFace Transformers](https://docs.openlit.io/latest/integrations/huggingface)
33
33
  - [✅ Amazon Bedrock](https://docs.openlit.io/latest/integrations/bedrock)
34
34
  - [✅ Vertex AI](https://docs.openlit.io/latest/integrations/vertexai)
35
+ - [✅ Groq](https://docs.openlit.io/latest/integrations/groq)
35
36
 
36
37
  ### Vector DBs
37
38
  - [✅ ChromaDB](https://docs.openlit.io/latest/integrations/chromadb)
38
39
  - [✅ Pinecone](https://docs.openlit.io/latest/integrations/pinecone)
40
+ - [✅ Qdrant](https://docs.openlit.io/latest/integrations/qdrant)
39
41
 
40
42
  ### Frameworks
41
43
  - [✅ Langchain](https://docs.openlit.io/latest/integrations/langchain)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "openlit"
3
- version = "1.3.0"
3
+ version = "1.5.0"
4
4
  description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications, facilitating the integration of observability into your GenAI-driven projects"
5
5
  authors = ["OpenLIT"]
6
6
  repository = "https://github.com/openlit/openlit/tree/main/openlit/python"
@@ -19,11 +19,13 @@ from openlit.instrumentation.cohere import CohereInstrumentor
19
19
  from openlit.instrumentation.mistral import MistralInstrumentor
20
20
  from openlit.instrumentation.bedrock import BedrockInstrumentor
21
21
  from openlit.instrumentation.vertexai import VertexAIInstrumentor
22
+ from openlit.instrumentation.groq import GroqInstrumentor
22
23
  from openlit.instrumentation.langchain import LangChainInstrumentor
23
24
  from openlit.instrumentation.llamaindex import LlamaIndexInstrumentor
24
25
  from openlit.instrumentation.haystack import HaystackInstrumentor
25
26
  from openlit.instrumentation.chroma import ChromaInstrumentor
26
27
  from openlit.instrumentation.pinecone import PineconeInstrumentor
28
+ from openlit.instrumentation.qdrant import QdrantInstrumentor
27
29
  from openlit.instrumentation.transformers import TransformersInstrumentor
28
30
 
29
31
  # Set up logging for error and information messages.
@@ -151,11 +153,13 @@ def init(environment="default", application_name="default", tracer=None, otlp_en
151
153
  "mistral": "mistralai",
152
154
  "bedrock": "boto3",
153
155
  "vertexai": "vertexai",
156
+ "groq": "groq",
154
157
  "langchain": "langchain",
155
158
  "llama_index": "llama_index",
156
159
  "haystack": "haystack",
157
160
  "chroma": "chromadb",
158
161
  "pinecone": "pinecone",
162
+ "qdrant": "qdrant_client",
159
163
  "transformers": "transformers"
160
164
  }
161
165
 
@@ -201,11 +205,13 @@ def init(environment="default", application_name="default", tracer=None, otlp_en
201
205
  "mistral": MistralInstrumentor(),
202
206
  "bedrock": BedrockInstrumentor(),
203
207
  "vertexai": VertexAIInstrumentor(),
208
+ "groq": GroqInstrumentor(),
204
209
  "langchain": LangChainInstrumentor(),
205
210
  "llama_index": LlamaIndexInstrumentor(),
206
211
  "haystack": HaystackInstrumentor(),
207
212
  "chroma": ChromaInstrumentor(),
208
213
  "pinecone": PineconeInstrumentor(),
214
+ "qdrant": QdrantInstrumentor(),
209
215
  "transformers": TransformersInstrumentor()
210
216
  }
211
217
 
@@ -85,7 +85,7 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,
85
85
  if gen_ai_endpoint == "chroma.add":
86
86
  db_operation = SemanticConvetion.DB_OPERATION_ADD
87
87
  span.set_attribute(SemanticConvetion.DB_OPERATION,
88
- SemanticConvetion.DB_OPERATION_GET)
88
+ SemanticConvetion.DB_OPERATION_ADD)
89
89
  span.set_attribute(SemanticConvetion.DB_ID_COUNT,
90
90
  object_count(kwargs.get("ids")))
91
91
  span.set_attribute(SemanticConvetion.DB_VECTOR_COUNT,
@@ -0,0 +1,50 @@
1
+ # pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
2
+ """Initializer of Auto Instrumentation of Groq Functions"""
3
+
4
+ from typing import Collection
5
+ import importlib.metadata
6
+ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
7
+ from wrapt import wrap_function_wrapper
8
+
9
+ from openlit.instrumentation.groq.groq import chat
10
+ from openlit.instrumentation.groq.async_groq import async_chat
11
+
12
+ _instruments = ("groq >= 0.5.0",)
13
+
14
+ class GroqInstrumentor(BaseInstrumentor):
15
+ """
16
+ An instrumentor for Groq's client library.
17
+ """
18
+
19
+ def instrumentation_dependencies(self) -> Collection[str]:
20
+ return _instruments
21
+
22
+ def _instrument(self, **kwargs):
23
+ application_name = kwargs.get("application_name", "default_application")
24
+ environment = kwargs.get("environment", "default_environment")
25
+ tracer = kwargs.get("tracer")
26
+ metrics = kwargs.get("metrics_dict")
27
+ pricing_info = kwargs.get("pricing_info", {})
28
+ trace_content = kwargs.get("trace_content", False)
29
+ disable_metrics = kwargs.get("disable_metrics")
30
+ version = importlib.metadata.version("groq")
31
+
32
+ #sync
33
+ wrap_function_wrapper(
34
+ "groq.resources.chat.completions",
35
+ "Completions.create",
36
+ chat("groq.chat.completions", version, environment, application_name,
37
+ tracer, pricing_info, trace_content, metrics, disable_metrics),
38
+ )
39
+
40
+ #async
41
+ wrap_function_wrapper(
42
+ "groq.resources.chat.completions",
43
+ "AsyncCompletions.create",
44
+ async_chat("groq.chat.completions", version, environment, application_name,
45
+ tracer, pricing_info, trace_content, metrics, disable_metrics),
46
+ )
47
+
48
+ def _uninstrument(self, **kwargs):
49
+ # Proper uninstrumentation logic to revert patched methods
50
+ pass
@@ -0,0 +1,331 @@
1
+ # pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, used-before-assignment, too-many-branches
2
+ """
3
+ Module for monitoring Groq 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 get_chat_model_cost, handle_exception
10
+ from openlit.semcov import SemanticConvetion
11
+
12
+ # Initialize logger for logging potential issues and operations
13
+ logger = logging.getLogger(__name__)
14
+
15
+ def async_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 completions 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 Groq API.
25
+ tracer: OpenTelemetry tracer for creating spans.
26
+ pricing_info: Information used for calculating the cost of Groq usage.
27
+ trace_content: Flag indicating whether to trace the actual content.
28
+
29
+ Returns:
30
+ A function that wraps the chat completions method to add telemetry.
31
+ """
32
+
33
+ async def wrapper(wrapped, instance, args, kwargs):
34
+ """
35
+ Wraps the 'chat.completions' 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.completions' method to be wrapped.
42
+ instance: The instance of the class where the original method is defined.
43
+ args: Positional arguments for the 'chat.completions' method.
44
+ kwargs: Keyword arguments for the 'chat.completions' method.
45
+
46
+ Returns:
47
+ The response from the original 'chat.completions' 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
+ async 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
+ async for chunk in await wrapped(*args, **kwargs):
63
+ # Collect message IDs and aggregated response from events
64
+ if len(chunk.choices) > 0:
65
+ # pylint: disable=line-too-long
66
+ if hasattr(chunk.choices[0], "delta") and hasattr(chunk.choices[0].delta, "content"):
67
+ content = chunk.choices[0].delta.content
68
+ if content:
69
+ llmresponse += content
70
+ if chunk.x_groq is not None and chunk.x_groq.usage is not None:
71
+ prompt_tokens = chunk.x_groq.usage.prompt_tokens
72
+ completion_tokens = chunk.x_groq.usage.completion_tokens
73
+ total_tokens = chunk.x_groq.usage.total_tokens
74
+ response_id = chunk.x_groq.id
75
+ yield chunk
76
+
77
+ # Handling exception ensure observability without disrupting operation
78
+ try:
79
+ # Format 'messages' into a single string
80
+ message_prompt = kwargs.get("messages", "")
81
+ formatted_messages = []
82
+ for message in message_prompt:
83
+ role = message["role"]
84
+ content = message["content"]
85
+
86
+ if isinstance(content, list):
87
+ content_str = ", ".join(
88
+ # pylint: disable=line-too-long
89
+ f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
90
+ if "type" in item else f'text: {item["text"]}'
91
+ for item in content
92
+ )
93
+ formatted_messages.append(f"{role}: {content_str}")
94
+ else:
95
+ formatted_messages.append(f"{role}: {content}")
96
+ prompt = "\n".join(formatted_messages)
97
+
98
+ # Calculate cost of the operation
99
+ cost = get_chat_model_cost(kwargs.get("model", "gpt-3.5-turbo"),
100
+ pricing_info, prompt_tokens,
101
+ completion_tokens)
102
+
103
+ # Set Span attributes
104
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
105
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
106
+ SemanticConvetion.GEN_AI_SYSTEM_GROQ)
107
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
108
+ SemanticConvetion.GEN_AI_TYPE_CHAT)
109
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
110
+ gen_ai_endpoint)
111
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
112
+ response_id)
113
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
114
+ environment)
115
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
116
+ application_name)
117
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
118
+ kwargs.get("model", "gpt-3.5-turbo"))
119
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
120
+ kwargs.get("user", ""))
121
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
122
+ kwargs.get("top_p", 1))
123
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
124
+ kwargs.get("max_tokens", ""))
125
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
126
+ kwargs.get("temperature", 1))
127
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_PRESENCE_PENALTY,
128
+ kwargs.get("presence_penalty", 0))
129
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FREQUENCY_PENALTY,
130
+ kwargs.get("frequency_penalty", 0))
131
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
132
+ kwargs.get("seed", ""))
133
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
134
+ True)
135
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
136
+ prompt_tokens)
137
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
138
+ completion_tokens)
139
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
140
+ prompt_tokens + completion_tokens)
141
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
142
+ cost)
143
+ if trace_content:
144
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
145
+ prompt)
146
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,
147
+ llmresponse)
148
+
149
+ span.set_status(Status(StatusCode.OK))
150
+
151
+ if disable_metrics is False:
152
+ attributes = {
153
+ TELEMETRY_SDK_NAME:
154
+ "openlit",
155
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
156
+ application_name,
157
+ SemanticConvetion.GEN_AI_SYSTEM:
158
+ SemanticConvetion.GEN_AI_SYSTEM_GROQ,
159
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
160
+ environment,
161
+ SemanticConvetion.GEN_AI_TYPE:
162
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
163
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
164
+ kwargs.get("model", "gpt-3.5-turbo")
165
+ }
166
+
167
+ metrics["genai_requests"].add(1, attributes)
168
+ metrics["genai_total_tokens"].add(
169
+ total_tokens, attributes
170
+ )
171
+ metrics["genai_completion_tokens"].add(completion_tokens, attributes)
172
+ metrics["genai_prompt_tokens"].add(prompt_tokens, attributes)
173
+ metrics["genai_cost"].record(cost, attributes)
174
+
175
+ except Exception as e:
176
+ handle_exception(span, e)
177
+ logger.error("Error in trace creation: %s", e)
178
+
179
+ return stream_generator()
180
+
181
+ # Handling for non-streaming responses
182
+ else:
183
+ # pylint: disable=line-too-long
184
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
185
+ response = await wrapped(*args, **kwargs)
186
+
187
+ try:
188
+ # Format 'messages' into a single string
189
+ message_prompt = kwargs.get("messages", "")
190
+ formatted_messages = []
191
+ for message in message_prompt:
192
+ role = message["role"]
193
+ content = message["content"]
194
+
195
+ if isinstance(content, list):
196
+ content_str = ", ".join(
197
+ # pylint: disable=line-too-long
198
+ f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
199
+ if "type" in item else f'text: {item["text"]}'
200
+ for item in content
201
+ )
202
+ formatted_messages.append(f"{role}: {content_str}")
203
+ else:
204
+ formatted_messages.append(f"{role}: {content}")
205
+ prompt = "\n".join(formatted_messages)
206
+
207
+ # Set base span attribues
208
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
209
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
210
+ SemanticConvetion.GEN_AI_SYSTEM_GROQ)
211
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
212
+ SemanticConvetion.GEN_AI_TYPE_CHAT)
213
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
214
+ gen_ai_endpoint)
215
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
216
+ response.x_groq["id"])
217
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
218
+ environment)
219
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
220
+ application_name)
221
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
222
+ kwargs.get("model", "llama3-8b-8192"))
223
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
224
+ kwargs.get("top_p", 1))
225
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
226
+ kwargs.get("max_tokens", ""))
227
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
228
+ kwargs.get("name", ""))
229
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
230
+ kwargs.get("temperature", 1))
231
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_PRESENCE_PENALTY,
232
+ kwargs.get("presence_penalty", 0))
233
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FREQUENCY_PENALTY,
234
+ kwargs.get("frequency_penalty", 0))
235
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
236
+ kwargs.get("seed", ""))
237
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
238
+ False)
239
+ if trace_content:
240
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
241
+ prompt)
242
+
243
+ # Set span attributes when tools is not passed to the function call
244
+ if "tools" not in kwargs:
245
+ # Calculate cost of the operation
246
+ cost = get_chat_model_cost(kwargs.get("model", "llama3-8b-8192"),
247
+ pricing_info, response.usage.prompt_tokens,
248
+ response.usage.completion_tokens)
249
+
250
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
251
+ response.usage.prompt_tokens)
252
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
253
+ response.usage.completion_tokens)
254
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
255
+ response.usage.total_tokens)
256
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
257
+ response.choices[0].finish_reason)
258
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
259
+ cost)
260
+
261
+ # Set span attributes for when n = 1 (default)
262
+ if "n" not in kwargs or kwargs["n"] == 1:
263
+ if trace_content:
264
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,
265
+ response.choices[0].message.content)
266
+
267
+ # Set span attributes for when n > 0
268
+ else:
269
+ i = 0
270
+ while i < kwargs["n"] and trace_content is True:
271
+ attribute_name = f"gen_ai.content.completion.{i}"
272
+ span.set_attribute(attribute_name,
273
+ response.choices[i].message.content)
274
+ i += 1
275
+
276
+ # Return original response
277
+ return response
278
+
279
+ # Set span attributes when tools is passed to the function call
280
+ elif "tools" in kwargs:
281
+ # Calculate cost of the operation
282
+ cost = get_chat_model_cost(kwargs.get("model", "gpt-3.5-turbo"),
283
+ pricing_info, response.usage.prompt_tokens,
284
+ response.usage.completion_tokens)
285
+
286
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,
287
+ "Function called with tools")
288
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
289
+ response.usage.prompt_tokens)
290
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
291
+ response.usage.completion_tokens)
292
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
293
+ response.usage.total_tokens)
294
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
295
+ cost)
296
+
297
+ span.set_status(Status(StatusCode.OK))
298
+
299
+ if disable_metrics is False:
300
+ attributes = {
301
+ TELEMETRY_SDK_NAME:
302
+ "openlit",
303
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
304
+ application_name,
305
+ SemanticConvetion.GEN_AI_SYSTEM:
306
+ SemanticConvetion.GEN_AI_SYSTEM_GROQ,
307
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
308
+ environment,
309
+ SemanticConvetion.GEN_AI_TYPE:
310
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
311
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
312
+ kwargs.get("model", "gpt-3.5-turbo")
313
+ }
314
+
315
+ metrics["genai_requests"].add(1, attributes)
316
+ metrics["genai_total_tokens"].add(response.usage.total_tokens, attributes)
317
+ metrics["genai_completion_tokens"].add(response.usage.completion_tokens, attributes)
318
+ metrics["genai_prompt_tokens"].add(response.usage.prompt_tokens, attributes)
319
+ metrics["genai_cost"].record(cost, attributes)
320
+
321
+ # Return original response
322
+ return response
323
+
324
+ except Exception as e:
325
+ handle_exception(span, e)
326
+ logger.error("Error in trace creation: %s", e)
327
+
328
+ # Return original response
329
+ return response
330
+
331
+ return wrapper