2023-03-05 14:07:25 +00:00
|
|
|
import sys
|
2023-03-10 00:29:12 +00:00
|
|
|
import asyncio
|
2023-03-05 14:07:25 +00:00
|
|
|
import re
|
|
|
|
import os
|
|
|
|
from typing import Optional
|
|
|
|
from nio import AsyncClient, MatrixRoom, RoomMessageText, LoginResponse, AsyncClientConfig
|
|
|
|
from nio.store.database import SqliteStore
|
|
|
|
from ask_gpt import ask
|
|
|
|
from send_message import send_room_message
|
|
|
|
from v3 import Chatbot
|
2023-03-10 13:43:18 +00:00
|
|
|
from log import getlogger
|
2023-03-10 10:53:51 +00:00
|
|
|
"""
|
|
|
|
free api_endpoint from https://github.com/ayaka14732/ChatGPTAPIFree
|
|
|
|
"""
|
|
|
|
api_endpoint_list = {
|
|
|
|
"free": "https://chatgpt-api.shn.hk/v1/",
|
|
|
|
"paid": "https://api.openai.com/v1/chat/completions"
|
|
|
|
}
|
2023-03-10 13:43:18 +00:00
|
|
|
logger = getlogger()
|
2023-03-10 10:53:51 +00:00
|
|
|
|
2023-03-05 14:07:25 +00:00
|
|
|
|
|
|
|
class Bot:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
homeserver: str,
|
|
|
|
user_id: str,
|
|
|
|
password: str,
|
|
|
|
device_id: str,
|
2023-03-10 00:29:12 +00:00
|
|
|
api_key: Optional[str] = "",
|
2023-03-05 14:07:25 +00:00
|
|
|
room_id: Optional[str] = '',
|
|
|
|
):
|
|
|
|
self.homeserver = homeserver
|
|
|
|
self.user_id = user_id
|
|
|
|
self.password = password
|
|
|
|
self.device_id = device_id
|
|
|
|
self.room_id = room_id
|
|
|
|
self.api_key = api_key
|
|
|
|
# initialize AsyncClient object
|
|
|
|
self.store_path = os.getcwd()
|
|
|
|
self.config = AsyncClientConfig(store=SqliteStore,
|
|
|
|
store_name="bot",
|
|
|
|
store_sync_tokens=True,
|
|
|
|
)
|
|
|
|
self.client = AsyncClient(self.homeserver, user=self.user_id, device_id=self.device_id,
|
|
|
|
config=self.config, store_path=self.store_path)
|
|
|
|
# regular expression to match keyword [!gpt {prompt}] [!chat {prompt}]
|
|
|
|
self.gpt_prog = re.compile(r"^\s*!gpt\s*(.+)$")
|
|
|
|
self.chat_prog = re.compile(r"^\s*!chat\s*(.+)$")
|
2023-03-10 10:53:51 +00:00
|
|
|
# initialize chatbot and api_endpoint
|
2023-03-10 00:29:12 +00:00
|
|
|
if self.api_key != '':
|
|
|
|
self.chatbot = Chatbot(api_key=self.api_key)
|
2023-03-05 14:07:25 +00:00
|
|
|
|
2023-03-10 10:53:51 +00:00
|
|
|
self.api_endpoint = api_endpoint_list['paid']
|
|
|
|
# request header for !gpt command
|
|
|
|
self.headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Authorization": "Bearer " + self.api_key,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
self.headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
}
|
|
|
|
|
2023-03-05 14:07:25 +00:00
|
|
|
# message_callback event
|
|
|
|
async def message_callback(self, room: MatrixRoom, event: RoomMessageText) -> None:
|
2023-03-10 13:43:18 +00:00
|
|
|
# print info to console
|
|
|
|
print(
|
|
|
|
f"Message received in room {room.display_name}\n"
|
|
|
|
f"{room.user_name(event.sender)} | {event.body}"
|
|
|
|
)
|
|
|
|
|
2023-03-10 04:42:33 +00:00
|
|
|
# remove newline character from event.body
|
|
|
|
event.body = re.sub("\r\n|\r|\n", " ", event.body)
|
2023-03-09 16:21:12 +00:00
|
|
|
if self.room_id == '':
|
|
|
|
room_id = room.room_id
|
|
|
|
else:
|
|
|
|
room_id = self.room_id
|
2023-03-05 14:07:25 +00:00
|
|
|
# chatgpt
|
|
|
|
n = self.chat_prog.match(event.body)
|
|
|
|
if n:
|
2023-03-10 00:29:12 +00:00
|
|
|
if self.api_key != '':
|
|
|
|
# sending typing state
|
|
|
|
await self.client.room_typing(room_id)
|
|
|
|
prompt = n.group(1)
|
|
|
|
try:
|
|
|
|
# run synchronous function in different thread
|
|
|
|
text = await asyncio.to_thread(self.chatbot.ask, prompt)
|
|
|
|
text = text.strip()
|
|
|
|
await send_room_message(self.client, room_id, send_text=text)
|
|
|
|
except Exception as e:
|
2023-03-10 13:43:18 +00:00
|
|
|
logger.error("Error", exc_info=True)
|
2023-03-10 00:29:12 +00:00
|
|
|
print(f"Error: {e}")
|
|
|
|
pass
|
|
|
|
else:
|
2023-03-10 13:43:18 +00:00
|
|
|
logger.warning("No API_KEY provided")
|
2023-03-10 00:29:12 +00:00
|
|
|
await send_room_message(self.client, room_id, send_text="API_KEY not provided")
|
2023-03-05 14:07:25 +00:00
|
|
|
|
2023-03-10 11:19:49 +00:00
|
|
|
m = self.gpt_prog.match(event.body)
|
|
|
|
if m:
|
|
|
|
# sending typing state
|
|
|
|
await self.client.room_typing(room_id)
|
|
|
|
prompt = m.group(1)
|
|
|
|
try:
|
|
|
|
# 默认等待30s
|
|
|
|
text = await asyncio.wait_for(ask(prompt, self.api_endpoint, self.headers), timeout=30)
|
|
|
|
except TimeoutError:
|
2023-03-10 13:43:18 +00:00
|
|
|
logger.error("timeoutException", exc_info=True)
|
|
|
|
text = "Timeout error"
|
2023-03-10 11:19:49 +00:00
|
|
|
|
|
|
|
text = text.strip()
|
|
|
|
await send_room_message(self.client, room_id, send_text=text)
|
|
|
|
|
2023-03-05 14:07:25 +00:00
|
|
|
# bot login
|
|
|
|
async def login(self) -> None:
|
|
|
|
resp = await self.client.login(password=self.password)
|
|
|
|
if not isinstance(resp, LoginResponse):
|
|
|
|
print(f"Login Failed: {resp}")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# sync messages in the room
|
|
|
|
async def sync_forever(self, timeout=30000):
|
|
|
|
self.client.add_event_callback(self.message_callback, RoomMessageText)
|
|
|
|
await self.client.sync_forever(timeout=timeout, full_state=True)
|