Some more fixes, reduce retries.
This commit is contained in:
parent
ddc44bb9da
commit
93c5758e02
@ -83,7 +83,7 @@ class AIResponder(object):
|
|||||||
return messages
|
return messages
|
||||||
|
|
||||||
async def draw(self, description: str) -> BytesIO:
|
async def draw(self, description: str) -> BytesIO:
|
||||||
for _ in range(7):
|
for _ in range(3):
|
||||||
try:
|
try:
|
||||||
response = await openai.Image.acreate(prompt=description, n=1, size="512x512")
|
response = await openai.Image.acreate(prompt=description, n=1, size="512x512")
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
@ -133,6 +133,7 @@ class AIResponder(object):
|
|||||||
result = await openai.ChatCompletion.acreate(model=self.config["model"],
|
result = await openai.ChatCompletion.acreate(model=self.config["model"],
|
||||||
messages=messages,
|
messages=messages,
|
||||||
temperature=self.config["temperature"],
|
temperature=self.config["temperature"],
|
||||||
|
max_tokens=self.config["max-tokens"],
|
||||||
top_p=self.config["top-p"],
|
top_p=self.config["top-p"],
|
||||||
presence_penalty=self.config["presence-penalty"],
|
presence_penalty=self.config["presence-penalty"],
|
||||||
frequency_penalty=self.config["frequency-penalty"])
|
frequency_penalty=self.config["frequency-penalty"])
|
||||||
@ -155,21 +156,21 @@ class AIResponder(object):
|
|||||||
return answer
|
return answer
|
||||||
messages = [{"role": "system", "content": self.config["fix-description"]},
|
messages = [{"role": "system", "content": self.config["fix-description"]},
|
||||||
{"role": "user", "content": answer}]
|
{"role": "user", "content": answer}]
|
||||||
for _ in range(3):
|
try:
|
||||||
try:
|
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)
|
max_tokens=2048)
|
||||||
logging.info(f"got this message as fix:\n{pp(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:end + 1]
|
||||||
logging.info(f"fixed answer:\n{pp(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)}")
|
||||||
return answer
|
return answer
|
||||||
|
|
||||||
def update_history(self, question: Dict[str, Any], answer: Dict[str, Any], limit: int) -> None:
|
def update_history(self, question: Dict[str, Any], answer: Dict[str, Any], limit: int) -> None:
|
||||||
@ -185,7 +186,8 @@ class AIResponder(object):
|
|||||||
limit = self.config["history-limit"]
|
limit = self.config["history-limit"]
|
||||||
if self.short_path(message, limit):
|
if self.short_path(message, limit):
|
||||||
return AIResponse(None, False, None, None, False)
|
return AIResponse(None, False, None, None, False)
|
||||||
for _ in range(5):
|
retries = 3
|
||||||
|
while retries > 0:
|
||||||
messages = self._message(message, limit)
|
messages = self._message(message, limit)
|
||||||
logging.info(f"try to send this messages:\n{pp(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)
|
||||||
@ -200,8 +202,10 @@ class AIResponder(object):
|
|||||||
response = parse_response(answer['content'])
|
response = parse_response(answer['content'])
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
logging.error(f"failed to parse the fixed answer: {pp(err)}\n{repr(answer['content'])}")
|
logging.error(f"failed to parse the fixed answer: {pp(err)}\n{repr(answer['content'])}")
|
||||||
|
retries -= 1
|
||||||
continue
|
continue
|
||||||
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):
|
||||||
|
retries -= 1
|
||||||
continue
|
continue
|
||||||
logging.info(f"got this answer:\n{pp(response)}")
|
logging.info(f"got this answer:\n{pp(response)}")
|
||||||
self.update_history(messages[-1], answer, limit)
|
self.update_history(messages[-1], answer, limit)
|
||||||
|
|||||||
@ -21,7 +21,7 @@ class TestBotBase(unittest.IsolatedAsyncioTestCase):
|
|||||||
self.config_data = {
|
self.config_data = {
|
||||||
"openai-token": os.environ['OPENAI_TOKEN'],
|
"openai-token": os.environ['OPENAI_TOKEN'],
|
||||||
"model": "gpt-4",
|
"model": "gpt-4",
|
||||||
"max_tokens": 1024,
|
"max-tokens": 1024,
|
||||||
"temperature": 0.9,
|
"temperature": 0.9,
|
||||||
"top-p": 1.0,
|
"top-p": 1.0,
|
||||||
"presence-penalty": 1.0,
|
"presence-penalty": 1.0,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user