Skip to content

Commit

Permalink
change sse endpoint to simple streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherChudzicki committed Jan 6, 2025
1 parent f92ad44 commit b02a342
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' \
Expand Down
21 changes: 9 additions & 12 deletions ai_chatbots/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -121,34 +121,31 @@ 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):
await self.channel_layer.group_discard(
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:
Expand Down
4 changes: 2 additions & 2 deletions ai_chatbots/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
]

0 comments on commit b02a342

Please sign in to comment.