Commit 93d6973

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-06-16 15:41:12
feat(gpt): customize GPT chats via environment variables
1 parent c9dc907
Changed files (1)
src
llm
src/llm/gpt.py
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
+import os
 
 from loguru import logger
 from pyrogram.client import Client
@@ -18,7 +19,7 @@ from messages.parser import parse_msg
 from messages.progress import modify_progress
 from messages.sender import send2tg
 from messages.utils import count_without_entities, equal_prefix, startswith_prefix
-from utils import slim_cid, strings_list
+from utils import slim_cid, strings_list, true
 
 HELP = f"""🤖**GPT对话**
 `{PREFIX.GPT}` 后接提示词即可与GPT对话
@@ -49,6 +50,24 @@ def is_gpt_conversation(minfo: dict) -> bool:
     if BOT_TIPS in minfo["text"]:
         return False
 
+    """Customization via Environment Variables.
+    Useful for running multiple bots in a same chat. (Multiple LLM providers)
+
+    GPT_{cid}_BAN_{uid}=1 : Ban user for using AI chat
+    GPT_{cid}_ALLOW_USERS={uids} : Only allow users (comma separated userid) for using AI chat.
+    GPT_{cid}_IGNORE_REPLY=1 : Ignore messages that is replying to another message
+    GPT_{cid}_IGNORE_PREFIX=/gpt,/ds : Ignore prefix for specific chat ids
+    """
+    cid = slim_cid(minfo["cid"])
+    if (uids := os.getenv(f"GPT_{cid}_ALLOW_USERS")) and str(minfo["uid"]) not in strings_list(uids):
+        return False
+    if true(os.getenv(f"GPT_{cid}_BAN_{minfo['uid']}")):
+        return False
+    if true(os.getenv(f"GPT_{cid}_IGNORE_REPLY")) and minfo["reply_mid"]:
+        return False
+    if startswith_prefix(minfo["text"], prefix=os.getenv(f"GPT_{cid}_IGNORE_PREFIX", "")):
+        return False
+
     # starts with /prefix
     if startswith_prefix(minfo["text"], prefix=[PREFIX.GPT, PREFIX.GENIMG]):
         return True
@@ -110,7 +129,7 @@ async def gpt_response(
         dict: {"texts": str, "thoughts": str, "prefix": str, "model_name": str, "sent_messages": list[Message]}
     """
     # ruff: noqa: RET502, RET503
-    info = parse_msg(message)
+    info = parse_msg(message, use_cache=False)
     # send docs if message == "/ai"
     if info["mtype"] == "text" and equal_prefix(info["text"], prefix=PREFIX.GPT):
         if not message.reply_to_message:  # without reply