|
| 1 | +"""DuhClient -- async and sync client for the duh REST API.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass |
| 6 | +from typing import Any, cast |
| 7 | + |
| 8 | +import httpx |
| 9 | + |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class AskResult: |
| 13 | + decision: str |
| 14 | + confidence: float |
| 15 | + dissent: str | None |
| 16 | + cost: float |
| 17 | + thread_id: str | None |
| 18 | + protocol_used: str |
| 19 | + |
| 20 | + |
| 21 | +@dataclass |
| 22 | +class ThreadSummary: |
| 23 | + thread_id: str |
| 24 | + question: str |
| 25 | + status: str |
| 26 | + created_at: str |
| 27 | + |
| 28 | + |
| 29 | +@dataclass |
| 30 | +class RecallResult: |
| 31 | + thread_id: str |
| 32 | + question: str |
| 33 | + decision: str | None |
| 34 | + confidence: float | None |
| 35 | + |
| 36 | + |
| 37 | +class DuhAPIError(Exception): |
| 38 | + """Error from the duh API.""" |
| 39 | + |
| 40 | + def __init__(self, status_code: int, detail: str) -> None: |
| 41 | + self.status_code = status_code |
| 42 | + self.detail = detail |
| 43 | + super().__init__(f"HTTP {status_code}: {detail}") |
| 44 | + |
| 45 | + |
| 46 | +class DuhClient: |
| 47 | + """Client for the duh consensus engine REST API. |
| 48 | +
|
| 49 | + Provides both async and sync interfaces. |
| 50 | +
|
| 51 | + Usage (async):: |
| 52 | +
|
| 53 | + async with DuhClient("http://localhost:8080") as client: |
| 54 | + result = await client.ask("What is the best auth strategy?") |
| 55 | + print(result.decision) |
| 56 | +
|
| 57 | + Usage (sync):: |
| 58 | +
|
| 59 | + client = DuhClient("http://localhost:8080") |
| 60 | + result = client.ask_sync("What is the best auth strategy?") |
| 61 | + print(result.decision) |
| 62 | + """ |
| 63 | + |
| 64 | + def __init__( |
| 65 | + self, |
| 66 | + base_url: str = "http://localhost:8080", |
| 67 | + api_key: str | None = None, |
| 68 | + timeout: float = 120.0, |
| 69 | + ) -> None: |
| 70 | + headers: dict[str, str] = {} |
| 71 | + if api_key: |
| 72 | + headers["X-API-Key"] = api_key |
| 73 | + self._base_url = base_url.rstrip("/") |
| 74 | + self._async_client = httpx.AsyncClient( |
| 75 | + base_url=self._base_url, |
| 76 | + headers=headers, |
| 77 | + timeout=timeout, |
| 78 | + ) |
| 79 | + self._sync_client = httpx.Client( |
| 80 | + base_url=self._base_url, |
| 81 | + headers=headers, |
| 82 | + timeout=timeout, |
| 83 | + ) |
| 84 | + |
| 85 | + async def __aenter__(self) -> DuhClient: |
| 86 | + return self |
| 87 | + |
| 88 | + async def __aexit__(self, *args: Any) -> None: |
| 89 | + await self.aclose() |
| 90 | + |
| 91 | + async def aclose(self) -> None: |
| 92 | + await self._async_client.aclose() |
| 93 | + |
| 94 | + def close(self) -> None: |
| 95 | + self._sync_client.close() |
| 96 | + |
| 97 | + def _raise_for_status(self, response: httpx.Response) -> None: |
| 98 | + if response.status_code >= 400: |
| 99 | + try: |
| 100 | + detail = response.json().get("detail", response.text) |
| 101 | + except Exception: |
| 102 | + detail = response.text |
| 103 | + raise DuhAPIError(response.status_code, detail) |
| 104 | + |
| 105 | + # -- Async methods --------------------------------------------------------- |
| 106 | + |
| 107 | + async def ask( |
| 108 | + self, |
| 109 | + question: str, |
| 110 | + *, |
| 111 | + protocol: str = "consensus", |
| 112 | + rounds: int = 3, |
| 113 | + decompose: bool = False, |
| 114 | + tools: bool = False, |
| 115 | + ) -> AskResult: |
| 116 | + resp = await self._async_client.post( |
| 117 | + "/api/ask", |
| 118 | + json={ |
| 119 | + "question": question, |
| 120 | + "protocol": protocol, |
| 121 | + "rounds": rounds, |
| 122 | + "decompose": decompose, |
| 123 | + "tools": tools, |
| 124 | + }, |
| 125 | + ) |
| 126 | + self._raise_for_status(resp) |
| 127 | + return AskResult(**resp.json()) |
| 128 | + |
| 129 | + async def threads( |
| 130 | + self, |
| 131 | + *, |
| 132 | + status: str | None = None, |
| 133 | + limit: int = 20, |
| 134 | + offset: int = 0, |
| 135 | + ) -> list[ThreadSummary]: |
| 136 | + params: dict[str, Any] = {"limit": limit, "offset": offset} |
| 137 | + if status: |
| 138 | + params["status"] = status |
| 139 | + resp = await self._async_client.get("/api/threads", params=params) |
| 140 | + self._raise_for_status(resp) |
| 141 | + return [ThreadSummary(**t) for t in resp.json()["threads"]] |
| 142 | + |
| 143 | + async def show(self, thread_id: str) -> dict[str, Any]: |
| 144 | + resp = await self._async_client.get(f"/api/threads/{thread_id}") |
| 145 | + self._raise_for_status(resp) |
| 146 | + return cast("dict[str, Any]", resp.json()) |
| 147 | + |
| 148 | + async def recall( |
| 149 | + self, query: str, *, limit: int = 10 |
| 150 | + ) -> list[RecallResult]: |
| 151 | + resp = await self._async_client.get( |
| 152 | + "/api/recall", params={"query": query, "limit": limit} |
| 153 | + ) |
| 154 | + self._raise_for_status(resp) |
| 155 | + return [RecallResult(**r) for r in resp.json()["results"]] |
| 156 | + |
| 157 | + async def feedback( |
| 158 | + self, |
| 159 | + thread_id: str, |
| 160 | + result: str, |
| 161 | + *, |
| 162 | + notes: str | None = None, |
| 163 | + ) -> dict[str, str]: |
| 164 | + body: dict[str, Any] = {"thread_id": thread_id, "result": result} |
| 165 | + if notes: |
| 166 | + body["notes"] = notes |
| 167 | + resp = await self._async_client.post("/api/feedback", json=body) |
| 168 | + self._raise_for_status(resp) |
| 169 | + return cast("dict[str, str]", resp.json()) |
| 170 | + |
| 171 | + async def models(self) -> list[dict[str, Any]]: |
| 172 | + resp = await self._async_client.get("/api/models") |
| 173 | + self._raise_for_status(resp) |
| 174 | + return cast("list[dict[str, Any]]", resp.json()["models"]) |
| 175 | + |
| 176 | + async def cost(self) -> dict[str, Any]: |
| 177 | + resp = await self._async_client.get("/api/cost") |
| 178 | + self._raise_for_status(resp) |
| 179 | + return cast("dict[str, Any]", resp.json()) |
| 180 | + |
| 181 | + async def health(self) -> bool: |
| 182 | + try: |
| 183 | + resp = await self._async_client.get("/api/health") |
| 184 | + return resp.status_code == 200 |
| 185 | + except httpx.HTTPError: |
| 186 | + return False |
| 187 | + |
| 188 | + # -- Sync wrappers --------------------------------------------------------- |
| 189 | + |
| 190 | + def ask_sync(self, question: str, **kwargs: Any) -> AskResult: |
| 191 | + resp = self._sync_client.post( |
| 192 | + "/api/ask", json={"question": question, **kwargs} |
| 193 | + ) |
| 194 | + self._raise_for_status(resp) |
| 195 | + return AskResult(**resp.json()) |
| 196 | + |
| 197 | + def threads_sync(self, **kwargs: Any) -> list[ThreadSummary]: |
| 198 | + resp = self._sync_client.get("/api/threads", params=kwargs) |
| 199 | + self._raise_for_status(resp) |
| 200 | + return [ThreadSummary(**t) for t in resp.json()["threads"]] |
| 201 | + |
| 202 | + def recall_sync(self, query: str, **kwargs: Any) -> list[RecallResult]: |
| 203 | + resp = self._sync_client.get( |
| 204 | + "/api/recall", params={"query": query, **kwargs} |
| 205 | + ) |
| 206 | + self._raise_for_status(resp) |
| 207 | + return [RecallResult(**r) for r in resp.json()["results"]] |
| 208 | + |
| 209 | + def models_sync(self) -> list[dict[str, Any]]: |
| 210 | + resp = self._sync_client.get("/api/models") |
| 211 | + self._raise_for_status(resp) |
| 212 | + return cast("list[dict[str, Any]]", resp.json()["models"]) |
| 213 | + |
| 214 | + def health_sync(self) -> bool: |
| 215 | + try: |
| 216 | + resp = self._sync_client.get("/api/health") |
| 217 | + return resp.status_code == 200 |
| 218 | + except httpx.HTTPError: |
| 219 | + return False |
0 commit comments