matrix_chatgpt_bot/src/main.py

107 lines
3.9 KiB
Python
Raw Normal View History

2023-04-13 15:22:24 +00:00
import asyncio
2023-03-05 14:07:25 +00:00
import json
2023-04-10 02:52:18 +00:00
import os
2023-06-05 03:27:37 +00:00
from pathlib import Path
2023-09-13 07:27:34 +00:00
import signal
import sys
2023-09-13 06:36:35 +00:00
2023-03-05 14:07:25 +00:00
from bot import Bot
2023-04-10 02:52:18 +00:00
from log import getlogger
2023-03-05 14:07:25 +00:00
2023-04-10 02:52:18 +00:00
logger = getlogger()
2023-03-05 14:07:25 +00:00
2023-04-11 05:42:43 +00:00
2023-03-05 14:07:25 +00:00
async def main():
2023-04-20 07:39:14 +00:00
need_import_keys = False
2023-06-05 03:27:37 +00:00
config_path = Path(os.path.dirname(__file__)).parent / "config.json"
if os.path.isfile(config_path):
2023-09-13 07:27:34 +00:00
try:
fp = open(config_path, encoding="utf8")
config = json.load(fp)
except Exception:
logger.error("config.json load error, please check the file")
sys.exit(1)
2023-04-11 05:42:43 +00:00
matrix_bot = Bot(
homeserver=config.get("homeserver"),
user_id=config.get("user_id"),
password=config.get("password"),
device_id=config.get("device_id"),
room_id=config.get("room_id"),
import_keys_path=config.get("import_keys_path"),
import_keys_password=config.get("import_keys_password"),
2023-09-13 07:27:34 +00:00
openai_api_key=config.get("openai_api_key"),
gpt_api_endpoint=config.get("gpt_api_endpoint"),
gpt_model=config.get("gpt_model"),
2023-09-17 15:48:21 +00:00
max_tokens=config.get("max_tokens"),
top_p=config.get("top_p"),
presence_penalty=config.get("presence_penalty"),
frequency_penalty=config.get("frequency_penalty"),
reply_count=config.get("reply_count"),
2023-09-13 07:27:34 +00:00
system_prompt=config.get("system_prompt"),
2023-09-17 15:48:21 +00:00
temperature=config.get("temperature"),
lc_admin=config.get("lc_admin"),
2023-09-17 04:27:16 +00:00
image_generation_endpoint=config.get("image_generation_endpoint"),
image_generation_backend=config.get("image_generation_backend"),
2023-09-17 15:48:21 +00:00
timeout=config.get("timeout"),
)
if (
config.get("import_keys_path")
and config.get("import_keys_password") is not None
):
2023-04-20 07:39:14 +00:00
need_import_keys = True
else:
matrix_bot = Bot(
homeserver=os.environ.get("HOMESERVER"),
user_id=os.environ.get("USER_ID"),
password=os.environ.get("PASSWORD"),
device_id=os.environ.get("DEVICE_ID"),
room_id=os.environ.get("ROOM_ID"),
import_keys_path=os.environ.get("IMPORT_KEYS_PATH"),
import_keys_password=os.environ.get("IMPORT_KEYS_PASSWORD"),
2023-09-13 07:27:34 +00:00
openai_api_key=os.environ.get("OPENAI_API_KEY"),
gpt_api_endpoint=os.environ.get("GPT_API_ENDPOINT"),
gpt_model=os.environ.get("GPT_MODEL"),
2023-09-17 15:48:21 +00:00
max_tokens=os.environ.get("MAX_TOKENS"),
top_p=os.environ.get("TOP_P"),
presence_penalty=os.environ.get("PRESENCE_PENALTY"),
frequency_penalty=os.environ.get("FREQUENCY_PENALTY"),
reply_count=os.environ.get("REPLY_COUNT"),
2023-09-13 07:27:34 +00:00
system_prompt=os.environ.get("SYSTEM_PROMPT"),
2023-09-17 15:48:21 +00:00
temperature=os.environ.get("TEMPERATURE"),
lc_admin=os.environ.get("LC_ADMIN"),
2023-09-17 04:27:16 +00:00
image_generation_endpoint=os.environ.get("IMAGE_GENERATION_ENDPOINT"),
image_generation_backend=os.environ.get("IMAGE_GENERATION_BACKEND"),
2023-09-17 15:48:21 +00:00
timeout=os.environ.get("TIMEOUT"),
)
if (
os.environ.get("IMPORT_KEYS_PATH")
and os.environ.get("IMPORT_KEYS_PASSWORD") is not None
):
2023-04-20 07:39:14 +00:00
need_import_keys = True
2023-04-10 02:52:18 +00:00
await matrix_bot.login()
2023-04-20 07:39:14 +00:00
if need_import_keys:
logger.info("start import_keys process, this may take a while...")
await matrix_bot.import_keys()
2023-09-13 07:27:34 +00:00
sync_task = asyncio.create_task(
matrix_bot.sync_forever(timeout=30000, full_state=True)
)
# handle signal interrupt
loop = asyncio.get_running_loop()
for signame in ("SIGINT", "SIGTERM"):
loop.add_signal_handler(
getattr(signal, signame),
lambda: asyncio.create_task(matrix_bot.close(sync_task)),
)
await sync_task
2023-03-05 14:07:25 +00:00
if __name__ == "__main__":
logger.info("matrix chatgpt bot start.....")
asyncio.run(main())