| 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 | |
| 96 | await asyncio.sleep(self.retry_pause_seconds) |
| 97 | retry_count += 1 |
| 98 | |
| 99 | @override |
| 100 | async def __aenter__(self) -> Self: |