MCPcopy Index your code
hub / github.com/microsoft/TypeChat / HttpxLanguageModel

Class HttpxLanguageModel

python/src/typechat/_internal/model.py:38–111  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36]
37
38class HttpxLanguageModel(TypeChatLanguageModel, AsyncContextManager):
39 url: str
40 headers: dict[str, str]
41 default_params: dict[str, str]
42 # Specifies the maximum number of retry attempts.
43 max_retry_attempts: int = 3
44 # Specifies the delay before retrying in milliseconds.
45 retry_pause_seconds: float = 1.0
46 # Specifies how long a request should wait in seconds
47 # before timing out with a Failure.
48 timeout_seconds = 10
49 _async_client: httpx.AsyncClient
50
51 def __init__(self, url: str, headers: dict[str, str], default_params: dict[str, str]):
52 super().__init__()
53 self.url = url
54 self.headers = headers
55 self.default_params = default_params
56 self._async_client = httpx.AsyncClient()
57
58 @override
59 async def complete(self, prompt: str | list[PromptSection]) -> Success[str] | Failure:
60 headers = {
61 "Content-Type": "application/json",
62 **self.headers,
63 }
64
65 if isinstance(prompt, str):
66 prompt = [{"role": "user", "content": prompt}]
67
68 body = {
69 **self.default_params,
70 "messages": prompt,
71 "temperature": 0.0,
72 "n": 1,
73 }
74 retry_count = 0
75 while True:
76 try:
77 response = await self._async_client.post(
78 self.url,
79 headers=headers,
80 json=body,
81 timeout=self.timeout_seconds
82 )
83 if response.is_success:
84 json_result = cast(
85 dict[Literal["choices"], list[dict[Literal["message"], PromptSection]]],
86 response.json()
87 )
88 return Success(json_result["choices"][0]["message"]["content"] or "")
89
90 if response.status_code not in _TRANSIENT_ERROR_CODES or retry_count >= self.max_retry_attempts:
91 return Failure(f"REST API error {response.status_code}: {response.reason_phrase}")
92 except Exception as e:
93 if retry_count >= self.max_retry_attempts:
94 return Failure(str(e) or f"{repr(e)} raised from within internal TypeChat language model.")
95

Calls

no outgoing calls

Tested by

no test coverage detected