async def get_streaming_response(self, query):
if self.retriever is not None:
print("Preparing Streaming...!!!")
callback = self.window_memory.callbacks[0]
await self.window_memory.arun(query, callback=callback)
print("nStreaming done..!!")
else:
print("No retriever found to process query.")
yield "No retriever available"
I am using AsyncIteratorCallbackHandler
class AsyncIteratorCallbackHandler(BaseCallbackHandler):
def init(self):
self.stdout_handler = StreamingStdOutCallbackHandler()
async def on_llm_new_token(self, token: str, **kwargs: Any) -> Any:
await self.stdout_handler.on_llm_new_token(token, **kwargs)
I am using this to get the responce stream, and it does in the terminal but the streaming is not happening while using fast api
@router.post(“/ask”)
async def get_chat_response(
thread_id: Optional[UUID] = Query(None, description=”The ID of the thread”),
query: str = Body(None, description=”Message query from user”)
):
try:
window_memory_manager = WindowMemoryManager(
llm=get_llm_config(), base_retriever=retriever, thread_id=thread_id,
cohere_api_key=cohere_api_key
)
response_gen = window_memory_manager.generate_chat_responses(query)
return StreamingResponse(response_gen, media_type="text/event-stream")
# return {"statusCode": 200 , "message": "Chat response generated successfully.", "response": response_gen}
except Exception as e:
print("Something went wrong:", e)
raise HTTPException(status_code=500, detail=str(e))
Could someone please help in getting the response stream using fastapi?
Streaming models response using fast api
Srikanth Dongala is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.