-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement get_total_tokens; Minor fixes
- Loading branch information
Showing
12 changed files
with
214 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
from .cli.create_agent_template import create_agent_template | ||
from .cli.import_agent import import_agent | ||
from .files import get_file_purpose, get_tools | ||
from .oai import get_openai_client, set_openai_client, set_openai_key | ||
from .usage_tracking import AbstractTracker, LangfuseUsageTracker, SQLiteUsageTracker | ||
from .oai import ( | ||
get_openai_client, | ||
get_usage_tracker, | ||
set_openai_client, | ||
set_openai_key, | ||
set_usage_tracker, | ||
) | ||
from .tracking import AbstractTracker, LangfuseUsageTracker, SQLiteUsageTracker | ||
from .validators import llm_validator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from langfuse import Langfuse | ||
from langfuse.decorators import observe | ||
from openai.types.beta.threads.runs.run_step import Usage | ||
|
||
from agency_swarm.util.tracking.abstract_tracker import AbstractTracker | ||
|
||
|
||
class LangfuseUsageTracker(AbstractTracker): | ||
def __init__(self): | ||
self.client = Langfuse() | ||
|
||
def track_usage( | ||
self, usage: Usage, assistant_id: str, thread_id: str, model: str | ||
) -> None: | ||
""" | ||
Track usage by recording a generation event in Langfuse. | ||
""" | ||
self.client.generation( | ||
model=model, | ||
metadata={ | ||
"assistant_id": assistant_id, | ||
"thread_id": thread_id, | ||
}, | ||
usage={ | ||
"input": usage.prompt_tokens, | ||
"output": usage.completion_tokens, | ||
"total": usage.total_tokens, | ||
"unit": "TOKENS", | ||
}, | ||
) | ||
|
||
def get_total_tokens(self) -> Usage: | ||
""" | ||
Retrieve total usage from Langfuse by summing over all recorded generations. | ||
""" | ||
generations = self.client.fetch_observations(type="GENERATION").data | ||
|
||
prompt_tokens = 0 | ||
completion_tokens = 0 | ||
total_tokens = 0 | ||
|
||
for generation in generations: | ||
if generation.usage: | ||
prompt_tokens += generation.usage.input or 0 | ||
completion_tokens += generation.usage.output or 0 | ||
total_tokens += generation.usage.total or 0 | ||
|
||
return Usage( | ||
prompt_tokens=prompt_tokens, | ||
completion_tokens=completion_tokens, | ||
total_tokens=total_tokens, | ||
) | ||
|
||
def close(self) -> None: | ||
# Nothing to close | ||
pass | ||
|
||
@classmethod | ||
def get_observe_decorator(cls): | ||
return observe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from typing import Literal | ||
|
||
from agency_swarm.util.tracking.langfuse_tracker import LangfuseUsageTracker | ||
from agency_swarm.util.tracking.sqlite_tracker import SQLiteUsageTracker | ||
|
||
|
||
def get_tracker_by_name(tracker_type: Literal["sqlite", "langfuse"] = "sqlite"): | ||
if tracker_type == "langfuse": | ||
return LangfuseUsageTracker() | ||
return SQLiteUsageTracker() |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.