From 9f335de6b21abd03fedee9e734ac1f146a20db99 Mon Sep 17 00:00:00 2001 From: yym68686 Date: Thu, 28 Sep 2023 10:23:11 +0800 Subject: [PATCH] Integrating gpt4free --- bot.py | 31 +++++++++++++++++++++++++++++-- config.py | 4 +++- gpt4free.py | 25 ++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/bot.py b/bot.py index 96b356c7..42b955cd 100644 --- a/bot.py +++ b/bot.py @@ -2,6 +2,7 @@ import os import config import logging +import traceback import decorators from md2tgmd import escape from runasync import run_async @@ -99,7 +100,7 @@ async def getChatGPT(update, context, title, robot, message, use_search=config.S ) messageid = message.message_id try: - if not config.API: + if not config.API or config.USE_G4F: result = f"`🤖️ {config.GPT_ENGINE}`\n\n" import gpt4free if "gpt-3.5" in config.GPT_ENGINE: @@ -155,6 +156,7 @@ async def getChatGPT(update, context, title, robot, message, use_search=config.S print('\033[31m') print("response_msg", result) print("error", e) + traceback.print_exc() print('\033[0m') if config.API: robot.reset(convo_id=str(update.message.chat_id), system_prompt=config.systemprompt) @@ -225,6 +227,9 @@ async def delete_message(update, context, messageid, delay=10): InlineKeyboardButton("搜索已打开", callback_data="搜索"), InlineKeyboardButton("联网解析PDF已打开", callback_data="pdf"), ], + [ + InlineKeyboardButton("gpt4free已关闭", callback_data="gpt4free"), + ], ] if os.environ.get('GOOGLE_API_KEY', None) == None and os.environ.get('GOOGLE_CSE_ID', None) == None: first_buttons[1][1] = InlineKeyboardButton("google已关闭", callback_data="google") @@ -245,7 +250,7 @@ async def button_press(update, context): callback_query = update.callback_query await callback_query.answer() data = callback_query.data - if ("gpt" or "cluade") in data: + if ("gpt-" or "cluade") in data: if config.ENGINE_FLAG: config.GPT_ENGINE = data else: @@ -377,6 +382,28 @@ async def button_press(update, context): reply_markup=InlineKeyboardMarkup(first_buttons), parse_mode='MarkdownV2' ) + elif "gpt4free" in data: + config.USE_G4F = not config.USE_G4F + print(config.USE_G4F) + if config.USE_G4F == False: + first_buttons[3][0] = InlineKeyboardButton("gpt4free已关闭", callback_data="gpt4free") + else: + first_buttons[3][0] = InlineKeyboardButton("gpt4free已打开", callback_data="gpt4free") + + info_message = ( + f"`Hi, {update.effective_user.username}!`\n\n" + f"**Default engine:** `{config.GPT_ENGINE}`\n" + f"**Default search model:** `{config.DEFAULT_SEARCH_MODEL}`\n" + f"**temperature:** `{config.temperature}`\n" + f"**API_URL:** `{config.API_URL}`\n\n" + f"**API:** `{config.API}`\n\n" + f"**WEB_HOOK:** `{config.WEB_HOOK}`\n\n" + ) + message = await callback_query.edit_message_text( + text=escape(info_message), + reply_markup=InlineKeyboardMarkup(first_buttons), + parse_mode='MarkdownV2' + ) @decorators.Authorization async def info(update, context): diff --git a/config.py b/config.py index 72ac06d6..dcff5a5c 100644 --- a/config.py +++ b/config.py @@ -30,4 +30,6 @@ whitelist = os.environ.get('whitelist', None) if whitelist: - whitelist = [int(id) for id in whitelist.split(",")] \ No newline at end of file + whitelist = [int(id) for id in whitelist.split(",")] + +USE_G4F = False \ No newline at end of file diff --git a/gpt4free.py b/gpt4free.py index 65a04180..26d6c733 100644 --- a/gpt4free.py +++ b/gpt4free.py @@ -15,14 +15,37 @@ def get_response(message, model="gpt-3.5-turbo"): for message in response: yield message + +# def get_response(message, callback, model="gpt-3.5-turbo"): +# response = g4f.ChatCompletion.create( +# model=model, +# messages=[{"role": "user", "content": message}], +# stream=True, +# ) +# for message in response: +# callback(message) + +# def handle_response(response): +# yield response + if __name__ == "__main__": import asyncio message = r""" - 鲁迅和周树人为什么打架 +在多线程编程场景下,为了避免There is no current event loop in thread错误,怎么使用回调函数返回这个函数的结果: +def get_response(message, model="gpt-3.5-turbo"): + response = g4f.ChatCompletion.create( + model=model, + messages=[{"role": "user", "content": message}], + stream=True, + ) + for message in response: + yield message """ # result = asyncio.run(get_response(message, "gpt-4")) # print(result) + # for result in get_response(message, handle_response): + # print(result, flush=True, end='') for result in get_response(message, "gpt-4"): print(result, flush=True, end='') \ No newline at end of file