Fix !gpt command

This commit is contained in:
hibobmaster 2023-09-16 16:11:38 +08:00
parent 180826534b
commit 0197e8b3d2
Signed by: bobmaster
SSH key fingerprint: SHA256:5ZYgd8fg+PcNZNy4SzcSKu5JtqZyBF8kUhY7/k2viDk
2 changed files with 13 additions and 8 deletions

View file

@ -564,10 +564,11 @@ class Bot:
sender_id=sender_id, sender_id=sender_id,
user_message=raw_user_message, user_message=raw_user_message,
) )
except Exception: except Exception as e:
await self.send_general_error_message( await self.send_general_error_message(
room_id, reply_to_event_id, sender_id, raw_user_message room_id, reply_to_event_id, sender_id, raw_user_message
) )
logger.error(e)
# !lc command # !lc command
async def lc( async def lc(

View file

@ -4,8 +4,7 @@ A simple wrapper for the official ChatGPT API
""" """
import json import json
from typing import AsyncGenerator 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 httpx
import tiktoken import tiktoken
@ -267,11 +266,16 @@ class Chatbot:
model: str = None, model: str = None,
**kwargs, **kwargs,
) -> str: ) -> str:
async with self.aclient.post( response = await self.aclient.post(
url=self.api_url, url=self.api_url,
json={ json={
"model": model or self.engine, "model": model or self.engine,
"messages": prompt, "messages": [
{
"role": role,
"content": prompt,
}
],
# kwargs # kwargs
"temperature": kwargs.get("temperature", self.temperature), "temperature": kwargs.get("temperature", self.temperature),
"top_p": kwargs.get("top_p", self.top_p), "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)}"}, headers={"Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"},
timeout=kwargs.get("timeout", self.timeout), timeout=kwargs.get("timeout", self.timeout),
) as response: )
resp = await response.read() resp = response.json()
return json.loads(resp)["choices"][0]["message"]["content"] return resp["choices"][0]["message"]["content"]