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

Logging error #944

Merged
merged 1 commit into from
Apr 9, 2024
Merged
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
81 changes: 20 additions & 61 deletions libs/superagent/app/api/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from app.models.response import (
AgentToolList as AgentToolListResponse,
)
from app.utils.analytics import track_agent_invocation
from app.utils.api import get_current_api_user, handle_exception
from app.utils.callbacks import CostCalcAsyncHandler, CustomAsyncIteratorCallbackHandler
from app.utils.helpers import stream_dict_keys
Expand Down Expand Up @@ -458,39 +459,6 @@ async def invoke(
if not model and metadata.get("model"):
model = metadata.get("model")

def track_agent_invocation(result):
intermediate_steps_to_obj = [
{
**vars(toolClass),
"message_log": str(toolClass.message_log),
"response": response,
}
for toolClass, response in result.get("intermediate_steps", [])
]

analytics.track(
api_user.id,
"Invoked Agent",
{
"agent": agent_config.id,
"llm_model": agent_config.llmModel,
"sessionId": session_id,
# default http status code is 200
"response": {
"status_code": result.get("status_code", 200),
"error": result.get("error", None),
},
"output": result.get("output", None),
"input": result.get("input", None),
"intermediate_steps": intermediate_steps_to_obj,
"prompt_tokens": costCallback.prompt_tokens,
"completion_tokens": costCallback.completion_tokens,
"prompt_tokens_cost_usd": costCallback.prompt_tokens_cost_usd,
"completion_tokens_cost_usd": costCallback.completion_tokens_cost_usd,
"type": agent_config.type,
},
)

costCallback = CostCalcAsyncHandler(model=model)

monitoring_callbacks = [costCallback]
Expand Down Expand Up @@ -547,18 +515,15 @@ async def send_message(
result = task.result()

if SEGMENT_WRITE_KEY:
try:
track_agent_invocation(
{
"user_id": api_user.id,
"agent": agent_config,
"session_id": session_id,
**result,
**vars(cost_callback),
}
)
except Exception as e:
logger.error(f"Error tracking agent invocation: {e}")
track_agent_invocation(
{
"user_id": api_user.id,
"agent": agent_config,
"session_id": session_id,
**result,
**vars(cost_callback),
}
)

if "intermediate_steps" in result:
for step in result["intermediate_steps"]:
Expand All @@ -581,10 +546,7 @@ async def send_message(
except Exception as error:
logger.error(f"Error in send_message: {error}")
if SEGMENT_WRITE_KEY:
try:
track_agent_invocation({"error": str(error), "status_code": 500})
except Exception as e:
logger.error(f"Error tracking agent invocation: {e}")
track_agent_invocation({"error": str(error), "status_code": 500})
yield ("event: error\n" f"data: {error}\n\n")
finally:
streaming_callback.done.set()
Expand Down Expand Up @@ -634,18 +596,15 @@ async def send_message(
)

if not enable_streaming and SEGMENT_WRITE_KEY:
try:
track_agent_invocation(
{
"user_id": api_user.id,
"agent": agent_config,
"session_id": session_id,
**output,
**vars(cost_callback),
}
)
except Exception as e:
logger.error(f"Error tracking agent invocation: {e}")
track_agent_invocation(
{
"user_id": api_user.id,
"agent": agent_config,
"session_id": session_id,
**output,
**vars(cost_callback),
}
)

if output_schema:
from langchain.output_parsers.json import SimpleJsonOutputParser
Expand Down
27 changes: 6 additions & 21 deletions libs/superagent/app/utils/analytics.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import segment.analytics as analytics
from decouple import config
from pydantic import BaseModel
Expand All @@ -7,6 +9,8 @@
SEGMENT_WRITE_KEY = config("SEGMENT_WRITE_KEY", None)
analytics.write_key = SEGMENT_WRITE_KEY

logger = logging.getLogger(__name__)


class AgentInvocationData(BaseModel):
user_id: str
Expand Down Expand Up @@ -57,24 +61,5 @@ def track_agent_invocation(data: AgentInvocationData):
"completion_tokens_cost_usd": data.get("completion_tokens_cost_usd", 0),
},
)
except Exception:
analytics.track(
data["user_id"],
"Invoked Agent",
{
"agentId": data.get("agent", {}).id,
"workflowId": data.get("workflow_id", None),
"llm_model": data.get("agent", {}).llmModel,
"sessionId": data["session_id"],
"response": {
"status_code": data.get("status_code", 200),
"error": data.get("error", None),
},
"output": data.get("output", None),
"input": data.get("input", None),
"prompt_tokens": data.get("prompt_tokens", 0),
"completion_tokens": data.get("completion_tokens", 0),
"prompt_tokens_cost_usd": data.get("prompt_tokens_cost_usd", 0),
"completion_tokens_cost_usd": data.get("completion_tokens_cost_usd", 0),
},
)
except Exception as e:
logger.error(f"Error tracking agent invocation: {e}")