Improve log output.
This commit is contained in:
parent
cceb0e3ea3
commit
7f3cb66043
@ -11,6 +11,12 @@ from pprint import pformat
|
|||||||
from typing import Optional, List, Dict, Any, Tuple
|
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):
|
class AIMessageBase(object):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
pass
|
pass
|
||||||
@ -142,13 +148,13 @@ class AIResponder(object):
|
|||||||
result = await openai.ChatCompletion.acreate(model=self.config["fix-model"],
|
result = await openai.ChatCompletion.acreate(model=self.config["fix-model"],
|
||||||
messages=messages,
|
messages=messages,
|
||||||
temperature=0.2)
|
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']
|
response = result['choices'][0]['message']['content']
|
||||||
start, end = response.find("```"), response.rfind("```")
|
start, end = response.find("```"), response.rfind("```")
|
||||||
if start == -1 or end == -1 or (start + 3) >= end:
|
if start == -1 or end == -1 or (start + 3) >= end:
|
||||||
return answer
|
return answer
|
||||||
response = response[start + 3:end]
|
response = response[start + 3:end]
|
||||||
logging.info(f"fixed answer:\n{pformat(response)}")
|
logging.info(f"fixed answer:\n{pp(response)}")
|
||||||
return response
|
return response
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
logging.warning(f"failed to execute a fix for the answer: {repr(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)
|
return AIResponse(None, False, None, None, False)
|
||||||
for _ in range(5):
|
for _ in range(5):
|
||||||
messages = self._message(message, limit)
|
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)
|
answer, limit = await self._acreate(messages, limit)
|
||||||
if answer is None:
|
if answer is None:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
response = json.loads(answer['content'])
|
response = json.loads(answer['content'])
|
||||||
except Exception as err:
|
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'])
|
answer['content'] = await self.fix(answer['content'])
|
||||||
try:
|
try:
|
||||||
response = json.loads(answer['content'])
|
response = json.loads(answer['content'])
|
||||||
except Exception as err:
|
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)
|
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):
|
if 'hack' not in response or type(response.get('picture', None)) not in (type(None), str):
|
||||||
continue
|
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)
|
self.update_history(messages[-1], answer, limit)
|
||||||
return await self.post_process(response)
|
return await self.post_process(response)
|
||||||
raise RuntimeError("Failed to generate answer after multiple retries")
|
raise RuntimeError("Failed to generate answer after multiple retries")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user