32 lines
886 B
Python
32 lines
886 B
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import aiohttp
|
|
from decouple import config
|
|
|
|
|
|
RAG_API_URL = config("RAG_API_URL", default="http://127.0.0.1:8001")
|
|
RAG_API_TIMEOUT_SECONDS = float(config("RAG_API_TIMEOUT_SECONDS", default="60"))
|
|
|
|
|
|
class RagApiError(Exception):
|
|
pass
|
|
|
|
|
|
async def ask_rag_api(message: str, history: list[dict[str, str]]) -> dict[str, Any]:
|
|
timeout = aiohttp.ClientTimeout(total=RAG_API_TIMEOUT_SECONDS)
|
|
payload = {
|
|
"message": message,
|
|
"history": history,
|
|
}
|
|
|
|
async with aiohttp.ClientSession(timeout=timeout) as session:
|
|
async with session.post(f"{RAG_API_URL}/chat", json=payload) as response:
|
|
if response.status != 200:
|
|
text = await response.text()
|
|
raise RagApiError(f"RAG API returned {response.status}: {text}")
|
|
|
|
return await response.json()
|
|
|