Skip to content

Commit

Permalink
Add multi-agent tracking demo
Browse files Browse the repository at this point in the history
  • Loading branch information
bonk1t committed Dec 6, 2024
1 parent 733af7e commit 2addeee
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 40 deletions.
8 changes: 4 additions & 4 deletions agency_swarm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from .tools import BaseTool
from .util import (
get_openai_client,
get_usage_tracker,
get_tracker,
llm_validator,
set_openai_client,
set_openai_key,
set_usage_tracker,
set_tracker,
)
from .util.streaming import (
AgencyEventHandler,
Expand All @@ -24,6 +24,6 @@
"set_openai_client",
"set_openai_key",
"llm_validator",
"set_usage_tracker",
"get_usage_tracker",
"set_tracker",
"get_tracker",
]
6 changes: 3 additions & 3 deletions agency_swarm/agency/agency.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from agency_swarm.user import User
from agency_swarm.util.errors import RefusalError
from agency_swarm.util.files import get_file_purpose, get_tools
from agency_swarm.util.oai import get_usage_tracker
from agency_swarm.util.oai import get_tracker
from agency_swarm.util.shared_state import SharedState
from agency_swarm.util.streaming import (
AgencyEventHandler,
Expand Down Expand Up @@ -154,7 +154,7 @@ def __init__(
self._create_special_tools()
self._init_agents()

@get_usage_tracker().get_observe_decorator()
@get_tracker().get_observe_decorator()
def get_completion(
self,
message: str,
Expand Down Expand Up @@ -210,7 +210,7 @@ def get_completion(

return res

@get_usage_tracker().get_observe_decorator()
@get_tracker().get_observe_decorator()
def get_completion_stream(
self,
message: str,
Expand Down
4 changes: 2 additions & 2 deletions agency_swarm/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from .files import get_file_purpose, get_tools
from .oai import (
get_openai_client,
get_usage_tracker,
get_tracker,
set_openai_client,
set_openai_key,
set_usage_tracker,
set_tracker,
)
from .tracking import AbstractTracker, LangfuseUsageTracker, SQLiteUsageTracker
from .validators import llm_validator
16 changes: 8 additions & 8 deletions agency_swarm/util/oai.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@
client_lock = threading.Lock()
client = None
_openai_module = None
_usage_tracker = "sqlite" # Default usage tracker
_tracker = "sqlite" # Default usage tracker


def set_usage_tracker(usage_tracker: str):
def set_tracker(tracker: str):
"""Set the global usage tracker.
Args:
usage_tracker: The usage tracking mechanism to use.
tracker: The usage tracking mechanism to use.
"""
global _usage_tracker, client, _openai_module
global _tracker, client, _openai_module
with client_lock:
_usage_tracker = usage_tracker
_tracker = tracker
client = get_openai_client()


def get_usage_tracker():
def get_tracker():
"""Get the current usage tracker instance.
Returns:
AbstractTracker: The current usage tracker instance.
"""
return get_tracker_by_name(_usage_tracker)
return get_tracker_by_name(_tracker)


def get_openai_client():
Expand Down Expand Up @@ -81,7 +81,7 @@ def _get_openai_module() -> object:
if _openai_module is None:
try:
# Use Langfuse OpenAI client if configured
if _usage_tracker == "langfuse" and all(
if _tracker == "langfuse" and all(
os.getenv(key) for key in ["LANGFUSE_SECRET_KEY", "LANGFUSE_PUBLIC_KEY"]
):
from langfuse.openai import openai
Expand Down
6 changes: 3 additions & 3 deletions agency_swarm/util/streaming/agency_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from openai.lib.streaming import AssistantEventHandler
from openai.types.beta.threads.runs.run_step import RunStep

from agency_swarm.util.oai import get_usage_tracker
from agency_swarm.util.oai import get_tracker
from agency_swarm.util.tracking.abstract_tracker import AbstractTracker


Expand Down Expand Up @@ -43,8 +43,8 @@ def on_run_step_done(cls, run_step: RunStep) -> None:
Implements tracking of usage for the run step.
"""
if run_step.usage:
usage_tracker = get_usage_tracker()
usage_tracker.track_usage(
tracker = get_tracker()
tracker.track_usage(
usage=run_step.usage,
assistant_id=run_step.assistant_id,
thread_id=run_step.thread_id,
Expand Down
2 changes: 2 additions & 0 deletions agency_swarm/util/tracking/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from .abstract_tracker import AbstractTracker
from .langfuse_tracker import LangfuseUsageTracker
from .sqlite_tracker import SQLiteUsageTracker
from .tracker_factory import get_tracker_by_name

__all__ = [
"AbstractTracker",
"SQLiteUsageTracker",
"LangfuseUsageTracker",
"get_tracker_by_name",
]
43 changes: 43 additions & 0 deletions tests/demos/demo_observability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from dotenv import load_dotenv

load_dotenv()

from agency_swarm import Agency, Agent
from agency_swarm.util.oai import _get_openai_module, set_tracker

# Test Langfuse configuration
set_tracker("langfuse")
openai = _get_openai_module()
openai.langfuse_auth_check()

# Create multiple agents with different roles
manager = Agent(
name="Project Manager",
description="Manages projects and coordinates between team members",
temperature=0.5,
)

developer = Agent(
name="Developer",
description="Implements technical solutions and writes code",
temperature=0.7,
)

analyst = Agent(
name="Data Analyst",
description="Analyzes data and provides insights",
temperature=0.4,
)

# Create agency with communication flows
agency = Agency(
[
manager, # Manager is the entry point
[manager, developer], # Manager can communicate with Developer
[manager, analyst], # Manager can communicate with Analyst
[developer, analyst], # Developer can communicate with Analyst
]
)

# Run the demo with Gradio interface
agency.demo_gradio()
20 changes: 0 additions & 20 deletions tests/test_usage_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import pytest
from openai.types.beta.threads.runs.run_step import Usage

from agency_swarm.util.oai import (
_get_openai_module,
set_usage_tracker,
)
from agency_swarm.util.tracking import LangfuseUsageTracker, SQLiteUsageTracker


Expand Down Expand Up @@ -116,19 +112,3 @@ def test_langfuse_get_total_tokens_multiple(mock_langfuse, langfuse_tracker):

def test_get_observe_decorator(langfuse_tracker):
assert callable(langfuse_tracker.get_observe_decorator())


if __name__ == "__main__":
from dotenv import load_dotenv

load_dotenv()
from agency_swarm import Agency, Agent

# Test Langfuse configuration
set_usage_tracker("langfuse")
openai = _get_openai_module()
openai.langfuse_auth_check()

agent = Agent(name="test_agent")
agency = Agency(agency_chart=[agent])
agency.demo_gradio()

0 comments on commit 2addeee

Please sign in to comment.