mirror of
https://github.com/cdryzun/tg_bot_collections.git
synced 2025-04-29 00:27:09 +08:00
feat: qwen 110B
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
This commit is contained in:
parent
77ead6119c
commit
55fd800eb1
@ -55,6 +55,12 @@ Note, if you are using third party service, you need to `export OPENAI_API_BASE=
|
||||
2. export GROQ_API_KEY=${the_key}
|
||||
3. use `llama_pro: ${message}` to ask
|
||||
|
||||
## Bot -> qwen
|
||||
|
||||
1. visit https://api.together.xyz/settings/api-keys get the key
|
||||
2. export TOGETHER_API_KEY=${the_key}
|
||||
3. use `qwen_pro: ${message}` to ask
|
||||
|
||||
|
||||
## HOW TO Install and Run
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
from os import environ
|
||||
import time
|
||||
|
||||
from openai import OpenAI
|
||||
from telebot import TeleBot
|
||||
from telebot.types import Message
|
||||
|
||||
|
169
handlers/qwen.py
Normal file
169
handlers/qwen.py
Normal file
@ -0,0 +1,169 @@
|
||||
# qwen use https://api.together.xyz
|
||||
from os import environ
|
||||
import time
|
||||
|
||||
from telebot import TeleBot
|
||||
from telebot.types import Message
|
||||
|
||||
from . import *
|
||||
|
||||
from together import Together
|
||||
from telegramify_markdown import convert
|
||||
from telegramify_markdown.customize import markdown_symbol
|
||||
|
||||
markdown_symbol.head_level_1 = "📌" # If you want, Customizing the head level 1 symbol
|
||||
markdown_symbol.link = "🔗" # If you want, Customizing the link symbol
|
||||
|
||||
QWEN_API_KEY = environ.get("TOGETHER_API_KEY")
|
||||
QWEN_MODEL = "Qwen/Qwen1.5-110B-Chat"
|
||||
|
||||
if QWEN_API_KEY:
|
||||
client = Together(api_key=QWEN_API_KEY)
|
||||
|
||||
# Global history cache
|
||||
qwen_player_dict = {}
|
||||
qwen_pro_player_dict = {}
|
||||
|
||||
|
||||
def qwen_handler(message: Message, bot: TeleBot) -> None:
|
||||
"""qwen : /qwen <question>"""
|
||||
m = message.text.strip()
|
||||
|
||||
player_message = []
|
||||
# restart will lose all TODO
|
||||
if str(message.from_user.id) not in qwen_player_dict:
|
||||
qwen_player_dict[str(message.from_user.id)] = (
|
||||
player_message # for the imuutable list
|
||||
)
|
||||
else:
|
||||
player_message = qwen_player_dict[str(message.from_user.id)]
|
||||
if m.strip() == "clear":
|
||||
bot.reply_to(
|
||||
message,
|
||||
"just clear your qwen messages history",
|
||||
)
|
||||
player_message.clear()
|
||||
return
|
||||
if m[:4].lower() == "new ":
|
||||
m = m[4:].strip()
|
||||
player_message.clear()
|
||||
m = enrich_text_with_urls(m)
|
||||
|
||||
who = "qwen"
|
||||
# show something, make it more responsible
|
||||
reply_id = bot_reply_first(message, who, bot)
|
||||
|
||||
player_message.append({"role": "user", "content": m})
|
||||
# keep the last 5, every has two ask and answer.
|
||||
if len(player_message) > 10:
|
||||
player_message = player_message[2:]
|
||||
|
||||
qwen_reply_text = ""
|
||||
try:
|
||||
r = client.chat.completions.create(
|
||||
messages=player_message, max_tokens=8192, model=QWEN_MODEL
|
||||
)
|
||||
content = r.choices[0].message.content.encode("utf8").decode()
|
||||
if not content:
|
||||
qwen_reply_text = f"{who} did not answer."
|
||||
player_message.pop()
|
||||
else:
|
||||
qwen_reply_text = content
|
||||
player_message.append(
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": qwen_reply_text,
|
||||
}
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
bot_reply_markdown(reply_id, who, "answer wrong", bot)
|
||||
# pop my user
|
||||
player_message.pop()
|
||||
return
|
||||
|
||||
# reply back as Markdown and fallback to plain text if failed.
|
||||
bot_reply_markdown(reply_id, who, qwen_reply_text, bot)
|
||||
|
||||
|
||||
def qwen_pro_handler(message: Message, bot: TeleBot) -> None:
|
||||
"""qwen_pro : /qwen_pro <question>"""
|
||||
m = message.text.strip()
|
||||
|
||||
player_message = []
|
||||
# restart will lose all TODO
|
||||
if str(message.from_user.id) not in qwen_player_dict:
|
||||
qwen_player_dict[str(message.from_user.id)] = (
|
||||
player_message # for the imuutable list
|
||||
)
|
||||
else:
|
||||
player_message = qwen_player_dict[str(message.from_user.id)]
|
||||
if m.strip() == "clear":
|
||||
bot.reply_to(
|
||||
message,
|
||||
"just clear your qwen messages history",
|
||||
)
|
||||
player_message.clear()
|
||||
return
|
||||
if m[:4].lower() == "new ":
|
||||
m = m[4:].strip()
|
||||
player_message.clear()
|
||||
m = enrich_text_with_urls(m)
|
||||
|
||||
who = "qwen Pro"
|
||||
reply_id = bot_reply_first(message, who, bot)
|
||||
|
||||
player_message.append({"role": "user", "content": m})
|
||||
# keep the last 5, every has two ask and answer.
|
||||
if len(player_message) > 10:
|
||||
player_message = player_message[2:]
|
||||
|
||||
try:
|
||||
r = client.chat.completions.create(
|
||||
messages=player_message,
|
||||
max_tokens=8192,
|
||||
model=QWEN_MODEL,
|
||||
stream=True,
|
||||
)
|
||||
s = ""
|
||||
start = time.time()
|
||||
for chunk in r:
|
||||
if chunk.choices[0].delta.content is None:
|
||||
break
|
||||
s += chunk.choices[0].delta.content
|
||||
if time.time() - start > 1.7:
|
||||
start = time.time()
|
||||
bot_reply_markdown(reply_id, who, s, bot, split_text=False)
|
||||
|
||||
if not bot_reply_markdown(reply_id, who, s, bot):
|
||||
# maybe not complete
|
||||
# maybe the same message
|
||||
player_message.clear()
|
||||
return
|
||||
|
||||
player_message.append(
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": convert(s),
|
||||
}
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
bot_reply_markdown(reply_id, who, "answer wrong", bot)
|
||||
player_message.clear()
|
||||
return
|
||||
|
||||
|
||||
if QWEN_API_KEY:
|
||||
|
||||
def register(bot: TeleBot) -> None:
|
||||
bot.register_message_handler(qwen_handler, commands=["qwen"], pass_bot=True)
|
||||
bot.register_message_handler(qwen_handler, regexp="^qwen:", pass_bot=True)
|
||||
bot.register_message_handler(
|
||||
qwen_pro_handler, commands=["qwen_pro"], pass_bot=True
|
||||
)
|
||||
bot.register_message_handler(
|
||||
qwen_pro_handler, regexp="^qwen_pro:", pass_bot=True
|
||||
)
|
20
pyproject.toml
Normal file
20
pyproject.toml
Normal file
@ -0,0 +1,20 @@
|
||||
|
||||
[project]
|
||||
# PEP 621 project metadata
|
||||
# See https://www.python.org/dev/peps/pep-0621/
|
||||
dependencies = [
|
||||
"pyTelegramBotAPI>=4.16",
|
||||
"cairosvg",
|
||||
"github-poster",
|
||||
"prettymapp",
|
||||
"google-generativeai>=0.4",
|
||||
"anthropic",
|
||||
"telegramify-markdown",
|
||||
"openai",
|
||||
"requests",
|
||||
"urlextract",
|
||||
"requests",
|
||||
"groq",
|
||||
"together>=1.1.5",
|
||||
]
|
||||
requires-python = ">=3.10"
|
120
requirements.txt
120
requirements.txt
@ -1,12 +1,108 @@
|
||||
pyTelegramBotAPI>=4.16
|
||||
cairosvg
|
||||
github_poster
|
||||
prettymapp
|
||||
google-generativeai>=0.4
|
||||
anthropic
|
||||
telegramify-markdown
|
||||
openai
|
||||
requests
|
||||
urlextract
|
||||
requests
|
||||
groq
|
||||
# This file is @generated by PDM.
|
||||
# Please do not edit it manually.
|
||||
|
||||
aiohttp==3.9.5
|
||||
aiosignal==1.3.1
|
||||
annotated-types==0.6.0
|
||||
anthropic==0.25.8
|
||||
anyio==4.3.0
|
||||
async-timeout==4.0.3; python_version < "3.11"
|
||||
attrs==23.2.0
|
||||
cachetools==5.3.3
|
||||
cairocffi==1.7.0
|
||||
cairosvg==2.7.1
|
||||
certifi==2024.2.2
|
||||
cffi==1.16.0
|
||||
charset-normalizer==3.3.2
|
||||
click==8.1.7
|
||||
click-plugins==1.1.1
|
||||
cligj==0.7.2
|
||||
colorama==0.4.6; platform_system == "Windows"
|
||||
colour==0.1.5
|
||||
contourpy==1.2.1
|
||||
cssselect2==0.7.0
|
||||
cycler==0.12.1
|
||||
defusedxml==0.7.1
|
||||
distro==1.9.0
|
||||
emoji==2.11.1
|
||||
eval-type-backport==0.2.0
|
||||
exceptiongroup==1.2.1; python_version < "3.11"
|
||||
filelock==3.14.0
|
||||
fiona==1.9.6
|
||||
fonttools==4.51.0
|
||||
frozenlist==1.4.1
|
||||
fsspec==2024.3.1
|
||||
geopandas==0.14.4
|
||||
github-poster==2.7.4
|
||||
google-ai-generativelanguage==0.6.2
|
||||
google-api-core==2.19.0
|
||||
google-api-python-client==2.128.0
|
||||
google-auth==2.29.0
|
||||
google-auth-httplib2==0.2.0
|
||||
google-generativeai==0.5.2
|
||||
googleapis-common-protos==1.63.0
|
||||
groq==0.5.0
|
||||
grpcio==1.63.0
|
||||
grpcio-status==1.62.2
|
||||
h11==0.14.0
|
||||
httpcore==1.0.5
|
||||
httplib2==0.22.0
|
||||
httpx==0.27.0
|
||||
huggingface-hub==0.23.0
|
||||
idna==3.7
|
||||
kiwisolver==1.4.5
|
||||
markdown-it-py==3.0.0
|
||||
matplotlib==3.8.4
|
||||
mdurl==0.1.2
|
||||
mistletoe==1.3.0
|
||||
multidict==6.0.5
|
||||
networkx==3.3
|
||||
numpy==1.26.4
|
||||
openai==1.26.0
|
||||
osmnx==1.9.2
|
||||
packaging==24.0
|
||||
pandas==2.2.2
|
||||
pendulum==3.0.0
|
||||
pillow==10.3.0
|
||||
platformdirs==4.2.1
|
||||
prettymapp==0.3.0
|
||||
proto-plus==1.23.0
|
||||
protobuf==4.25.3
|
||||
pyarrow==16.0.0
|
||||
pyasn1==0.6.0
|
||||
pyasn1-modules==0.4.0
|
||||
pycparser==2.22
|
||||
pydantic==2.7.1
|
||||
pydantic-core==2.18.2
|
||||
pygments==2.18.0
|
||||
pyogrio==0.7.2
|
||||
pyparsing==3.1.2
|
||||
pyproj==3.6.1
|
||||
pytelegrambotapi==4.17.0
|
||||
python-dateutil==2.9.0.post0
|
||||
pytz==2024.1
|
||||
pyyaml==6.0.1
|
||||
requests==2.31.0
|
||||
rich==13.7.1
|
||||
rsa==4.9
|
||||
shapely==2.0.4
|
||||
shellingham==1.5.4
|
||||
six==1.16.0
|
||||
sniffio==1.3.1
|
||||
svgwrite==1.4.3
|
||||
tabulate==0.9.0
|
||||
telegramify-markdown==0.1.2
|
||||
time-machine==2.14.1; implementation_name != "pypy"
|
||||
tinycss2==1.3.0
|
||||
together==1.1.5
|
||||
tokenizers==0.19.1
|
||||
tqdm==4.66.4
|
||||
typer==0.12.3
|
||||
typing-extensions==4.11.0
|
||||
tzdata==2024.1
|
||||
uritemplate==4.1.1
|
||||
uritools==4.0.2
|
||||
urlextract==1.9.0
|
||||
urllib3==2.2.1
|
||||
webencodings==0.5.1
|
||||
yarl==1.9.4
|
||||
|
Loading…
x
Reference in New Issue
Block a user