junaid17 commited on
Commit
7ad395b
·
verified ·
1 Parent(s): e517ecf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -6
app.py CHANGED
@@ -90,11 +90,10 @@ async def chat_endpoint(request: ChatRequest):
90
  async def event_generator():
91
  print(f"--- 🚀 Starting stream for {request.thread_id} ---")
92
 
93
- # Use 'v2' if you are on the latest LangGraph, but 'v1' is safer for compatibility
94
  async for event in rag_app.astream_events(inputs, config=config, version="v1"):
95
 
96
- # [DEBUG] Print the event type to your Hugging Face Logs
97
- # This will show us if the events are firing but named differently
98
  event_type = event.get("event")
99
 
100
  # Logic: We don't care about the event name.
@@ -103,11 +102,11 @@ async def chat_endpoint(request: ChatRequest):
103
  chunk = data.get("chunk")
104
 
105
  # Check if chunk exists and has .content attribute (standard LangChain message chunk)
106
- if chunk and hasattr(chunk, "content") and chunk.content:
107
  content = chunk.content
108
 
109
  # Filter out empty strings or weird artifacts
110
- if content.strip() != "":
111
  # JSON encode the content
112
  chunk_json = json.dumps({"content": content})
113
  yield f"data: {chunk_json}\n\n"
@@ -122,7 +121,7 @@ async def chat_endpoint(request: ChatRequest):
122
  "Cache-Control": "no-cache",
123
  "Connection": "keep-alive",
124
  "Content-Type": "text/event-stream",
125
- "X-Accel-Buffering": "no",
126
  },
127
  )
128
 
 
90
  async def event_generator():
91
  print(f"--- 🚀 Starting stream for {request.thread_id} ---")
92
 
93
+ # We iterate over ALL events
94
  async for event in rag_app.astream_events(inputs, config=config, version="v1"):
95
 
96
+ # [DEBUG] Print event types to your terminal so you can see what's happening
 
97
  event_type = event.get("event")
98
 
99
  # Logic: We don't care about the event name.
 
102
  chunk = data.get("chunk")
103
 
104
  # Check if chunk exists and has .content attribute (standard LangChain message chunk)
105
+ if chunk and hasattr(chunk, "content"):
106
  content = chunk.content
107
 
108
  # Filter out empty strings or weird artifacts
109
+ if content:
110
  # JSON encode the content
111
  chunk_json = json.dumps({"content": content})
112
  yield f"data: {chunk_json}\n\n"
 
121
  "Cache-Control": "no-cache",
122
  "Connection": "keep-alive",
123
  "Content-Type": "text/event-stream",
124
+ "X-Accel-Buffering": "no", # Critical for HF
125
  },
126
  )
127