mirror of
https://github.com/langbot-app/LangBot.git
synced 2025-11-26 03:44:58 +08:00
Compare commits
97 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bd0438df76 | ||
|
|
9ca1fc59ef | ||
|
|
84a80a5ec8 | ||
|
|
4b2e248646 | ||
|
|
b90e45590a | ||
|
|
ff93d563a8 | ||
|
|
53228498ed | ||
|
|
8ece82e43a | ||
|
|
8b4684675e | ||
|
|
8cca12fff2 | ||
|
|
a74111612e | ||
|
|
c7799a65c4 | ||
|
|
aabb01c50f | ||
|
|
95e2ada965 | ||
|
|
3fe7d53c76 | ||
|
|
e8634bb1ab | ||
|
|
dbe46b5770 | ||
|
|
6d9fba30b1 | ||
|
|
6a866bf871 | ||
|
|
3c961e4652 | ||
|
|
7abd999420 | ||
|
|
fca8fbb135 | ||
|
|
c67caf18df | ||
|
|
fe956fe4a5 | ||
|
|
0e52f679a2 | ||
|
|
b9500283ec | ||
|
|
441b69b528 | ||
|
|
898bcdc96b | ||
|
|
02bc1fc45e | ||
|
|
5585981dc3 | ||
|
|
a4777f194b | ||
|
|
41aeda8dc0 | ||
|
|
2ed522667e | ||
|
|
1932444666 | ||
|
|
b49b7e963d | ||
|
|
435c11ff27 | ||
|
|
2e93600437 | ||
|
|
faecb70d0f | ||
|
|
92e1ac5c3a | ||
|
|
8963a2117b | ||
|
|
aa300258ab | ||
|
|
48841daff5 | ||
|
|
8878f1ed87 | ||
|
|
f6205d79c0 | ||
|
|
d6d5dac6b3 | ||
|
|
05b979e68a | ||
|
|
9f7d9e4c0d | ||
|
|
98a9fed726 | ||
|
|
720a218259 | ||
|
|
60c0adc6f9 | ||
|
|
bc8c346e68 | ||
|
|
a198b6da0b | ||
|
|
0f3dc35df4 | ||
|
|
7b6e6b046a | ||
|
|
9e503191d6 | ||
|
|
1fd23a0d8d | ||
|
|
3811700a78 | ||
|
|
8762ba3d9c | ||
|
|
c42b5aab5a | ||
|
|
d724899ec0 | ||
|
|
81aacdd76e | ||
|
|
0aa072b4e8 | ||
|
|
6335e9dd8b | ||
|
|
a785289ac9 | ||
|
|
f8bace040c | ||
|
|
d62d597695 | ||
|
|
d938129884 | ||
|
|
327f448321 | ||
|
|
19af3740c1 | ||
|
|
11b1110eed | ||
|
|
682b897e21 | ||
|
|
998ad7623c | ||
|
|
4f1db33abc | ||
|
|
ca6cb60bdd | ||
|
|
133e48a5a9 | ||
|
|
d659d01b1e | ||
|
|
34f73fd84b | ||
|
|
54b87ff79d | ||
|
|
6c2843e7c1 | ||
|
|
6761a31982 | ||
|
|
9401a79b2b | ||
|
|
7a4905d943 | ||
|
|
4db1d2b3a3 | ||
|
|
2ffe2967d6 | ||
|
|
0875c0f266 | ||
|
|
68c7de5199 | ||
|
|
4dfb8597ae | ||
|
|
e21a27ff23 | ||
|
|
91ad7944de | ||
|
|
c86602ebaf | ||
|
|
f75ac292db | ||
|
|
2742c249bf | ||
|
|
36f04849ab | ||
|
|
a60c896e89 | ||
|
|
c442320c7f | ||
|
|
6aeae7e9f5 | ||
|
|
cae79aac48 |
16
README.md
16
README.md
@@ -9,7 +9,7 @@
|
||||
<a href="https://trendshift.io/repositories/12901" target="_blank"><img src="https://trendshift.io/api/badge/repositories/12901" alt="RockChinQ%2FLangBot | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
||||
|
||||
<a href="https://docs.langbot.app">项目主页</a> |
|
||||
<a href="https://docs.langbot.app/insight/intro.htmll">功能介绍</a> |
|
||||
<a href="https://docs.langbot.app/insight/intro.html">功能介绍</a> |
|
||||
<a href="https://docs.langbot.app/insight/guide.html">部署文档</a> |
|
||||
<a href="https://docs.langbot.app/usage/faq.html">常见问题</a> |
|
||||
<a href="https://docs.langbot.app/plugin/plugin-intro.html">插件介绍</a> |
|
||||
@@ -115,6 +115,20 @@
|
||||
| [阿里云百炼](https://bailian.console.aliyun.com/) | ✅ | 大模型聚合平台, LLMOps 平台 |
|
||||
| [火山方舟](https://console.volcengine.com/ark/region:ark+cn-beijing/model?vendor=Bytedance&view=LIST_VIEW) | ✅ | 大模型聚合平台, LLMOps 平台 |
|
||||
|
||||
### TTS
|
||||
|
||||
| 平台/模型 | 备注 |
|
||||
| --- | --- |
|
||||
| [FishAudio](https://fish.audio/zh-CN/discovery/) | [插件](https://github.com/the-lazy-me/NewChatVoice) |
|
||||
| [海豚 AI](https://www.ttson.cn/?source=thelazy) | [插件](https://github.com/the-lazy-me/NewChatVoice) |
|
||||
| [AzureTTS](https://portal.azure.com/) | [插件](https://github.com/Ingnaryk/LangBot_AzureTTS) |
|
||||
|
||||
### 文生图
|
||||
|
||||
| 平台/模型 | 备注 |
|
||||
| --- | --- |
|
||||
| 阿里云百炼 | [插件](https://github.com/Thetail001/LangBot_BailianTextToImagePlugin)
|
||||
|
||||
## 😘 社区贡献
|
||||
|
||||
LangBot 离不开以下贡献者和社区内所有人的贡献,我们欢迎任何形式的贡献和反馈。
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<a href="https://trendshift.io/repositories/12901" target="_blank"><img src="https://trendshift.io/api/badge/repositories/12901" alt="RockChinQ%2FLangBot | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
||||
|
||||
<a href="https://docs.langbot.app">Home</a> |
|
||||
<a href="https://docs.langbot.app/insight/intro.htmll">Features</a> |
|
||||
<a href="https://docs.langbot.app/insight/intro.html">Features</a> |
|
||||
<a href="https://docs.langbot.app/insight/guide.html">Deployment</a> |
|
||||
<a href="https://docs.langbot.app/usage/faq.html">FAQ</a> |
|
||||
<a href="https://docs.langbot.app/plugin/plugin-intro.html">Plugin</a> |
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<a href="https://trendshift.io/repositories/12901" target="_blank"><img src="https://trendshift.io/api/badge/repositories/12901" alt="RockChinQ%2FLangBot | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
||||
|
||||
<a href="https://docs.langbot.app">ホーム</a> |
|
||||
<a href="https://docs.langbot.app/insight/intro.htmll">機能</a> |
|
||||
<a href="https://docs.langbot.app/insight/intro.html">機能</a> |
|
||||
<a href="https://docs.langbot.app/insight/guide.html">デプロイ</a> |
|
||||
<a href="https://docs.langbot.app/usage/faq.html">FAQ</a> |
|
||||
<a href="https://docs.langbot.app/plugin/plugin-intro.html">プラグイン</a> |
|
||||
|
||||
@@ -10,6 +10,10 @@ spec:
|
||||
ComponentTemplate:
|
||||
fromFiles:
|
||||
- pkg/platform/adapter.yaml
|
||||
- pkg/provider/modelmgr/requester.yaml
|
||||
MessagePlatformAdapter:
|
||||
fromDirs:
|
||||
- path: pkg/platform/sources/
|
||||
LLMAPIRequester:
|
||||
fromDirs:
|
||||
- path: pkg/provider/modelmgr/requesters/
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import json
|
||||
import dingtalk_stream
|
||||
from dingtalk_stream import AckMessage
|
||||
|
||||
@@ -17,11 +18,12 @@ class EchoTextHandler(dingtalk_stream.ChatbotHandler):
|
||||
await self.client.update_incoming_message(incoming_message)
|
||||
|
||||
return AckMessage.STATUS_OK, 'OK'
|
||||
|
||||
|
||||
async def get_incoming_message(self):
|
||||
"""异步等待消息的到来"""
|
||||
while self.incoming_message is None:
|
||||
await asyncio.sleep(0.1) # 异步等待,避免阻塞
|
||||
|
||||
return self.incoming_message
|
||||
|
||||
async def get_dingtalk_client(client_id, client_secret):
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import base64
|
||||
import json
|
||||
import time
|
||||
from typing import Callable
|
||||
import dingtalk_stream
|
||||
@@ -92,8 +93,31 @@ class DingTalkClient:
|
||||
base64_str = base64.b64encode(file_bytes).decode('utf-8') # 返回字符串格式
|
||||
return base64_str
|
||||
else:
|
||||
raise Exception("获取图片失败")
|
||||
|
||||
raise Exception("获取文件失败")
|
||||
|
||||
async def get_audio_url(self,download_code:str):
|
||||
if not await self.check_access_token():
|
||||
await self.get_access_token()
|
||||
url = 'https://api.dingtalk.com/v1.0/robot/messageFiles/download'
|
||||
params = {
|
||||
"downloadCode":download_code,
|
||||
"robotCode":self.robot_code
|
||||
}
|
||||
headers ={
|
||||
"x-acs-dingtalk-access-token": self.access_token
|
||||
}
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(url, headers=headers, json=params)
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
download_url = result.get("downloadUrl")
|
||||
if download_url:
|
||||
return await self.download_url_to_base64(download_url)
|
||||
else:
|
||||
raise Exception("获取音频失败")
|
||||
else:
|
||||
raise Exception(f"Error: {response.status_code}, {response.text}")
|
||||
|
||||
async def update_incoming_message(self, message):
|
||||
"""异步更新 DingTalkClient 中的 incoming_message"""
|
||||
message_data = await self.get_message(message)
|
||||
@@ -133,6 +157,8 @@ class DingTalkClient:
|
||||
|
||||
async def get_message(self,incoming_message:dingtalk_stream.chatbot.ChatbotMessage):
|
||||
try:
|
||||
|
||||
# print(json.dumps(incoming_message.to_dict(), indent=4, ensure_ascii=False))
|
||||
message_data = {
|
||||
"IncomingMessage":incoming_message,
|
||||
}
|
||||
@@ -160,14 +186,65 @@ class DingTalkClient:
|
||||
message_data['Picture'] = await self.download_image(incoming_message.get_image_list()[0])
|
||||
|
||||
message_data['Type'] = 'image'
|
||||
elif incoming_message.message_type == 'audio':
|
||||
message_data['Audio'] = await self.get_audio_url(incoming_message.to_dict()['content']['downloadCode'])
|
||||
|
||||
# 删掉开头的@消息
|
||||
if message_data["Content"].startswith("@"+self.robot_name):
|
||||
message_data["Content"][len("@"+self.robot_name):]
|
||||
message_data['Type'] = 'audio'
|
||||
|
||||
copy_message_data = message_data.copy()
|
||||
del copy_message_data['IncomingMessage']
|
||||
# print("message_data:", json.dumps(copy_message_data, indent=4, ensure_ascii=False))
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
|
||||
return message_data
|
||||
|
||||
async def send_proactive_message_to_one(self,target_id:str,content:str):
|
||||
if not await self.check_access_token():
|
||||
await self.get_access_token()
|
||||
|
||||
url = 'https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend'
|
||||
|
||||
headers ={
|
||||
"x-acs-dingtalk-access-token":self.access_token,
|
||||
"Content-Type":"application/json",
|
||||
}
|
||||
|
||||
data ={
|
||||
"robotCode":self.robot_code,
|
||||
"userIds":[target_id],
|
||||
"msgKey": "sampleText",
|
||||
"msgParam": json.dumps({"content":content}),
|
||||
}
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(url,headers=headers,json=data)
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
async def send_proactive_message_to_group(self,target_id:str,content:str):
|
||||
if not await self.check_access_token():
|
||||
await self.get_access_token()
|
||||
|
||||
url = 'https://api.dingtalk.com/v1.0/robot/groupMessages/send'
|
||||
|
||||
headers ={
|
||||
"x-acs-dingtalk-access-token":self.access_token,
|
||||
"Content-Type":"application/json",
|
||||
}
|
||||
|
||||
data ={
|
||||
"robotCode":self.robot_code,
|
||||
"openConversationId":target_id,
|
||||
"msgKey": "sampleText",
|
||||
"msgParam": json.dumps({"content":content}),
|
||||
}
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(url,headers=headers,json=data)
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
|
||||
async def start(self):
|
||||
"""启动 WebSocket 连接,监听消息"""
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from typing import Dict, Any, Optional
|
||||
import dingtalk_stream
|
||||
|
||||
class DingTalkEvent(dict):
|
||||
@staticmethod
|
||||
@@ -15,7 +16,7 @@ class DingTalkEvent(dict):
|
||||
return self.get("Content","")
|
||||
|
||||
@property
|
||||
def incoming_message(self):
|
||||
def incoming_message(self) -> Optional["dingtalk_stream.chatbot.ChatbotMessage"]:
|
||||
return self.get("IncomingMessage")
|
||||
|
||||
@property
|
||||
@@ -25,6 +26,10 @@ class DingTalkEvent(dict):
|
||||
@property
|
||||
def picture(self):
|
||||
return self.get("Picture","")
|
||||
|
||||
@property
|
||||
def audio(self):
|
||||
return self.get("Audio","")
|
||||
|
||||
@property
|
||||
def conversation(self):
|
||||
@@ -61,4 +66,4 @@ class DingTalkEvent(dict):
|
||||
Returns:
|
||||
str: 字符串表示。
|
||||
"""
|
||||
return f"<WecomEvent {super().__repr__()}>"
|
||||
return f"<DingTalkEvent {super().__repr__()}>"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
# 微信公众号的加解密算法与企业微信一样,所以直接使用企业微信的加解密算法文件
|
||||
from collections import deque
|
||||
import time
|
||||
import traceback
|
||||
from ..wecom_api.WXBizMsgCrypt3 import WXBizMsgCrypt
|
||||
@@ -12,6 +13,7 @@ import httpx
|
||||
import asyncio
|
||||
import time
|
||||
import xml.etree.ElementTree as ET
|
||||
from pkg.platform.sources import officialaccount as oa
|
||||
|
||||
|
||||
|
||||
@@ -25,6 +27,8 @@ xml_template = """
|
||||
</xml>
|
||||
"""
|
||||
|
||||
user_msg_queue = {}
|
||||
|
||||
class OAClient():
|
||||
|
||||
def __init__(self,token:str,EncodingAESKey:str,AppID:str,Appsecret:str):
|
||||
@@ -169,6 +173,152 @@ class OAClient():
|
||||
await handler(event)
|
||||
|
||||
|
||||
|
||||
class OAClientForLongerResponse():
|
||||
|
||||
def __init__(self,token:str,EncodingAESKey:str,AppID:str,Appsecret:str):
|
||||
self.token = token
|
||||
self.aes = EncodingAESKey
|
||||
self.appid = AppID
|
||||
self.appsecret = Appsecret
|
||||
self.base_url = 'https://api.weixin.qq.com'
|
||||
self.access_token = ''
|
||||
self.app = Quart(__name__)
|
||||
self.app.add_url_rule('/callback/command', 'handle_callback', self.handle_callback_request, methods=['GET', 'POST'])
|
||||
self._message_handlers = {
|
||||
"example":[],
|
||||
}
|
||||
self.access_token_expiry_time = None
|
||||
|
||||
async def handle_callback_request(self):
|
||||
try:
|
||||
start_time = time.time()
|
||||
signature = request.args.get("signature", "")
|
||||
timestamp = request.args.get("timestamp", "")
|
||||
nonce = request.args.get("nonce", "")
|
||||
echostr = request.args.get("echostr", "")
|
||||
msg_signature = request.args.get("msg_signature", "")
|
||||
|
||||
if msg_signature is None:
|
||||
raise Exception("msg_signature不在请求体中")
|
||||
|
||||
if request.method == 'GET':
|
||||
check_str = "".join(sorted([self.token, timestamp, nonce]))
|
||||
check_signature = hashlib.sha1(check_str.encode("utf-8")).hexdigest()
|
||||
return echostr if check_signature == signature else "拒绝请求"
|
||||
|
||||
elif request.method == "POST":
|
||||
encryt_msg = await request.data
|
||||
wxcpt = WXBizMsgCrypt(self.token, self.aes, self.appid)
|
||||
ret, xml_msg = wxcpt.DecryptMsg(encryt_msg, msg_signature, timestamp, nonce)
|
||||
xml_msg = xml_msg.decode('utf-8')
|
||||
|
||||
if ret != 0:
|
||||
raise Exception("消息解密失败")
|
||||
|
||||
# 解析 XML
|
||||
root = ET.fromstring(xml_msg)
|
||||
from_user = root.find("FromUserName").text
|
||||
to_user = root.find("ToUserName").text
|
||||
|
||||
|
||||
from pkg.platform.sources import officialaccount as oa
|
||||
|
||||
|
||||
if oa.msg_queue.get(from_user) and oa.msg_queue[from_user][0]["content"]:
|
||||
queue_top = oa.msg_queue[from_user].pop(0)
|
||||
queue_content = queue_top["content"]
|
||||
|
||||
# 弹出用户消息
|
||||
if user_msg_queue.get(from_user) and user_msg_queue[from_user]:
|
||||
user_msg_queue[from_user].pop(0)
|
||||
|
||||
response_xml = xml_template.format(
|
||||
to_user=from_user,
|
||||
from_user=to_user,
|
||||
create_time=int(time.time()),
|
||||
content=queue_content
|
||||
)
|
||||
return response_xml
|
||||
|
||||
else:
|
||||
response_xml = xml_template.format(
|
||||
to_user=from_user,
|
||||
from_user=to_user,
|
||||
create_time=int(time.time()),
|
||||
content="AI正在思考中,请发送任意内容获取回答。"
|
||||
)
|
||||
|
||||
if user_msg_queue.get(from_user) and user_msg_queue[from_user][0]["content"]:
|
||||
return response_xml
|
||||
else:
|
||||
message_data = await self.get_message(xml_msg)
|
||||
|
||||
if message_data:
|
||||
event = OAEvent.from_payload(message_data)
|
||||
if event:
|
||||
user_msg_queue.setdefault(from_user,[]).append(
|
||||
{
|
||||
"content":event.message,
|
||||
}
|
||||
)
|
||||
await self._handle_message(event)
|
||||
|
||||
return response_xml
|
||||
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
|
||||
async def get_message(self, xml_msg: str):
|
||||
|
||||
root = ET.fromstring(xml_msg)
|
||||
|
||||
message_data = {
|
||||
"ToUserName": root.find("ToUserName").text,
|
||||
"FromUserName": root.find("FromUserName").text,
|
||||
"CreateTime": int(root.find("CreateTime").text),
|
||||
"MsgType": root.find("MsgType").text,
|
||||
"Content": root.find("Content").text if root.find("Content") is not None else None,
|
||||
"MsgId": int(root.find("MsgId").text) if root.find("MsgId") is not None else None,
|
||||
}
|
||||
|
||||
return message_data
|
||||
|
||||
|
||||
|
||||
async def run_task(self, host: str, port: int, *args, **kwargs):
|
||||
"""
|
||||
启动 Quart 应用。
|
||||
"""
|
||||
await self.app.run_task(host=host, port=port, *args, **kwargs)
|
||||
|
||||
|
||||
|
||||
def on_message(self, msg_type: str):
|
||||
"""
|
||||
注册消息类型处理器。
|
||||
"""
|
||||
def decorator(func: Callable[[OAEvent], None]):
|
||||
if msg_type not in self._message_handlers:
|
||||
self._message_handlers[msg_type] = []
|
||||
self._message_handlers[msg_type].append(func)
|
||||
return func
|
||||
return decorator
|
||||
|
||||
async def _handle_message(self, event: OAEvent):
|
||||
"""
|
||||
处理消息事件。
|
||||
"""
|
||||
|
||||
msg_type = event.type
|
||||
if msg_type in self._message_handlers:
|
||||
for handler in self._message_handlers[msg_type]:
|
||||
await handler(event)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ class WecomClient():
|
||||
else:
|
||||
raise Exception("未获取用户")
|
||||
|
||||
async def send_to_all(self,content:str):
|
||||
async def send_to_all(self,content:str,agent_id:int):
|
||||
if not self.check_access_token_for_contacts():
|
||||
self.access_token_for_contacts = await self.get_access_token(self.secret_for_contacts)
|
||||
|
||||
@@ -77,7 +77,7 @@ class WecomClient():
|
||||
params = {
|
||||
"touser" : user_ids_string,
|
||||
"msgtype" : "text",
|
||||
"agentid" : 1000002,
|
||||
"agentid" : agent_id,
|
||||
"text" : {
|
||||
"content" : content,
|
||||
},
|
||||
|
||||
@@ -29,6 +29,7 @@ from ..discover import engine as discover_engine
|
||||
from ..utils import logcache, ip
|
||||
from . import taskmgr
|
||||
from . import entities as core_entities
|
||||
from .bootutils import config
|
||||
|
||||
|
||||
class Application:
|
||||
@@ -203,6 +204,8 @@ class Application:
|
||||
case core_entities.LifecycleControlScope.PROVIDER.value:
|
||||
self.logger.info("执行热重载 scope="+scope)
|
||||
|
||||
latest_llm_model_config = await config.load_json_config("data/metadata/llm-models.json", "templates/metadata/llm-models.json")
|
||||
self.llm_models_meta = latest_llm_model_config
|
||||
llm_model_mgr_inst = llm_model_mgr.ModelManager(self)
|
||||
await llm_model_mgr_inst.initialize()
|
||||
self.model_mgr = llm_model_mgr_inst
|
||||
|
||||
@@ -72,6 +72,9 @@ class Query(pydantic.BaseModel):
|
||||
user_message: typing.Optional[llm_entities.Message] = None
|
||||
"""此次请求的用户消息对象,由前置处理器阶段设置"""
|
||||
|
||||
variables: typing.Optional[dict[str, typing.Any]] = None
|
||||
"""变量,由前置处理器阶段设置。在prompt中嵌入或由 Runner 传递到 LLMOps 平台。"""
|
||||
|
||||
use_model: typing.Optional[entities.LLMModelInfo] = None
|
||||
"""使用的模型,由前置处理器阶段设置"""
|
||||
|
||||
@@ -86,10 +89,31 @@ class Query(pydantic.BaseModel):
|
||||
|
||||
# ======= 内部保留 =======
|
||||
current_stage: "pkg.pipeline.stagemgr.StageInstContainer" = None
|
||||
"""当前所处阶段"""
|
||||
|
||||
class Config:
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
# ========== 插件可调用的 API(请求 API) ==========
|
||||
|
||||
def set_variable(self, key: str, value: typing.Any):
|
||||
"""设置变量"""
|
||||
if self.variables is None:
|
||||
self.variables = {}
|
||||
self.variables[key] = value
|
||||
|
||||
def get_variable(self, key: str) -> typing.Any:
|
||||
"""获取变量"""
|
||||
if self.variables is None:
|
||||
return None
|
||||
return self.variables.get(key)
|
||||
|
||||
def get_variables(self) -> dict[str, typing.Any]:
|
||||
"""获取所有变量"""
|
||||
if self.variables is None:
|
||||
return {}
|
||||
return self.variables
|
||||
|
||||
|
||||
class Conversation(pydantic.BaseModel):
|
||||
"""对话,包含于 Session 中,一个 Session 可以有多个历史 Conversation,但只有一个当前使用的 Conversation"""
|
||||
|
||||
@@ -23,6 +23,7 @@ class GewechatConfigMigration(migration.Migration):
|
||||
"adapter": "gewechat",
|
||||
"enable": False,
|
||||
"gewechat_url": "http://your-gewechat-server:2531",
|
||||
"gewechat_file_url": "http://your-gewechat-server:2532",
|
||||
"port": 2286,
|
||||
"callback_url": "http://your-callback-url:2286/gewechat/callback",
|
||||
"app_id": "",
|
||||
|
||||
29
pkg/core/migrations/m034_gewechat_file_url_config.py
Normal file
29
pkg/core/migrations/m034_gewechat_file_url_config.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from .. import migration
|
||||
|
||||
|
||||
@migration.migration_class("gewechat-file-url-config", 34)
|
||||
class GewechatFileUrlConfigMigration(migration.Migration):
|
||||
"""迁移"""
|
||||
|
||||
async def need_migrate(self) -> bool:
|
||||
"""判断当前环境是否需要运行此迁移"""
|
||||
|
||||
for adapter in self.ap.platform_cfg.data['platform-adapters']:
|
||||
if adapter['adapter'] == 'gewechat':
|
||||
if 'gewechat_file_url' not in adapter:
|
||||
return True
|
||||
return False
|
||||
|
||||
async def run(self):
|
||||
"""执行迁移"""
|
||||
for adapter in self.ap.platform_cfg.data['platform-adapters']:
|
||||
if adapter['adapter'] == 'gewechat':
|
||||
if 'gewechat_file_url' not in adapter:
|
||||
parsed_url = urlparse(adapter['gewechat_url'])
|
||||
adapter['gewechat_file_url'] = f"{parsed_url.scheme}://{parsed_url.hostname}:2532"
|
||||
|
||||
await self.ap.platform_cfg.dump_config()
|
||||
26
pkg/core/migrations/m035_wxoa_mode.py
Normal file
26
pkg/core/migrations/m035_wxoa_mode.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from .. import migration
|
||||
|
||||
|
||||
@migration.migration_class("wxoa-mode", 35)
|
||||
class WxoaModeMigration(migration.Migration):
|
||||
"""迁移"""
|
||||
|
||||
async def need_migrate(self) -> bool:
|
||||
"""判断当前环境是否需要运行此迁移"""
|
||||
|
||||
for adapter in self.ap.platform_cfg.data['platform-adapters']:
|
||||
if adapter['adapter'] == 'officialaccount':
|
||||
if 'Mode' not in adapter:
|
||||
return True
|
||||
return False
|
||||
|
||||
async def run(self):
|
||||
"""执行迁移"""
|
||||
for adapter in self.ap.platform_cfg.data['platform-adapters']:
|
||||
if adapter['adapter'] == 'officialaccount':
|
||||
if 'Mode' not in adapter:
|
||||
adapter['Mode'] = 'drop'
|
||||
|
||||
await self.ap.platform_cfg.dump_config()
|
||||
@@ -11,7 +11,7 @@ from ..migrations import m015_gitee_ai_config, m016_dify_service_api, m017_dify_
|
||||
from ..migrations import m020_wecom_config, m021_lark_config, m022_lmstudio_config, m023_siliconflow_config, m024_discord_config, m025_gewechat_config
|
||||
from ..migrations import m026_qqofficial_config, m027_wx_official_account_config, m028_aliyun_requester_config
|
||||
from ..migrations import m029_dashscope_app_api_config, m030_lark_config_cmpl, m031_dingtalk_config, m032_volcark_config
|
||||
from ..migrations import m033_dify_thinking_config
|
||||
from ..migrations import m033_dify_thinking_config, m034_gewechat_file_url_config, m035_wxoa_mode
|
||||
|
||||
@stage.stage_class("MigrationStage")
|
||||
class MigrationStage(stage.BootingStage):
|
||||
|
||||
@@ -187,6 +187,9 @@ class ComponentDiscoveryEngine:
|
||||
if name == 'ComponentTemplate':
|
||||
continue
|
||||
components[name] = self.load_blueprint_comp_group(component, owner)
|
||||
|
||||
self.ap.logger.debug(f'Components: {components}')
|
||||
|
||||
return blueprint_manifest, components
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
|
||||
from .. import stage, entities, stagemgr
|
||||
from ...core import entities as core_entities
|
||||
@@ -34,7 +35,7 @@ class PreProcessor(stage.PipelineStage):
|
||||
|
||||
conversation = await self.ap.sess_mgr.get_conversation(session)
|
||||
|
||||
# 从会话取出消息和情景预设到query
|
||||
# 设置query
|
||||
query.session = session
|
||||
query.prompt = conversation.prompt.copy()
|
||||
query.messages = conversation.messages.copy()
|
||||
@@ -43,6 +44,11 @@ class PreProcessor(stage.PipelineStage):
|
||||
|
||||
query.use_funcs = conversation.use_funcs if query.use_model.tool_call_supported else None
|
||||
|
||||
query.variables = {
|
||||
"session_id": f"{query.session.launcher_type.value}_{query.session.launcher_id}",
|
||||
"conversation_id": conversation.uuid,
|
||||
"msg_create_time": int(query.message_event.time) if query.message_event.time else int(datetime.datetime.now().timestamp()),
|
||||
}
|
||||
|
||||
# 检查vision是否启用,没启用就删除所有图片
|
||||
if not self.ap.provider_cfg.data['enable-vision'] or (self.ap.provider_cfg.data['runner'] == 'local-agent' and not query.use_model.vision_supported):
|
||||
|
||||
@@ -110,7 +110,7 @@ class PlatformManager:
|
||||
if len(self.adapters) == 0:
|
||||
self.ap.logger.warning('未运行平台适配器,请根据文档配置并启用平台适配器。')
|
||||
|
||||
async def write_back_config(self, adapter_inst: msadapter.MessagePlatformAdapter, config: dict):
|
||||
async def write_back_config(self, adapter_name: str, adapter_inst: msadapter.MessagePlatformAdapter, config: dict):
|
||||
index = -2
|
||||
|
||||
for i, adapter in enumerate(self.adapters):
|
||||
@@ -132,7 +132,7 @@ class PlatformManager:
|
||||
break
|
||||
|
||||
new_cfg = {
|
||||
'adapter': adapter_inst.name,
|
||||
'adapter': adapter_name,
|
||||
'enable': True,
|
||||
**config
|
||||
}
|
||||
|
||||
@@ -28,16 +28,23 @@ class DingTalkMessageConverter(adapter.MessageConverter):
|
||||
return msg.text
|
||||
|
||||
@staticmethod
|
||||
async def target2yiri(event:DingTalkEvent):
|
||||
async def target2yiri(event:DingTalkEvent, bot_name:str):
|
||||
yiri_msg_list = []
|
||||
yiri_msg_list.append(
|
||||
platform_message.Source(id = '0',time=datetime.datetime.now())
|
||||
platform_message.Source(id = event.incoming_message.message_id,time=datetime.datetime.now())
|
||||
)
|
||||
|
||||
for atUser in event.incoming_message.at_users:
|
||||
if atUser.dingtalk_id == event.incoming_message.chatbot_user_id:
|
||||
yiri_msg_list.append(platform_message.At(target=bot_name))
|
||||
|
||||
if event.content:
|
||||
yiri_msg_list.append(platform_message.Plain(text=event.content))
|
||||
text_content = event.content.replace("@"+bot_name, '')
|
||||
yiri_msg_list.append(platform_message.Plain(text=text_content))
|
||||
if event.picture:
|
||||
yiri_msg_list.append(platform_message.Image(base64=event.picture))
|
||||
if event.audio:
|
||||
yiri_msg_list.append(platform_message.Voice(base64=event.audio))
|
||||
|
||||
chain = platform_message.MessageChain(yiri_msg_list)
|
||||
|
||||
@@ -54,33 +61,33 @@ class DingTalkEventConverter(adapter.EventConverter):
|
||||
|
||||
@staticmethod
|
||||
async def target2yiri(
|
||||
event:DingTalkEvent
|
||||
event:DingTalkEvent,
|
||||
bot_name:str
|
||||
):
|
||||
|
||||
message_chain = await DingTalkMessageConverter.target2yiri(event)
|
||||
message_chain = await DingTalkMessageConverter.target2yiri(event, bot_name)
|
||||
|
||||
|
||||
if event.conversation == 'FriendMessage':
|
||||
|
||||
return platform_events.FriendMessage(
|
||||
sender=platform_entities.Friend(
|
||||
id= 0,
|
||||
nickname ='nickname',
|
||||
id=event.incoming_message.sender_id,
|
||||
nickname = event.incoming_message.sender_nick,
|
||||
remark=""
|
||||
),
|
||||
message_chain = message_chain,
|
||||
time = datetime.datetime.now(),
|
||||
time = event.incoming_message.create_at,
|
||||
source_platform_object=event,
|
||||
)
|
||||
elif event.conversation == 'GroupMessage':
|
||||
message_chain.insert(0, platform_message.At(target="justbot"))
|
||||
sender = platform_entities.GroupMember(
|
||||
id = 111,
|
||||
member_name="name",
|
||||
id = event.incoming_message.sender_id,
|
||||
member_name=event.incoming_message.sender_nick,
|
||||
permission= 'MEMBER',
|
||||
group = platform_entities.Group(
|
||||
id = 111,
|
||||
name = 'MEMBER',
|
||||
id = event.incoming_message.conversation_id,
|
||||
name = event.incoming_message.conversation_title,
|
||||
permission=platform_entities.Permission.Member
|
||||
),
|
||||
special_title='',
|
||||
@@ -88,7 +95,7 @@ class DingTalkEventConverter(adapter.EventConverter):
|
||||
last_speak_timestamp=0,
|
||||
mute_time_remaining=0
|
||||
)
|
||||
time = datetime.datetime.now(),
|
||||
time = event.incoming_message.create_at
|
||||
return platform_events.GroupMessage(
|
||||
sender =sender,
|
||||
message_chain = message_chain,
|
||||
@@ -117,6 +124,8 @@ class DingTalkAdapter(adapter.MessagePlatformAdapter):
|
||||
missing_keys = [key for key in required_keys if key not in config]
|
||||
if missing_keys:
|
||||
raise ParamNotEnoughError("钉钉缺少相关配置项,请查看文档或联系管理员")
|
||||
|
||||
self.bot_account_id = self.config["robot_name"]
|
||||
|
||||
self.bot = DingTalkClient(
|
||||
client_id=config["client_id"],
|
||||
@@ -143,7 +152,11 @@ class DingTalkAdapter(adapter.MessagePlatformAdapter):
|
||||
async def send_message(
|
||||
self, target_type: str, target_id: str, message: platform_message.MessageChain
|
||||
):
|
||||
pass
|
||||
content = await DingTalkMessageConverter.yiri2target(message)
|
||||
if target_type == 'person':
|
||||
await self.bot.send_proactive_message_to_one(target_id,content)
|
||||
if target_type == 'group':
|
||||
await self.bot.send_proactive_message_to_group(target_id,content)
|
||||
|
||||
def register_listener(
|
||||
self,
|
||||
@@ -153,10 +166,9 @@ class DingTalkAdapter(adapter.MessagePlatformAdapter):
|
||||
],
|
||||
):
|
||||
async def on_message(event: DingTalkEvent):
|
||||
self.bot_account_id = 'justbot'
|
||||
try:
|
||||
return await callback(
|
||||
await self.event_converter.target2yiri(event), self
|
||||
await self.event_converter.target2yiri(event, self.config["robot_name"]), self
|
||||
)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
@@ -167,7 +179,6 @@ class DingTalkAdapter(adapter.MessagePlatformAdapter):
|
||||
self.bot.on_message("GroupMessage")(on_message)
|
||||
|
||||
async def run_async(self):
|
||||
|
||||
await self.bot.start()
|
||||
|
||||
async def kill(self) -> bool:
|
||||
|
||||
@@ -28,7 +28,10 @@ from ...utils import image
|
||||
|
||||
|
||||
class GewechatMessageConverter(adapter.MessageConverter):
|
||||
|
||||
|
||||
def __init__(self, config: dict):
|
||||
self.config = config
|
||||
|
||||
@staticmethod
|
||||
async def yiri2target(
|
||||
message_chain: platform_message.MessageChain
|
||||
@@ -40,59 +43,125 @@ class GewechatMessageConverter(adapter.MessageConverter):
|
||||
elif isinstance(component, platform_message.Plain):
|
||||
content_list.append({"type": "text", "content": component.text})
|
||||
elif isinstance(component, platform_message.Image):
|
||||
# content_list.append({"type": "image", "image_id": component.image_id})
|
||||
pass
|
||||
if not component.url:
|
||||
pass
|
||||
content_list.append({"type": "image", "image": component.url})
|
||||
|
||||
|
||||
elif isinstance(component, platform_message.Voice):
|
||||
content_list.append({"type": "voice", "url": component.url, "length": component.length})
|
||||
elif isinstance(component, platform_message.Forward):
|
||||
for node in component.node_list:
|
||||
content_list.extend(await GewechatMessageConverter.yiri2target(node.message_chain))
|
||||
|
||||
return content_list
|
||||
|
||||
@staticmethod
|
||||
async def target2yiri(
|
||||
self,
|
||||
message: dict,
|
||||
bot_account_id: str
|
||||
) -> platform_message.MessageChain:
|
||||
|
||||
|
||||
|
||||
|
||||
if message["Data"]["MsgType"] == 1:
|
||||
# 检查消息开头,如果有 wxid_sbitaz0mt65n22:\n 则删掉
|
||||
regex = re.compile(r"^wxid_.*:")
|
||||
# print(message)
|
||||
|
||||
line_split = message["Data"]["Content"]["string"].split("\n")
|
||||
|
||||
if len(line_split) > 0 and regex.match(line_split[0]):
|
||||
message["Data"]["Content"]["string"] = "\n".join(line_split[1:])
|
||||
|
||||
at_string = f'@{bot_account_id}'
|
||||
# 正则表达式模式,匹配'@'后跟任意数量的非空白字符
|
||||
pattern = r'@\S+'
|
||||
at_string = f"@{bot_account_id}"
|
||||
content_list = []
|
||||
if at_string in message["Data"]["Content"]["string"]:
|
||||
content_list.append(platform_message.At(target=bot_account_id))
|
||||
content_list.append(platform_message.Plain(message["Data"]["Content"]["string"].replace(at_string, "", 1)))
|
||||
content_list.append(platform_message.Plain(message["Data"]["Content"]["string"].replace(at_string, '', 1)))
|
||||
# 更优雅的替换改名后@机器人,仅仅限于单独AT的情况
|
||||
elif '在群聊中@了你' in message["Data"]["PushContent"]:
|
||||
content_list.append(platform_message.At(target=bot_account_id))
|
||||
content_list.append(platform_message.Plain(re.sub(pattern, '', message["Data"]["Content"]["string"])))
|
||||
else:
|
||||
content_list = [platform_message.Plain(message["Data"]["Content"]["string"])]
|
||||
|
||||
return platform_message.MessageChain(content_list)
|
||||
|
||||
elif message["Data"]["MsgType"] == 3:
|
||||
image_base64 = message["Data"]["ImgBuf"]["buffer"]
|
||||
image_xml = message["Data"]["Content"]["string"]
|
||||
if not image_xml:
|
||||
return platform_message.MessageChain([
|
||||
platform_message.Plain(text="[图片内容为空]")
|
||||
])
|
||||
|
||||
|
||||
try:
|
||||
base64_str, image_format = await image.get_gewechat_image_base64(
|
||||
gewechat_url=self.config["gewechat_url"],
|
||||
gewechat_file_url=self.config["gewechat_file_url"],
|
||||
app_id=self.config["app_id"],
|
||||
xml_content=image_xml,
|
||||
token=self.config["token"],
|
||||
image_type=2,
|
||||
)
|
||||
|
||||
return platform_message.MessageChain([
|
||||
platform_message.Image(
|
||||
base64=f"data:image/{image_format};base64,{base64_str}"
|
||||
)
|
||||
])
|
||||
except Exception as e:
|
||||
print(f"处理图片消息失败: {str(e)}")
|
||||
return platform_message.MessageChain([
|
||||
platform_message.Plain(text=f"[图片处理失败]")
|
||||
])
|
||||
elif message["Data"]["MsgType"] == 34:
|
||||
audio_base64 = message["Data"]["ImgBuf"]["buffer"]
|
||||
return platform_message.MessageChain(
|
||||
[platform_message.Image(base64=f"data:image/jpeg;base64,{image_base64}")]
|
||||
[platform_message.Voice(base64=f"data:audio/silk;base64,{audio_base64}")]
|
||||
)
|
||||
elif message["Data"]["MsgType"] == 49:
|
||||
# 支持微信聊天记录的消息类型,将 XML 内容转换为 MessageChain 传递
|
||||
try:
|
||||
content = message["Data"]["Content"]["string"]
|
||||
|
||||
try:
|
||||
content_bytes = content.encode('utf-8')
|
||||
decoded_content = base64.b64decode(content_bytes)
|
||||
return platform_message.MessageChain(
|
||||
[platform_message.Unknown(content=decoded_content)]
|
||||
)
|
||||
except Exception as e:
|
||||
return platform_message.MessageChain(
|
||||
[platform_message.Plain(text=content)]
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error processing type 49 message: {str(e)}")
|
||||
return platform_message.MessageChain(
|
||||
[platform_message.Plain(text="[无法解析的消息]")]
|
||||
)
|
||||
|
||||
class GewechatEventConverter(adapter.EventConverter):
|
||||
|
||||
|
||||
def __init__(self, config: dict):
|
||||
self.config = config
|
||||
self.message_converter = GewechatMessageConverter(config)
|
||||
|
||||
@staticmethod
|
||||
async def yiri2target(
|
||||
event: platform_events.MessageEvent
|
||||
) -> dict:
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
async def target2yiri(
|
||||
self,
|
||||
event: dict,
|
||||
bot_account_id: str
|
||||
) -> platform_events.MessageEvent:
|
||||
message_chain = await GewechatMessageConverter.target2yiri(copy.deepcopy(event), bot_account_id)
|
||||
message_chain = await self.message_converter.target2yiri(copy.deepcopy(event), bot_account_id)
|
||||
|
||||
if not message_chain:
|
||||
return None
|
||||
@@ -120,7 +189,7 @@ class GewechatEventConverter(adapter.EventConverter):
|
||||
time=event["Data"]["CreateTime"],
|
||||
source_platform_object=event,
|
||||
)
|
||||
elif 'wxid_' in event["Data"]["FromUserName"]["string"]:
|
||||
else:
|
||||
return platform_events.FriendMessage(
|
||||
sender=platform_entities.Friend(
|
||||
id=event["Data"]["FromUserName"]["string"],
|
||||
@@ -134,7 +203,9 @@ class GewechatEventConverter(adapter.EventConverter):
|
||||
|
||||
|
||||
class GeWeChatAdapter(adapter.MessagePlatformAdapter):
|
||||
|
||||
|
||||
name: str = "gewechat" # 定义适配器名称
|
||||
|
||||
bot: gewechat_client.GewechatClient
|
||||
quart_app: quart.Quart
|
||||
|
||||
@@ -144,8 +215,8 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter):
|
||||
|
||||
ap: app.Application
|
||||
|
||||
message_converter: GewechatMessageConverter = GewechatMessageConverter()
|
||||
event_converter: GewechatEventConverter = GewechatEventConverter()
|
||||
message_converter: GewechatMessageConverter
|
||||
event_converter: GewechatEventConverter
|
||||
|
||||
listeners: typing.Dict[
|
||||
typing.Type[platform_events.Event],
|
||||
@@ -158,10 +229,20 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter):
|
||||
|
||||
self.quart_app = quart.Quart(__name__)
|
||||
|
||||
self.message_converter = GewechatMessageConverter(config)
|
||||
self.event_converter = GewechatEventConverter(config)
|
||||
|
||||
@self.quart_app.route('/gewechat/callback', methods=['POST'])
|
||||
async def gewechat_callback():
|
||||
data = await quart.request.json
|
||||
# print(json.dumps(data, indent=4, ensure_ascii=False))
|
||||
|
||||
if 'data' in data:
|
||||
data['Data'] = data['data']
|
||||
if 'type_name' in data:
|
||||
data['TypeName'] = data['type_name']
|
||||
# print(json.dumps(data, indent=4, ensure_ascii=False))
|
||||
|
||||
|
||||
if 'testMsg' in data:
|
||||
return 'ok'
|
||||
@@ -183,7 +264,31 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter):
|
||||
target_id: str,
|
||||
message: platform_message.MessageChain
|
||||
):
|
||||
pass
|
||||
geweap_msg = await self.message_converter.yiri2target(message)
|
||||
# 此处加上群消息at处理
|
||||
ats = [item["target"] for item in geweap_msg if item["type"] == "at"]
|
||||
|
||||
|
||||
for msg in geweap_msg:
|
||||
# at主动发送消息
|
||||
if msg['type'] == 'text':
|
||||
if ats:
|
||||
member_info = self.bot.get_chatroom_member_detail(
|
||||
self.config["app_id"],
|
||||
target_id,
|
||||
ats[::-1]
|
||||
)["data"]
|
||||
|
||||
for member in member_info:
|
||||
msg['content'] = f'@{member["nickName"]} {msg["content"]}'
|
||||
self.bot.post_text(app_id=self.config['app_id'], to_wxid=target_id, content=msg['content'],
|
||||
ats=",".join(ats))
|
||||
|
||||
elif msg['type'] == 'image':
|
||||
|
||||
self.bot.post_image(app_id=self.config['app_id'], to_wxid=target_id, img_url=msg["image"])
|
||||
|
||||
|
||||
|
||||
async def reply_message(
|
||||
self,
|
||||
@@ -212,7 +317,7 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter):
|
||||
app_id=self.config["app_id"],
|
||||
to_wxid=message_source.source_platform_object["Data"]["FromUserName"]["string"],
|
||||
content=msg["content"],
|
||||
ats=','.join(ats)
|
||||
ats=",".join(ats)
|
||||
)
|
||||
|
||||
async def is_muted(self, group_id: int) -> bool:
|
||||
@@ -257,7 +362,7 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter):
|
||||
|
||||
self.ap.logger.info(f"Gewechat 登录成功,app_id: {app_id}")
|
||||
|
||||
await self.ap.platform_mgr.write_back_config(self, self.config)
|
||||
await self.ap.platform_mgr.write_back_config('gewechat', self, self.config)
|
||||
|
||||
# 获取 nickname
|
||||
profile = self.bot.get_profile(self.config["app_id"])
|
||||
@@ -281,4 +386,4 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter):
|
||||
)
|
||||
|
||||
async def kill(self) -> bool:
|
||||
pass
|
||||
pass
|
||||
|
||||
@@ -17,6 +17,13 @@ spec:
|
||||
type: string
|
||||
required: true
|
||||
default: ""
|
||||
- name: gewechat_file_url
|
||||
label:
|
||||
en_US: GeWeChat file download URL
|
||||
zh_CN: GeWeChat 文件下载URL
|
||||
type: string
|
||||
required: true
|
||||
default: ""
|
||||
- name: port
|
||||
label:
|
||||
en_US: Port
|
||||
|
||||
@@ -2,21 +2,19 @@ from __future__ import annotations
|
||||
import typing
|
||||
import asyncio
|
||||
import traceback
|
||||
import time
|
||||
|
||||
import datetime
|
||||
from pkg.core import app
|
||||
from pkg.platform.adapter import MessagePlatformAdapter
|
||||
from pkg.platform.types import events as platform_events, message as platform_message
|
||||
|
||||
import aiocqhttp
|
||||
import aiohttp
|
||||
from collections import deque
|
||||
from libs.official_account_api.oaevent import OAEvent
|
||||
from pkg.platform.adapter import MessagePlatformAdapter
|
||||
from pkg.platform.types import events as platform_events, message as platform_message
|
||||
from libs.official_account_api.api import OAClient
|
||||
from libs.official_account_api.api import OAClientForLongerResponse
|
||||
from pkg.core import app
|
||||
from .. import adapter
|
||||
from ...pipeline.longtext.strategies import forward
|
||||
from ...core import app
|
||||
from ..types import message as platform_message
|
||||
from ..types import events as platform_events
|
||||
@@ -26,6 +24,7 @@ from ...command.errors import ParamNotEnoughError
|
||||
|
||||
# 生成的ai回答
|
||||
generated_content = {}
|
||||
msg_queue = {}
|
||||
|
||||
class OAMessageConverter(adapter.MessageConverter):
|
||||
@staticmethod
|
||||
@@ -88,17 +87,30 @@ class OfficialAccountAdapter(adapter.MessagePlatformAdapter):
|
||||
"EncodingAESKey",
|
||||
"AppSecret",
|
||||
"AppID",
|
||||
"Mode",
|
||||
]
|
||||
missing_keys = [key for key in required_keys if key not in config]
|
||||
if missing_keys:
|
||||
raise ParamNotEnoughError("企业微信缺少相关配置项,请查看文档或联系管理员")
|
||||
raise ParamNotEnoughError("微信公众号缺少相关配置项,请查看文档或联系管理员")
|
||||
|
||||
self.bot = OAClient(
|
||||
token=config['token'],
|
||||
EncodingAESKey=config['EncodingAESKey'],
|
||||
Appsecret=config['AppSecret'],
|
||||
AppID=config['AppID'],
|
||||
)
|
||||
|
||||
if self.config['Mode'] == "drop":
|
||||
self.bot = OAClient(
|
||||
token=config['token'],
|
||||
EncodingAESKey=config['EncodingAESKey'],
|
||||
Appsecret=config['AppSecret'],
|
||||
AppID=config['AppID'],
|
||||
)
|
||||
elif self.config['Mode'] == "passive":
|
||||
self.bot = OAClientForLongerResponse(
|
||||
token=config['token'],
|
||||
EncodingAESKey=config['EncodingAESKey'],
|
||||
Appsecret=config['AppSecret'],
|
||||
AppID=config['AppID'],
|
||||
)
|
||||
else:
|
||||
raise KeyError("请设置微信公众号通信模式")
|
||||
|
||||
|
||||
async def reply_message(self, message_source: platform_events.FriendMessage, message: platform_message.MessageChain, quote_origin: bool = False):
|
||||
global generated_content
|
||||
@@ -109,6 +121,20 @@ class OfficialAccountAdapter(adapter.MessagePlatformAdapter):
|
||||
|
||||
generated_content[message_source.message_chain.message_id] = content
|
||||
|
||||
from_user = message_source.sender.id
|
||||
|
||||
|
||||
if from_user not in msg_queue:
|
||||
msg_queue[from_user] = []
|
||||
|
||||
msg_queue[from_user].append(
|
||||
{
|
||||
"msg_id":message_source.message_chain.message_id,
|
||||
"content":content,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def send_message(
|
||||
self, target_type: str, target_id: str, message: platform_message.MessageChain
|
||||
):
|
||||
|
||||
@@ -2,15 +2,14 @@ from __future__ import annotations
|
||||
import typing
|
||||
import asyncio
|
||||
import traceback
|
||||
import time
|
||||
|
||||
import datetime
|
||||
import aiocqhttp
|
||||
import aiohttp
|
||||
|
||||
from pkg.platform.adapter import MessagePlatformAdapter
|
||||
from pkg.platform.types import events as platform_events, message as platform_message
|
||||
from pkg.core import app
|
||||
from .. import adapter
|
||||
from ...pipeline.longtext.strategies import forward
|
||||
|
||||
from ...core import app
|
||||
from ..types import message as platform_message
|
||||
from ..types import events as platform_events
|
||||
@@ -74,7 +73,11 @@ class QQOfficialEventConverter(adapter.EventConverter):
|
||||
remark = "",
|
||||
)
|
||||
return platform_events.FriendMessage(
|
||||
sender = friend,message_chain = yiri_chain,time = event.timestamp,
|
||||
sender = friend,message_chain = yiri_chain,time = int(
|
||||
datetime.datetime.strptime(
|
||||
event.timestamp, "%Y-%m-%dT%H:%M:%S%z"
|
||||
).timestamp()
|
||||
),
|
||||
source_platform_object=event
|
||||
)
|
||||
|
||||
@@ -96,7 +99,7 @@ class QQOfficialEventConverter(adapter.EventConverter):
|
||||
member_name= event.t,
|
||||
permission= 'MEMBER',
|
||||
group = platform_entities.Group(
|
||||
id = 0,
|
||||
id = event.group_openid,
|
||||
name = 'MEMBER',
|
||||
permission= platform_entities.Permission.Member
|
||||
),
|
||||
@@ -105,7 +108,11 @@ class QQOfficialEventConverter(adapter.EventConverter):
|
||||
last_speak_timestamp=0,
|
||||
mute_time_remaining=0
|
||||
)
|
||||
time = event.timestamp
|
||||
time = int(
|
||||
datetime.datetime.strptime(
|
||||
event.timestamp, "%Y-%m-%dT%H:%M:%S%z"
|
||||
).timestamp()
|
||||
)
|
||||
return platform_events.GroupMessage(
|
||||
sender = sender,
|
||||
message_chain=yiri_chain,
|
||||
@@ -119,7 +126,7 @@ class QQOfficialEventConverter(adapter.EventConverter):
|
||||
member_name=event.t,
|
||||
permission= 'MEMBER',
|
||||
group = platform_entities.Group(
|
||||
id = 0,
|
||||
id = event.channel_id,
|
||||
name = 'MEMBER',
|
||||
permission=platform_entities.Permission.Member
|
||||
),
|
||||
@@ -128,7 +135,11 @@ class QQOfficialEventConverter(adapter.EventConverter):
|
||||
last_speak_timestamp=0,
|
||||
mute_time_remaining=0
|
||||
)
|
||||
time = event.timestamp,
|
||||
time = int(
|
||||
datetime.datetime.strptime(
|
||||
event.timestamp, "%Y-%m-%dT%H:%M:%S%z"
|
||||
).timestamp()
|
||||
)
|
||||
return platform_events.GroupMessage(
|
||||
sender =sender,
|
||||
message_chain = yiri_chain,
|
||||
|
||||
@@ -2,18 +2,15 @@ from __future__ import annotations
|
||||
import typing
|
||||
import asyncio
|
||||
import traceback
|
||||
import time
|
||||
|
||||
import datetime
|
||||
|
||||
import aiocqhttp
|
||||
import aiohttp
|
||||
from libs.wecom_api.api import WecomClient
|
||||
from pkg.platform.adapter import MessagePlatformAdapter
|
||||
from pkg.platform.types import events as platform_events, message as platform_message
|
||||
from libs.wecom_api.wecomevent import WecomEvent
|
||||
from pkg.core import app
|
||||
from .. import adapter
|
||||
from ...pipeline.longtext.strategies import forward
|
||||
from ...core import app
|
||||
from ..types import message as platform_message
|
||||
from ..types import events as platform_events
|
||||
@@ -200,7 +197,19 @@ class WecomAdapter(adapter.MessagePlatformAdapter):
|
||||
async def send_message(
|
||||
self, target_type: str, target_id: str, message: platform_message.MessageChain
|
||||
):
|
||||
pass
|
||||
"""企业微信目前只有发送给个人的方法,
|
||||
构造target_id的方式为前半部分为账户id,后半部分为agent_id,中间使用“|”符号隔开。
|
||||
"""
|
||||
content_list = await WecomMessageConverter.yiri2target(message, self.bot)
|
||||
parts = target_id.split("|")
|
||||
user_id = parts[0]
|
||||
agent_id = int(parts[1])
|
||||
if target_type == 'person':
|
||||
for content in content_list:
|
||||
if content["type"] == "text":
|
||||
await self.bot.send_private_msg(user_id,agent_id,content["content"])
|
||||
if content["type"] == "image":
|
||||
await self.bot.send_image(user_id,agent_id,content["media"])
|
||||
|
||||
def register_listener(
|
||||
self,
|
||||
|
||||
@@ -13,27 +13,20 @@ import pydantic.v1 as pydantic
|
||||
class Entity(pydantic.BaseModel):
|
||||
"""实体,表示一个用户或群。"""
|
||||
id: int
|
||||
"""QQ 号或群号。"""
|
||||
@abc.abstractmethod
|
||||
def get_avatar_url(self) -> str:
|
||||
"""头像图片链接。"""
|
||||
|
||||
"""ID。"""
|
||||
@abc.abstractmethod
|
||||
def get_name(self) -> str:
|
||||
"""名称。"""
|
||||
|
||||
|
||||
class Friend(Entity):
|
||||
"""好友。"""
|
||||
"""私聊对象。"""
|
||||
id: typing.Union[int, str]
|
||||
"""QQ 号。"""
|
||||
"""ID。"""
|
||||
nickname: typing.Optional[str]
|
||||
"""昵称。"""
|
||||
remark: typing.Optional[str]
|
||||
"""备注。"""
|
||||
def get_avatar_url(self) -> str:
|
||||
return f'http://q4.qlogo.cn/g?b=qq&nk={self.id}&s=140'
|
||||
|
||||
def get_name(self) -> str:
|
||||
return self.nickname or self.remark or ''
|
||||
|
||||
@@ -59,8 +52,6 @@ class Group(Entity):
|
||||
"""群名称。"""
|
||||
permission: Permission
|
||||
"""Bot 在群中的权限。"""
|
||||
def get_avatar_url(self) -> str:
|
||||
return f'https://p.qlogo.cn/gh/{self.id}/{self.id}/'
|
||||
|
||||
def get_name(self) -> str:
|
||||
return self.name
|
||||
@@ -69,11 +60,11 @@ class Group(Entity):
|
||||
class GroupMember(Entity):
|
||||
"""群成员。"""
|
||||
id: typing.Union[int, str]
|
||||
"""QQ 号。"""
|
||||
"""群员 ID。"""
|
||||
member_name: str
|
||||
"""群成员名称。"""
|
||||
"""群员名称。"""
|
||||
permission: Permission
|
||||
"""Bot 在群中的权限。"""
|
||||
"""在群中的权限。"""
|
||||
group: Group
|
||||
"""群。"""
|
||||
special_title: str = ''
|
||||
@@ -84,61 +75,6 @@ class GroupMember(Entity):
|
||||
"""最后一次发言的时间。"""
|
||||
mute_time_remaining: int = 0
|
||||
"""禁言剩余时间。"""
|
||||
def get_avatar_url(self) -> str:
|
||||
return f'http://q4.qlogo.cn/g?b=qq&nk={self.id}&s=140'
|
||||
|
||||
def get_name(self) -> str:
|
||||
return self.member_name
|
||||
|
||||
|
||||
class Client(Entity):
|
||||
"""来自其他客户端的用户。"""
|
||||
id: typing.Union[int, str]
|
||||
"""识别 id。"""
|
||||
platform: str
|
||||
"""来源平台。"""
|
||||
def get_avatar_url(self) -> str:
|
||||
raise NotImplementedError
|
||||
|
||||
def get_name(self) -> str:
|
||||
return self.platform
|
||||
|
||||
|
||||
class Subject(pydantic.BaseModel):
|
||||
"""另一种实体类型表示。"""
|
||||
id: typing.Union[int, str]
|
||||
"""QQ 号或群号。"""
|
||||
kind: typing.Literal['Friend', 'Group', 'Stranger']
|
||||
"""类型。"""
|
||||
|
||||
|
||||
class Config(pydantic.BaseModel):
|
||||
"""配置项类型。"""
|
||||
def modify(self, **kwargs) -> 'Config':
|
||||
"""修改部分设置。"""
|
||||
for k, v in kwargs.items():
|
||||
if k in self.__fields__:
|
||||
setattr(self, k, v)
|
||||
else:
|
||||
raise ValueError(f'未知配置项: {k}')
|
||||
return self
|
||||
|
||||
|
||||
class GroupConfigModel(Config):
|
||||
"""群配置。"""
|
||||
name: str
|
||||
"""群名称。"""
|
||||
confess_talk: bool
|
||||
"""是否允许坦白说。"""
|
||||
allow_member_invite: bool
|
||||
"""是否允许成员邀请好友入群。"""
|
||||
auto_approve: bool
|
||||
"""是否开启自动审批入群。"""
|
||||
anonymous_chat: bool
|
||||
"""是否开启匿名聊天。"""
|
||||
announcement: str = ''
|
||||
"""群公告。"""
|
||||
|
||||
|
||||
class MemberInfoModel(Config, GroupMember):
|
||||
"""群成员信息。"""
|
||||
|
||||
@@ -43,21 +43,6 @@ class Event(pydantic.BaseModel):
|
||||
return Event
|
||||
|
||||
|
||||
###############################
|
||||
# Bot Event
|
||||
class BotEvent(Event):
|
||||
"""Bot 自身事件。
|
||||
|
||||
Args:
|
||||
type: 事件名。
|
||||
qq: Bot 的 QQ 号。
|
||||
"""
|
||||
type: str
|
||||
"""事件名。"""
|
||||
qq: int
|
||||
"""Bot 的 QQ 号。"""
|
||||
|
||||
|
||||
###############################
|
||||
# Message Event
|
||||
class MessageEvent(Event):
|
||||
@@ -72,6 +57,9 @@ class MessageEvent(Event):
|
||||
message_chain: platform_message.MessageChain
|
||||
"""消息内容。"""
|
||||
|
||||
time: float | None = None
|
||||
"""消息发送时间戳。"""
|
||||
|
||||
source_platform_object: typing.Optional[typing.Any] = None
|
||||
"""原消息平台对象。
|
||||
供消息平台适配器开发者使用,如果回复用户时需要使用原消息事件对象的信息,
|
||||
@@ -79,7 +67,7 @@ class MessageEvent(Event):
|
||||
|
||||
|
||||
class FriendMessage(MessageEvent):
|
||||
"""好友消息。
|
||||
"""私聊消息。
|
||||
|
||||
Args:
|
||||
type: 事件名。
|
||||
@@ -111,19 +99,3 @@ class GroupMessage(MessageEvent):
|
||||
@property
|
||||
def group(self) -> platform_entities.Group:
|
||||
return self.sender.group
|
||||
|
||||
|
||||
class StrangerMessage(MessageEvent):
|
||||
"""陌生人消息。
|
||||
|
||||
Args:
|
||||
type: 事件名。
|
||||
sender: 发送消息的人。
|
||||
message_chain: 消息内容。
|
||||
"""
|
||||
type: str = 'StrangerMessage'
|
||||
"""事件名。"""
|
||||
sender: platform_entities.Friend
|
||||
"""发送消息的人。"""
|
||||
message_chain: platform_message.MessageChain
|
||||
"""消息内容。"""
|
||||
|
||||
@@ -116,18 +116,6 @@ class MessageChain(PlatformBaseModel):
|
||||
print('At Me')
|
||||
```
|
||||
|
||||
消息链对索引操作进行了增强。以消息组件类型为索引,获取消息链中的全部该类型的消息组件。
|
||||
```py
|
||||
plain_list = message_chain[Plain]
|
||||
'[Plain("Hello World!")]'
|
||||
```
|
||||
|
||||
可以用加号连接两个消息链。
|
||||
```py
|
||||
MessageChain(['Hello World!']) + MessageChain(['Goodbye World!'])
|
||||
# 返回 MessageChain([Plain("Hello World!"), Plain("Goodbye World!")])
|
||||
```
|
||||
|
||||
"""
|
||||
__root__: typing.List[MessageComponent]
|
||||
|
||||
@@ -488,9 +476,9 @@ class Quote(MessageComponent):
|
||||
group_id: typing.Optional[typing.Union[int, str]] = None
|
||||
"""被引用回复的原消息所接收的群号,当为好友消息时为0。"""
|
||||
sender_id: typing.Optional[typing.Union[int, str]] = None
|
||||
"""被引用回复的原消息的发送者的QQ号。"""
|
||||
"""被引用回复的原消息的发送者的ID。"""
|
||||
target_id: typing.Optional[typing.Union[int, str]] = None
|
||||
"""被引用回复的原消息的接收者者的QQ号(或群号)。"""
|
||||
"""被引用回复的原消息的接收者者的ID或群ID。"""
|
||||
origin: MessageChain
|
||||
"""被引用回复的原消息的消息链对象。"""
|
||||
|
||||
@@ -504,7 +492,7 @@ class At(MessageComponent):
|
||||
type: str = "At"
|
||||
"""消息组件类型。"""
|
||||
target: typing.Union[int, str]
|
||||
"""群员 QQ 号。"""
|
||||
"""群员 ID。"""
|
||||
display: typing.Optional[str] = None
|
||||
"""At时显示的文字,发送消息时无效,自动使用群名片。"""
|
||||
def __eq__(self, other):
|
||||
@@ -527,9 +515,9 @@ class Image(MessageComponent):
|
||||
type: str = "Image"
|
||||
"""消息组件类型。"""
|
||||
image_id: typing.Optional[str] = None
|
||||
"""图片的 image_id,群图片与好友图片格式不同。不为空时将忽略 url 属性。"""
|
||||
"""图片的 image_id,不为空时将忽略 url 属性。"""
|
||||
url: typing.Optional[pydantic.HttpUrl] = None
|
||||
"""图片的 URL,发送时可作网络图片的链接;接收时为腾讯图片服务器的链接,可用于图片下载。"""
|
||||
"""图片的 URL,发送时可作网络图片的链接;接收时为图片的链接,可用于图片下载。"""
|
||||
path: typing.Union[str, Path, None] = None
|
||||
"""图片的路径,发送本地图片。"""
|
||||
base64: typing.Optional[str] = None
|
||||
@@ -663,7 +651,7 @@ class Voice(MessageComponent):
|
||||
voice_id: typing.Optional[str] = None
|
||||
"""语音的 voice_id,不为空时将忽略 url 属性。"""
|
||||
url: typing.Optional[str] = None
|
||||
"""语音的 URL,发送时可作网络语音的链接;接收时为腾讯语音服务器的链接,可用于语音下载。"""
|
||||
"""语音的 URL,发送时可作网络语音的链接;接收时为语音文件的链接,可用于语音下载。"""
|
||||
path: typing.Optional[str] = None
|
||||
"""语音的路径,发送本地语音。"""
|
||||
base64: typing.Optional[str] = None
|
||||
@@ -691,8 +679,6 @@ class Voice(MessageComponent):
|
||||
):
|
||||
"""下载语音到本地。
|
||||
|
||||
语音采用 silk v3 格式,silk 格式的编码解码请使用 [graiax-silkcoder](https://pypi.org/project/graiax-silkcoder/)。
|
||||
|
||||
Args:
|
||||
filename: 下载到本地的文件路径。与 `directory` 二选一。
|
||||
directory: 下载到本地的文件夹路径。与 `filename` 二选一。
|
||||
@@ -750,13 +736,13 @@ class Voice(MessageComponent):
|
||||
class ForwardMessageNode(pydantic.BaseModel):
|
||||
"""合并转发中的一条消息。"""
|
||||
sender_id: typing.Optional[typing.Union[int, str]] = None
|
||||
"""发送人QQ号。"""
|
||||
"""发送人ID。"""
|
||||
sender_name: typing.Optional[str] = None
|
||||
"""显示名称。"""
|
||||
message_chain: typing.Optional[MessageChain] = None
|
||||
"""消息内容。"""
|
||||
message_id: typing.Optional[int] = None
|
||||
"""消息的 message_id,可以只使用此属性,从缓存中读取消息内容。"""
|
||||
"""消息的 message_id。"""
|
||||
time: typing.Optional[datetime] = None
|
||||
"""发送时间。"""
|
||||
@pydantic.validator('message_chain', check_fields=False)
|
||||
|
||||
@@ -4,7 +4,7 @@ import aiohttp
|
||||
|
||||
from . import entities, requester
|
||||
from ...core import app
|
||||
|
||||
from ...discover import engine
|
||||
from . import token
|
||||
from .requesters import bailianchatcmpl, chatcmpl, anthropicmsgs, moonshotchatcmpl, deepseekchatcmpl, ollamachat, giteeaichatcmpl, volcarkchatcmpl, xaichatcmpl, zhipuaichatcmpl, lmstudiochatcmpl, siliconflowchatcmpl, volcarkchatcmpl
|
||||
|
||||
@@ -16,6 +16,8 @@ class ModelManager:
|
||||
|
||||
ap: app.Application
|
||||
|
||||
requester_components: list[engine.Component]
|
||||
|
||||
model_list: list[entities.LLMModelInfo]
|
||||
|
||||
requesters: dict[str, requester.LLMAPIRequester]
|
||||
@@ -38,14 +40,21 @@ class ModelManager:
|
||||
|
||||
async def initialize(self):
|
||||
|
||||
self.requester_components = self.ap.discover.get_components_by_kind('LLMAPIRequester')
|
||||
|
||||
# 初始化token_mgr, requester
|
||||
for k, v in self.ap.provider_cfg.data['keys'].items():
|
||||
self.token_mgrs[k] = token.TokenManager(k, v)
|
||||
|
||||
for api_cls in requester.preregistered_requesters:
|
||||
# for api_cls in requester.preregistered_requesters:
|
||||
# api_inst = api_cls(self.ap)
|
||||
# await api_inst.initialize()
|
||||
# self.requesters[api_inst.name] = api_inst
|
||||
for component in self.requester_components:
|
||||
api_cls = component.get_python_component_class()
|
||||
api_inst = api_cls(self.ap)
|
||||
await api_inst.initialize()
|
||||
self.requesters[api_inst.name] = api_inst
|
||||
self.requesters[component.metadata.name] = api_inst
|
||||
|
||||
# 尝试从api获取最新的模型信息
|
||||
try:
|
||||
|
||||
@@ -10,18 +10,6 @@ from . import entities as modelmgr_entities
|
||||
from ..tools import entities as tools_entities
|
||||
|
||||
|
||||
preregistered_requesters: list[typing.Type[LLMAPIRequester]] = []
|
||||
|
||||
def requester_class(name: str):
|
||||
|
||||
def decorator(cls: typing.Type[LLMAPIRequester]) -> typing.Type[LLMAPIRequester]:
|
||||
cls.name = name
|
||||
preregistered_requesters.append(cls)
|
||||
return cls
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
class LLMAPIRequester(metaclass=abc.ABCMeta):
|
||||
"""LLM API请求器
|
||||
"""
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import typing
|
||||
import json
|
||||
import traceback
|
||||
import base64
|
||||
|
||||
@@ -16,7 +17,6 @@ from ...tools import entities as tools_entities
|
||||
from ....utils import image
|
||||
|
||||
|
||||
@requester.requester_class("anthropic-messages")
|
||||
class AnthropicMessages(requester.LLMAPIRequester):
|
||||
"""Anthropic Messages API 请求器"""
|
||||
|
||||
@@ -69,11 +69,32 @@ class AnthropicMessages(requester.LLMAPIRequester):
|
||||
req_messages = []
|
||||
|
||||
for m in messages:
|
||||
if isinstance(m.content, str) and m.content.strip() != "":
|
||||
req_messages.append(m.dict(exclude_none=True))
|
||||
elif isinstance(m.content, list):
|
||||
if m.role == 'tool':
|
||||
tool_call_id = m.tool_call_id
|
||||
|
||||
msg_dict = m.dict(exclude_none=True)
|
||||
req_messages.append({
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "tool_result",
|
||||
"tool_use_id": tool_call_id,
|
||||
"content": m.content
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
continue
|
||||
|
||||
msg_dict = m.dict(exclude_none=True)
|
||||
|
||||
if isinstance(m.content, str) and m.content.strip() != "":
|
||||
msg_dict["content"] = [
|
||||
{
|
||||
"type": "text",
|
||||
"text": m.content
|
||||
}
|
||||
]
|
||||
elif isinstance(m.content, list):
|
||||
|
||||
for i, ce in enumerate(m.content):
|
||||
|
||||
@@ -90,25 +111,60 @@ class AnthropicMessages(requester.LLMAPIRequester):
|
||||
}
|
||||
msg_dict["content"][i] = alter_image_ele
|
||||
|
||||
req_messages.append(msg_dict)
|
||||
if m.tool_calls:
|
||||
|
||||
for tool_call in m.tool_calls:
|
||||
msg_dict["content"].append({
|
||||
"type": "tool_use",
|
||||
"id": tool_call.id,
|
||||
"name": tool_call.function.name,
|
||||
"input": json.loads(tool_call.function.arguments)
|
||||
})
|
||||
|
||||
del msg_dict["tool_calls"]
|
||||
|
||||
req_messages.append(msg_dict)
|
||||
|
||||
|
||||
args["messages"] = req_messages
|
||||
|
||||
if funcs:
|
||||
tools = await self.ap.tool_mgr.generate_tools_for_anthropic(funcs)
|
||||
|
||||
# anthropic的tools处在beta阶段,sdk不稳定,故暂时不支持
|
||||
#
|
||||
# if funcs:
|
||||
# tools = await self.ap.tool_mgr.generate_tools_for_openai(funcs)
|
||||
|
||||
# if tools:
|
||||
# args["tools"] = tools
|
||||
if tools:
|
||||
args["tools"] = tools
|
||||
|
||||
try:
|
||||
# print(json.dumps(args, indent=4, ensure_ascii=False))
|
||||
resp = await self.client.messages.create(**args)
|
||||
|
||||
return llm_entities.Message(
|
||||
content=resp.content[0].text,
|
||||
role=resp.role
|
||||
)
|
||||
args = {
|
||||
'content': '',
|
||||
'role': resp.role,
|
||||
}
|
||||
|
||||
assert type(resp) is anthropic.types.message.Message
|
||||
|
||||
for block in resp.content:
|
||||
if block.type == 'thinking':
|
||||
args['content'] = '<think>' + block.thinking + '</think>\n' + args['content']
|
||||
elif block.type == 'text':
|
||||
args['content'] += block.text
|
||||
elif block.type == 'tool_use':
|
||||
assert type(block) is anthropic.types.tool_use_block.ToolUseBlock
|
||||
tool_call = llm_entities.ToolCall(
|
||||
id=block.id,
|
||||
type="function",
|
||||
function=llm_entities.FunctionCall(
|
||||
name=block.name,
|
||||
arguments=json.dumps(block.input)
|
||||
)
|
||||
)
|
||||
if 'tool_calls' not in args:
|
||||
args['tool_calls'] = []
|
||||
args['tool_calls'].append(tool_call)
|
||||
|
||||
return llm_entities.Message(**args)
|
||||
except anthropic.AuthenticationError as e:
|
||||
raise errors.RequesterError(f'api-key 无效: {e.message}')
|
||||
except anthropic.BadRequestError as e:
|
||||
|
||||
34
pkg/provider/modelmgr/requesters/anthropicmsgs.yaml
Normal file
34
pkg/provider/modelmgr/requesters/anthropicmsgs.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
apiVersion: v1
|
||||
kind: LLMAPIRequester
|
||||
metadata:
|
||||
name: anthropic-messages
|
||||
label:
|
||||
en_US: Anthropic
|
||||
zh_CN: Anthropic
|
||||
spec:
|
||||
config:
|
||||
- name: base-url
|
||||
label:
|
||||
en_US: Base URL
|
||||
zh_CN: 基础 URL
|
||||
type: string
|
||||
required: true
|
||||
default: "https://api.anthropic.com/v1"
|
||||
- name: args
|
||||
label:
|
||||
en_US: Args
|
||||
zh_CN: 附加参数
|
||||
type: object
|
||||
required: true
|
||||
default: {}
|
||||
- name: timeout
|
||||
label:
|
||||
en_US: Timeout
|
||||
zh_CN: 超时时间
|
||||
type: int
|
||||
required: true
|
||||
default: 120
|
||||
execution:
|
||||
python:
|
||||
path: ./anthropicmsgs.py
|
||||
attr: AnthropicMessages
|
||||
@@ -7,7 +7,6 @@ from .. import requester
|
||||
from ....core import app
|
||||
|
||||
|
||||
@requester.requester_class("bailian-chat-completions")
|
||||
class BailianChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
"""阿里云百炼大模型平台 ChatCompletion API 请求器"""
|
||||
|
||||
|
||||
34
pkg/provider/modelmgr/requesters/bailianchatcmpl.yaml
Normal file
34
pkg/provider/modelmgr/requesters/bailianchatcmpl.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
apiVersion: v1
|
||||
kind: LLMAPIRequester
|
||||
metadata:
|
||||
name: bailian-chat-completions
|
||||
label:
|
||||
en_US: Aliyun Bailian
|
||||
zh_CN: 阿里云百炼
|
||||
spec:
|
||||
config:
|
||||
- name: base-url
|
||||
label:
|
||||
en_US: Base URL
|
||||
zh_CN: 基础 URL
|
||||
type: string
|
||||
required: true
|
||||
default: "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
||||
- name: args
|
||||
label:
|
||||
en_US: Args
|
||||
zh_CN: 附加参数
|
||||
type: object
|
||||
required: true
|
||||
default: {}
|
||||
- name: timeout
|
||||
label:
|
||||
en_US: Timeout
|
||||
zh_CN: 超时时间
|
||||
type: int
|
||||
required: true
|
||||
default: 120
|
||||
execution:
|
||||
python:
|
||||
path: ./bailianchatcmpl.py
|
||||
attr: BailianChatCompletions
|
||||
@@ -20,7 +20,6 @@ from ...tools import entities as tools_entities
|
||||
from ....utils import image
|
||||
|
||||
|
||||
@requester.requester_class("openai-chat-completions")
|
||||
class OpenAIChatCompletions(requester.LLMAPIRequester):
|
||||
"""OpenAI ChatCompletion API 请求器"""
|
||||
|
||||
|
||||
34
pkg/provider/modelmgr/requesters/chatcmpl.yaml
Normal file
34
pkg/provider/modelmgr/requesters/chatcmpl.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
apiVersion: v1
|
||||
kind: LLMAPIRequester
|
||||
metadata:
|
||||
name: openai-chat-completions
|
||||
label:
|
||||
en_US: OpenAI
|
||||
zh_CN: OpenAI
|
||||
spec:
|
||||
config:
|
||||
- name: base-url
|
||||
label:
|
||||
en_US: Base URL
|
||||
zh_CN: 基础 URL
|
||||
type: string
|
||||
required: true
|
||||
default: "https://api.openai.com/v1"
|
||||
- name: args
|
||||
label:
|
||||
en_US: Args
|
||||
zh_CN: 附加参数
|
||||
type: object
|
||||
required: true
|
||||
default: {}
|
||||
- name: timeout
|
||||
label:
|
||||
en_US: Timeout
|
||||
zh_CN: 超时时间
|
||||
type: int
|
||||
required: true
|
||||
default: 120
|
||||
execution:
|
||||
python:
|
||||
path: ./chatcmpl.py
|
||||
attr: OpenAIChatCompletions
|
||||
@@ -7,7 +7,6 @@ from ... import entities as llm_entities
|
||||
from ...tools import entities as tools_entities
|
||||
|
||||
|
||||
@requester.requester_class("deepseek-chat-completions")
|
||||
class DeepseekChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
"""Deepseek ChatCompletion API 请求器"""
|
||||
|
||||
|
||||
34
pkg/provider/modelmgr/requesters/deepseekchatcmpl.yaml
Normal file
34
pkg/provider/modelmgr/requesters/deepseekchatcmpl.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
apiVersion: v1
|
||||
kind: LLMAPIRequester
|
||||
metadata:
|
||||
name: deepseek-chat-completions
|
||||
label:
|
||||
en_US: DeepSeek
|
||||
zh_CN: 深度求索
|
||||
spec:
|
||||
config:
|
||||
- name: base-url
|
||||
label:
|
||||
en_US: Base URL
|
||||
zh_CN: 基础 URL
|
||||
type: string
|
||||
required: true
|
||||
default: "https://api.deepseek.com"
|
||||
- name: args
|
||||
label:
|
||||
en_US: Args
|
||||
zh_CN: 附加参数
|
||||
type: object
|
||||
required: true
|
||||
default: {}
|
||||
- name: timeout
|
||||
label:
|
||||
en_US: Timeout
|
||||
zh_CN: 超时时间
|
||||
type: int
|
||||
required: true
|
||||
default: 120
|
||||
execution:
|
||||
python:
|
||||
path: ./deepseekchatcmpl.py
|
||||
attr: DeepseekChatCompletions
|
||||
@@ -14,7 +14,6 @@ from ...tools import entities as tools_entities
|
||||
from .. import entities as modelmgr_entities
|
||||
|
||||
|
||||
@requester.requester_class("gitee-ai-chat-completions")
|
||||
class GiteeAIChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
"""Gitee AI ChatCompletions API 请求器"""
|
||||
|
||||
|
||||
34
pkg/provider/modelmgr/requesters/giteeaichatcmpl.yaml
Normal file
34
pkg/provider/modelmgr/requesters/giteeaichatcmpl.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
apiVersion: v1
|
||||
kind: LLMAPIRequester
|
||||
metadata:
|
||||
name: gitee-ai-chat-completions
|
||||
label:
|
||||
en_US: Gitee AI
|
||||
zh_CN: Gitee AI
|
||||
spec:
|
||||
config:
|
||||
- name: base-url
|
||||
label:
|
||||
en_US: Base URL
|
||||
zh_CN: 基础 URL
|
||||
type: string
|
||||
required: true
|
||||
default: "https://ai.gitee.com/v1"
|
||||
- name: args
|
||||
label:
|
||||
en_US: Args
|
||||
zh_CN: 附加参数
|
||||
type: object
|
||||
required: true
|
||||
default: {}
|
||||
- name: timeout
|
||||
label:
|
||||
en_US: Timeout
|
||||
zh_CN: 超时时间
|
||||
type: int
|
||||
required: true
|
||||
default: 120
|
||||
execution:
|
||||
python:
|
||||
path: ./giteeaichatcmpl.py
|
||||
attr: GiteeAIChatCompletions
|
||||
@@ -7,7 +7,6 @@ from .. import requester
|
||||
from ....core import app
|
||||
|
||||
|
||||
@requester.requester_class("lmstudio-chat-completions")
|
||||
class LmStudioChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
"""LMStudio ChatCompletion API 请求器"""
|
||||
|
||||
|
||||
34
pkg/provider/modelmgr/requesters/lmstudiochatcmpl.yaml
Normal file
34
pkg/provider/modelmgr/requesters/lmstudiochatcmpl.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
apiVersion: v1
|
||||
kind: LLMAPIRequester
|
||||
metadata:
|
||||
name: lmstudio-chat-completions
|
||||
label:
|
||||
en_US: LM Studio
|
||||
zh_CN: LM Studio
|
||||
spec:
|
||||
config:
|
||||
- name: base-url
|
||||
label:
|
||||
en_US: Base URL
|
||||
zh_CN: 基础 URL
|
||||
type: string
|
||||
required: true
|
||||
default: "http://127.0.0.1:1234/v1"
|
||||
- name: args
|
||||
label:
|
||||
en_US: Args
|
||||
zh_CN: 附加参数
|
||||
type: object
|
||||
required: true
|
||||
default: {}
|
||||
- name: timeout
|
||||
label:
|
||||
en_US: Timeout
|
||||
zh_CN: 超时时间
|
||||
type: int
|
||||
required: true
|
||||
default: 120
|
||||
execution:
|
||||
python:
|
||||
path: ./lmstudiochatcmpl.py
|
||||
attr: LmStudioChatCompletions
|
||||
@@ -9,7 +9,6 @@ from ... import entities as llm_entities
|
||||
from ...tools import entities as tools_entities
|
||||
|
||||
|
||||
@requester.requester_class("moonshot-chat-completions")
|
||||
class MoonshotChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
"""Moonshot ChatCompletion API 请求器"""
|
||||
|
||||
|
||||
34
pkg/provider/modelmgr/requesters/moonshotchatcmpl.yaml
Normal file
34
pkg/provider/modelmgr/requesters/moonshotchatcmpl.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
apiVersion: v1
|
||||
kind: LLMAPIRequester
|
||||
metadata:
|
||||
name: moonshot-chat-completions
|
||||
label:
|
||||
en_US: Moonshot
|
||||
zh_CN: 月之暗面
|
||||
spec:
|
||||
config:
|
||||
- name: base-url
|
||||
label:
|
||||
en_US: Base URL
|
||||
zh_CN: 基础 URL
|
||||
type: string
|
||||
required: true
|
||||
default: "https://api.moonshot.com/v1"
|
||||
- name: args
|
||||
label:
|
||||
en_US: Args
|
||||
zh_CN: 附加参数
|
||||
type: object
|
||||
required: true
|
||||
default: {}
|
||||
- name: timeout
|
||||
label:
|
||||
en_US: Timeout
|
||||
zh_CN: 超时时间
|
||||
type: int
|
||||
required: true
|
||||
default: 120
|
||||
execution:
|
||||
python:
|
||||
path: ./moonshotchatcmpl.py
|
||||
attr: MoonshotChatCompletions
|
||||
@@ -20,7 +20,6 @@ from ....utils import image
|
||||
REQUESTER_NAME: str = "ollama-chat"
|
||||
|
||||
|
||||
@requester.requester_class(REQUESTER_NAME)
|
||||
class OllamaChatCompletions(requester.LLMAPIRequester):
|
||||
"""Ollama平台 ChatCompletion API请求器"""
|
||||
client: ollama.AsyncClient
|
||||
|
||||
34
pkg/provider/modelmgr/requesters/ollamachat.yaml
Normal file
34
pkg/provider/modelmgr/requesters/ollamachat.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
apiVersion: v1
|
||||
kind: LLMAPIRequester
|
||||
metadata:
|
||||
name: ollama-chat
|
||||
label:
|
||||
en_US: Ollama
|
||||
zh_CN: Ollama
|
||||
spec:
|
||||
config:
|
||||
- name: base-url
|
||||
label:
|
||||
en_US: Base URL
|
||||
zh_CN: 基础 URL
|
||||
type: string
|
||||
required: true
|
||||
default: "http://127.0.0.1:11434"
|
||||
- name: args
|
||||
label:
|
||||
en_US: Args
|
||||
zh_CN: 附加参数
|
||||
type: object
|
||||
required: true
|
||||
default: {}
|
||||
- name: timeout
|
||||
label:
|
||||
en_US: Timeout
|
||||
zh_CN: 超时时间
|
||||
type: int
|
||||
required: true
|
||||
default: 120
|
||||
execution:
|
||||
python:
|
||||
path: ./ollamachat.py
|
||||
attr: OllamaChatCompletions
|
||||
@@ -7,7 +7,6 @@ from .. import requester
|
||||
from ....core import app
|
||||
|
||||
|
||||
@requester.requester_class("siliconflow-chat-completions")
|
||||
class SiliconFlowChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
"""SiliconFlow ChatCompletion API 请求器"""
|
||||
|
||||
|
||||
34
pkg/provider/modelmgr/requesters/siliconflowchatcmpl.yaml
Normal file
34
pkg/provider/modelmgr/requesters/siliconflowchatcmpl.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
apiVersion: v1
|
||||
kind: LLMAPIRequester
|
||||
metadata:
|
||||
name: siliconflow-chat-completions
|
||||
label:
|
||||
en_US: SiliconFlow
|
||||
zh_CN: 硅基流动
|
||||
spec:
|
||||
config:
|
||||
- name: base-url
|
||||
label:
|
||||
en_US: Base URL
|
||||
zh_CN: 基础 URL
|
||||
type: string
|
||||
required: true
|
||||
default: "https://api.siliconflow.cn/v1"
|
||||
- name: args
|
||||
label:
|
||||
en_US: Args
|
||||
zh_CN: 附加参数
|
||||
type: object
|
||||
required: true
|
||||
default: {}
|
||||
- name: timeout
|
||||
label:
|
||||
en_US: Timeout
|
||||
zh_CN: 超时时间
|
||||
type: int
|
||||
required: true
|
||||
default: 120
|
||||
execution:
|
||||
python:
|
||||
path: ./siliconflowchatcmpl.py
|
||||
attr: SiliconFlowChatCompletions
|
||||
@@ -7,7 +7,6 @@ from .. import requester
|
||||
from ....core import app
|
||||
|
||||
|
||||
@requester.requester_class("volcark-chat-completions")
|
||||
class VolcArkChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
"""火山方舟大模型平台 ChatCompletion API 请求器"""
|
||||
|
||||
|
||||
34
pkg/provider/modelmgr/requesters/volcarkchatcmpl.yaml
Normal file
34
pkg/provider/modelmgr/requesters/volcarkchatcmpl.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
apiVersion: v1
|
||||
kind: LLMAPIRequester
|
||||
metadata:
|
||||
name: volcark-chat-completions
|
||||
label:
|
||||
en_US: Volc Engine Ark
|
||||
zh_CN: 火山方舟
|
||||
spec:
|
||||
config:
|
||||
- name: base-url
|
||||
label:
|
||||
en_US: Base URL
|
||||
zh_CN: 基础 URL
|
||||
type: string
|
||||
required: true
|
||||
default: "https://ark.cn-beijing.volces.com/api/v3"
|
||||
- name: args
|
||||
label:
|
||||
en_US: Args
|
||||
zh_CN: 附加参数
|
||||
type: object
|
||||
required: true
|
||||
default: {}
|
||||
- name: timeout
|
||||
label:
|
||||
en_US: Timeout
|
||||
zh_CN: 超时时间
|
||||
type: int
|
||||
required: true
|
||||
default: 120
|
||||
execution:
|
||||
python:
|
||||
path: ./volcarkchatcmpl.py
|
||||
attr: VolcArkChatCompletions
|
||||
@@ -7,7 +7,6 @@ from .. import requester
|
||||
from ....core import app
|
||||
|
||||
|
||||
@requester.requester_class("xai-chat-completions")
|
||||
class XaiChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
"""xAI ChatCompletion API 请求器"""
|
||||
|
||||
|
||||
34
pkg/provider/modelmgr/requesters/xaichatcmpl.yaml
Normal file
34
pkg/provider/modelmgr/requesters/xaichatcmpl.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
apiVersion: v1
|
||||
kind: LLMAPIRequester
|
||||
metadata:
|
||||
name: xai-chat-completions
|
||||
label:
|
||||
en_US: xAI
|
||||
zh_CN: xAI
|
||||
spec:
|
||||
config:
|
||||
- name: base-url
|
||||
label:
|
||||
en_US: Base URL
|
||||
zh_CN: 基础 URL
|
||||
type: string
|
||||
required: true
|
||||
default: "https://api.x.ai/v1"
|
||||
- name: args
|
||||
label:
|
||||
en_US: Args
|
||||
zh_CN: 附加参数
|
||||
type: object
|
||||
required: true
|
||||
default: {}
|
||||
- name: timeout
|
||||
label:
|
||||
en_US: Timeout
|
||||
zh_CN: 超时时间
|
||||
type: int
|
||||
required: true
|
||||
default: 120
|
||||
execution:
|
||||
python:
|
||||
path: ./xaichatcmpl.py
|
||||
attr: XaiChatCompletions
|
||||
@@ -7,7 +7,6 @@ from . import chatcmpl
|
||||
from .. import requester
|
||||
|
||||
|
||||
@requester.requester_class("zhipuai-chat-completions")
|
||||
class ZhipuAIChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
"""智谱AI ChatCompletion API 请求器"""
|
||||
|
||||
|
||||
34
pkg/provider/modelmgr/requesters/zhipuaichatcmpl.yaml
Normal file
34
pkg/provider/modelmgr/requesters/zhipuaichatcmpl.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
apiVersion: v1
|
||||
kind: LLMAPIRequester
|
||||
metadata:
|
||||
name: zhipuai-chat-completions
|
||||
label:
|
||||
en_US: ZhipuAI
|
||||
zh_CN: 智谱 AI
|
||||
spec:
|
||||
config:
|
||||
- name: base-url
|
||||
label:
|
||||
en_US: Base URL
|
||||
zh_CN: 基础 URL
|
||||
type: string
|
||||
required: true
|
||||
default: "https://open.bigmodel.cn/api/paas/v4"
|
||||
- name: args
|
||||
label:
|
||||
en_US: Args
|
||||
zh_CN: 附加参数
|
||||
type: object
|
||||
required: true
|
||||
default: {}
|
||||
- name: timeout
|
||||
label:
|
||||
en_US: Timeout
|
||||
zh_CN: 超时时间
|
||||
type: int
|
||||
required: true
|
||||
default: 120
|
||||
execution:
|
||||
python:
|
||||
path: ./zhipuaichatcmpl.py
|
||||
attr: ZhipuAIChatCompletions
|
||||
@@ -167,6 +167,10 @@ class DashScopeAPIRunner(runner.RequestRunner):
|
||||
image_ids = [] # 用户输入的图片ID列表 (暂不支持)
|
||||
|
||||
plain_text, image_ids = await self._preprocess_user_message(query)
|
||||
|
||||
biz_params = {}
|
||||
biz_params.update(self.biz_params)
|
||||
biz_params.update(query.variables)
|
||||
|
||||
#发送对话请求
|
||||
response = dashscope.Application.call(
|
||||
@@ -176,7 +180,7 @@ class DashScopeAPIRunner(runner.RequestRunner):
|
||||
stream=True, # 流式输出
|
||||
incremental_output=True, # 增量输出,使用流式输出需要开启增量输出
|
||||
session_id=query.session.using_conversation.uuid, # 会话ID用于,多轮对话
|
||||
biz_params=self.biz_params # 工作流应用的自定义输入参数传递
|
||||
biz_params=biz_params, # 工作流应用的自定义输入参数传递
|
||||
# rag_options={ # 主要用于文件交互,暂不支持
|
||||
# "session_file_ids": ["FILE_ID1"], # FILE_ID1 替换为实际的临时文件ID,逗号隔开多个
|
||||
# }
|
||||
|
||||
@@ -5,6 +5,7 @@ import json
|
||||
import uuid
|
||||
import re
|
||||
import base64
|
||||
import datetime
|
||||
|
||||
import aiohttp
|
||||
|
||||
@@ -69,6 +70,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
|
||||
"""
|
||||
plain_text = ""
|
||||
image_ids = []
|
||||
|
||||
if isinstance(query.user_message.content, list):
|
||||
for ce in query.user_message.content:
|
||||
if ce.type == "text":
|
||||
@@ -109,8 +111,12 @@ class DifyServiceAPIRunner(runner.RequestRunner):
|
||||
|
||||
basic_mode_pending_chunk = ''
|
||||
|
||||
inputs = {}
|
||||
|
||||
inputs.update(query.variables)
|
||||
|
||||
async for chunk in self.dify_client.chat_messages(
|
||||
inputs={},
|
||||
inputs=inputs,
|
||||
query=plain_text,
|
||||
user=f"{query.session.launcher_type.value}_{query.session.launcher_id}",
|
||||
conversation_id=cov_id,
|
||||
@@ -160,8 +166,12 @@ class DifyServiceAPIRunner(runner.RequestRunner):
|
||||
|
||||
ignored_events = ["agent_message"]
|
||||
|
||||
inputs = {}
|
||||
|
||||
inputs.update(query.variables)
|
||||
|
||||
async for chunk in self.dify_client.chat_messages(
|
||||
inputs={},
|
||||
inputs=inputs,
|
||||
query=plain_text,
|
||||
user=f"{query.session.launcher_type.value}_{query.session.launcher_id}",
|
||||
response_mode="streaming",
|
||||
@@ -225,8 +235,8 @@ class DifyServiceAPIRunner(runner.RequestRunner):
|
||||
|
||||
if not query.session.using_conversation.uuid:
|
||||
query.session.using_conversation.uuid = str(uuid.uuid4())
|
||||
|
||||
cov_id = query.session.using_conversation.uuid
|
||||
|
||||
query.variables["conversation_id"] = query.session.using_conversation.uuid
|
||||
|
||||
plain_text, image_ids = await self._preprocess_user_message(query)
|
||||
|
||||
@@ -241,12 +251,17 @@ class DifyServiceAPIRunner(runner.RequestRunner):
|
||||
|
||||
ignored_events = ["text_chunk", "workflow_started"]
|
||||
|
||||
inputs = { # these variables are legacy variables, we need to keep them for compatibility
|
||||
"langbot_user_message_text": plain_text,
|
||||
"langbot_session_id": query.variables["session_id"],
|
||||
"langbot_conversation_id": query.variables["conversation_id"],
|
||||
"langbot_msg_create_time": query.variables["msg_create_time"],
|
||||
}
|
||||
|
||||
inputs.update(query.variables)
|
||||
|
||||
async for chunk in self.dify_client.workflow_run(
|
||||
inputs={
|
||||
"langbot_user_message_text": plain_text,
|
||||
"langbot_session_id": f"{query.session.launcher_type.value}_{query.session.launcher_id}",
|
||||
"langbot_conversation_id": cov_id,
|
||||
},
|
||||
inputs=inputs,
|
||||
user=f"{query.session.launcher_type.value}_{query.session.launcher_id}",
|
||||
files=files,
|
||||
timeout=self.ap.provider_cfg.data["dify-service-api"]["workflow"]["timeout"],
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
semantic_version = "v3.4.9.2"
|
||||
semantic_version = "v3.4.10.4"
|
||||
|
||||
debug_mode = False
|
||||
|
||||
|
||||
@@ -8,6 +8,106 @@ import aiohttp
|
||||
import PIL.Image
|
||||
import httpx
|
||||
|
||||
import os
|
||||
import aiofiles
|
||||
import pathlib
|
||||
import asyncio
|
||||
from urllib.parse import urlparse
|
||||
|
||||
|
||||
async def get_gewechat_image_base64(
|
||||
gewechat_url: str,
|
||||
gewechat_file_url: str,
|
||||
app_id: str,
|
||||
xml_content: str,
|
||||
token: str,
|
||||
image_type: int = 2,
|
||||
) -> typing.Tuple[str, str]:
|
||||
"""从gewechat服务器获取图片并转换为base64格式
|
||||
|
||||
Args:
|
||||
gewechat_url (str): gewechat服务器地址(用于获取图片URL)
|
||||
gewechat_file_url (str): gewechat文件下载服务地址
|
||||
app_id (str): gewechat应用ID
|
||||
xml_content (str): 图片的XML内容
|
||||
token (str): Gewechat API Token
|
||||
image_type (int, optional): 图片类型. Defaults to 2.
|
||||
|
||||
Returns:
|
||||
typing.Tuple[str, str]: (base64编码, 图片格式)
|
||||
|
||||
Raises:
|
||||
aiohttp.ClientTimeout: 请求超时(15秒)或连接超时(2秒)
|
||||
Exception: 其他错误
|
||||
"""
|
||||
headers = {
|
||||
'X-GEWE-TOKEN': token,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
# 设置超时
|
||||
timeout = aiohttp.ClientTimeout(
|
||||
total=15.0, # 总超时时间15秒
|
||||
connect=2.0, # 连接超时2秒
|
||||
sock_connect=2.0, # socket连接超时2秒
|
||||
sock_read=15.0 # socket读取超时15秒
|
||||
)
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||
# 获取图片下载链接
|
||||
try:
|
||||
async with session.post(
|
||||
f"{gewechat_url}/v2/api/message/downloadImage",
|
||||
headers=headers,
|
||||
json={
|
||||
"appId": app_id,
|
||||
"type": image_type,
|
||||
"xml": xml_content
|
||||
}
|
||||
) as response:
|
||||
if response.status != 200:
|
||||
raise Exception(f"获取gewechat图片下载失败: {await response.text()}")
|
||||
|
||||
resp_data = await response.json()
|
||||
if resp_data.get("ret") != 200:
|
||||
raise Exception(f"获取gewechat图片下载链接失败: {resp_data}")
|
||||
|
||||
file_url = resp_data['data']['fileUrl']
|
||||
except asyncio.TimeoutError:
|
||||
raise Exception("获取图片下载链接超时")
|
||||
except aiohttp.ClientError as e:
|
||||
raise Exception(f"获取图片下载链接网络错误: {str(e)}")
|
||||
|
||||
# 解析原始URL并替换端口
|
||||
base_url = gewechat_file_url
|
||||
download_url = f"{base_url}/download/{file_url}"
|
||||
|
||||
# 下载图片
|
||||
try:
|
||||
async with session.get(download_url) as img_response:
|
||||
if img_response.status != 200:
|
||||
raise Exception(f"下载图片失败: {await img_response.text()}, URL: {download_url}")
|
||||
|
||||
image_data = await img_response.read()
|
||||
|
||||
content_type = img_response.headers.get('Content-Type', '')
|
||||
if content_type:
|
||||
image_format = content_type.split('/')[-1]
|
||||
else:
|
||||
image_format = file_url.split('.')[-1]
|
||||
|
||||
base64_str = base64.b64encode(image_data).decode('utf-8')
|
||||
|
||||
return base64_str, image_format
|
||||
except asyncio.TimeoutError:
|
||||
raise Exception(f"下载图片超时, URL: {download_url}")
|
||||
except aiohttp.ClientError as e:
|
||||
raise Exception(f"下载图片网络错误: {str(e)}, URL: {download_url}")
|
||||
except Exception as e:
|
||||
raise Exception(f"获取图片失败: {str(e)}") from e
|
||||
|
||||
|
||||
async def get_wecom_image_base64(pic_url: str) -> tuple[str, str]:
|
||||
"""
|
||||
下载企业微信图片并转换为 base64
|
||||
|
||||
@@ -74,19 +74,29 @@
|
||||
"name": "claude-3-opus-latest",
|
||||
"requester": "anthropic-messages",
|
||||
"token_mgr": "anthropic",
|
||||
"vision_supported": true
|
||||
"vision_supported": true,
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "claude-3-5-sonnet-latest",
|
||||
"requester": "anthropic-messages",
|
||||
"token_mgr": "anthropic",
|
||||
"vision_supported": true
|
||||
"vision_supported": true,
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "claude-3-5-haiku-latest",
|
||||
"requester": "anthropic-messages",
|
||||
"token_mgr": "anthropic",
|
||||
"vision_supported": true
|
||||
"vision_supported": true,
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "claude-3-7-sonnet-latest",
|
||||
"requester": "anthropic-messages",
|
||||
"token_mgr": "anthropic",
|
||||
"vision_supported": true,
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "moonshot-v1-8k",
|
||||
@@ -156,61 +166,72 @@
|
||||
{
|
||||
"name": "glm-4-plus",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
"token_mgr": "zhipuai",
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "glm-4-0520",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
"token_mgr": "zhipuai",
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "glm-4-air",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
"token_mgr": "zhipuai",
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "glm-4-airx",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
"token_mgr": "zhipuai",
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "glm-4-long",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
"token_mgr": "zhipuai",
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "glm-4-flashx",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
"token_mgr": "zhipuai",
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "glm-4-flash",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
"token_mgr": "zhipuai",
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "glm-4v-plus",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai",
|
||||
"vision_supported": true
|
||||
"vision_supported": true,
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "glm-4v",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai",
|
||||
"vision_supported": true
|
||||
"vision_supported": true,
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "glm-4v-flash",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai",
|
||||
"vision_supported": true
|
||||
"vision_supported": true,
|
||||
"tool_call_supported": true
|
||||
},
|
||||
{
|
||||
"name": "glm-zero-preview",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai",
|
||||
"vision_supported": true
|
||||
"vision_supported": true,
|
||||
"tool_call_supported": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -16,7 +16,7 @@
|
||||
},
|
||||
"income-msg-check": true,
|
||||
"ignore-rules": {
|
||||
"prefix": ["/"],
|
||||
"prefix": [],
|
||||
"regexp": []
|
||||
},
|
||||
"check-sensitive-words": true,
|
||||
|
||||
@@ -64,6 +64,7 @@
|
||||
"adapter": "gewechat",
|
||||
"enable": false,
|
||||
"gewechat_url": "http://your-gewechat-server:2531",
|
||||
"gewechat_file_url": "http://your-gewechat-server:2532",
|
||||
"port": 2286,
|
||||
"callback_url": "http://your-callback-url:2286/gewechat/callback",
|
||||
"app_id": "",
|
||||
@@ -76,6 +77,7 @@
|
||||
"EncodingAESKey":"",
|
||||
"AppID":"",
|
||||
"AppSecret":"",
|
||||
"Mode":"drop",
|
||||
"host": "0.0.0.0",
|
||||
"port": 2287
|
||||
},
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
"items": {
|
||||
"type": "string",
|
||||
"format": "regex",
|
||||
"pattern": "^(person|group)_(\\d)*$"
|
||||
"pattern": "^(person|group)_.*$"
|
||||
},
|
||||
"default": []
|
||||
},
|
||||
@@ -34,7 +34,7 @@
|
||||
"items": {
|
||||
"type": "string",
|
||||
"format": "regex",
|
||||
"pattern": "^(person|group)_(\\d)*$"
|
||||
"pattern": "^(person|group)_.*$"
|
||||
},
|
||||
"default": []
|
||||
}
|
||||
@@ -99,7 +99,7 @@
|
||||
}
|
||||
},
|
||||
"patternProperties": {
|
||||
"^\\d+$": {
|
||||
"^.*$": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"at": {
|
||||
|
||||
@@ -325,6 +325,11 @@
|
||||
"default": "",
|
||||
"description": "gewechat 的 url"
|
||||
},
|
||||
"gewechat_file_url": {
|
||||
"type": "string",
|
||||
"default": "",
|
||||
"description": "gewechat 文件下载URL"
|
||||
},
|
||||
"port": {
|
||||
"type": "integer",
|
||||
"default": 2286,
|
||||
@@ -380,6 +385,12 @@
|
||||
"default": "",
|
||||
"description": "微信公众号的AppSecret"
|
||||
},
|
||||
"Mode": {
|
||||
"type": "string",
|
||||
"default": "drop",
|
||||
"description": "对于超过15s的响应的处理模式",
|
||||
"enum": ["drop", "passive"]
|
||||
},
|
||||
"host": {
|
||||
"type": "string",
|
||||
"default": "0.0.0.0",
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"items": {
|
||||
"type": "string",
|
||||
"format": "regex",
|
||||
"pattern": "^(person|group)_(\\d+)$"
|
||||
"pattern": "^(person|group)_.*$"
|
||||
},
|
||||
"default": []
|
||||
},
|
||||
@@ -52,7 +52,7 @@
|
||||
}
|
||||
},
|
||||
"patternProperties": {
|
||||
"^(person|group)_(\\d+)$": {
|
||||
"^(person|group)_.*$": {
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user