junaid17 commited on
Commit
2df4e3c
·
verified ·
1 Parent(s): 595aeaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -9
app.py CHANGED
@@ -94,22 +94,49 @@ async def chat_endpoint(request: ChatRequest):
94
  }
95
 
96
  async def event_generator():
97
- async for event in rag_app.astream_events(inputs, config=config, version="v1"):
98
- kind = event["event"]
99
- if kind == "on_chat_model_stream":
100
- content = event["data"]["chunk"].content
101
-
102
- if content:
103
- data = content.replace("\n", "\\n")
104
- yield f"data: {data}\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
  return StreamingResponse(
107
  event_generator(),
108
  media_type="text/event-stream",
109
  headers={
110
- "Cache-Control": "no-cache",
111
  "Connection": "keep-alive",
 
112
  "X-Accel-Buffering": "no",
 
113
  },
114
  )
115
 
 
94
  }
95
 
96
  async def event_generator():
97
+ try:
98
+ # Send initial connection message
99
+ yield ": connected\n\n"
100
+
101
+ last_yield_time = asyncio.get_event_loop().time()
102
+
103
+ async for event in rag_app.astream_events(inputs, config=config, version="v1"):
104
+ # Send heartbeat every 15 seconds to keep connection alive
105
+ current_time = asyncio.get_event_loop().time()
106
+ if current_time - last_yield_time > 15:
107
+ yield ": heartbeat\n\n"
108
+ last_yield_time = current_time
109
+ kind = event["event"]
110
+ if kind == "on_chat_model_stream":
111
+ chunk = event["data"]["chunk"]
112
+ content = chunk.content if hasattr(chunk, "content") else str(chunk)
113
+
114
+ if content:
115
+ # Escape newlines and special chars for JSON
116
+ escaped_content = json.dumps(content)
117
+ yield f"data: {escaped_content}\n\n"
118
+
119
+ # Update last yield time
120
+ last_yield_time = asyncio.get_event_loop().time()
121
+
122
+ # CRITICAL: Force immediate flush
123
+ await asyncio.sleep(0.01)
124
+
125
+ yield "data: [DONE]\n\n"
126
+
127
+ except Exception as e:
128
+ error_msg = json.dumps({"error": str(e)})
129
+ yield f"data: {error_msg}\n\n"
130
 
131
  return StreamingResponse(
132
  event_generator(),
133
  media_type="text/event-stream",
134
  headers={
135
+ "Cache-Control": "no-cache, no-transform",
136
  "Connection": "keep-alive",
137
+ "Content-Type": "text/event-stream",
138
  "X-Accel-Buffering": "no",
139
+ "Transfer-Encoding": "chunked",
140
  },
141
  )
142