INNER CODE UNIT · Python
chat_stream
mfmezger/conversational-agent-langchain · frontend/client.py:21
async def chat_stream(self, messages: list[dict], collection_name: str = "default") -> AsyncGenerator[str, None]:
"""Stream chat response from the backend."""
payload = {"messages": messages, "collection_name": collection_name}
headers = {"accept": "application/json", "Content-Type": "application/json"}
async with httpx.AsyncClient(timeout=600.0) as client, client.stream("POST", f"{self.base_url}/rag/stream", json=payload, headers=headers) as response:
response.raise_for_status()
async for line in response.aiter_lines():
yield line
async def upload_documents(self, files: list[tuple], collection_name: str = "default", file_ending: str = ".pdf") -> httpx.Response:
"""Upload documents to the backend."""
params = {"collection_name": collection_name, "file_ending": file_ending}
# httpx handles files differently than requests
# files list of tuples (name, (filename, content, content_type))
async with httpx.AsyncClient(timeout=6000.0) as client:
return await client.post(f"{self.base_url}/embeddings/documents", params=params, files=files)
async def search(self, query: str, collection_name: str = "default") -> httpx.Response: