mirror of
https://github.com/cdryzun/tg_bot_collections.git
synced 2025-04-29 00:27:09 +08:00
fix: threadthings
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
This commit is contained in:
parent
6be33a9bc0
commit
4cf8c8f8d9
@ -278,7 +278,7 @@ class TelegraphAPI:
|
|||||||
data = {
|
data = {
|
||||||
"access_token": self.access_token,
|
"access_token": self.access_token,
|
||||||
"title": title,
|
"title": title,
|
||||||
"content": json.dumps(content),
|
"content": json.dumps(content, ensure_ascii=False),
|
||||||
"return_content": return_content,
|
"return_content": return_content,
|
||||||
"author_name": author_name if author_name else self.author_name,
|
"author_name": author_name if author_name else self.author_name,
|
||||||
"author_url": author_url if author_url else self.author_url,
|
"author_url": author_url if author_url else self.author_url,
|
||||||
|
@ -6,13 +6,10 @@ from expiringdict import ExpiringDict
|
|||||||
from os import environ
|
from os import environ
|
||||||
import time
|
import time
|
||||||
import datetime
|
import datetime
|
||||||
import threading
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
import queue
|
|
||||||
|
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
import google.generativeai as genai
|
import google.generativeai as genai
|
||||||
from google.generativeai import ChatSession
|
|
||||||
from google.generativeai.types.generation_types import StopCandidateException
|
|
||||||
from telebot import TeleBot
|
from telebot import TeleBot
|
||||||
from together import Together
|
from together import Together
|
||||||
from telebot.types import Message
|
from telebot.types import Message
|
||||||
@ -62,7 +59,7 @@ safety_settings = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
model = genai.GenerativeModel(
|
model = genai.GenerativeModel(
|
||||||
model_name="models/gemini-1.5-pro-latest",
|
model_name="gemini-1.5-flash-latest",
|
||||||
generation_config=generation_config,
|
generation_config=generation_config,
|
||||||
safety_settings=safety_settings,
|
safety_settings=safety_settings,
|
||||||
)
|
)
|
||||||
@ -87,26 +84,6 @@ client = OpenAI(api_key=CHATGPT_API_KEY, base_url=CHATGPT_BASE_URL, timeout=300)
|
|||||||
qwen_client = Together(api_key=QWEN_API_KEY, timeout=300)
|
qwen_client = Together(api_key=QWEN_API_KEY, timeout=300)
|
||||||
|
|
||||||
|
|
||||||
def _run_in_thread(func, *args, **kwargs):
|
|
||||||
result_queue = queue.Queue()
|
|
||||||
|
|
||||||
def wrapper():
|
|
||||||
try:
|
|
||||||
result = func(*args, **kwargs)
|
|
||||||
result_queue.put(result)
|
|
||||||
except Exception as e:
|
|
||||||
result_queue.put(e)
|
|
||||||
|
|
||||||
thread = threading.Thread(target=wrapper)
|
|
||||||
thread.start()
|
|
||||||
thread.join()
|
|
||||||
|
|
||||||
result = result_queue.get()
|
|
||||||
if isinstance(result, Exception):
|
|
||||||
raise result
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def md_handler(message: Message, bot: TeleBot):
|
def md_handler(message: Message, bot: TeleBot):
|
||||||
"""pretty md: /md <address>"""
|
"""pretty md: /md <address>"""
|
||||||
who = ""
|
who = ""
|
||||||
@ -198,21 +175,23 @@ def answer_it_handler(message: Message, bot: TeleBot):
|
|||||||
full = "Question:\n" + m + "\n---\n"
|
full = "Question:\n" + m + "\n---\n"
|
||||||
##### Gemini #####
|
##### Gemini #####
|
||||||
who = "Gemini Pro"
|
who = "Gemini Pro"
|
||||||
# show something, make it more responsible
|
|
||||||
reply_id = bot_reply_first(latest_message, who, bot)
|
reply_id = bot_reply_first(latest_message, who, bot)
|
||||||
chatgpt_answer = _run_in_thread(get_gpt_answer, m)
|
|
||||||
|
#### excutor thread ####
|
||||||
|
executor = ThreadPoolExecutor(max_workers=5)
|
||||||
|
chatgpt_thread = executor.submit(get_gpt_answer, m)
|
||||||
|
claude_thread = None
|
||||||
|
|
||||||
claude_answer = ""
|
claude_answer = ""
|
||||||
if ANTHROPIC_API_KEY:
|
if ANTHROPIC_API_KEY:
|
||||||
claude_answer = _run_in_thread(get_claude_answer, m)
|
claude_thread = executor.submit(get_claude_answer, m)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
r = model.generate_content(m, stream=True)
|
r = model.generate_content(m, stream=True)
|
||||||
s = ""
|
s = ""
|
||||||
start = time.time()
|
start = time.time()
|
||||||
for e in r:
|
for e in r:
|
||||||
s += e.text
|
s += e.text
|
||||||
if time.time() - start > 1.7:
|
if time.time() - start > 1.5:
|
||||||
start = time.time()
|
start = time.time()
|
||||||
bot_reply_markdown(reply_id, who, s, bot, split_text=False)
|
bot_reply_markdown(reply_id, who, s, bot, split_text=False)
|
||||||
bot_reply_markdown(reply_id, who, s, bot)
|
bot_reply_markdown(reply_id, who, s, bot)
|
||||||
@ -227,6 +206,7 @@ def answer_it_handler(message: Message, bot: TeleBot):
|
|||||||
who = "ChatGPT Pro"
|
who = "ChatGPT Pro"
|
||||||
reply_id = bot_reply_first(latest_message, who, bot)
|
reply_id = bot_reply_first(latest_message, who, bot)
|
||||||
# get gpt answer using thread
|
# get gpt answer using thread
|
||||||
|
chatgpt_answer = chatgpt_thread.result()
|
||||||
|
|
||||||
bot_reply_markdown(reply_id, who, chatgpt_answer, bot)
|
bot_reply_markdown(reply_id, who, chatgpt_answer, bot)
|
||||||
|
|
||||||
@ -236,6 +216,7 @@ def answer_it_handler(message: Message, bot: TeleBot):
|
|||||||
##### Claude #####
|
##### Claude #####
|
||||||
if USE_CLAUDE and ANTHROPIC_API_KEY:
|
if USE_CLAUDE and ANTHROPIC_API_KEY:
|
||||||
who = "Claude Pro"
|
who = "Claude Pro"
|
||||||
|
claude_answer = claude_thread.result()
|
||||||
reply_id = bot_reply_first(latest_message, who, bot)
|
reply_id = bot_reply_first(latest_message, who, bot)
|
||||||
bot_reply_markdown(reply_id, who, claude_answer, bot)
|
bot_reply_markdown(reply_id, who, claude_answer, bot)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user