answer_it_handler to process image message. support gemini thread

This commit is contained in:
F4ria 2024-07-10 19:58:43 +08:00
parent 5ea841bdba
commit 474e80db53

View File

@ -282,8 +282,10 @@ def answer_it_handler(message: Message, bot: TeleBot) -> None:
#### Answers Thread #### #### Answers Thread ####
executor = ThreadPoolExecutor(max_workers=Stream_Thread) executor = ThreadPoolExecutor(max_workers=Stream_Thread)
if GEMINI_USE_THREAD and GOOGLE_GEMINI_KEY and not local_image_path: if GEMINI_USE_THREAD and GOOGLE_GEMINI_KEY:
gemini_future = executor.submit(gemini_answer, latest_message, bot, m) gemini_future = executor.submit(
gemini_answer, latest_message, bot, m, local_image_path
)
if CHATGPT_USE and CHATGPT_API_KEY: if CHATGPT_USE and CHATGPT_API_KEY:
chatgpt_future = executor.submit( chatgpt_future = executor.submit(
chatgpt_answer, latest_message, bot, m, local_image_path chatgpt_answer, latest_message, bot, m, local_image_path
@ -318,11 +320,11 @@ def answer_it_handler(message: Message, bot: TeleBot) -> None:
g_s = "" g_s = ""
g_reply_id = bot_reply_first(latest_message, g_who, bot) g_reply_id = bot_reply_first(latest_message, g_who, bot)
try: try:
if not local_image_path: if local_image_path:
g_r = convo.send_message(m, stream=True)
else:
gemini_image_file = genai.upload_file(path=local_image_path) gemini_image_file = genai.upload_file(path=local_image_path)
g_r = convo.send_message([m, gemini_image_file], stream=True) g_r = convo.send_message([m, gemini_image_file], stream=True)
else:
g_r = convo.send_message(m, stream=True)
g_start = time.time() g_start = time.time()
g_overall_start = time.time() g_overall_start = time.time()
@ -359,7 +361,7 @@ def answer_it_handler(message: Message, bot: TeleBot) -> None:
#### Answers List #### #### Answers List ####
if GEMINI_USE_THREAD and GOOGLE_GEMINI_KEY and not local_image_path: if GEMINI_USE_THREAD and GOOGLE_GEMINI_KEY:
answer_gemini, gemini_chat_id = gemini_future.result() answer_gemini, gemini_chat_id = gemini_future.result()
full_chat_id_list.append(gemini_chat_id) full_chat_id_list.append(gemini_chat_id)
full_answer += answer_gemini full_answer += answer_gemini
@ -430,13 +432,17 @@ def llm_background_ph_update(path: str, full_answer: str, m: str) -> str:
return full_answer return full_answer
def gemini_answer(latest_message: Message, bot: TeleBot, m): def gemini_answer(latest_message: Message, bot: TeleBot, m, local_image_path):
"""gemini answer""" """gemini answer"""
who = "Gemini Pro" who = "Gemini Pro"
# show something, make it more responsible # show something, make it more responsible
reply_id = bot_reply_first(latest_message, who, bot) reply_id = bot_reply_first(latest_message, who, bot)
try: try:
if local_image_path:
gemini_image_file = genai.upload_file(path=local_image_path)
r = convo.send_message([m, gemini_image_file], stream=True)
else:
r = convo.send_message(m, stream=True) r = convo.send_message(m, stream=True)
s = "" s = ""
start = time.time() start = time.time()