INNER CODE UNIT · Python
AgentClient
mfmezger/conversational-agent-langchain · frontend/client.py:9
class AgentClient:
"""Client for interacting with the Agent API."""
def __init__(self, base_url: str | None = None) -> None:
"""Initialize the client."""
if base_url:
self.base_url = base_url
else:
host = os.getenv("BACKEND_HOST", "localhost")
port = os.getenv("BACKEND_PORT", "8001")
self.base_url = f"http://{host}:{port}"
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()