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

working on old again

Shawn Qiua18844d55dff
diff --git a/agentops/client.py b/agentops/client.py
index a843a9ae..2e625f7c 100644
--- a/agentops/client.py
+++ b/agentops/client.py
@@ -67,9 +67,8 @@ class Client:
self.start_session(tags)
- if 'openai' in sys.modules:
- self.llm_tracker = LlmTracker(self)
- self.llm_tracker.override_api('openai')
+ self.llm_tracker = LlmTracker(self)
+ self.llm_tracker.override_api('openai')
def handle_exception(self, exc_type, exc_value, exc_traceback):
"""
diff --git a/agentops/helpers.py b/agentops/helpers.py
index 132da3b3..b4359b93 100644
--- a/agentops/helpers.py
+++ b/agentops/helpers.py
@@ -7,6 +7,28 @@ def get_ISO_time():
return datetime.utcfromtimestamp(time.time()).isoformat(timespec='milliseconds') + 'Z'
+def compare_versions(version1, version2):
+ # Split the version strings into lists of integers
+ v1_components = list(map(int, version1.split(".")))
+ v2_components = list(map(int, version2.split(".")))
+
+ # Compare each component
+ for v1, v2 in zip(v1_components, v2_components):
+ if v1 < v2:
+ return -1
+ elif v1 > v2:
+ return 1
+
+ # If all components are equal, check the length of the version strings
+ if len(v1_components) < len(v2_components):
+ return -1
+ elif len(v1_components) > len(v2_components):
+ return 1
+
+ # Versions are equal
+ return 0
+
+
class Models(Enum):
GPT_3_5_TURBO = "gpt-3.5-turbo"
GPT_3_5_TURBO_0301 = "gpt-3.5-turbo-0301"
diff --git a/agentops/llm_tracker.py b/agentops/llm_tracker.py
index 834c07d8..bf47dea1 100644
--- a/agentops/llm_tracker.py
+++ b/agentops/llm_tracker.py
@@ -1,16 +1,23 @@
import functools
import inspect
+import sys
from importlib import import_module
from .event import Event
-from .helpers import get_ISO_time
+from .helpers import get_ISO_time, compare_versions
class LlmTracker:
SUPPORTED_APIS = {
- 'openai': (
- "ChatCompletion.create",
- "ChatCompletion.acreate",
- )
+ 'openai': {
+ '1.0.0': (
+ "chat.completions.create",
+ ),
+ '0.0.0':
+ (
+ "ChatCompletion.create",
+ "ChatCompletion.acreate",
+ ),
+ }
}
def __init__(self, client):
@@ -113,18 +120,35 @@ class LlmTracker:
"""
Overrides key methods of the specified API to record events.
"""
- if api not in self.SUPPORTED_APIS:
- raise ValueError(f"Unsupported API: {api}")
-
- module = import_module(api)
-
- for method_path in self.SUPPORTED_APIS[api]:
- method_parts = method_path.split(".")
- original_method = functools.reduce(getattr, method_parts, module)
- new_method = self._override_method(api, original_method)
-
- if len(method_parts) == 1:
- setattr(module, method_parts[0], new_method)
+ if api in sys.modules:
+ if api not in self.SUPPORTED_APIS:
+ raise ValueError(f"Unsupported API: {api}")
+
+
+ module = import_module(api)
+
+ if hasattr(module, '__version__'):
+ for version in self.SUPPORTED_APIS[api]:
+ if compare_versions(module.__version__, version) >= 0:
+ for method_path in self.SUPPORTED_APIS[api][version]:
+ method_parts = method_path.split(".")
+ original_method = functools.reduce(getattr, method_parts, module)
+ new_method = self._override_method(api, original_method)
+
+ if len(method_parts) == 1:
+ setattr(module, method_parts[0], new_method)
+ else:
+ parent = functools.reduce(getattr, method_parts[:-1], module)
+ setattr(parent, method_parts[-1], new_method)
+ break
else:
- parent = functools.reduce(getattr, method_parts[:-1], module)
- setattr(parent, method_parts[-1], new_method)
+ for method_path in self.SUPPORTED_APIS[api]['0.0.0']:
+ method_parts = method_path.split(".")
+ original_method = functools.reduce(getattr, method_parts, module)
+ new_method = self._override_method(api, original_method)
+
+ if len(method_parts) == 1:
+ setattr(module, method_parts[0], new_method)
+ else:
+ parent = functools.reduce(getattr, method_parts[:-1], module)
+ setattr(parent, method_parts[-1], new_method)