Open chunhualiao opened 1 month ago
This situation occurs because the API call returned None. Autosurvey requires high concurrent API calls in a short period. You can consider the following solutions:
API_model
class in src/model.py
to increase the max_try
parameter in __req
, or extend the code to support API calls from multiple keys.I used a combination of two hacks. The chance of the error now is much less.
def batch_chat(self, text_batch, temperature=0):
+ max_threads=5 # limit max concurrent threads using model API
+
res_l = ['No response'] * len(text_batch)
thread_l = []
for i, text in zip(range(len(text_batch)), text_batch):
+
+ # Wait for a thread to finish if the maximum number is reached
+ while len(thread_l) >= max_threads:
+ for t in thread_l:
+ if not t .is_alive():
+ thread_l.remove(t)
+ time.sleep(0.3) # Short delay to avoid busy-waiting
+
+ try:
+ response = requests.request("POST", url, headers=headers, data=payload)
+ response.raise_for_status() # Check for HTTP errors
+
+ response_data = json.loads(response.text)
+
+ # Type Check AFTER successful JSON parsing
+ if 'choices' in response_data and len(response_data['choices']) > 0:
+ content = response_data['choices'][0]['message']['content']
+ if isinstance(content, str):
+ return content
+ else:
+ error_msg = f"LLM API returned unexpected content type: {type(content)}. Content: {content}"
+ print(error_msg)
+ logging.error(error_msg)
+ raise TypeError(error_msg)
+ else:
+ error_msg = f"LLM API response missing 'choices' or empty 'choices' list: {response_data}"
+ print(error_msg)
+ logging.error(error_msg)
+ raise ValueError(error_msg)
+
+ except requests.exceptions.RequestException as e:
+ logging.error(f"Request error during API request: {e}, Retry attempt: {_ + 1}")
+ except json.JSONDecodeError as e:
+ logging.error(f"JSON decode error: {e}, Retry attempt: {_ + 1}")
+
+ time.sleep(0.2) # Short delay before trying next time
+
+ # If all retries fail
+ logging.error("All API request retries failed.")
+ return None
Ubuntu 22.04.4 LTS
Python 3.10.12
/workspace/AutoSurvey# python main.py --topic "Large language models for automatic writing papers" \