feat: update codebase to use httpx2 and enhance docstrings for clarity

This commit is contained in:
grillazz
2026-10-22 13:42:10 +02:00
parent 96b383e391
commit 332324c861
10 changed files with 87 additions and 107 deletions
+19 -21
View File
@@ -1,25 +1,28 @@
from collections.abc import AsyncGenerator
"""Streaming client for OpenAI-compatible chat completion endpoints."""
import httpx
from collections.abc import AsyncIterator
import httpx2
import orjson
class StreamLLMService:
"""Send prompts to an LLM endpoint and yield newline-delimited JSON events."""
def __init__(self, base_url: str = "http://localhost:11434/v1") -> None:
"""Configure the endpoint and default model."""
self.base_url = base_url
self.model = "llama3.2"
async def stream_chat(self, prompt: str) -> AsyncGenerator[bytes]:
"""Stream chat completion responses from LLM."""
# Send the user a message first
async def stream_chat(self, prompt: str) -> AsyncIterator[bytes]:
"""Yield the prompt followed by streamed model response chunks."""
user_msg = {
"role": "user",
"content": prompt,
}
yield orjson.dumps(user_msg) + b"\n"
# Open client as context manager and stream responses
async with httpx.AsyncClient(base_url=self.base_url) as client:
async with httpx2.AsyncClient(base_url=self.base_url) as client:
async with client.stream(
"POST",
"/chat/completions",
@@ -32,21 +35,16 @@ class StreamLLMService:
) as response:
async for line in response.aiter_lines():
if line.startswith("data: ") and line != "data: [DONE]":
try:
json_line = line[6:] # Remove "data: " prefix
data = orjson.loads(json_line)
content = (
data.get("choices", [{}])[0]
.get("delta", {})
.get("content", "")
)
if content:
model_msg = {"role": "model", "content": content}
yield orjson.dumps(model_msg) + b"\n"
except Exception:
pass
data = orjson.loads(line.removeprefix("data: "))
choices = data.get("choices", [])
if not choices:
continue
content = choices[0].get("delta", {}).get("content", "")
if content:
model_msg = {"role": "model", "content": content}
yield orjson.dumps(model_msg) + b"\n"
# FastAPI dependency
def get_llm_service(base_url: str | None = None) -> StreamLLMService:
"""Build the service using the supplied URL or its local default."""
return StreamLLMService(base_url=base_url or "http://localhost:11434/v1")