Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: allow concurrent processing for `async def send_message` function (#1445)
Sarah Wooders7ac11c103320
diff --git a/memgpt/server/rest_api/agents/message.py b/memgpt/server/rest_api/agents/message.pyindex c1faff4e7..17d121fff 100644--- a/memgpt/server/rest_api/agents/message.py+++ b/memgpt/server/rest_api/agents/message.py@@ -128,7 +128,6 @@ def setup_agents_message_router(server: SyncServer, interface: QueuingInterface,This endpoint accepts a message from a user and processes it through the agent.It can optionally stream the response if 'stream' is set to True."""- # agent_id = uuid.UUID(request.agent_id) if request.agent_id else Noneif request.role == "user" or request.role is None:message_func = server.user_message@@ -137,6 +136,12 @@ def setup_agents_message_router(server: SyncServer, interface: QueuingInterface,else:raise HTTPException(status_code=500, detail=f"Bad role {request.role}")+ def handle_exception(exception_loop: AbstractEventLoop, context):+ # context["message"] will always be there; but context["exception"] may not+ error = context.get("exception") or context["message"]+ print(f"handling asyncio exception {context}")+ interface.error(str(error))+if request.stream:# For streaming responsetry:@@ -155,12 +160,6 @@ def setup_agents_message_router(server: SyncServer, interface: QueuingInterface,)else:- def handle_exception(exception_loop: AbstractEventLoop, context):- # context["message"] will always be there; but context["exception"] may not- error = context.get("exception") or context["message"]- print(f"handling asyncio exception {context}")- interface.error(str(error))-# Run the synchronous function in a thread poolloop = asyncio.get_event_loop()loop.set_exception_handler(handle_exception)@@ -189,7 +188,18 @@ def setup_agents_message_router(server: SyncServer, interface: QueuingInterface,else:interface.clear()try:- message_func(user_id=user_id, agent_id=agent_id, message=request.message)+ # message_func(user_id=user_id, agent_id=agent_id, message=request.message)+ # call in thread pool to prevent blocking+ loop = asyncio.get_event_loop()+ loop.set_exception_handler(handle_exception)+ task = loop.run_in_executor(+ None,+ message_func,+ user_id,+ agent_id,+ request.message,+ )+ await taskexcept HTTPException:raiseexcept Exception as e:diff --git a/tests/test_concurrent_connections.py b/tests/test_concurrent_connections.pyindex 21e1f9a0a..5aecf2755 100644--- a/tests/test_concurrent_connections.py+++ b/tests/test_concurrent_connections.py@@ -121,13 +121,14 @@ def test_concurrent_messages(admin_client):response = client.send_message(agent_id=agent.id, message=message, role="user")et = time.time()print(f"Message sent from {st} to {et}")+ print(response.messages)results.append((st, et))except Exception as e:print("ERROR", e)threads = []print("Starting threads...")- for i in range(2):+ for i in range(5):thread = threading.Thread(target=_send_message)threads.append(thread)thread.start()