From b02a34284828c92d001bfddeeab95ade7f8189cf Mon Sep 17 00:00:00 2001 From: Chris Chudzicki Date: Mon, 6 Jan 2025 10:32:11 -0500 Subject: [PATCH] change sse endpoint to simple streaming --- README.md | 4 ++-- ai_chatbots/consumers.py | 21 +++++++++------------ ai_chatbots/routing.py | 4 ++-- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index d2bea6c..99a9c32 100644 --- a/README.md +++ b/README.md @@ -62,10 +62,10 @@ pre-commit init-templatedir ~/.git-template ## Sample Requests -Run the following curl command to test the SSE recommendation agent API: +Run the following curl command to test the HTTP recommendation agent API: ``` -curl 'http://ai.open.odl.local:8002/sse/recommendation_agent/' \ +curl 'http://ai.open.odl.local:8002/http/recommendation_agent/' \ -H 'Accept: */*' \ -H 'Connection: keep-alive' \ -H 'Origin: http://ai.open.odl.local:8002' \ diff --git a/ai_chatbots/consumers.py b/ai_chatbots/consumers.py index a701554..d508f9e 100644 --- a/ai_chatbots/consumers.py +++ b/ai_chatbots/consumers.py @@ -76,9 +76,9 @@ async def receive(self, text_data: str) -> str: await self.send(text_data="!endResponse") -class RecommendationBotSSEConsumer(AsyncHttpConsumer): +class RecommendationBotHttpConsumer(AsyncHttpConsumer): """ - Async HTTP SSE consumer for the recommendation agent. + Async HTTP consumer for the recommendation agent. """ async def handle(self, message: str): @@ -121,18 +121,17 @@ async def handle(self, message: str): # Headers are only sent after the first body event. # Set "more_body" to tell the interface server to not # finish the response yet: - payload = "event: ping\ndata: null\n\n" - await self.send_body(payload.encode("utf-8"), more_body=True) + await self.send_chunk("") try: message_text = process_message(message, agent) for chunk in agent.get_completion(message_text): - await self.send_event(event=chunk, more_body=True) + await self.send_chunk(chunk) except: # noqa: E722 log.exception("Error in RecommendationAgentConsumer") finally: - await self.send_event(event="", more_body=False) + await self.send_chunk("", more_body=False) await self.disconnect() async def disconnect(self): @@ -140,15 +139,13 @@ async def disconnect(self): f"recommendation_bot_{self.user_id}", self.channel_name ) - async def send_event(self, event: str, more_body): - # Send response event - log.info(event) - data = f"event: agent_response\ndata: {event}\n\n" - await self.send_body(data.encode("utf-8"), more_body=more_body) + async def send_chunk(self, chunk: str, *, more_body: bool = True): + log.info(chunk) + await self.send_body(body=chunk.encode("utf-8"), more_body=more_body) async def http_request(self, message): """ - Receives an SSE request and holds the connection open + Receives a request and holds the connection open until the client or server chooses to disconnect. """ try: diff --git a/ai_chatbots/routing.py b/ai_chatbots/routing.py index 4e22ace..58e088c 100644 --- a/ai_chatbots/routing.py +++ b/ai_chatbots/routing.py @@ -13,8 +13,8 @@ http_patterns = [ re_path( - r"sse/recommendation_agent/", - consumers.RecommendationBotSSEConsumer.as_asgi(), + r"http/recommendation_agent/", + consumers.RecommendationBotHttpConsumer.as_asgi(), name="recommendation_agent_sse", ), ]