From d9525b032fa59f93327a4c870104d206a45c8f2e Mon Sep 17 00:00:00 2001 From: yym68686 Date: Wed, 20 Dec 2023 18:42:40 +0800 Subject: [PATCH] 1. Fixed bug: potential timeout issues 2. Update README document 3. Add insufficient balance error handling 4. Fixed bug: potential webhook unresponsive issues --- README.md | 2 +- bot.py | 4 ++-- config.py | 6 +++++- utils/chatgpt2api.py | 8 ++++++-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0a8a37ee..1711915f 100644 --- a/README.md +++ b/README.md @@ -36,9 +36,9 @@ Join the [Telegram Group](https://t.me/+_01cz9tAkUc1YzZl) chat to share your use | ---------------------- | ------------------------------------------------------------ | | **BOT_TOKEN (required)** | Telegram bot token. Create a bot on [BotFather](https://t.me/BotFather) to get the BOT_TOKEN. | | **API (required)** | OpenAI or third-party API key. | +| GPT_ENGINE (optional) | Set the default QA model; the default is:`gpt-4-1106-preview`. This item can be freely switched using the bot's "info" command, and it doesn't need to be set in principle. | | WEB_HOOK (optional) | Whenever the telegram bot receives a user message, the message will be passed to WEB_HOOK, where the bot will listen to it and process the received messages in a timely manner. | | API_URL(optional) | If you are using the OpenAI official API, you don't need to set this. If you using a third-party API, you need to fill in the third-party proxy website. The default is: https://api.openai.com/v1/chat/completions | -| GPT_ENGINE (optional) | Set the default QA model; the default is:`gpt-3.5-turbo`. This item can be freely switched using the bot's "info" command, and it doesn't need to be set in principle. | | NICK (optional) | The default is empty, and NICK is the name of the bot. The bot will only respond when the message starts with NICK that the user inputs, otherwise the bot will respond to any message. Especially in group chats, if there is no NICK, the bot will reply to all messages. | | PASS_HISTORY (optional) | The default is true. The bot remembers the conversation history and considers the context when replying next time. If set to false, the bot will forget the conversation history and only consider the current conversation. | | GOOGLE_API_KEY (optional)| If you need to use Google search, you need to set it. If you do not set this environment variable, the bot will default to provide duckduckgo search. Create credentials in Google Cloud's [APIs & Services](https://console.cloud.google.com/apis/api/customsearch.googleapis.com) and the API Key will be GOOGLE_API_KEY on the credentials page. Google search can be queried 100 times a day, which is completely sufficient for light use. When the usage limit has been reached, the bot will automatically turn off Google search. | diff --git a/bot.py b/bot.py index d9577399..2ddb968d 100644 --- a/bot.py +++ b/bot.py @@ -554,9 +554,9 @@ async def post_init(application: Application) -> None: if WEB_HOOK: print("WEB_HOOK:", WEB_HOOK) - application.run_webhook("127.0.0.1", PORT, webhook_url=WEB_HOOK) + application.run_webhook("0.0.0.0", PORT, webhook_url=WEB_HOOK) else: # application.run_polling() time_out = 600 - application.run_polling(read_timeout=time_out, write_timeout=time_out) + application.run_polling(read_timeout=time_out, write_timeout=time_out, pool_timeout=time_out, connect_timeout=time_out) # application.run_polling(read_timeout=time_out, write_timeout=time_out, pool_timeout=time_out, connect_timeout=time_out, timeout=time_out) \ No newline at end of file diff --git a/config.py b/config.py index 6a7c7ba9..0c173f7e 100644 --- a/config.py +++ b/config.py @@ -28,7 +28,11 @@ from utils.chatgpt2api import Chatbot as GPT from utils.chatgpt2api import Imagebot, claudebot if API: - ChatGPTbot = GPT(api_key=f"{API}", engine=GPT_ENGINE, system_prompt=systemprompt, temperature=temperature) + try: + ChatGPTbot = GPT(api_key=f"{API}", engine=GPT_ENGINE, system_prompt=systemprompt, temperature=temperature) + except: + ChatGPTbot = GPT(api_key=f"{API}", engine="gpt-3.5-turbo-1106", system_prompt=systemprompt, temperature=temperature) + try: GPT4visionbot = GPT(api_key=f"{API}", engine="gpt-4-vision-preview", system_prompt=systemprompt, temperature=temperature) except: diff --git a/utils/chatgpt2api.py b/utils/chatgpt2api.py index 9bed94db..f2706a42 100644 --- a/utils/chatgpt2api.py +++ b/utils/chatgpt2api.py @@ -470,8 +470,12 @@ def get_message_token(self, url, json_post): if response.status_code != 200: json_response = json.loads(response.text) string = json_response["error"]["message"] - print(json_response, string) - string = re.findall(r"\((.*?)\)", string)[0] + print(json_response) + try: + string = re.findall(r"\((.*?)\)", string)[0] + except: + if "You exceeded your current quota" in json_response: + raise Exception("当前账号余额不足!") numbers = re.findall(r"\d+\.?\d*", string) numbers = [int(i) for i in numbers] if len(numbers) == 2: