Skip to content

Commit

Permalink
1. Remove flask dependency
Browse files Browse the repository at this point in the history
2. add application.run_polling() functionality

3. web hook is no longer a required environment variable

4. add concurrent message processing capability for the bot

5. implement message rate limiting functionality for the bot.

6. update g4f version to 0.1.8.7
  • Loading branch information
yym68686 committed Nov 20, 2023
1 parent 73fc123 commit 6531535
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 59 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Join the [Telegram Group](https://t.me/+_01cz9tAkUc1YzZl) chat to share your use

## ✨ Features

✅ Supports ChatGPT and GPT4 API
✅ Supports GPT3.5 and GPT4/GPT4 Turbo API, DALLE 3

✅ Supports online search using duckduckgo and Google🔍. DuckDuckGo search is provided by default, and the official API for Google search needs to be applied by the user. It can provide real-time information that GPT could not answer before, such as Weibo hot search today, weather in a certain place today, and the progress of a certain person or news.

Expand All @@ -31,8 +31,8 @@ Join the [Telegram Group](https://t.me/+_01cz9tAkUc1YzZl) chat to share your use
| Variable Name | Comment |
| ---------------------- | ------------------------------------------------------------ |
| **BOT_TOKEN (required)** | Telegram bot token. Create a bot on [BotFather](https://t.me/BotFather) to get the BOT_TOKEN. |
| **WEB_HOOK (required)** | 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 (required)** | OpenAI or third-party API key. |
| WEB_HOOK | 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. |
Expand Down Expand Up @@ -182,5 +182,4 @@ The markdown rendering of the message used is another [project](https://github.c

## License

This project is licensed under GPLv3, which means you are free to copy, distribute, and modify the software, as long as all modifications and derivative works are also released under the same license.

This project is licensed under GPLv3, which means you are free to copy, distribute, and modify the software, as long as all modifications and derivative works are also released under the same license.
27 changes: 19 additions & 8 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import traceback
import utils.decorators as decorators
from utils.md2tgmd import escape
from utils.runasync import run_async
from chatgpt2api.chatgpt2api import Chatbot as GPT
from telegram.constants import ChatAction
from utils.agent import docQA, get_doc_from_local
from telegram import BotCommand, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import CommandHandler, MessageHandler, ApplicationBuilder, filters, CallbackQueryHandler
from telegram.ext import CommandHandler, MessageHandler, ApplicationBuilder, filters, CallbackQueryHandler, Application, AIORateLimiter
from config import WEB_HOOK, PORT


logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
Expand Down Expand Up @@ -548,10 +548,8 @@ async def error(update, context):
async def unknown(update, context): # 当用户输入未知命令时,返回文本
await context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, I didn't understand that command.")

def setup(token):
application = ApplicationBuilder().read_timeout(10).connection_pool_size(50000).pool_timeout(1200.0).token(token).build()

run_async(application.bot.set_my_commands([
async def post_init(application: Application) -> None:
await application.bot.set_my_commands([
BotCommand('info', 'basic information'),
BotCommand('pic', 'Generate image'),
BotCommand('search', 'search Google or duckduckgo'),
Expand All @@ -560,7 +558,17 @@ def setup(token):
BotCommand('qa', 'Document Q&A with Embedding Database Search'),
BotCommand('start', 'Start the bot'),
BotCommand('reset', 'Reset the bot'),
]))
])

if __name__ == '__main__':
application = (
ApplicationBuilder()
.token("5898265830:AAGJFGstJD21VjQRWHdg5XSnJGoc7m4Yvvw")
.concurrent_updates(True)
.rate_limiter(AIORateLimiter(max_retries=5))
.post_init(post_init)
.build()
)

application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("pic", image))
Expand All @@ -576,4 +584,7 @@ def setup(token):
application.add_handler(MessageHandler(filters.COMMAND, unknown))
application.add_error_handler(error)

return application
if WEB_HOOK:
application.run_webhook("127.0.0.1", PORT, webhook_url=WEB_HOOK)
else:
application.run_polling()
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ services:
image: yym68686/chatgpt:1.0
environment:
- BOT_TOKEN=
- WEB_HOOK=
- API=
- API_URL=
ports:
Expand Down
39 changes: 0 additions & 39 deletions main.py

This file was deleted.

6 changes: 2 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
--index-url https://pypi.python.org/simple/
tiktoken
requests
waitress
flask[async]
python-telegram-bot==20.4
python-telegram-bot[webhook,rate-limiter]==20.4

# langchain
chromadb
Expand All @@ -16,4 +14,4 @@ unstructured[pdf]
duckduckgo-search==3.8.5
langchain==0.0.271
oauth2client==3.0.0
g4f==0.1.8.2
g4f==0.1.8.7
2 changes: 1 addition & 1 deletion setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
set -eu
rm -rf ChatGPT-Telegram-Bot/
git clone --depth 1 -b main https://github.com/yym68686/ChatGPT-Telegram-Bot.git
python -u /home/ChatGPT-Telegram-Bot/main.py
python -u /home/ChatGPT-Telegram-Bot/bot.py
6 changes: 4 additions & 2 deletions test/test_keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def getgooglesearchurl(result, numresults=3):
# print("google urls", urls)
return urls

chainllm = ChatOpenAI(temperature=0.5, openai_api_base=os.environ.get('API_URL', None).split("chat")[0], model_name="gpt-3.5-turbo-16k", openai_api_key=os.environ.get('API', None))
chainllm = ChatOpenAI(temperature=0.5, openai_api_base=os.environ.get('API_URL', None).split("chat")[0], model_name="gpt-4-1106-preview", openai_api_key=os.environ.get('API', None))
keyword_prompt = PromptTemplate(
input_variables=["source"],
# template="*{source}*, ——我想通过网页搜索引擎,获取上述问题的可能答案。请你提取上述问题相关的关键词作为搜索用词(用空格隔开),直接给我结果(不要多余符号)。",
Expand All @@ -24,7 +24,9 @@ def getgooglesearchurl(result, numresults=3):
# template="请你帮我抽取关键词,输出的关键词之间用空格连接。输出除了关键词,不用解释,也不要出现其他内容,只要出现关键词,必须用空格连接关键词,不要出现其他任何连接符。下面是要提取关键词的文字:{source}",
)
key_chain = LLMChain(llm=chainllm, prompt=keyword_prompt)
result = key_chain.run("How much does the 'zeabur' software service cost per month? Is it free to use? Any limitations?")
result = key_chain.run("奶茶妹妹什么时候跟刘强东结婚的?")
# result = key_chain.run("鸿蒙是安卓套壳吗?")
# result = key_chain.run("How much does the 'zeabur' software service cost per month? Is it free to use? Any limitations?")
print(result)
# print(getgooglesearchurl("zeabur price"))
print(getgooglesearchurl(result))

0 comments on commit 6531535

Please sign in to comment.