add retry for xiaoniu api requests
This commit is contained in:
@@ -18,6 +18,8 @@ timeout_seconds = 45
|
|||||||
temperature = 0.35
|
temperature = 0.35
|
||||||
max_tokens = 120
|
max_tokens = 120
|
||||||
stream = true
|
stream = true
|
||||||
|
max_retries = 3
|
||||||
|
retry_delay_seconds = 1.0
|
||||||
|
|
||||||
[mode]
|
[mode]
|
||||||
group_default_mode = "social"
|
group_default_mode = "social"
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
@@ -18,6 +19,8 @@ class LLMClient:
|
|||||||
self.temperature = float(self.config.get("temperature", 0.7))
|
self.temperature = float(self.config.get("temperature", 0.7))
|
||||||
self.max_tokens = int(self.config.get("max_tokens", 500))
|
self.max_tokens = int(self.config.get("max_tokens", 500))
|
||||||
self.stream = bool(self.config.get("stream", True))
|
self.stream = bool(self.config.get("stream", True))
|
||||||
|
self.max_retries = max(int(self.config.get("max_retries", 3) or 3), 1)
|
||||||
|
self.retry_delay_seconds = float(self.config.get("retry_delay_seconds", 1.0) or 1.0)
|
||||||
self.last_error = ""
|
self.last_error = ""
|
||||||
|
|
||||||
def chat(
|
def chat(
|
||||||
@@ -61,25 +64,34 @@ class LLMClient:
|
|||||||
if self.api_key:
|
if self.api_key:
|
||||||
headers["Authorization"] = f"Bearer {self.api_key}"
|
headers["Authorization"] = f"Bearer {self.api_key}"
|
||||||
|
|
||||||
try:
|
for attempt in range(1, self.max_retries + 1):
|
||||||
if self.stream:
|
try:
|
||||||
return self._chat_streaming(payload, headers)
|
if self.stream:
|
||||||
response = requests.post(
|
text = self._chat_streaming(payload, headers)
|
||||||
f"{self.base_url}/{self.endpoint}",
|
else:
|
||||||
json=payload,
|
text = self._chat_non_streaming(payload, headers)
|
||||||
headers=headers,
|
if text:
|
||||||
timeout=self.timeout_seconds,
|
return text
|
||||||
)
|
except Exception as exc:
|
||||||
response.raise_for_status()
|
self.last_error = f"request_failed:attempt_{attempt}:{exc}"
|
||||||
data = response.json()
|
if attempt < self.max_retries:
|
||||||
text = self._extract_text(data)
|
time.sleep(self.retry_delay_seconds * attempt)
|
||||||
if text:
|
return ""
|
||||||
return text
|
|
||||||
self.last_error = f"empty_model_output:{self.model}"
|
def _chat_non_streaming(self, payload: Dict, headers: Dict[str, str]) -> str:
|
||||||
return ""
|
response = requests.post(
|
||||||
except Exception as exc:
|
f"{self.base_url}/{self.endpoint}",
|
||||||
self.last_error = f"request_failed:{exc}"
|
json=payload,
|
||||||
return ""
|
headers=headers,
|
||||||
|
timeout=self.timeout_seconds,
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
text = self._extract_text(data)
|
||||||
|
if text:
|
||||||
|
return text
|
||||||
|
self.last_error = f"empty_model_output:{self.model}"
|
||||||
|
return ""
|
||||||
|
|
||||||
def _chat_streaming(self, payload: Dict, headers: Dict[str, str]) -> str:
|
def _chat_streaming(self, payload: Dict, headers: Dict[str, str]) -> str:
|
||||||
chunks: List[str] = []
|
chunks: List[str] = []
|
||||||
|
|||||||
Reference in New Issue
Block a user