Improve log output.

This commit is contained in:
OK 2023-03-29 17:27:32 +02:00
parent cceb0e3ea3
commit 7f3cb66043

View File

@ -11,6 +11,12 @@ from pprint import pformat
from typing import Optional, List, Dict, Any, Tuple
def pp(*args, **kw):
if 'width' not in kw:
kw['width'] = 300
return pformat(*args, **kw)
class AIMessageBase(object):
def __init__(self) -> None:
pass
@ -142,13 +148,13 @@ class AIResponder(object):
result = await openai.ChatCompletion.acreate(model=self.config["fix-model"],
messages=messages,
temperature=0.2)
logging.info(f"got this message as fix:\n{pformat(result['choices'][0]['message']['content'])}")
logging.info(f"got this message as fix:\n{pp(result['choices'][0]['message']['content'])}")
response = result['choices'][0]['message']['content']
start, end = response.find("```"), response.rfind("```")
if start == -1 or end == -1 or (start + 3) >= end:
return answer
response = response[start + 3:end]
logging.info(f"fixed answer:\n{pformat(response)}")
logging.info(f"fixed answer:\n{pp(response)}")
return response
except Exception as err:
logging.warning(f"failed to execute a fix for the answer: {repr(err)}")
@ -169,23 +175,23 @@ class AIResponder(object):
return AIResponse(None, False, None, None, False)
for _ in range(5):
messages = self._message(message, limit)
logging.info(f"try to send this messages:\n{pformat(messages)}")
logging.info(f"try to send this messages:\n{pp(messages)}")
answer, limit = await self._acreate(messages, limit)
if answer is None:
continue
try:
response = json.loads(answer['content'])
except Exception as err:
logging.warning(f"failed to parse the answer: {pformat(err)}\n{repr(answer['content'])}")
logging.warning(f"failed to parse the answer: {pp(err)}\n{repr(answer['content'])}")
answer['content'] = await self.fix(answer['content'])
try:
response = json.loads(answer['content'])
except Exception as err:
logging.error(f"failed to parse the answer: {pformat(err)}\n{repr(answer['content'])}")
logging.error(f"failed to parse the answer: {pp(err)}\n{repr(answer['content'])}")
return AIResponse(None, False, f"ERROR: I could not parse this answer: {repr(answer['content'])}", None, False)
if 'hack' not in response or type(response.get('picture', None)) not in (type(None), str):
continue
logging.info(f"got this answer:\n{pformat(response)}")
logging.info(f"got this answer:\n{pp(response)}")
self.update_history(messages[-1], answer, limit)
return await self.post_process(response)
raise RuntimeError("Failed to generate answer after multiple retries")