Skip to content

Commit

Permalink
chore: Add temporary telemetry logs (#2284)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattzh72 authored Dec 19, 2024
1 parent e5f230e commit f71d921
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
11 changes: 11 additions & 0 deletions letta/server/rest_api/routers/v1/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pydantic import Field

from letta.constants import DEFAULT_MESSAGE_TOOL, DEFAULT_MESSAGE_TOOL_KWARG
from letta.log import get_logger
from letta.orm.errors import NoResultFound
from letta.schemas.agent import AgentState, CreateAgent, UpdateAgent
from letta.schemas.block import ( # , BlockLabelUpdate, BlockLimitUpdate
Expand Down Expand Up @@ -54,6 +55,8 @@

router = APIRouter(prefix="/agents", tags=["agents"])

logger = get_logger(__name__)


# TODO: This should be paginated
@router.get("/", response_model=List[AgentState], operation_id="list_agents")
Expand Down Expand Up @@ -453,6 +456,13 @@ def get_agent_messages(
"""
actor = server.user_manager.get_user_or_default(user_id=user_id)

# TODO: Temporary debugging logs for debugging very slow endpoint
import uuid

temp_rand_uuid = uuid.uuid4()

logger.info(f"[{temp_rand_uuid}] RECEIVED GET /messages for agent_id={agent_id} before={before} limit={limit}")

return server.get_agent_recall_cursor(
user_id=actor.id,
agent_id=agent_id,
Expand All @@ -462,6 +472,7 @@ def get_agent_messages(
return_message_object=msg_object,
assistant_message_tool_name=assistant_message_tool_name,
assistant_message_tool_kwarg=assistant_message_tool_kwarg,
temp_rand_uuid=temp_rand_uuid,
)


Expand Down
33 changes: 30 additions & 3 deletions letta/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,16 +979,32 @@ def get_agent_recall_cursor(
return_message_object: bool = True,
assistant_message_tool_name: str = constants.DEFAULT_MESSAGE_TOOL,
assistant_message_tool_kwarg: str = constants.DEFAULT_MESSAGE_TOOL_KWARG,
temp_rand_uuid: Optional[str] = None,
) -> Union[List[Message], List[LettaMessage]]:
# TODO: Thread actor directly through this function, since the top level caller most likely already retrieved the user
import datetime

start_time = datetime.datetime.utcnow()

logger.info(
f"[{temp_rand_uuid}] {datetime.datetime.utcnow()} - Fetching actor for user_id={user_id} (Elapsed: {(datetime.datetime.utcnow() - start_time).total_seconds()}s)"
)
actor = self.user_manager.get_user_or_default(user_id=user_id)

# Get the agent object (loaded in memory)
logger.info(
f"[{temp_rand_uuid}] {datetime.datetime.utcnow()} - Loading agent object for agent_id={agent_id} (Elapsed: {(datetime.datetime.utcnow() - start_time).total_seconds()}s)"
)
letta_agent = self.load_agent(agent_id=agent_id, actor=actor)

# iterate over records
logger.info(
f"[{temp_rand_uuid}] {datetime.datetime.utcnow()} - Resolving start_date and end_date for filtering messages (Elapsed: {(datetime.datetime.utcnow() - start_time).total_seconds()}s)"
)
start_date = self.message_manager.get_message_by_id(after, actor=actor).created_at if after else None
end_date = self.message_manager.get_message_by_id(before, actor=actor).created_at if before else None

logger.info(
f"[{temp_rand_uuid}] {datetime.datetime.utcnow()} - Fetching messages for agent_id={agent_id}, start_date={start_date}, end_date={end_date}, limit={limit}, reverse={reverse} (Elapsed: {(datetime.datetime.utcnow() - start_time).total_seconds()}s)"
)
records = letta_agent.message_manager.list_messages_for_agent(
agent_id=agent_id,
actor=actor,
Expand All @@ -998,10 +1014,15 @@ def get_agent_recall_cursor(
ascending=not reverse,
)

logger.info(
f"[{temp_rand_uuid}] {datetime.datetime.utcnow()} - Validating message types (Elapsed: {(datetime.datetime.utcnow() - start_time).total_seconds()}s)"
)
assert all(isinstance(m, Message) for m in records)

if not return_message_object:
# If we're GETing messages in reverse, we need to reverse the inner list (generated by to_letta_message)
logger.info(
f"[{temp_rand_uuid}] {datetime.datetime.utcnow()} - Converting messages to LettaMessage objects (Elapsed: {(datetime.datetime.utcnow() - start_time).total_seconds()}s)"
)
records = [
msg
for m in records
Expand All @@ -1012,8 +1033,14 @@ def get_agent_recall_cursor(
]

if reverse:
logger.info(
f"[{temp_rand_uuid}] {datetime.datetime.utcnow()} - Reversing message order (Elapsed: {(datetime.datetime.utcnow() - start_time).total_seconds()}s)"
)
records = records[::-1]

logger.info(
f"[{temp_rand_uuid}] {datetime.datetime.utcnow()} - Returning {len(records)} messages (Elapsed: {(datetime.datetime.utcnow() - start_time).total_seconds()}s)"
)
return records

def get_server_config(self, include_defaults: bool = False) -> dict:
Expand Down

0 comments on commit f71d921

Please sign in to comment.