From 0197e8b3d226438f4b3680f63b834cdc803a73a6 Mon Sep 17 00:00:00 2001 From: hibobmaster <32976627+hibobmaster@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:11:38 +0800 Subject: [PATCH] Fix !gpt command --- src/bot.py | 3 ++- src/gptbot.py | 18 +++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/bot.py b/src/bot.py index 74f498e..e1e7e95 100644 --- a/src/bot.py +++ b/src/bot.py @@ -564,10 +564,11 @@ class Bot: sender_id=sender_id, user_message=raw_user_message, ) - except Exception: + except Exception as e: await self.send_general_error_message( room_id, reply_to_event_id, sender_id, raw_user_message ) + logger.error(e) # !lc command async def lc( diff --git a/src/gptbot.py b/src/gptbot.py index 8750cd5..454e0a1 100644 --- a/src/gptbot.py +++ b/src/gptbot.py @@ -4,8 +4,7 @@ A simple wrapper for the official ChatGPT API """ import json from typing import AsyncGenerator -from tenacity import retry, stop_after_attempt, wait_random_exponential - +from tenacity import retry, wait_random_exponential, stop_after_attempt import httpx import tiktoken @@ -267,11 +266,16 @@ class Chatbot: model: str = None, **kwargs, ) -> str: - async with self.aclient.post( + response = await self.aclient.post( url=self.api_url, json={ "model": model or self.engine, - "messages": prompt, + "messages": [ + { + "role": role, + "content": prompt, + } + ], # kwargs "temperature": kwargs.get("temperature", self.temperature), "top_p": kwargs.get("top_p", self.top_p), @@ -287,6 +291,6 @@ class Chatbot: }, headers={"Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"}, timeout=kwargs.get("timeout", self.timeout), - ) as response: - resp = await response.read() - return json.loads(resp)["choices"][0]["message"]["content"] + ) + resp = response.json() + return resp["choices"][0]["message"]["content"]