Comment the code a bit
This commit is contained in:
@@ -231,34 +231,58 @@ class AIResponder(object):
|
||||
pickle.dump(self.history, fd)
|
||||
|
||||
async def send(self, message: AIMessage) -> AIResponse:
|
||||
# Get the history limit from the configuration
|
||||
limit = self.config["history-limit"]
|
||||
|
||||
# Check if a short path applies, return an empty AIResponse if it does
|
||||
if self.short_path(message, limit):
|
||||
return AIResponse(None, False, None, None, None, False)
|
||||
|
||||
# Number of retries for sending the message
|
||||
retries = 3
|
||||
|
||||
while retries > 0:
|
||||
# Get the message queue
|
||||
messages = self._message(message, limit)
|
||||
logging.info(f"try to send this messages:\n{pp(messages)}")
|
||||
|
||||
# Attempt to send the message to the AI
|
||||
answer, limit = await self._acreate(messages, limit)
|
||||
|
||||
if answer is None:
|
||||
continue
|
||||
|
||||
# Attempt to parse the AI's response
|
||||
try:
|
||||
response = parse_response(answer['content'])
|
||||
except Exception as err:
|
||||
logging.warning(f"failed to parse the answer: {pp(err)}\n{repr(answer['content'])}")
|
||||
answer['content'] = await self.fix(answer['content'])
|
||||
|
||||
# Retry parsing the fixed content
|
||||
try:
|
||||
response = parse_response(answer['content'])
|
||||
except Exception as err:
|
||||
logging.error(f"failed to parse the fixed answer: {pp(err)}\n{repr(answer['content'])}")
|
||||
retries -= 1
|
||||
continue
|
||||
if type(response.get('picture')) not in (type(None), str):
|
||||
|
||||
# Check if the response has the correct picture format
|
||||
if not isinstance(response.get("picture"), (type(None), str)):
|
||||
logging.warning(f"picture key is wrong in response: {pp(response)}")
|
||||
retries -= 1
|
||||
continue
|
||||
|
||||
# Post-process the message and update the answer's content
|
||||
answer_message = await self.post_process(message, response)
|
||||
answer['content'] = str(answer_message)
|
||||
|
||||
# Update message history
|
||||
self.update_history(messages[-1], answer, limit)
|
||||
logging.info(f"got this answer:\n{str(answer_message)}")
|
||||
|
||||
# Return the updated answer message
|
||||
return answer_message
|
||||
|
||||
# Raise an error if the process failed after all retries
|
||||
raise RuntimeError("Failed to generate answer after multiple retries")
|
||||
|
||||
Reference in New Issue
Block a user