| 12345678910111213141516171819202122232425 |
- import httpx
- from core_domain import ChatCompletionRequestContract, ChatCompletionResponseContract
- class ModelGatewayClientError(Exception):
- pass
- class ModelGatewayClient:
- def __init__(self, *, base_url: str, timeout_seconds: float = 60.0) -> None:
- self.base_url = base_url.rstrip("/")
- self.timeout_seconds = timeout_seconds
- def create_chat_completion(
- self,
- payload: ChatCompletionRequestContract) -> ChatCompletionResponseContract:
- try:
- with httpx.Client(timeout=self.timeout_seconds) as client:
- response = client.post(
- f"{self.base_url}/models/chat-completions",
- json=payload.model_dump(mode="json"))
- response.raise_for_status()
- return ChatCompletionResponseContract.model_validate(response.json())
- except httpx.HTTPError as exc:
- raise ModelGatewayClientError(f"model-gateway-service request failed: {exc}") from exc
|