Compare commits

...

27 Commits

Author SHA1 Message Date
Junyan Qin
d659d01b1e chore: release v3.4.9.4 2025-02-25 17:03:00 +08:00
Junyan Qin
34f73fd84b fix: typo 2025-02-25 17:02:36 +08:00
Junyan Qin (Chin)
54b87ff79d Merge pull request #1128 from wang149876/master
让llm重载可以直接获取本地最新的llm_models.json里面的内容
2025-02-25 16:54:53 +08:00
wang149876
6c2843e7c1 精简为直接复制给llm_models_meta 2025-02-25 16:52:00 +08:00
Junyan Qin (Chin)
6761a31982 Merge pull request #1127 from Yi-Lyu/master
围绕 Gewechat 修改,1)支持聊天记录的消息; 2)图片消息改为图片常规尺寸图片放弃原来的缩略图
2025-02-25 16:15:17 +08:00
Junyan Qin
9401a79b2b feat: update file download url 2025-02-25 16:12:45 +08:00
wang149876
7a4905d943 让llm重载可以直接获取本地最新的llm_models.json里面的内容 2025-02-25 12:56:00 +08:00
Ethan
4db1d2b3a3 fix: comment out debug print statement in gewechat callback 2025-02-25 11:53:23 +08:00
Ethan
2ffe2967d6 feat: add download image port configuration and improve image retrieval process 2025-02-25 11:32:35 +08:00
Ethan
0875c0f266 Merge branch 'RockChinQ:master' into master 2025-02-25 08:48:01 +08:00
Junyan Qin
68c7de5199 chore: release v3.4.9.3 2025-02-24 23:01:10 +08:00
Junyan Qin
4dfb8597ae fix: #1124 2025-02-24 23:00:19 +08:00
Ethan
e21a27ff23 增加微信聊天中图片获取能力,较之前的微信图片仅提供缩略图的情况,改善为获取微信聊天中实际图片大小,方便后续 ocr 或者 llm vision 识别聊天图片内容。 2025-02-24 20:36:03 +08:00
Ethan
91ad7944de 增加微信聊天中图片获取能力,较之前的微信图片仅提供缩略图的情况,改善为获取微信聊天中实际图片大小,方便后续 ocr 或者 llm vision 识别聊天图片内容。 2025-02-24 20:18:35 +08:00
Ethan
c86602ebaf 增加微信聊天中图片获取能力,较之前的微信图片仅提供缩略图的情况,改善为获取微信聊天中实际图片大小,方便后续 ocr 或者 llm vision 识别聊天图片内容。 2025-02-24 20:17:15 +08:00
Ethan
f75ac292db 增加微信聊天中图片获取能力,较之前的微信图片仅提供缩略图的情况,改善为获取微信聊天中实际图片大小,方便后续 ocr 或者 llm vision 识别聊天图片内容。 2025-02-24 20:11:27 +08:00
Ethan
2742c249bf 增加微信聊天中图片获取能力,较之前的微信图片仅提供缩略图的情况,改善为获取微信聊天中实际图片大小,方便后续 ocr 或者 llm vision 识别聊天图片内容。 2025-02-24 20:09:11 +08:00
Ethan
36f04849ab Merge remote-tracking branch 'origin/master'
# Conflicts:
#	pkg/platform/sources/gewechat.py
2025-02-24 20:03:18 +08:00
Ethan
a60c896e89 增加微信聊天中图片获取能力,较之前的微信图片仅提供缩略图的情况,改善为获取微信聊天中实际图片大小,方便后续 ocr 或者 llm vision 识别聊天图片内容。 2025-02-24 20:02:49 +08:00
Ethan
c442320c7f 增加微信聊天中图片获取能力,较之前的微信图片仅提供缩略图的情况,改善为获取微信聊天中实际图片大小,方便后续 ocr 或者 llm vision 识别聊天图片内容。 2025-02-24 19:53:43 +08:00
Ethan
6aeae7e9f5 解决运行报错(base LangBot v3.4.9.2):
[02-24 05:46:37.616] manager.py (169) - [ERROR] : 平台适配器运行出错: 'GeWeChatAdapter' object has no attribute 'name'
2025-02-24 18:53:29 +08:00
Ethan
cae79aac48 添加微信消息类型 49(聊天记录)的支持,支持处理聊天记录类型的微信消息。
微信聊天记录是 xml 数据格式,本质上也是字符串,可以按照字符串Plain类型来处理。
2025-02-24 18:09:02 +08:00
Junyan Qin
0623f4009a chore: release v3.4.9.2 2025-02-24 15:01:00 +08:00
Junyan Qin
06adeb72c4 fix: components.yaml encoding error on windows 2025-02-24 15:00:17 +08:00
Junyan Qin
ef044f4fc7 chore: release v3.4.9.1 2025-02-24 12:23:08 +08:00
Junyan Qin
7cd4e904ca perf: add converting options for dify thinking tips (#1108) 2025-02-24 12:17:33 +08:00
Junyan Qin
c724494ee7 fix: revert streaming resp in chatcmpl 2025-02-24 11:07:42 +08:00
17 changed files with 285 additions and 86 deletions

View File

@@ -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

View File

@@ -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": "",

View File

@@ -0,0 +1,26 @@
from __future__ import annotations
from .. import migration
@migration.migration_class("dify-thinking-config", 33)
class DifyThinkingConfigMigration(migration.Migration):
"""迁移"""
async def need_migrate(self) -> bool:
"""判断当前环境是否需要运行此迁移"""
if 'options' not in self.ap.provider_cfg.data["dify-service-api"]:
return True
if 'convert-thinking-tips' not in self.ap.provider_cfg.data["dify-service-api"]["options"]:
return True
return False
async def run(self):
"""执行迁移"""
self.ap.provider_cfg.data["dify-service-api"]["options"] = {
"convert-thinking-tips": "plain"
}
await self.ap.provider_cfg.dump_config()

View 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()

View File

@@ -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, m034_gewechat_file_url_config
@stage.stage_class("MigrationStage")
class MigrationStage(stage.BootingStage):

View File

@@ -139,7 +139,7 @@ class ComponentDiscoveryEngine:
def load_component_manifest(self, path: str, owner: str = 'builtin', no_save: bool = False) -> Component:
"""加载组件清单"""
with open(path, 'r') as f:
with open(path, 'r', encoding='utf-8') as f:
manifest = yaml.safe_load(f)
comp = Component(
owner=owner,

View File

@@ -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
}

View File

@@ -29,6 +29,9 @@ 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
@@ -48,8 +51,8 @@ class GewechatMessageConverter(adapter.MessageConverter):
return content_list
@staticmethod
async def target2yiri(
self,
message: dict,
bot_account_id: str
) -> platform_message.MessageChain:
@@ -74,25 +77,72 @@ class GewechatMessageConverter(adapter.MessageConverter):
return platform_message.MessageChain(content_list)
elif message["Data"]["MsgType"] == 3:
image_base64 = message["Data"]["ImgBuf"]["buffer"]
return platform_message.MessageChain(
[platform_message.Image(base64=f"data:image/jpeg;base64,{image_base64}")]
)
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="[图片处理失败]")
])
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 +170,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"],
@@ -135,6 +185,8 @@ class GewechatEventConverter(adapter.EventConverter):
class GeWeChatAdapter(adapter.MessagePlatformAdapter):
name: str = "gewechat" # 定义适配器名称
bot: gewechat_client.GewechatClient
quart_app: quart.Quart
@@ -144,8 +196,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,6 +210,9 @@ 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
@@ -257,7 +312,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"])

View File

@@ -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

View File

@@ -49,70 +49,7 @@ class OpenAIChatCompletions(requester.LLMAPIRequester):
self,
args: dict,
) -> chat_completion.ChatCompletion:
args["stream"] = True
chunk = None
pending_content = ""
tool_calls = []
resp_gen: openai.AsyncStream = await self.client.chat.completions.create(**args)
async for chunk in resp_gen:
# print(chunk)
if not chunk or not chunk.id or not chunk.choices or not chunk.choices[0] or not chunk.choices[0].delta:
continue
if chunk.choices[0].delta.content is not None:
pending_content += chunk.choices[0].delta.content
if chunk.choices[0].delta.tool_calls is not None:
for tool_call in chunk.choices[0].delta.tool_calls:
for tc in tool_calls:
if tc.index == tool_call.index:
tc.function.arguments += tool_call.function.arguments
break
else:
tool_calls.append(tool_call)
if chunk.choices[0].finish_reason is not None:
break
real_tool_calls = []
for tc in tool_calls:
function = chat_completion_message_tool_call.Function(
name=tc.function.name,
arguments=tc.function.arguments
)
real_tool_calls.append(chat_completion_message_tool_call.ChatCompletionMessageToolCall(
id=tc.id,
function=function,
type="function"
))
return chat_completion.ChatCompletion(
id=chunk.id,
object="chat.completion",
created=chunk.created,
choices=[
chat_completion.Choice(
index=0,
message=chat_completion.ChatCompletionMessage(
role="assistant",
content=pending_content,
tool_calls=real_tool_calls if len(real_tool_calls) > 0 else None
),
finish_reason=chunk.choices[0].finish_reason if hasattr(chunk.choices[0], 'finish_reason') and chunk.choices[0].finish_reason is not None else 'stop',
logprobs=chunk.choices[0].logprobs,
)
],
model=chunk.model,
service_tier=chunk.service_tier if hasattr(chunk, 'service_tier') else None,
system_fingerprint=chunk.system_fingerprint if hasattr(chunk, 'system_fingerprint') else None,
usage=chunk.usage if hasattr(chunk, 'usage') else None
) if chunk else None
return await self.client.chat.completions.create(**args)
async def _make_msg(
self,

View File

@@ -3,6 +3,7 @@ from __future__ import annotations
import typing
import json
import uuid
import re
import base64
import aiohttp
@@ -41,6 +42,23 @@ class DifyServiceAPIRunner(runner.RequestRunner):
base_url=self.ap.provider_cfg.data["dify-service-api"]["base-url"],
)
def _try_convert_thinking(self, resp_text: str) -> str:
"""尝试转换 Dify 的思考提示"""
if not resp_text.startswith("<details style=\"color:gray;background-color: #f8f8f8;padding: 8px;border-radius: 4px;\" open> <summary> Thinking... </summary>"):
return resp_text
if self.ap.provider_cfg.data["dify-service-api"]["options"]["convert-thinking-tips"] == "original":
return resp_text
if self.ap.provider_cfg.data["dify-service-api"]["options"]["convert-thinking-tips"] == "remove":
return re.sub(r'<details style="color:gray;background-color: #f8f8f8;padding: 8px;border-radius: 4px;" open> <summary> Thinking... </summary>.*?</details>', '', resp_text, flags=re.DOTALL)
if self.ap.provider_cfg.data["dify-service-api"]["options"]["convert-thinking-tips"] == "plain":
pattern = r'<details style="color:gray;background-color: #f8f8f8;padding: 8px;border-radius: 4px;" open> <summary> Thinking... </summary>(.*?)</details>'
thinking_text = re.search(pattern, resp_text, flags=re.DOTALL)
content_text = re.sub(pattern, '', resp_text, flags=re.DOTALL)
return f"<think>{thinking_text.group(1)}</think>\n{content_text}"
async def _preprocess_user_message(
self, query: core_entities.Query
) -> tuple[str, list[str]]:
@@ -109,7 +127,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
if chunk['data']['node_type'] == 'answer':
yield llm_entities.Message(
role="assistant",
content=chunk['data']['outputs']['answer'],
content=self._try_convert_thinking(chunk['data']['outputs']['answer']),
)
elif mode == "basic":
if chunk['event'] == 'message':
@@ -117,7 +135,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
elif chunk['event'] == 'message_end':
yield llm_entities.Message(
role="assistant",
content=basic_mode_pending_chunk,
content=self._try_convert_thinking(basic_mode_pending_chunk),
)
basic_mode_pending_chunk = ''

View File

@@ -1,4 +1,4 @@
semantic_version = "v3.4.9"
semantic_version = "v3.4.9.4"
debug_mode = False

View File

@@ -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

View File

@@ -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": "",

View File

@@ -106,6 +106,9 @@
"dify-service-api": {
"base-url": "https://api.dify.ai/v1",
"app-type": "chat",
"options": {
"convert-thinking-tips": "plain"
},
"chat": {
"api-key": "app-1234567890",
"timeout": 120

View File

@@ -325,6 +325,11 @@
"default": "",
"description": "gewechat 的 url"
},
"gewechat_file_url": {
"type": "string",
"default": "",
"description": "gewechat 文件下载URL"
},
"port": {
"type": "integer",
"default": 2286,

View File

@@ -404,6 +404,20 @@
"enum": ["chat", "workflow", "agent"],
"default": "chat"
},
"options": {
"type": "object",
"title": "Dify Service API 配置选项",
"properties": {
"convert-thinking-tips": {
"type": "string",
"title": "转换思考提示",
"description": "设置转换思考提示。值为 original 时,不转换思考提示;值为 plain 时,将思考提示转换为类似 DeepSeek 官方的<think>...</think>格式;值为 remove 时,删除思考提示",
"enum": ["original", "plain", "remove"],
"default": "plain"
}
}
},
"chat": {
"type": "object",
"title": "聊天助手 API 参数",