model_gateway_client.py 1022 B

12345678910111213141516171819202122232425262728
  1. import httpx
  2. from core_domain import ChatCompletionRequestContract, ChatCompletionResponseContract
  3. class ModelGatewayClientError(Exception):
  4. pass
  5. class ModelGatewayClient:
  6. def __init__(self, *, base_url: str, timeout_seconds: float = 60.0) -> None:
  7. self.base_url = base_url.rstrip("/")
  8. self.timeout_seconds = timeout_seconds
  9. def create_chat_completion(
  10. self,
  11. payload: ChatCompletionRequestContract,
  12. ) -> ChatCompletionResponseContract:
  13. try:
  14. with httpx.Client(timeout=self.timeout_seconds) as client:
  15. response = client.post(
  16. f"{self.base_url}/models/chat-completions",
  17. json=payload.model_dump(mode="json"),
  18. )
  19. response.raise_for_status()
  20. return ChatCompletionResponseContract.model_validate(response.json())
  21. except httpx.HTTPError as exc:
  22. raise ModelGatewayClientError(f"model-gateway-service request failed: {exc}") from exc