Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: add support for use_assistant_message in list_messages (#804)
cthomasa5d7f66f59d4
diff --git a/letta/schemas/message.py b/letta/schemas/message.pyindex bc5869f6c..59bac7c53 100644--- a/letta/schemas/message.py+++ b/letta/schemas/message.py@@ -1,3 +1,5 @@+from __future__ import annotations+import copyimport jsonimport warnings@@ -176,9 +178,47 @@ class Message(BaseMessage):json_message["created_at"] = self.created_at.isoformat()return json_message+ @staticmethod+ def to_letta_messages_from_list(+ messages: List[Message],+ use_assistant_message: bool = True,+ assistant_message_tool_name: str = DEFAULT_MESSAGE_TOOL,+ assistant_message_tool_kwarg: str = DEFAULT_MESSAGE_TOOL_KWARG,+ ) -> List[LettaMessage]:+ if use_assistant_message:+ message_ids_to_remove = []+ assistant_messages_by_tool_call = {+ tool_call.id: msg+ for msg in messages+ if msg.role == MessageRole.assistant and msg.tool_calls+ for tool_call in msg.tool_calls+ }+ for message in messages:+ if (+ message.role == MessageRole.tool+ and message.tool_call_id in assistant_messages_by_tool_call+ and assistant_messages_by_tool_call[message.tool_call_id].tool_calls+ and assistant_message_tool_name+ in [tool_call.function.name for tool_call in assistant_messages_by_tool_call[message.tool_call_id].tool_calls]+ ):+ message_ids_to_remove.append(message.id)++ messages = [msg for msg in messages if msg.id not in message_ids_to_remove]++ # Convert messages to LettaMessages+ return [+ msg+ for m in messages+ for msg in m.to_letta_message(+ use_assistant_message=use_assistant_message,+ assistant_message_tool_name=assistant_message_tool_name,+ assistant_message_tool_kwarg=assistant_message_tool_kwarg,+ )+ ]+def to_letta_message(self,- assistant_message: bool = False,+ use_assistant_message: bool = False,assistant_message_tool_name: str = DEFAULT_MESSAGE_TOOL,assistant_message_tool_kwarg: str = DEFAULT_MESSAGE_TOOL_KWARG,) -> List[LettaMessage]:@@ -201,7 +241,7 @@ class Message(BaseMessage):for tool_call in self.tool_calls:# If we're supporting using assistant message,# then we want to treat certain function calls as a special case- if assistant_message and tool_call.function.name == assistant_message_tool_name:+ if use_assistant_message and tool_call.function.name == assistant_message_tool_name:# We need to unpack the actual message contents from the function calltry:func_args = json.loads(tool_call.function.arguments)diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.pyindex 91f1f191a..e4cb0da63 100644--- a/letta/server/rest_api/routers/v1/agents.py+++ b/letta/server/rest_api/routers/v1/agents.py@@ -5,13 +5,14 @@ from fastapi import APIRouter, BackgroundTasks, Body, Depends, Header, HTTPExcepfrom fastapi.responses import JSONResponsefrom pydantic import Field+from letta.constants import DEFAULT_MESSAGE_TOOL, DEFAULT_MESSAGE_TOOL_KWARGfrom letta.log import get_loggerfrom letta.orm.errors import NoResultFoundfrom letta.schemas.agent import AgentState, CreateAgent, UpdateAgentfrom letta.schemas.block import Block, BlockUpdate, CreateBlock # , BlockLabelUpdate, BlockLimitUpdatefrom letta.schemas.job import JobStatus, JobUpdatefrom letta.schemas.letta_message import LettaMessageUnion-from letta.schemas.letta_request import LettaRequest, LettaRequestConfig, LettaStreamingRequest+from letta.schemas.letta_request import LettaRequest, LettaStreamingRequestfrom letta.schemas.letta_response import LettaResponsefrom letta.schemas.memory import ContextWindowOverview, CreateArchivalMemory, Memoryfrom letta.schemas.message import Message, MessageUpdate@@ -402,7 +403,9 @@ def list_messages(after: Optional[str] = Query(None, description="Message after which to retrieve the returned messages."),before: Optional[str] = Query(None, description="Message before which to retrieve the returned messages."),limit: int = Query(10, description="Maximum number of messages to retrieve."),- config: LettaRequestConfig = Query(LettaRequestConfig(), description="Configuration options for the LettaRequest."),+ use_assistant_message: bool = Query(True, description="Whether to use assistant messages"),+ assistant_message_tool_name: str = Query(DEFAULT_MESSAGE_TOOL, description="The name of the designated message tool."),+ assistant_message_tool_kwarg: str = Query(DEFAULT_MESSAGE_TOOL_KWARG, description="The name of the message argument."),user_id: Optional[str] = Header(None, alias="user_id"), # Extract user_id from header, default to None if not present):"""@@ -418,8 +421,9 @@ def list_messages(limit=limit,reverse=True,return_message_object=False,- assistant_message_tool_name=config.assistant_message_tool_name,- assistant_message_tool_kwarg=config.assistant_message_tool_kwarg,+ use_assistant_message=use_assistant_message,+ assistant_message_tool_name=assistant_message_tool_name,+ assistant_message_tool_kwarg=assistant_message_tool_kwarg,)diff --git a/letta/server/server.py b/letta/server/server.pyindex 7eb141546..35c2e16c3 100644--- a/letta/server/server.py+++ b/letta/server/server.py@@ -871,6 +871,7 @@ class SyncServer(Server):limit: Optional[int] = 100,reverse: Optional[bool] = False,return_message_object: bool = True,+ use_assistant_message: bool = True,assistant_message_tool_name: str = constants.DEFAULT_MESSAGE_TOOL,assistant_message_tool_kwarg: str = constants.DEFAULT_MESSAGE_TOOL_KWARG,) -> Union[List[Message], List[LettaMessage]]:@@ -890,14 +891,12 @@ class SyncServer(Server):)if not return_message_object:- records = [- msg- for m in records- for msg in m.to_letta_message(- assistant_message_tool_name=assistant_message_tool_name,- assistant_message_tool_kwarg=assistant_message_tool_kwarg,- )- ]+ records = Message.to_letta_messages_from_list(+ messages=records,+ use_assistant_message=use_assistant_message,+ assistant_message_tool_name=assistant_message_tool_name,+ assistant_message_tool_kwarg=assistant_message_tool_kwarg,+ )if reverse:records = records[::-1]diff --git a/letta/services/job_manager.py b/letta/services/job_manager.pyindex 59877ee05..b774a9a12 100644--- a/letta/services/job_manager.py+++ b/letta/services/job_manager.py@@ -308,7 +308,7 @@ class JobManager:msgfor m in messagesfor msg in m.to_letta_message(- assistant_message=request_config["use_assistant_message"],+ use_assistant_message=request_config["use_assistant_message"],assistant_message_tool_name=request_config["assistant_message_tool_name"],assistant_message_tool_kwarg=request_config["assistant_message_tool_kwarg"],)diff --git a/tests/test_server.py b/tests/test_server.pyindex 7e8c30839..d997af1eb 100644--- a/tests/test_server.py+++ b/tests/test_server.py@@ -636,6 +636,7 @@ def _test_get_messages_letta_format(limit=1000,reverse=reverse,return_message_object=True,+ use_assistant_message=False,)assert all(isinstance(m, Message) for m in messages)@@ -645,6 +646,7 @@ def _test_get_messages_letta_format(limit=1000,reverse=reverse,return_message_object=False,+ use_assistant_message=False,)assert all(isinstance(m, LettaMessage) for m in letta_messages)