Skip to content

Python SDK for AI agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks including CrewAI, Agno, OpenAI Agents SDK, Langchain, Autogen, AG2, and CamelAI

added section for handling 1.0 requests

reibsfcbba953522d
diff --git a/agentops/llm_tracker.py b/agentops/llm_tracker.py
index ad3aa0ab..8b73b823 100644
--- a/agentops/llm_tracker.py
+++ b/agentops/llm_tracker.py
@@ -24,6 +24,8 @@ class LlmTracker:
def __init__(self, client):
self.client = client
self.event_stream = None
+ self.override_openai_completion()
+ self.override_openai_async_completion()
def _handle_response_openai(self, response, kwargs, init_timestamp):
def handle_stream_chunk(chunk):
@@ -75,6 +77,74 @@ class LlmTracker:
yield chunk
return generator()
+ else:
+ try:
+ self.client.record(Event(
+ event_type=response.object,
+ params=kwargs,
+ result='Success',
+ returns={"content": response.choices[0].message},
+ action_type='llm',
+ model=response.model,
+ prompt=kwargs['messages'],
+ init_timestamp=init_timestamp
+ ))
+ except:
+ print(
+ f"Unable to parse response for LLM call {kwargs} - skipping upload to AgentOps")
+
+ return response
+
+ def _handle_async_response_openai(self, response, kwargs, init_timestamp):
+ def handle_stream_chunk(chunk):
+ try:
+ model = chunk['model']
+ choices = chunk['choices']
+ token = choices[0]['delta'].get('content', '')
+ finish_reason = choices[0]['finish_reason']
+
+ if self.event_stream == None:
+ self.event_stream = Event(
+ event_type='openai stream',
+ params=kwargs,
+ result='Success',
+ returns={"finish_reason": None,
+ "content": token},
+ action_type='llm',
+ model=model,
+ prompt=kwargs["messages"],
+ init_timestamp=init_timestamp
+ )
+ else:
+ self.event_stream.returns['content'] += token
+
+ if finish_reason:
+ self.event_stream.returns['finish_reason'] = finish_reason
+ # Update end_timestamp
+ self.event_stream.end_timestamp = get_ISO_time()
+ self.client.record(self.event_stream)
+ self.event_stream = None
+ except:
+ print(
+ f"Unable to parse a chunk for LLM call {kwargs} - skipping upload to AgentOps")
+
+ # if the response is a generator, decorate the generator
+ if inspect.isasyncgen(response):
+ async def generator():
+ async for chunk in response:
+ handle_stream_chunk(chunk)
+
+ yield chunk
+ return generator()
+
+ if inspect.isgenerator(response):
+ def generator():
+ for chunk in response:
+ handle_stream_chunk(chunk)
+
+ yield chunk
+ return generator()
+
else:
try:
self.client.record(Event(
@@ -148,3 +218,44 @@ class LlmTracker:
else:
for method_path in self.SUPPORTED_APIS[api]['0.0.0']:
self._override_method(api, method_path, module)
+
+ def override_openai_completion(self):
+ from openai.resources.chat import completions
+
+ # Store the original method
+ original_create = completions.Completions.create
+
+ # Define the patched function
+ def patched_function(*args, **kwargs):
+ print('patched function')
+ print(args)
+ print(kwargs)
+ # Call the original function with its original arguments
+ result = original_create(*args, **kwargs)
+ self._handle_response_openai(result, kwargs, get_ISO_time())
+
+ # You can add additional logic here if needed
+ return result
+
+ # Override the original method with the patched one
+ completions.Completions.create = patched_function
+
+ def override_openai_async_completion(self):
+ from openai.resources.chat import completions
+
+ # Store the original method
+ original_create = completions.AsyncCompletions.create
+ # Define the patched function
+
+ def patched_function(*args, **kwargs):
+ print('patched async function')
+ # Call the original function with its original arguments
+ result = original_create(*args, **kwargs)
+ self._handle_response_openai(result, kwargs, get_ISO_time())
+
+ # You can add additional logic here if needed
+ return result
+
+ # Override the original method with the patched one
+ completions.AsyncCompletions.create = patched_function
+ print('patched async')