Solve new API thing differently.
This commit is contained in:
parent
a2c7aec1e3
commit
7bcadecb17
@ -270,7 +270,7 @@ class AIResponder(object):
|
||||
return True
|
||||
return False
|
||||
|
||||
async def _acreate(self, messages: List[Dict[str, Any]], limit: int) -> Tuple[Optional[openai.types.chat.ChatCompletionMessage], int]:
|
||||
async def _acreate(self, messages: List[Dict[str, Any]], limit: int) -> Tuple[Optional[Dict[str, Any]], int]:
|
||||
model = self.config["model"]
|
||||
try:
|
||||
result = await self.client.chat.completions.create(model=model,
|
||||
@ -280,7 +280,8 @@ class AIResponder(object):
|
||||
top_p=self.config["top-p"],
|
||||
presence_penalty=self.config["presence-penalty"],
|
||||
frequency_penalty=self.config["frequency-penalty"])
|
||||
answer = result.choices[0].message
|
||||
answer_obj = result.choices[0].message
|
||||
answer = {'content': answer_obj.content, 'role': answer_obj.role}
|
||||
self.rate_limit_backoff = exponential_backoff()
|
||||
logging.info(f"generated response {result.usage}: {repr(answer)}")
|
||||
return answer, limit
|
||||
@ -390,16 +391,16 @@ class AIResponder(object):
|
||||
|
||||
# Attempt to parse the AI's response
|
||||
try:
|
||||
response = parse_json(answer.content)
|
||||
response = parse_json(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'])
|
||||
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_json(answer.content)
|
||||
response = parse_json(answer['content'])
|
||||
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
|
||||
|
||||
@ -414,7 +415,7 @@ class AIResponder(object):
|
||||
|
||||
# Post-process the message and update the answer's content
|
||||
answer_message = await self.post_process(message, response)
|
||||
answer.content = str(answer_message)
|
||||
answer['content'] = str(answer_message)
|
||||
|
||||
# Update message history
|
||||
self.update_history(messages[-1], answer, limit, message.historise_question)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user