Skip to content

Commit

Permalink
The GPT4 model of gpt4free supports streaming transmission, update g4…
Browse files Browse the repository at this point in the history
…f version to 0.1.6.6
  • Loading branch information
yym68686 committed Oct 18, 2023
1 parent c776de8 commit a73b564
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 50 deletions.
34 changes: 13 additions & 21 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 0 additions & 29 deletions gpt4free.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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:
Expand All @@ -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"""
Expand Down

0 comments on commit a73b564

Please sign in to comment.