File size: 1,541 Bytes
458c8e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# backend_client.py
from __future__ import annotations
import requests
from typing import Optional, List, Tuple

class LangGraphClient:
    """
    Minimal client to talk to your LangGraph app running on http://localhost:8010.
    Adjust ENDPOINTS to match your server routes/payloads.
    """

    def __init__(self, base_url: str = "http://localhost:8010"):
        self.base_url = base_url.rstrip("/")
        # Example endpoints — change if your server differs
        self.endpoints = {
            "chat": f"{self.base_url}/chat"   # expects JSON {session_id, message, history?}
        }

    def send_message(
        self,
        message: str,
        session_id: Optional[str] = "default",
        history: Optional[List[Tuple[str, str]]] = None,
        timeout: int = 30
    ) -> str:
        """
        Sends a message to the backend and returns the assistant reply text.
        """
        payload = {
            "session_id": session_id,
            "message": message,
            "history": history or []  # [[user, assistant], ...] if your server uses it
        }
        try:
            r = requests.post(self.endpoints["chat"], json=payload, timeout=timeout)
            r.raise_for_status()
            data = r.json()
            # Expecting {"reply": "..."}; adjust if your API returns a different shape
            reply = data.get("reply") or data.get("message") or data.get("text") or ""
            return str(reply)
        except requests.RequestException as e:
            return f"[Backend error] {e}"