Skip to content

Commit

Permalink
Integrating gpt4free
Browse files Browse the repository at this point in the history
  • Loading branch information
yym68686 committed Sep 28, 2023
1 parent 4282d69 commit 9f335de
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
31 changes: 29 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import config
import logging
import traceback
import decorators
from md2tgmd import escape
from runasync import run_async
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@

whitelist = os.environ.get('whitelist', None)
if whitelist:
whitelist = [int(id) for id in whitelist.split(",")]
whitelist = [int(id) for id in whitelist.split(",")]

USE_G4F = False
25 changes: 24 additions & 1 deletion gpt4free.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='')

0 comments on commit 9f335de

Please sign in to comment.