Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] TaskWeaver Integration #513

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,7 @@
@property
def parent_key(self):
return self._config.parent_key

@property
def current_client(self):
return self if self.is_initialized else None

Check warning on line 436 in agentops/client.py

View check run for this annotation

Codecov / codecov/patch

agentops/client.py#L436

Added line #L436 was not covered by tests
3 changes: 3 additions & 0 deletions agentops/llms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class LlmTracker:
"client.answer.create",
),
},
"taskweaver": {
"0.0.12a0": ("OpenAIService.chat_completion",),
},
}

def __init__(self, client):
Expand Down
119 changes: 119 additions & 0 deletions agentops/llms/taskweaver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from typing import Optional
import pprint
from agentops.llms.base import InstrumentedProvider
from agentops.events import LLMEvent, ErrorEvent
from agentops.session import Session
from agentops.utils import get_ISO_time, check_call_stack_for_agent_id
from agentops.decorators import singleton

Check warning on line 7 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L1-L7

Added lines #L1 - L7 were not covered by tests


@singleton
class TaskWeaverProvider(InstrumentedProvider):
def __init__(self, client):
super().__init__(client)
self._provider_name = "TaskWeaver"
self.original_completion = {}
self.original_completion_async = {}

Check warning on line 16 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L10-L16

Added lines #L10 - L16 were not covered by tests

def handle_response(self, response, kwargs, init_timestamp, session: Optional[Session] = None):
llm_event = LLMEvent(

Check warning on line 19 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L18-L19

Added lines #L18 - L19 were not covered by tests
provider=self._provider_name,
init_timestamp=init_timestamp,
returns=response
)

try:
llm_event.returns = response
llm_event.agent_id = check_call_stack_for_agent_id()
llm_event.model = kwargs.get("model", "unknown")
llm_event.prompt = kwargs.get("messages", [])

Check warning on line 29 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L25-L29

Added lines #L25 - L29 were not covered by tests

if hasattr(response, "choices") and len(response.choices) > 0:
choice = response.choices[0]
llm_event.completion = {

Check warning on line 33 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L32-L33

Added lines #L32 - L33 were not covered by tests
"role": choice.message.role if hasattr(choice.message, "role") else "assistant",
"content": choice.message.content,
"function_call": getattr(choice.message, "function_call", None),
"tool_calls": getattr(choice.message, "tool_calls", None),
}

if hasattr(response, "usage"):
llm_event.prompt_tokens = response.usage.prompt_tokens
llm_event.completion_tokens = response.usage.completion_tokens

Check warning on line 42 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L41-L42

Added lines #L41 - L42 were not covered by tests

llm_event.end_timestamp = get_ISO_time()
self._safe_record(session, llm_event)

Check warning on line 45 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L44-L45

Added lines #L44 - L45 were not covered by tests

except Exception as e:
self._safe_record(session, ErrorEvent(trigger_event=llm_event, exception=e))
kwargs_str = pprint.pformat(kwargs)
response_str = pprint.pformat(response)
print(f"Unable to parse response for LLM call. Skipping upload to AgentOps\n"

Check warning on line 51 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L47-L51

Added lines #L47 - L51 were not covered by tests
f"response:\n {response_str}\n"
f"kwargs:\n {kwargs_str}\n")

return response

Check warning on line 55 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L55

Added line #L55 was not covered by tests

def override(self):
from taskweaver.llm import CompletionService
from taskweaver.llm.openai import OpenAIService
from taskweaver.llm.anthropic import AnthropicService
from taskweaver.llm.azure_ml import AzureMLService
from taskweaver.llm.google_genai import GoogleGenAIService
from taskweaver.llm.groq import GroqService
from taskweaver.llm.ollama import OllamaService
from taskweaver.llm.qwen import QwenService
from taskweaver.llm.zhipuai import ZhipuAIService

Check warning on line 66 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L57-L66

Added lines #L57 - L66 were not covered by tests

services = [

Check warning on line 68 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L68

Added line #L68 was not covered by tests
OpenAIService, AnthropicService, AzureMLService,
GoogleGenAIService, GroqService, OllamaService,
QwenService, ZhipuAIService
]

for service in services:
self._override_service(service)

Check warning on line 75 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L75

Added line #L75 was not covered by tests

def _override_service(self, service_class):
service_name = service_class.__name__

Check warning on line 78 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L77-L78

Added lines #L77 - L78 were not covered by tests

def patched_chat_completion(self, *args, **kwargs):
init_timestamp = get_ISO_time()
session = kwargs.pop("session", None)
result = self.original_completion[service_name](*args, **kwargs)
return self.handle_response(result, kwargs, init_timestamp, session=session)

Check warning on line 84 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L80-L84

Added lines #L80 - L84 were not covered by tests

async def patched_chat_completion_async(self, *args, **kwargs):
init_timestamp = get_ISO_time()
session = kwargs.pop("session", None)
result = await self.original_completion_async[service_name](*args, **kwargs)
return self.handle_response(result, kwargs, init_timestamp, session=session)

Check warning on line 90 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L86-L90

Added lines #L86 - L90 were not covered by tests

self.original_completion[service_name] = service_class.chat_completion
self.original_completion_async[service_name] = service_class.chat_completion_async

Check warning on line 93 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L92-L93

Added lines #L92 - L93 were not covered by tests

service_class.chat_completion = patched_chat_completion
service_class.chat_completion_async = patched_chat_completion_async

Check warning on line 96 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L95-L96

Added lines #L95 - L96 were not covered by tests

def undo_override(self):
from taskweaver.llm import CompletionService
from taskweaver.llm.openai import OpenAIService
from taskweaver.llm.anthropic import AnthropicService
from taskweaver.llm.azure_ml import AzureMLService
from taskweaver.llm.google_genai import GoogleGenAIService
from taskweaver.llm.groq import GroqService
from taskweaver.llm.ollama import OllamaService
from taskweaver.llm.qwen import QwenService
from taskweaver.llm.zhipuai import ZhipuAIService

Check warning on line 107 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L98-L107

Added lines #L98 - L107 were not covered by tests

services = [

Check warning on line 109 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L109

Added line #L109 was not covered by tests
OpenAIService, AnthropicService, AzureMLService,
GoogleGenAIService, GroqService, OllamaService,
QwenService, ZhipuAIService
]

for service in services:
service_name = service.__name__

Check warning on line 116 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L116

Added line #L116 was not covered by tests
if service_name in self.original_completion:
service.chat_completion = self.original_completion[service_name]
service.chat_completion_async = self.original_completion_async[service_name]

Check warning on line 119 in agentops/llms/taskweaver.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/taskweaver.py#L118-L119

Added lines #L118 - L119 were not covered by tests
Loading