From a73b564dfccf8997b8898209361b9ebd9287e633 Mon Sep 17 00:00:00 2001 From: yym68686 Date: Wed, 18 Oct 2023 23:32:30 +0800 Subject: [PATCH] The GPT4 model of gpt4free supports streaming transmission, update g4f version to 0.1.6.6 --- bot.py | 34 +++++++++++++--------------------- gpt4free.py | 29 ----------------------------- 2 files changed, 13 insertions(+), 50 deletions(-) diff --git a/bot.py b/bot.py index c7cbfe24..ebd2b79e 100644 --- a/bot.py +++ b/bot.py @@ -106,27 +106,19 @@ async def getChatGPT(update, context, title, robot, message, use_search=config.S get_answer = gpt4free.get_response try: - if (config.USE_G4F or not config.API) and "gpt-3.5" not in config.GPT_ENGINE: - result = f"`🤖️ {config.GPT_ENGINE}`\n\n" - tmpresult = await gpt4free.get_async_response(text, config.GPT_ENGINE) - tmpresult = gpt4free.bing(tmpresult) - result = result + tmpresult - await context.bot.edit_message_text(chat_id=update.message.chat_id, message_id=messageid, text=escape(result), parse_mode='MarkdownV2', disable_web_page_preview=True) - lastresult = result - else: - for data in get_answer(text, convo_id=str(update.message.chat_id), pass_history=config.PASS_HISTORY): - result = result + data - tmpresult = result - modifytime = modifytime + 1 - if re.sub(r"```", '', result).count("`") % 2 != 0: - tmpresult = result + "`" - if result.count("```") % 2 != 0: - tmpresult = result + "\n```" - if modifytime % 20 == 0 and lastresult != tmpresult: - if 'claude2' in title: - tmpresult = re.sub(r",", ',', tmpresult) - await context.bot.edit_message_text(chat_id=update.message.chat_id, message_id=messageid, text=escape(tmpresult), parse_mode='MarkdownV2', disable_web_page_preview=True) - lastresult = tmpresult + for data in get_answer(text, convo_id=str(update.message.chat_id), pass_history=config.PASS_HISTORY): + result = result + data + tmpresult = result + modifytime = modifytime + 1 + if re.sub(r"```", '', result).count("`") % 2 != 0: + tmpresult = result + "`" + if result.count("```") % 2 != 0: + tmpresult = result + "\n```" + if modifytime % 20 == 0 and lastresult != tmpresult: + if 'claude2' in title: + tmpresult = re.sub(r",", ',', tmpresult) + await context.bot.edit_message_text(chat_id=update.message.chat_id, message_id=messageid, text=escape(tmpresult), parse_mode='MarkdownV2', disable_web_page_preview=True) + lastresult = tmpresult except Exception as e: print('\033[31m') print("response_msg", result) diff --git a/gpt4free.py b/gpt4free.py index 1175e65c..bd905a9b 100644 --- a/gpt4free.py +++ b/gpt4free.py @@ -1,16 +1,9 @@ import g4f import re import os -import asyncio import config from rich.console import Console from rich.markdown import Markdown -async def get_async_response(message, model="gpt-3.5-turbo"): - response = await g4f.ChatCompletion.create_async( - model=model, - messages=[{"role": "user", "content": message}], - ) - return response def get_response(message, **kwargs): response = g4f.ChatCompletion.create( @@ -21,19 +14,6 @@ def get_response(message, **kwargs): 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 - def bing(response): response = re.sub(r"\[\^\d+\^\]", "", response) if len(response.split("\n\n")) >= 2: @@ -44,15 +24,6 @@ def bing(response): if __name__ == "__main__": - # 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, "claude-v2"): - # print(bing(result), flush=True, end='') - # print(result, flush=True, end='') - console = Console() message = rf"""