|
3 | 3 | import config
|
4 | 4 | import logging
|
5 | 5 | import traceback
|
6 |
| -import decorators |
7 |
| -from md2tgmd import escape |
8 |
| -from runasync import run_async |
| 6 | +import utils.decorators as decorators |
| 7 | +from utils.md2tgmd import escape |
| 8 | +from utils.runasync import run_async |
9 | 9 | from chatgpt2api.chatgpt2api import Chatbot as GPT
|
10 | 10 | from telegram.constants import ChatAction
|
11 |
| -from agent import docQA, get_doc_from_local |
| 11 | +from utils.agent import docQA, get_doc_from_local |
12 | 12 | from telegram import BotCommand, InlineKeyboardButton, InlineKeyboardMarkup
|
13 | 13 | from telegram.ext import CommandHandler, MessageHandler, ApplicationBuilder, filters, CallbackQueryHandler
|
14 | 14 |
|
15 | 15 |
|
16 | 16 | logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
| 17 | +# logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") |
17 | 18 | logger = logging.getLogger()
|
18 | 19 |
|
19 | 20 | # 获取 httpx 的 logger
|
@@ -97,12 +98,65 @@ async def getChatGPT(update, context, title, robot, message, use_search=config.S
|
97 | 98 | reply_to_message_id=update.message.message_id,
|
98 | 99 | )
|
99 | 100 | messageid = message.message_id
|
100 |
| - if use_search and not has_command: |
101 |
| - get_answer = robot.search_summary |
102 |
| - else: |
103 |
| - get_answer = robot.ask_stream |
| 101 | + get_answer = robot.ask_stream |
| 102 | + # if use_search and not has_command: |
| 103 | + # get_answer = robot.search_summary |
| 104 | + # else: |
| 105 | + # get_answer = robot.ask_stream |
| 106 | + if not config.API or (config.USE_G4F and not config.SEARCH_USE_GPT): |
| 107 | + import utils.gpt4free as gpt4free |
| 108 | + get_answer = gpt4free.get_response |
| 109 | + |
| 110 | + try: |
| 111 | + for data in get_answer(text, convo_id=str(update.message.chat_id), pass_history=config.PASS_HISTORY): |
| 112 | + result = result + data |
| 113 | + tmpresult = result |
| 114 | + modifytime = modifytime + 1 |
| 115 | + if re.sub(r"```", '', result).count("`") % 2 != 0: |
| 116 | + tmpresult = result + "`" |
| 117 | + if result.count("```") % 2 != 0: |
| 118 | + tmpresult = result + "\n```" |
| 119 | + if modifytime % 20 == 0 and lastresult != tmpresult: |
| 120 | + if 'claude2' in title: |
| 121 | + tmpresult = re.sub(r",", ',', tmpresult) |
| 122 | + 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) |
| 123 | + lastresult = tmpresult |
| 124 | + except Exception as e: |
| 125 | + print('\033[31m') |
| 126 | + print("response_msg", result) |
| 127 | + print("error", e) |
| 128 | + traceback.print_exc() |
| 129 | + print('\033[0m') |
| 130 | + if config.API: |
| 131 | + robot.reset(convo_id=str(update.message.chat_id), system_prompt=config.systemprompt) |
| 132 | + if "You exceeded your current quota, please check your plan and billing details." in str(e): |
| 133 | + print("OpenAI api 已过期!") |
| 134 | + await context.bot.delete_message(chat_id=update.message.chat_id, message_id=messageid) |
| 135 | + messageid = '' |
| 136 | + config.API = '' |
| 137 | + result += f"`出错啦!{e}`" |
| 138 | + print(result) |
| 139 | + if lastresult != result and messageid: |
| 140 | + if 'claude2' in title: |
| 141 | + result = re.sub(r",", ',', result) |
| 142 | + 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) |
| 143 | + |
| 144 | +async def search(update, context, title, robot): |
| 145 | + message = update.message.text if config.NICK is None else update.message.text[botNicKLength:].strip() if update.message.text[:botNicKLength].lower() == botNick else None |
| 146 | + result = title |
| 147 | + text = message |
| 148 | + modifytime = 0 |
| 149 | + lastresult = '' |
| 150 | + message = await context.bot.send_message( |
| 151 | + chat_id=update.message.chat_id, |
| 152 | + text="搜索中💭", |
| 153 | + parse_mode='MarkdownV2', |
| 154 | + reply_to_message_id=update.message.message_id, |
| 155 | + ) |
| 156 | + messageid = message.message_id |
| 157 | + get_answer = robot.search_summary |
104 | 158 | if not config.API or (config.USE_G4F and not config.SEARCH_USE_GPT):
|
105 |
| - import gpt4free |
| 159 | + import utils.gpt4free as gpt4free |
106 | 160 | get_answer = gpt4free.get_response
|
107 | 161 |
|
108 | 162 | try:
|
@@ -164,16 +218,16 @@ async def delete_message(update, context, messageid, delay=10):
|
164 | 218 | # [
|
165 | 219 | # InlineKeyboardButton("gpt-3.5-turbo-0613", callback_data="gpt-3.5-turbo-0613"),
|
166 | 220 | # ],
|
167 |
| - [ |
168 |
| - InlineKeyboardButton("gpt-4", callback_data="gpt-4"), |
169 |
| - InlineKeyboardButton("gpt-4-32k", callback_data="gpt-4-32k"), |
170 |
| - # InlineKeyboardButton("gpt-4-0314", callback_data="gpt-4-0314"), |
171 |
| - ], |
172 | 221 | [
|
173 | 222 | InlineKeyboardButton("gpt-4-1106-preview", callback_data="gpt-4-1106-preview"),
|
174 | 223 | # InlineKeyboardButton("gpt-4-32k", callback_data="gpt-4-32k"),
|
175 | 224 | # InlineKeyboardButton("gpt-4-32k-0314", callback_data="gpt-4-32k-0314"),
|
176 | 225 | ],
|
| 226 | + [ |
| 227 | + InlineKeyboardButton("gpt-4", callback_data="gpt-4"), |
| 228 | + InlineKeyboardButton("gpt-4-32k", callback_data="gpt-4-32k"), |
| 229 | + # InlineKeyboardButton("gpt-4-0314", callback_data="gpt-4-0314"), |
| 230 | + ], |
177 | 231 | # [
|
178 | 232 | # InlineKeyboardButton("gpt-4-0613", callback_data="gpt-4-0613"),
|
179 | 233 | # InlineKeyboardButton("gpt-4-32k-0613", callback_data="gpt-4-32k-0613"),
|
@@ -373,7 +427,7 @@ async def info(update, context):
|
373 | 427 | messageid = message.message_id
|
374 | 428 | await context.bot.delete_message(chat_id=update.effective_chat.id, message_id=update.message.message_id)
|
375 | 429 |
|
376 |
| -from agent import pdfQA, getmd5, persist_emdedding_pdf |
| 430 | +from utils.agent import pdfQA, getmd5, persist_emdedding_pdf |
377 | 431 | @decorators.Authorization
|
378 | 432 | async def handle_pdf(update, context):
|
379 | 433 | # 获取接收到的文件
|
@@ -456,14 +510,16 @@ def setup(token):
|
456 | 510 |
|
457 | 511 | run_async(application.bot.set_my_commands([
|
458 | 512 | BotCommand('info', 'basic information'),
|
459 |
| - BotCommand('qa', 'Document Q&A with Embedding Database Search'), |
| 513 | + BotCommand('search', 'search Google or duckduckgo'), |
460 | 514 | BotCommand('en2zh', 'translate to Chinese'),
|
461 | 515 | BotCommand('zh2en', 'translate to English'),
|
| 516 | + BotCommand('qa', 'Document Q&A with Embedding Database Search'), |
462 | 517 | BotCommand('start', 'Start the bot'),
|
463 | 518 | BotCommand('reset', 'Reset the bot'),
|
464 | 519 | ]))
|
465 | 520 |
|
466 | 521 | application.add_handler(CommandHandler("start", start))
|
| 522 | + application.add_handler(CommandHandler("search", lambda update, context: search(update, context, title=f"`🤖️ {config.GPT_ENGINE}`\n\n", robot=config.ChatGPTbot))) |
467 | 523 | application.add_handler(CallbackQueryHandler(button_press))
|
468 | 524 | application.add_handler(CommandHandler("reset", reset_chat))
|
469 | 525 | application.add_handler(CommandHandler("en2zh", lambda update, context: command_bot(update, context, "simplified chinese", robot=config.ChatGPTbot)))
|
|
0 commit comments