From 412f29060655405c05ee8b26ec346f75f8fbae1c Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Wed, 5 Feb 2025 21:55:10 +0800 Subject: [PATCH 01/33] fix(wrapper): potential tool_calls misjudgment --- pkg/pipeline/wrapper/wrapper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/pipeline/wrapper/wrapper.py b/pkg/pipeline/wrapper/wrapper.py index 1ffb3147..a06e4a80 100644 --- a/pkg/pipeline/wrapper/wrapper.py +++ b/pkg/pipeline/wrapper/wrapper.py @@ -102,7 +102,7 @@ class ResponseWrapper(stage.PipelineStage): new_query=query ) - if result.tool_calls is not None: # 有函数调用 + if result.tool_calls is not None and len(result.tool_calls) > 0: # 有函数调用 function_names = [tc.function.name for tc in result.tool_calls] From d0606b79b0a0f580631755d4d1517fd533ed4244 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Wed, 5 Feb 2025 22:10:50 +0800 Subject: [PATCH 02/33] chore: update issue template --- .github/ISSUE_TEMPLATE/bug-report.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/ISSUE_TEMPLATE/bug-report.yml b/.github/ISSUE_TEMPLATE/bug-report.yml index 397c49fe..7181f918 100644 --- a/.github/ISSUE_TEMPLATE/bug-report.yml +++ b/.github/ISSUE_TEMPLATE/bug-report.yml @@ -11,9 +11,12 @@ body: - 其他(或暂未使用) - Nakuru(go-cqhttp) - aiocqhttp(使用 OneBot 协议接入的) - - qq-botpy(QQ官方API) + - qq-botpy(QQ官方API WebSocket) + - qqofficial(QQ官方API Webhook) - lark(飞书) - wecom(企业微信) + - gewechat(个人微信) + - discord validations: required: true - type: input From eceaf85807e82eb79c0eafb65b946ee6a5908c72 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Thu, 6 Feb 2025 14:48:43 +0800 Subject: [PATCH 03/33] feat: use stream req in the chatcmpl (#992) --- pkg/provider/modelmgr/requesters/chatcmpl.py | 64 ++++++++++++++++++- .../modelmgr/requesters/deepseekchatcmpl.py | 3 + 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/pkg/provider/modelmgr/requesters/chatcmpl.py b/pkg/provider/modelmgr/requesters/chatcmpl.py index 9e3014af..55ee0687 100644 --- a/pkg/provider/modelmgr/requesters/chatcmpl.py +++ b/pkg/provider/modelmgr/requesters/chatcmpl.py @@ -8,6 +8,7 @@ from typing import AsyncGenerator import openai import openai.types.chat.chat_completion as chat_completion +import openai.types.chat.chat_completion_message_tool_call as chat_completion_message_tool_call import httpx import aiohttp import async_lru @@ -40,6 +41,7 @@ class OpenAIChatCompletions(requester.LLMAPIRequester): timeout=self.requester_cfg['timeout'], http_client=httpx.AsyncClient( trust_env=True, + timeout=self.requester_cfg['timeout'] ) ) @@ -47,7 +49,67 @@ class OpenAIChatCompletions(requester.LLMAPIRequester): self, args: dict, ) -> chat_completion.ChatCompletion: - return await self.client.chat.completions.create(**args) + 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: + 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) + + 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, + logprobs=chunk.choices[0].logprobs, + ) + ], + model=args["model"], + service_tier=chunk.service_tier, + system_fingerprint=chunk.system_fingerprint, + usage=chunk.usage + ) if chunk else None async def _make_msg( self, diff --git a/pkg/provider/modelmgr/requesters/deepseekchatcmpl.py b/pkg/provider/modelmgr/requesters/deepseekchatcmpl.py index bf414745..7b8c9ca8 100644 --- a/pkg/provider/modelmgr/requesters/deepseekchatcmpl.py +++ b/pkg/provider/modelmgr/requesters/deepseekchatcmpl.py @@ -46,6 +46,9 @@ class DeepseekChatCompletions(chatcmpl.OpenAIChatCompletions): # 发送请求 resp = await self._req(args) + if resp is None: + raise errors.RequesterError('接口返回为空,请确定模型提供商服务是否正常') + # 处理请求结果 message = await self._make_msg(resp) From dbe6272bd8c3cd29b39c0c89032080254ec872c7 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Thu, 6 Feb 2025 14:52:01 +0800 Subject: [PATCH 04/33] chore: release v3.4.6.1 --- pkg/core/notes/n003_print_version.py | 21 +++++++++++++++++++++ pkg/core/stages/show_notes.py | 2 +- pkg/utils/constants.py | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 pkg/core/notes/n003_print_version.py diff --git a/pkg/core/notes/n003_print_version.py b/pkg/core/notes/n003_print_version.py new file mode 100644 index 00000000..6eed21d6 --- /dev/null +++ b/pkg/core/notes/n003_print_version.py @@ -0,0 +1,21 @@ +from __future__ import annotations + +import typing +import os +import sys +import logging + +from .. import note, app + + +@note.note_class("PrintVersion", 3) +class PrintVersion(note.LaunchNote): + """打印版本信息 + """ + + async def need_show(self) -> bool: + return True + + async def yield_note(self) -> typing.AsyncGenerator[typing.Tuple[str, int], None]: + + yield f"当前版本:{self.ap.ver_mgr.get_current_version()}", logging.INFO diff --git a/pkg/core/stages/show_notes.py b/pkg/core/stages/show_notes.py index 91cdbf01..63d8f580 100644 --- a/pkg/core/stages/show_notes.py +++ b/pkg/core/stages/show_notes.py @@ -1,7 +1,7 @@ from __future__ import annotations from .. import stage, app, note -from ..notes import n001_classic_msgs, n002_selection_mode_on_windows +from ..notes import n001_classic_msgs, n002_selection_mode_on_windows, n003_print_version @stage.stage_class("ShowNotesStage") diff --git a/pkg/utils/constants.py b/pkg/utils/constants.py index 0dce973c..5d6ee96d 100644 --- a/pkg/utils/constants.py +++ b/pkg/utils/constants.py @@ -1,4 +1,4 @@ -semantic_version = "v3.4.6" +semantic_version = "v3.4.6.1" debug_mode = False From d0ceaff6ed89ff5cf0a4fec16ea92fa2453464d4 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 7 Feb 2025 02:56:43 +0000 Subject: [PATCH 05/33] chore(deps): bump jsonpath-plus from 10.1.0 to 10.2.0 in /web Bumps [jsonpath-plus](https://github.com/s3u/JSONPath) from 10.1.0 to 10.2.0. - [Release notes](https://github.com/s3u/JSONPath/releases) - [Changelog](https://github.com/JSONPath-Plus/JSONPath/blob/main/CHANGES.md) - [Commits](https://github.com/s3u/JSONPath/compare/v10.1.0...v10.2.0) --- updated-dependencies: - dependency-name: jsonpath-plus dependency-type: indirect ... Signed-off-by: dependabot[bot] --- web/package-lock.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/web/package-lock.json b/web/package-lock.json index d692c64b..331b2815 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -4410,14 +4410,14 @@ } }, "node_modules/jsonpath-plus": { - "version": "10.1.0", - "resolved": "https://registry.npmjs.org/jsonpath-plus/-/jsonpath-plus-10.1.0.tgz", - "integrity": "sha512-gHfV1IYqH8uJHYVTs8BJX1XKy2/rR93+f8QQi0xhx95aCiXn1ettYAd5T+7FU6wfqyDoX/wy0pm/fL3jOKJ9Lg==", + "version": "10.2.0", + "resolved": "https://registry.npmjs.org/jsonpath-plus/-/jsonpath-plus-10.2.0.tgz", + "integrity": "sha512-T9V+8iNYKFL2n2rF+w02LBOT2JjDnTjioaNFrxRy0Bv1y/hNsqR/EBK7Ojy2ythRHwmz2cRIls+9JitQGZC/sw==", "license": "MIT", "dependencies": { - "@jsep-plugin/assignment": "^1.2.1", - "@jsep-plugin/regex": "^1.0.3", - "jsep": "^1.3.9" + "@jsep-plugin/assignment": "^1.3.0", + "@jsep-plugin/regex": "^1.0.4", + "jsep": "^1.4.0" }, "bin": { "jsonpath": "bin/jsonpath-cli.js", From e83b0a7825f0d0ce3a15ec183c88197d4377277f Mon Sep 17 00:00:00 2001 From: Lorenzo Feng <42567930+7emotions@users.noreply.github.com> Date: Fri, 7 Feb 2025 21:19:47 +0800 Subject: [PATCH 06/33] fix: remove fatal clearance to message from QQWebhook --- pkg/platform/sources/qqofficial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/platform/sources/qqofficial.py b/pkg/platform/sources/qqofficial.py index f41e84db..c85868b0 100644 --- a/pkg/platform/sources/qqofficial.py +++ b/pkg/platform/sources/qqofficial.py @@ -47,7 +47,7 @@ class QQOfficialMessageConverter(adapter.MessageConverter): yiri_msg_list.append( platform_message.Image(base64=base64_url) ) - message = '' + yiri_msg_list.append(platform_message.Plain(text=message)) chain = platform_message.MessageChain(yiri_msg_list) return chain From 21cfb6ee6f305a4310082aa4a7d9032754167a4c Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Fri, 7 Feb 2025 23:57:51 +0800 Subject: [PATCH 07/33] fix: some field may not exist in chatcmplchunk --- pkg/provider/modelmgr/requesters/chatcmpl.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/provider/modelmgr/requesters/chatcmpl.py b/pkg/provider/modelmgr/requesters/chatcmpl.py index 55ee0687..0b1c4cf2 100644 --- a/pkg/provider/modelmgr/requesters/chatcmpl.py +++ b/pkg/provider/modelmgr/requesters/chatcmpl.py @@ -101,14 +101,14 @@ class OpenAIChatCompletions(requester.LLMAPIRequester): content=pending_content, tool_calls=real_tool_calls if len(real_tool_calls) > 0 else None ), - finish_reason=chunk.choices[0].finish_reason, + 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=args["model"], - service_tier=chunk.service_tier, - system_fingerprint=chunk.system_fingerprint, - usage=chunk.usage + 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 async def _make_msg( From 4d8ebc8c38b2b3c49ee636aa949bd0c9d642658a Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Sat, 8 Feb 2025 00:05:12 +0800 Subject: [PATCH 08/33] chore: release v3.4.6.2 --- pkg/utils/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/utils/constants.py b/pkg/utils/constants.py index 5d6ee96d..61ed497b 100644 --- a/pkg/utils/constants.py +++ b/pkg/utils/constants.py @@ -1,4 +1,4 @@ -semantic_version = "v3.4.6.1" +semantic_version = "v3.4.6.2" debug_mode = False From 9eefbcb6f2790397fb7df9c9925d993c0cea15c6 Mon Sep 17 00:00:00 2001 From: wanjiaju <邮箱> Date: Sat, 8 Feb 2025 10:27:19 +0800 Subject: [PATCH 09/33] =?UTF-8?q?=E9=98=BF=E9=87=8C=E4=BA=91=E7=99=BE?= =?UTF-8?q?=E7=82=BC=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增阿里云请求器配置、阿里云模型配置、阿里云令牌配置 新增硅基模型配置 --- pkg/provider/modelmgr/modelmgr.py | 2 +- .../modelmgr/requesters/aliyunchatcmpl.py | 21 ++++++++++++++++ templates/metadata/llm-models.json | 24 +++++++++++++++++++ templates/provider.json | 2 +- 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 pkg/provider/modelmgr/requesters/aliyunchatcmpl.py diff --git a/pkg/provider/modelmgr/modelmgr.py b/pkg/provider/modelmgr/modelmgr.py index 33a65ff3..6748291c 100644 --- a/pkg/provider/modelmgr/modelmgr.py +++ b/pkg/provider/modelmgr/modelmgr.py @@ -6,7 +6,7 @@ from . import entities, requester from ...core import app from . import token -from .requesters import chatcmpl, anthropicmsgs, moonshotchatcmpl, deepseekchatcmpl, ollamachat, giteeaichatcmpl, xaichatcmpl, zhipuaichatcmpl, lmstudiochatcmpl, siliconflowchatcmpl +from .requesters import chatcmpl, anthropicmsgs, moonshotchatcmpl, deepseekchatcmpl, ollamachat, giteeaichatcmpl, xaichatcmpl, zhipuaichatcmpl, lmstudiochatcmpl, siliconflowchatcmpl, aliyunchatcmpl FETCH_MODEL_LIST_URL = "https://api.qchatgpt.rockchin.top/api/v2/fetch/model_list" diff --git a/pkg/provider/modelmgr/requesters/aliyunchatcmpl.py b/pkg/provider/modelmgr/requesters/aliyunchatcmpl.py new file mode 100644 index 00000000..4f4900d7 --- /dev/null +++ b/pkg/provider/modelmgr/requesters/aliyunchatcmpl.py @@ -0,0 +1,21 @@ +from __future__ import annotations + +import openai + +from . import chatcmpl +from .. import requester +from ....core import app + + +@requester.requester_class("aliyun-chat-completions") +class AliyunChatCompletions(chatcmpl.OpenAIChatCompletions): + """Aliyun ChatCompletion API 请求器""" + + client: openai.AsyncClient + + requester_cfg: dict + + def __init__(self, ap: app.Application): + self.ap = ap + + self.requester_cfg = self.ap.provider_cfg.data['requester']['aliyun-chat-completions'] diff --git a/templates/metadata/llm-models.json b/templates/metadata/llm-models.json index b5c29cf3..110c18da 100644 --- a/templates/metadata/llm-models.json +++ b/templates/metadata/llm-models.json @@ -211,6 +211,30 @@ "requester": "zhipuai-chat-completions", "token_mgr": "zhipuai", "vision_supported": true + }, + { + "name": "siliconflow-r1", + "model_name": "deepseek-ai/DeepSeek-R1", + "requester": "siliconflow-chat-completions", + "token_mgr": "siliconflow" + }, + { + "name": "siliconflow-v3", + "model_name": "deepseek-ai/DeepSeek-V3", + "requester": "siliconflow-chat-completions", + "token_mgr": "siliconflow" + }, + { + "name": "aliyun-r1", + "model_name": "deepseek-r1", + "requester": "aliyun-chat-completions", + "token_mgr": "aliyun" + }, + { + "name": "aliyun-v3", + "model_name": "deepseek-v3", + "requester": "aliyun-chat-completions", + "token_mgr": "aliyun" } ] } \ No newline at end of file diff --git a/templates/provider.json b/templates/provider.json index 5ab5cd64..0d6e6ea0 100644 --- a/templates/provider.json +++ b/templates/provider.json @@ -84,7 +84,7 @@ "model": "gpt-4o", "prompt-mode": "normal", "prompt": { - "default": "" + "default": "" }, "runner": "local-agent", "dify-service-api": { From 8e9f43885a46c44dcff942acf4b2e7b923540990 Mon Sep 17 00:00:00 2001 From: wanjiaju <邮箱> Date: Sat, 8 Feb 2025 10:30:19 +0800 Subject: [PATCH 10/33] =?UTF-8?q?=E9=98=BF=E9=87=8C=E4=BA=91=E7=99=BE?= =?UTF-8?q?=E7=82=BC=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增阿里云请求器配置、阿里云模型配置、阿里云令牌配置 新增硅基模型配置 --- templates/provider.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/templates/provider.json b/templates/provider.json index 0d6e6ea0..c22ba42c 100644 --- a/templates/provider.json +++ b/templates/provider.json @@ -25,6 +25,9 @@ ], "siliconflow": [ "xxxxxxx" + ], + "aliyun": [ + "sk-aliyun_token" ] }, "requester": { @@ -79,6 +82,11 @@ "base-url": "https://api.siliconflow.cn/v1", "args": {}, "timeout": 120 + }, + "aliyun-chat-completions": { + "args": {}, + "base-url": "https://dashscope.aliyuncs.com/compatible-mode/v1", + "timeout": 120 } }, "model": "gpt-4o", From 52eb37d13dbca0e175aa23bde56080ff8ab9b7ea Mon Sep 17 00:00:00 2001 From: Civic_Crab <1308770968@qq.com> Date: Sun, 9 Feb 2025 06:32:49 +0800 Subject: [PATCH 11/33] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=98=BF=E9=87=8C?= =?UTF-8?q?=E4=BA=91=E7=99=BE=E7=82=BC=E7=9A=84=E9=80=9A=E7=94=A8=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E5=92=8C=E8=87=AA=E5=AE=9A=E4=B9=89=E5=A4=A7=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E5=BA=94=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/provider/modelmgr/modelmgr.py | 2 +- .../modelmgr/requesters/dashscopecmpl.py | 167 ++++++++++++++++++ .../modelmgr/requesters/qwenchatcmpl.py | 21 +++ templates/metadata/llm-models.json | 10 ++ templates/provider.json | 16 ++ 5 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 pkg/provider/modelmgr/requesters/dashscopecmpl.py create mode 100644 pkg/provider/modelmgr/requesters/qwenchatcmpl.py diff --git a/pkg/provider/modelmgr/modelmgr.py b/pkg/provider/modelmgr/modelmgr.py index 33a65ff3..737d3476 100644 --- a/pkg/provider/modelmgr/modelmgr.py +++ b/pkg/provider/modelmgr/modelmgr.py @@ -6,7 +6,7 @@ from . import entities, requester from ...core import app from . import token -from .requesters import chatcmpl, anthropicmsgs, moonshotchatcmpl, deepseekchatcmpl, ollamachat, giteeaichatcmpl, xaichatcmpl, zhipuaichatcmpl, lmstudiochatcmpl, siliconflowchatcmpl +from .requesters import chatcmpl, anthropicmsgs, moonshotchatcmpl, deepseekchatcmpl, ollamachat, giteeaichatcmpl, xaichatcmpl, zhipuaichatcmpl, lmstudiochatcmpl, siliconflowchatcmpl, dashscopecmpl, qwenchatcmpl FETCH_MODEL_LIST_URL = "https://api.qchatgpt.rockchin.top/api/v2/fetch/model_list" diff --git a/pkg/provider/modelmgr/requesters/dashscopecmpl.py b/pkg/provider/modelmgr/requesters/dashscopecmpl.py new file mode 100644 index 00000000..d2f2b2d2 --- /dev/null +++ b/pkg/provider/modelmgr/requesters/dashscopecmpl.py @@ -0,0 +1,167 @@ +from __future__ import annotations + +import re +import asyncio +import typing +import dashscope + +from .. import entities, errors, requester +from ....core import entities as core_entities, app +from ... import entities as llm_entities +from ...tools import entities as tools_entities + +#阿里云百炼平台的自定义应用支持资料引用,此函数可以将引用标签替换为参考资料 +def replace_references(text, references_dict): + # 修正正则表达式,匹配 [index_id] 形式的字符串 + pattern = re.compile(r'\[(.*?)\]') + + def replacement(match): + ref_key = match.group(1) # 获取引用编号 + if ref_key in references_dict: + return f"(参考资料来自:{references_dict[ref_key]})" + else: + return match.group(0) # 如果没有对应的参考资料,保留原样 + + # 使用 re.sub() 进行替换 + return pattern.sub(replacement, text) + + +@requester.requester_class("dashscope-chat-applications") +class DashscopeChatApplication(requester.LLMAPIRequester): + """Dashscope ChatApplications API 请求器""" + + requester_cfg: dict + + def __init__(self, ap: app.Application): + self.requester_cfg = ap.provider_cfg.data['requester']['dashscope-chat-applications'] + self.ap = ap + + async def initialize(self): + dashscope.api_key = self.ap.provider_cfg.data['keys']['dashscope'][0] + + async def _req(self, args: dict): + + #print("args:", args) + + #局部变量 + chunk = None + pending_content = "" + output = { + "role": "assistant", + "content": "", + "tool_calls": [], + "tool_call_id": None # Dashscope暂时不支持工具调用 + } #由于Dashscope的content的键值是text,所以需要定义一个新格式的字典适配llm_entities.Message + + references_dict = {} # 用于存储引用编号和对应的参考资料 + + #调用API + response = dashscope.Application.call( + api_key=dashscope.api_key, + app_id=args["model"], + prompt=args["messages"], + stream=True, # 设置流式输出 + tools=args.get("tools", None), + incremental_output = True, + ) + + #处理API返回的流式输出 + for chunk in response: + #print(chunk) + if not chunk: + continue + + #获取流式传输的output + stream_output = chunk.get("output", {}) + if stream_output.get("text") is not None: + pending_content += stream_output.get("text") + + + #获取模型传出的参考资料列表 + references_dict_list = stream_output.get("doc_references", []) + + #从模型传出的参考资料信息中提取用于替换的字典 + if references_dict_list is not None: + for doc in references_dict_list: + if doc.get("index_id") is not None: + references_dict[doc.get("index_id")] = doc.get("doc_name") + + #将参考资料替换到文本中 + pending_content = replace_references(pending_content, references_dict) + + #将流式传输的内容整合到output中 + output["content"] = pending_content + + return output if chunk else None + + async def _make_msg( + self, + chat_completion: dict, + ) -> llm_entities.Message: + chatcmpl_message = chat_completion + + # 确保 role 字段存在且不为 None + if 'role' not in chatcmpl_message or chatcmpl_message['role'] is None: + chatcmpl_message['role'] = 'assistant' + + message = llm_entities.Message(**chatcmpl_message) + #print("message:", message) + return message + + async def _closure( + self, + query: core_entities.Query, + req_messages: list[dict], + use_model: entities.LLMModelInfo, + use_funcs: list[tools_entities.LLMFunction] = None, + ) -> llm_entities.Message: + + args = self.requester_cfg['args'].copy() + args["model"] = use_model.name if use_model.model_name is None else use_model.model_name + + # 设置此次请求中的messages + messages = req_messages.copy() + + # 检查vision + for msg in messages: + if 'content' in msg and isinstance(msg["content"], list): + for me in msg["content"]: + if me["type"] == "image_base64": + me["image_url"] = { + "url": me["image_base64"] + } + me["type"] = "image_url" + del me["image_base64"] + + args["messages"] = messages + + # 发送请求 + resp = await self._req(args) + + # 处理请求结果 + message = await self._make_msg(resp) + + return message + + async def call( + self, + query: core_entities.Query, + model: entities.LLMModelInfo, + messages: typing.List[llm_entities.Message], + funcs: typing.List[tools_entities.LLMFunction] = None, + ) -> llm_entities.Message: + req_messages = [] # req_messages 仅用于类内,外部同步由 query.messages 进行 + for m in messages: + msg_dict = m.dict(exclude_none=True) + content = msg_dict.get("content") + if isinstance(content, list): + # 检查 content 列表中是否每个部分都是文本 + if all(isinstance(part, dict) and part.get("type") == "text" for part in content): + # 将所有文本部分合并为一个字符串 + msg_dict["content"] = "\n".join(part["text"] for part in content) + req_messages.append(msg_dict) + + try: + return await self._closure(query=query, req_messages=req_messages, use_model=model, use_funcs=funcs) + except asyncio.TimeoutError: + raise errors.RequesterError('请求超时') diff --git a/pkg/provider/modelmgr/requesters/qwenchatcmpl.py b/pkg/provider/modelmgr/requesters/qwenchatcmpl.py new file mode 100644 index 00000000..8dfbae8e --- /dev/null +++ b/pkg/provider/modelmgr/requesters/qwenchatcmpl.py @@ -0,0 +1,21 @@ +from __future__ import annotations + +import openai + +from . import chatcmpl +from .. import requester +from ....core import app + + +@requester.requester_class("siliconflow-chat-completions") +class QwenChatCompletions(chatcmpl.OpenAIChatCompletions): + """Qwen ChatCompletion API 请求器""" + + client: openai.AsyncClient + + requester_cfg: dict + + def __init__(self, ap: app.Application): + self.ap = ap + + self.requester_cfg = self.ap.provider_cfg.data['requester']['qwen-chat-completions'] diff --git a/templates/metadata/llm-models.json b/templates/metadata/llm-models.json index b5c29cf3..0dfb2a43 100644 --- a/templates/metadata/llm-models.json +++ b/templates/metadata/llm-models.json @@ -211,6 +211,16 @@ "requester": "zhipuai-chat-completions", "token_mgr": "zhipuai", "vision_supported": true + }, + { + "name": "your-dashscope-app-id", + "requester": "dashscope-chat-applications", + "token_mgr": "dashscope", + }, + { + "name": "qwen-plus", + "requester": "qwen-chat-completions", + "token_mgr": "qwen", } ] } \ No newline at end of file diff --git a/templates/provider.json b/templates/provider.json index 5ab5cd64..9db3c020 100644 --- a/templates/provider.json +++ b/templates/provider.json @@ -25,6 +25,12 @@ ], "siliconflow": [ "xxxxxxx" + ], + "dashscope": [ + "sk-1234567890" + ], + "qwen": [ + "sk-1234567890", ] }, "requester": { @@ -40,6 +46,16 @@ }, "timeout": 120 }, + "dashscope-chat-applications": { + "args": {}, + "base-url": "https://dashscope.aliyuncs.com/api/v1", + "timeout": 120 + }, + "qwen-chat-completions": { + "base-url": "https://dashscope.aliyuncs.com/compatible-mode/v1", + "args": {}, + "timeout": 120 + }, "moonshot-chat-completions": { "base-url": "https://api.moonshot.cn/v1", "args": {}, From cbec2f6d02480694b13333ff37e7849a62c69472 Mon Sep 17 00:00:00 2001 From: Civic_Crab <1308770968@qq.com> Date: Sun, 9 Feb 2025 06:37:55 +0800 Subject: [PATCH 12/33] =?UTF-8?q?=E6=96=B0=E5=A2=9Edashscope=E4=BE=9D?= =?UTF-8?q?=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 27349f64..dbee8256 100644 --- a/requirements.txt +++ b/requirements.txt @@ -29,6 +29,7 @@ lark-oapi discord.py cryptography gewechat-client +dashscope # indirect taskgroup==0.0.0a4 \ No newline at end of file From 9e718a2e8ad85db83db6d961b9f97d7546401e61 Mon Sep 17 00:00:00 2001 From: Civic_Crab <1308770968@qq.com> Date: Sun, 9 Feb 2025 06:39:39 +0800 Subject: [PATCH 13/33] =?UTF-8?q?=E6=96=B0=E5=A2=9Edashscope=E4=BE=9D?= =?UTF-8?q?=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/core/bootutils/deps.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/core/bootutils/deps.py b/pkg/core/bootutils/deps.py index b4a67f35..83a9b2a7 100644 --- a/pkg/core/bootutils/deps.py +++ b/pkg/core/bootutils/deps.py @@ -29,7 +29,8 @@ required_deps = { "lark_oapi": "lark-oapi", "discord": "discord.py", "cryptography": "cryptography", - "gewechat_client": "gewechat-client" + "gewechat_client": "gewechat-client", + "dashscope": "dashscope", } From d7687913a9b734d4fb83b9c866b774fd2b82f823 Mon Sep 17 00:00:00 2001 From: "Junyan Qin (Chin)" Date: Mon, 10 Feb 2025 11:04:57 +0800 Subject: [PATCH 14/33] doc(README.md): update trendingshift badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fc78032c..2921bf23 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@
-RockChinQ%2FQChatGPT | Trendshift +RockChinQ%2FLangBot | Trendshift 项目主页功能介绍 | From b6e054a73fdec30fcb109475920b7d1d19c9805d Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Tue, 11 Feb 2025 00:23:38 +0800 Subject: [PATCH 15/33] chore: migrations for `officialaccount` adapter --- .../m027_wx_official_account_config.py | 32 +++++++++++++ pkg/core/stages/migrate.py | 2 +- templates/platform.json | 4 +- templates/schema/platform.json | 45 +++++++++++++++++++ 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 pkg/core/migrations/m027_wx_official_account_config.py diff --git a/pkg/core/migrations/m027_wx_official_account_config.py b/pkg/core/migrations/m027_wx_official_account_config.py new file mode 100644 index 00000000..510b7108 --- /dev/null +++ b/pkg/core/migrations/m027_wx_official_account_config.py @@ -0,0 +1,32 @@ +from __future__ import annotations + +from .. import migration + + +@migration.migration_class("wx-official-account-config", 27) +class WXOfficialAccountConfigMigration(migration.Migration): + """迁移""" + + async def need_migrate(self) -> bool: + """判断当前环境是否需要运行此迁移""" + + for adapter in self.ap.platform_cfg.data['platform-adapters']: + if adapter['adapter'] == 'officialaccount': + return False + + return True + + async def run(self): + """执行迁移""" + self.ap.platform_cfg.data['platform-adapters'].append({ + "adapter": "officialaccount", + "enable": False, + "token": "", + "EncodingAESKey": "", + "AppID": "", + "AppSecret": "", + "host": "0.0.0.0", + "port": 2287 + }) + + await self.ap.platform_cfg.dump_config() diff --git a/pkg/core/stages/migrate.py b/pkg/core/stages/migrate.py index 16faa53a..a1983f0b 100644 --- a/pkg/core/stages/migrate.py +++ b/pkg/core/stages/migrate.py @@ -9,7 +9,7 @@ from ..migrations import m005_deepseek_cfg_completion, m006_vision_config, m007_ from ..migrations import m010_ollama_requester_config, m011_command_prefix_config, m012_runner_config, m013_http_api_config, m014_force_delay_config from ..migrations import m015_gitee_ai_config, m016_dify_service_api, m017_dify_api_timeout_params, m018_xai_config, m019_zhipuai_config 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 +from ..migrations import m026_qqofficial_config, m027_wx_official_account_config @stage.stage_class("MigrationStage") class MigrationStage(stage.BootingStage): diff --git a/templates/platform.json b/templates/platform.json index 5a1fca77..58742b37 100644 --- a/templates/platform.json +++ b/templates/platform.json @@ -68,11 +68,11 @@ }, { "adapter":"officialaccount", - "enable": true, + "enable": false, "token": "", "EncodingAESKey":"", - "AppSecret":"", "AppID":"", + "AppSecret":"", "host": "0.0.0.0", "port": 2287 } diff --git a/templates/schema/platform.json b/templates/schema/platform.json index f2db0f79..9a5deeb0 100644 --- a/templates/schema/platform.json +++ b/templates/schema/platform.json @@ -331,6 +331,51 @@ "description": "gewechat 的 token" } } + }, + { + "title": "微信公众号适配器", + "description": "用于接入微信公众号", + "properties": { + "adapter": { + "type": "string", + "const": "officialaccount" + }, + "enable": { + "type": "boolean", + "default": false, + "description": "是否启用此适配器" + }, + "token": { + "type": "string", + "default": "", + "description": "微信公众号的token" + }, + "EncodingAESKey": { + "type": "string", + "default": "", + "description": "微信公众号的EncodingAESKey" + }, + "AppID": { + "type": "string", + "default": "", + "description": "微信公众号的AppID" + }, + "AppSecret": { + "type": "string", + "default": "", + "description": "微信公众号的AppSecret" + }, + "host": { + "type": "string", + "default": "0.0.0.0", + "description": "监听的IP地址" + }, + "port": { + "type": "integer", + "default": 2287, + "description": "监听的端口" + } + } } ] } From 8d00e710d55fd1a48b9d50fcc5f5949925aa5556 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Tue, 11 Feb 2025 00:25:45 +0800 Subject: [PATCH 16/33] doc(README): add official account compatibility comment --- README.md | 1 + README_EN.md | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index 2921bf23..faab043b 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ | QQ 个人号 | ✅ | QQ 个人号私聊、群聊 | | QQ 官方机器人 | ✅ | QQ 官方机器人,支持频道、私聊、群聊 | | 企业微信 | ✅ | | +| 微信公众号 | ✅ | | | 飞书 | ✅ | | | Discord | ✅ | | | 个人微信 | ✅ | 使用 [Gewechat](https://github.com/Devo919/Gewechat) 接入 | diff --git a/README_EN.md b/README_EN.md index fd266515..59360ff6 100644 --- a/README_EN.md +++ b/README_EN.md @@ -85,6 +85,7 @@ Directly use the released version to run, see the [Manual Deployment](https://do | Personal QQ | ✅ | | | QQ Official API | ✅ | | | WeCom | ✅ | | +| WeChat Official Account | ✅ | | | Lark | ✅ | | | Discord | ✅ | | | Personal WeChat | ✅ | Use [Gewechat](https://github.com/Devo919/Gewechat) to access | From 1b1ccdd733f8ac69a86b8722067c8ba53d4bf150 Mon Sep 17 00:00:00 2001 From: Ikko Eltociear Ashimine Date: Tue, 11 Feb 2025 03:07:31 +0900 Subject: [PATCH 17/33] docs: add Japanese README I created Japanese translated README. --- README.md | 2 +- README_EN.md | 2 +- README_JP.md | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 README_JP.md diff --git a/README.md b/README.md index 2921bf23..15c167e3 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ ![Dynamic JSON Badge](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fapi.qchatgpt.rockchin.top%2Fapi%2Fv2%2Fview%2Frealtime%2Fcount_query%3Fminute%3D10080&query=%24.data.count&label=%E4%BD%BF%E7%94%A8%E9%87%8F%EF%BC%887%E6%97%A5%EF%BC%89) python -[简体中文](README.md) / [English](README_EN.md) +[简体中文](README.md) / [English](README_EN.md) / [日本語](README_JP.md)
diff --git a/README_EN.md b/README_EN.md index fd266515..02aca018 100644 --- a/README_EN.md +++ b/README_EN.md @@ -26,7 +26,7 @@ ![Dynamic JSON Badge](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fapi.qchatgpt.rockchin.top%2Fapi%2Fv2%2Fview%2Frealtime%2Fcount_query%3Fminute%3D10080&query=%24.data.count&label=Usage(7days)) python -[简体中文](README.md) / [English](README_EN.md) +[简体中文](README.md) / [English](README_EN.md) / [日本語](README_JP.md) diff --git a/README_JP.md b/README_JP.md new file mode 100644 index 00000000..675f9bdd --- /dev/null +++ b/README_JP.md @@ -0,0 +1,123 @@ +

+ +LangBot + + +

+ +RockChinQ%2FQChatGPT | Trendshift + +ホーム | +機能 | +デプロイ | +FAQ | +プラグイン | +プラグインの提出 + +
+😎高い安定性、🧩拡張サポート、🦄マルチモーダル - LLMネイティブインスタントメッセージングボットプラットフォーム🤖 +
+ +
+ + + +[![GitHub release (latest by date)](https://img.shields.io/github/v/release/RockChinQ/LangBot)](https://github.com/RockChinQ/LangBot/releases/latest) + ![Dynamic JSON Badge](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fapi.qchatgpt.rockchin.top%2Fapi%2Fv2%2Fview%2Frealtime%2Fcount_query%3Fminute%3D10080&query=%24.data.count&label=Usage(7days)) +python + +[简体中文](README.md) / [English](README_EN.md) / [日本語](README_JP.md) + +
+ +

+ +## ✨ 機能 + +- 💬 LLM / エージェントとのチャット: 複数のLLMをサポートし、グループチャットとプライベートチャットに対応。マルチラウンドの会話、ツールの呼び出し、マルチモーダル機能をサポート。 [Dify](https://dify.ai) と深く統合。現在、QQ、QQチャンネル、WeCom、Lark、Discord、個人WeChatをサポートし、将来的にはWhatsApp、Telegramなどもサポート予定。 +- 🛠️ 高い安定性、豊富な機能: ネイティブのアクセス制御、レート制限、敏感な単語のフィルタリングなどのメカニズムをサポート。使いやすく、複数のデプロイ方法をサポート。 +- 🧩 プラグイン拡張、活発なコミュニティ: イベント駆動、コンポーネント拡張などのプラグインメカニズムをサポート。豊富なエコシステム、現在数十の[プラグイン](https://docs.langbot.app/plugin/plugin-intro.html)が存在。 +- 😻 [新機能] Web UI: ブラウザを通じてLangBotインスタンスを管理することをサポート。詳細は[ドキュメント](https://docs.langbot.app/webui/intro.html)を参照。 + +## 📦 始め方 + +> [!IMPORTANT] +> +> - どのデプロイ方法を始める前に、必ず[新規ユーザーガイド](https://docs.langbot.app/insight/guide.html)をお読みください。 +> - すべてのドキュメントは中国語で提供されています。近い将来、i18nバージョンを提供する予定です。 + +#### Docker Compose デプロイ + +Dockerに慣れているユーザーに適しています。[Dockerデプロイ](https://docs.langbot.app/deploy/langbot/docker.html)のドキュメントを参照してください。 + +#### BTPanelでのワンクリックデプロイ + +LangBotはBTPanelにリストされています。BTPanelをインストールしている場合は、[ドキュメント](https://docs.langbot.app/deploy/langbot/one-click/bt.html)を使用して使用できます。 + +#### Zeaburクラウドデプロイ + +コミュニティが提供するZeaburテンプレート。 + +[![Deploy on Zeabur](https://zeabur.com/button.svg)](https://zeabur.com/zh-CN/templates/ZKTBDH) + +#### Railwayクラウドデプロイ + +[![Deploy on Railway](https://railway.com/button.svg)](https://railway.app/template/yRrAyL?referralCode=vogKPF) + +#### その他のデプロイ方法 + +リリースバージョンを直接使用して実行します。[手動デプロイ](https://docs.langbot.app/deploy/langbot/manual.html)のドキュメントを参照してください。 + +## 📸 デモ + +返信効果(インターネットプラグイン付き) + +- WebUIデモ: https://demo.langbot.dev/ + - ログイン情報: メール: `demo@langbot.app` パスワード: `langbot123456` + - 注意: WebUIの効果のみを示しています。公開環境では、機密情報を入力しないでください。 + +## 🔌 コンポーネントの互換性 + +### メッセージプラットフォーム + +| プラットフォーム | ステータス | 備考 | +| --- | --- | --- | +| 個人QQ | ✅ | | +| QQ公式API | ✅ | | +| WeCom | ✅ | | +| Lark | ✅ | | +| Discord | ✅ | | +| 個人WeChat | ✅ | [Gewechat](https://github.com/Devo919/Gewechat)を使用して接続 | +| Telegram | 🚧 | | +| WhatsApp | 🚧 | | +| DingTalk | 🚧 | | + +🚧: 開発中 + +### LLMs + +| LLM | ステータス | 備考 | +| --- | --- | --- | +| [OpenAI](https://platform.openai.com/) | ✅ | 任意のOpenAIインターフェース形式モデルに対応 | +| [DeepSeek](https://www.deepseek.com/) | ✅ | | +| [Moonshot](https://www.moonshot.cn/) | ✅ | | +| [Anthropic](https://www.anthropic.com/) | ✅ | | +| [xAI](https://x.ai/) | ✅ | | +| [Zhipu AI](https://open.bigmodel.cn/) | ✅ | | +| [Dify](https://dify.ai) | ✅ | LLMOpsプラットフォーム | +| [Ollama](https://ollama.com/) | ✅ | ローカルLLM実行プラットフォーム | +| [LMStudio](https://lmstudio.ai/) | ✅ | ローカルLLM実行プラットフォーム | +| [GiteeAI](https://ai.gitee.com/) | ✅ | LLMインターフェースゲートウェイ(MaaS) | +| [SiliconFlow](https://siliconflow.cn/) | ✅ | LLMゲートウェイ(MaaS) | +| [Aliyun Bailian](https://bailian.console.aliyun.com/) | ✅ | LLMゲートウェイ(MaaS) | + +## 🤝 コミュニティ貢献 + +以下の貢献者とコミュニティの皆さんの貢献に感謝します。 + + + + + + + From a6bc617a3bc66bc11f9a80bcbceec2e2a1b3f4ed Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Tue, 11 Feb 2025 12:19:12 +0800 Subject: [PATCH 18/33] docs: add discord link --- README.md | 1 + README_EN.md | 2 +- README_JP.md | 3 +-- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 15c167e3..908701cb 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@
+[![Discord](https://img.shields.io/discord/1335141740050649118?logo=discord&labelColor=%20%235462eb&logoColor=%20%23f5f5f5&color=%20%235462eb)](https://discord.gg/wdNEHETs87) [![QQ Group](https://img.shields.io/badge/%E7%A4%BE%E5%8C%BAQQ%E7%BE%A4-1030838208-blue)](https://qm.qq.com/q/PF9OuQCCcM) [![GitHub release (latest by date)](https://img.shields.io/github/v/release/RockChinQ/LangBot)](https://github.com/RockChinQ/LangBot/releases/latest) ![Dynamic JSON Badge](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fapi.qchatgpt.rockchin.top%2Fapi%2Fv2%2Fview%2Frealtime%2Fcount_query%3Fminute%3D10080&query=%24.data.count&label=%E4%BD%BF%E7%94%A8%E9%87%8F%EF%BC%887%E6%97%A5%EF%BC%89) diff --git a/README_EN.md b/README_EN.md index 02aca018..c0b53712 100644 --- a/README_EN.md +++ b/README_EN.md @@ -21,7 +21,7 @@
- +[![Discord](https://img.shields.io/discord/1335141740050649118?logo=discord&labelColor=%20%235462eb&logoColor=%20%23f5f5f5&color=%20%235462eb)](https://discord.gg/wdNEHETs87) [![GitHub release (latest by date)](https://img.shields.io/github/v/release/RockChinQ/LangBot)](https://github.com/RockChinQ/LangBot/releases/latest) ![Dynamic JSON Badge](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fapi.qchatgpt.rockchin.top%2Fapi%2Fv2%2Fview%2Frealtime%2Fcount_query%3Fminute%3D10080&query=%24.data.count&label=Usage(7days)) python diff --git a/README_JP.md b/README_JP.md index 675f9bdd..216f40f7 100644 --- a/README_JP.md +++ b/README_JP.md @@ -20,8 +20,7 @@
- - +[![Discord](https://img.shields.io/discord/1335141740050649118?logo=discord&labelColor=%20%235462eb&logoColor=%20%23f5f5f5&color=%20%235462eb)](https://discord.gg/wdNEHETs87) [![GitHub release (latest by date)](https://img.shields.io/github/v/release/RockChinQ/LangBot)](https://github.com/RockChinQ/LangBot/releases/latest) ![Dynamic JSON Badge](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fapi.qchatgpt.rockchin.top%2Fapi%2Fv2%2Fview%2Frealtime%2Fcount_query%3Fminute%3D10080&query=%24.data.count&label=Usage(7days)) python From ab8ef01c762774633561440ac4d688d7252d4593 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Tue, 11 Feb 2025 12:27:27 +0800 Subject: [PATCH 19/33] docs: update trendshift badge link --- README_EN.md | 2 +- README_JP.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README_EN.md b/README_EN.md index 9b923954..691774ef 100644 --- a/README_EN.md +++ b/README_EN.md @@ -5,7 +5,7 @@
-RockChinQ%2FQChatGPT | Trendshift +RockChinQ%2FLangBot | Trendshift HomeFeatures | diff --git a/README_JP.md b/README_JP.md index 216f40f7..d2303fba 100644 --- a/README_JP.md +++ b/README_JP.md @@ -5,7 +5,7 @@
-RockChinQ%2FQChatGPT | Trendshift +RockChinQ%2FLangBot | Trendshift ホーム機能 | From fabf93f741d4b7fedf8f5b524617b9bd453f83bf Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Tue, 11 Feb 2025 12:56:13 +0800 Subject: [PATCH 20/33] chore: release v3.4.7 --- pkg/utils/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/utils/constants.py b/pkg/utils/constants.py index 61ed497b..7e582b0f 100644 --- a/pkg/utils/constants.py +++ b/pkg/utils/constants.py @@ -1,4 +1,4 @@ -semantic_version = "v3.4.6.2" +semantic_version = "v3.4.7" debug_mode = False From 4c344e063681e60d1bd51512c5277722cadbdf55 Mon Sep 17 00:00:00 2001 From: Civic_Crab <1308770968@qq.com> Date: Tue, 11 Feb 2025 18:48:21 +0800 Subject: [PATCH 21/33] =?UTF-8?q?=E9=98=BF=E9=87=8C=E4=BA=91=E7=99=BE?= =?UTF-8?q?=E7=82=BC=E5=B9=B3=E5=8F=B0=E5=BA=94=E7=94=A8API=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/provider/modelmgr/modelmgr.py | 2 +- .../modelmgr/requesters/dashscopecmpl.py | 167 ------------- pkg/provider/runnermgr.py | 1 + pkg/provider/runners/dashscopeapi.py | 236 ++++++++++++++++++ templates/provider.json | 33 +-- 5 files changed, 255 insertions(+), 184 deletions(-) delete mode 100644 pkg/provider/modelmgr/requesters/dashscopecmpl.py create mode 100644 pkg/provider/runners/dashscopeapi.py diff --git a/pkg/provider/modelmgr/modelmgr.py b/pkg/provider/modelmgr/modelmgr.py index 737d3476..33a65ff3 100644 --- a/pkg/provider/modelmgr/modelmgr.py +++ b/pkg/provider/modelmgr/modelmgr.py @@ -6,7 +6,7 @@ from . import entities, requester from ...core import app from . import token -from .requesters import chatcmpl, anthropicmsgs, moonshotchatcmpl, deepseekchatcmpl, ollamachat, giteeaichatcmpl, xaichatcmpl, zhipuaichatcmpl, lmstudiochatcmpl, siliconflowchatcmpl, dashscopecmpl, qwenchatcmpl +from .requesters import chatcmpl, anthropicmsgs, moonshotchatcmpl, deepseekchatcmpl, ollamachat, giteeaichatcmpl, xaichatcmpl, zhipuaichatcmpl, lmstudiochatcmpl, siliconflowchatcmpl FETCH_MODEL_LIST_URL = "https://api.qchatgpt.rockchin.top/api/v2/fetch/model_list" diff --git a/pkg/provider/modelmgr/requesters/dashscopecmpl.py b/pkg/provider/modelmgr/requesters/dashscopecmpl.py deleted file mode 100644 index d2f2b2d2..00000000 --- a/pkg/provider/modelmgr/requesters/dashscopecmpl.py +++ /dev/null @@ -1,167 +0,0 @@ -from __future__ import annotations - -import re -import asyncio -import typing -import dashscope - -from .. import entities, errors, requester -from ....core import entities as core_entities, app -from ... import entities as llm_entities -from ...tools import entities as tools_entities - -#阿里云百炼平台的自定义应用支持资料引用,此函数可以将引用标签替换为参考资料 -def replace_references(text, references_dict): - # 修正正则表达式,匹配 [index_id] 形式的字符串 - pattern = re.compile(r'\[(.*?)\]') - - def replacement(match): - ref_key = match.group(1) # 获取引用编号 - if ref_key in references_dict: - return f"(参考资料来自:{references_dict[ref_key]})" - else: - return match.group(0) # 如果没有对应的参考资料,保留原样 - - # 使用 re.sub() 进行替换 - return pattern.sub(replacement, text) - - -@requester.requester_class("dashscope-chat-applications") -class DashscopeChatApplication(requester.LLMAPIRequester): - """Dashscope ChatApplications API 请求器""" - - requester_cfg: dict - - def __init__(self, ap: app.Application): - self.requester_cfg = ap.provider_cfg.data['requester']['dashscope-chat-applications'] - self.ap = ap - - async def initialize(self): - dashscope.api_key = self.ap.provider_cfg.data['keys']['dashscope'][0] - - async def _req(self, args: dict): - - #print("args:", args) - - #局部变量 - chunk = None - pending_content = "" - output = { - "role": "assistant", - "content": "", - "tool_calls": [], - "tool_call_id": None # Dashscope暂时不支持工具调用 - } #由于Dashscope的content的键值是text,所以需要定义一个新格式的字典适配llm_entities.Message - - references_dict = {} # 用于存储引用编号和对应的参考资料 - - #调用API - response = dashscope.Application.call( - api_key=dashscope.api_key, - app_id=args["model"], - prompt=args["messages"], - stream=True, # 设置流式输出 - tools=args.get("tools", None), - incremental_output = True, - ) - - #处理API返回的流式输出 - for chunk in response: - #print(chunk) - if not chunk: - continue - - #获取流式传输的output - stream_output = chunk.get("output", {}) - if stream_output.get("text") is not None: - pending_content += stream_output.get("text") - - - #获取模型传出的参考资料列表 - references_dict_list = stream_output.get("doc_references", []) - - #从模型传出的参考资料信息中提取用于替换的字典 - if references_dict_list is not None: - for doc in references_dict_list: - if doc.get("index_id") is not None: - references_dict[doc.get("index_id")] = doc.get("doc_name") - - #将参考资料替换到文本中 - pending_content = replace_references(pending_content, references_dict) - - #将流式传输的内容整合到output中 - output["content"] = pending_content - - return output if chunk else None - - async def _make_msg( - self, - chat_completion: dict, - ) -> llm_entities.Message: - chatcmpl_message = chat_completion - - # 确保 role 字段存在且不为 None - if 'role' not in chatcmpl_message or chatcmpl_message['role'] is None: - chatcmpl_message['role'] = 'assistant' - - message = llm_entities.Message(**chatcmpl_message) - #print("message:", message) - return message - - async def _closure( - self, - query: core_entities.Query, - req_messages: list[dict], - use_model: entities.LLMModelInfo, - use_funcs: list[tools_entities.LLMFunction] = None, - ) -> llm_entities.Message: - - args = self.requester_cfg['args'].copy() - args["model"] = use_model.name if use_model.model_name is None else use_model.model_name - - # 设置此次请求中的messages - messages = req_messages.copy() - - # 检查vision - for msg in messages: - if 'content' in msg and isinstance(msg["content"], list): - for me in msg["content"]: - if me["type"] == "image_base64": - me["image_url"] = { - "url": me["image_base64"] - } - me["type"] = "image_url" - del me["image_base64"] - - args["messages"] = messages - - # 发送请求 - resp = await self._req(args) - - # 处理请求结果 - message = await self._make_msg(resp) - - return message - - async def call( - self, - query: core_entities.Query, - model: entities.LLMModelInfo, - messages: typing.List[llm_entities.Message], - funcs: typing.List[tools_entities.LLMFunction] = None, - ) -> llm_entities.Message: - req_messages = [] # req_messages 仅用于类内,外部同步由 query.messages 进行 - for m in messages: - msg_dict = m.dict(exclude_none=True) - content = msg_dict.get("content") - if isinstance(content, list): - # 检查 content 列表中是否每个部分都是文本 - if all(isinstance(part, dict) and part.get("type") == "text" for part in content): - # 将所有文本部分合并为一个字符串 - msg_dict["content"] = "\n".join(part["text"] for part in content) - req_messages.append(msg_dict) - - try: - return await self._closure(query=query, req_messages=req_messages, use_model=model, use_funcs=funcs) - except asyncio.TimeoutError: - raise errors.RequesterError('请求超时') diff --git a/pkg/provider/runnermgr.py b/pkg/provider/runnermgr.py index f45abfa4..4cd38c04 100644 --- a/pkg/provider/runnermgr.py +++ b/pkg/provider/runnermgr.py @@ -5,6 +5,7 @@ from ..core import app from .runners import localagent from .runners import difysvapi +from .runners import dashscopeapi class RunnerManager: diff --git a/pkg/provider/runners/dashscopeapi.py b/pkg/provider/runners/dashscopeapi.py new file mode 100644 index 00000000..8a6aef9d --- /dev/null +++ b/pkg/provider/runners/dashscopeapi.py @@ -0,0 +1,236 @@ +from __future__ import annotations + +import typing +import json +import base64 +import re + +import dashscope + +from .. import runner +from ...core import entities as core_entities +from .. import entities as llm_entities +from ...utils import image + +class DashscopeAPIError(Exception): + """Dashscope API 请求失败""" + + def __init__(self, message: str): + self.message = message + super().__init__(self.message) + + +@runner.runner_class("dashscope-service-api") +class DashScopeAPIRunner(runner.RequestRunner): + "阿里云百炼DashsscopeAPI对话请求器" + + # 运行器内部使用的配置 + app_type: str # 应用类型 + app_id: str # 应用ID + api_key: str # API Key + references_quote: str # 引用资料提示(当展示回答来源功能开启时,这个变量会作为引用资料名前的提示,可在provider.json中配置) + biz_params: dict = {} # 工作流应用参数(仅在工作流应用中生效) + + async def initialize(self): + """初始化""" + valid_app_types = ["agent", "workflow"] + self.app_type = self.ap.provider_cfg.data["dashscope-service-api"]["app-type"] + #检查配置文件中使用的应用类型是否支持 + if (self.app_type not in valid_app_types): + raise DashscopeAPIError( + f"不支持的 Dashscope 应用类型: {self.app_type}" + ) + + #初始化Dashscope 参数配置 + self.app_id = self.ap.provider_cfg.data["dashscope-service-api"][self.app_type]["app-id"] + self.api_key = self.ap.provider_cfg.data["dashscope-service-api"][self.app_type]["api-key"] + self.references_quote = self.ap.provider_cfg.data["dashscope-service-api"][self.app_type]["references_quote"] + self.biz_params = self.ap.provider_cfg.data["dashscope-service-api"]["workflow"]["biz_params"] + + def _replace_references(self, text, references_dict): + """阿里云百炼平台的自定义应用支持资料引用,此函数可以将引用标签替换为参考资料""" + + # 匹配 [index_id] 形式的字符串 + pattern = re.compile(r'\[(.*?)\]') + + def replacement(match): + # 获取引用编号 + ref_key = match.group(1) + if ref_key in references_dict: + # 如果有对应的参考资料按照provider.json中的reference_quote返回提示,来自哪个参考资料文件 + return f"({self.references_quote} {references_dict[ref_key]})" + else: + # 如果没有对应的参考资料,保留原样 + return match.group(0) + + # 使用 re.sub() 进行替换 + return pattern.sub(replacement, text) + + async def _preprocess_user_message( + self, query: core_entities.Query + ) -> tuple[str, list[str]]: + """预处理用户消息,提取纯文本,阿里云提供的上传文件方法过于复杂,暂不支持上传文件(包括图片)""" + plain_text = "" + image_ids = [] + if isinstance(query.user_message.content, list): + for ce in query.user_message.content: + if ce.type == "text": + plain_text += ce.text + # 暂时不支持上传图片,保留代码以便后续扩展 + # elif ce.type == "image_base64": + # image_b64, image_format = await image.extract_b64_and_format(ce.image_base64) + # file_bytes = base64.b64decode(image_b64) + # file = ("img.png", file_bytes, f"image/{image_format}") + # file_upload_resp = await self.dify_client.upload_file( + # file, + # f"{query.session.launcher_type.value}_{query.session.launcher_id}", + # ) + # image_id = file_upload_resp["id"] + # image_ids.append(image_id) + elif isinstance(query.user_message.content, str): + plain_text = query.user_message.content + + return plain_text, image_ids + + + async def _agent_messages( + self, query: core_entities.Query + ) -> typing.AsyncGenerator[llm_entities.Message, None]: + """Dashscope 智能体对话请求""" + + #局部变量 + chunk = None # 流式传输的块 + pending_content = "" # 待处理的Agent输出内容 + references_dict = {} # 用于存储引用编号和对应的参考资料 + plain_text = "" # 用户输入的纯文本信息 + image_ids = [] # 用户输入的图片ID列表 (暂不支持) + + plain_text, image_ids = await self._preprocess_user_message(query) + + #发送对话请求 + response = dashscope.Application.call( + api_key=self.api_key, # 智能体应用的API Key + app_id=self.app_id, # 智能体应用的ID + prompt=plain_text, # 用户输入的文本信息 + stream=True, # 流式输出 + incremental_output=True, # 增量输出,使用流式输出需要开启增量输出 + session_id=query.session.using_conversation.uuid, # 会话ID用于,多轮对话 + # rag_options={ # 主要用于文件交互,暂不支持 + # "session_file_ids": ["FILE_ID1"], # FILE_ID1 替换为实际的临时文件ID,逗号隔开多个 + # } + ) + + for chunk in response: + if chunk.get("status_code") != 200: + raise DashscopeAPIError( + f"Dashscope API 请求失败: status_code={chunk.get('status_code')} message={chunk.get('message')} request_id={chunk.get('request_id')} " + ) + if not chunk: + continue + + #获取流式传输的output + stream_output = chunk.get("output", {}) + if stream_output.get("text") is not None: + pending_content += stream_output.get("text") + + #保存当前会话的session_id用于下次对话的语境 + query.session.using_conversation.uuid = stream_output.get("session_id") + + #获取模型传出的参考资料列表 + references_dict_list = stream_output.get("doc_references", []) + + #从模型传出的参考资料信息中提取用于替换的字典 + if references_dict_list is not None: + for doc in references_dict_list: + if doc.get("index_id") is not None: + references_dict[doc.get("index_id")] = doc.get("doc_name") + + #将参考资料替换到文本中 + pending_content = self._replace_references(pending_content, references_dict) + + yield llm_entities.Message( + role="assistant", + content=pending_content, + ) + + + async def _workflow_messages( + self, query: core_entities.Query + ) -> typing.AsyncGenerator[llm_entities.Message, None]: + """Dashscope 工作流对话请求""" + + #局部变量 + chunk = None # 流式传输的块 + pending_content = "" # 待处理的Agent输出内容 + references_dict = {} # 用于存储引用编号和对应的参考资料 + plain_text = "" # 用户输入的纯文本信息 + image_ids = [] # 用户输入的图片ID列表 (暂不支持) + + plain_text, image_ids = await self._preprocess_user_message(query) + + #发送对话请求 + response = dashscope.Application.call( + api_key=self.api_key, # 智能体应用的API Key + app_id=self.app_id, # 智能体应用的ID + prompt=plain_text, # 用户输入的文本信息 + stream=True, # 流式输出 + incremental_output=True, # 增量输出,使用流式输出需要开启增量输出 + session_id=query.session.using_conversation.uuid, # 会话ID用于,多轮对话 + biz_params=self.biz_params # 工作流应用的自定义输入参数传递 + # rag_options={ # 主要用于文件交互,暂不支持 + # "session_file_ids": ["FILE_ID1"], # FILE_ID1 替换为实际的临时文件ID,逗号隔开多个 + # } + ) + + #处理API返回的流式输出 + for chunk in response: + if chunk.get("status_code") != 200: + raise DashscopeAPIError( + f"Dashscope API 请求失败: status_code={chunk.get('status_code')} message={chunk.get('message')} request_id={chunk.get('request_id')} " + ) + if not chunk: + continue + + #获取流式传输的output + stream_output = chunk.get("output", {}) + if stream_output.get("text") is not None: + pending_content += stream_output.get("text") + + #保存当前会话的session_id用于下次对话的语境 + query.session.using_conversation.uuid = stream_output.get("session_id") + + #获取模型传出的参考资料列表 + references_dict_list = stream_output.get("doc_references", []) + + #从模型传出的参考资料信息中提取用于替换的字典 + if references_dict_list is not None: + for doc in references_dict_list: + if doc.get("index_id") is not None: + references_dict[doc.get("index_id")] = doc.get("doc_name") + + #将参考资料替换到文本中 + pending_content = self._replace_references(pending_content, references_dict) + + yield llm_entities.Message( + role="assistant", + content=pending_content, + ) + + + + async def run( + self, query: core_entities.Query + ) -> typing.AsyncGenerator[llm_entities.Message, None]: + """运行""" + if self.ap.provider_cfg.data["dashscope-service-api"]["app-type"] == "agent": + async for msg in self._agent_messages(query): + yield msg + elif self.ap.provider_cfg.data["dashscope-service-api"]["app-type"] == "workflow": + async for msg in self._workflow_messages(query): + yield msg + else: + raise DashscopeAPIError( + f"不支持的 Dashscope 应用类型: {self.ap.provider_cfg.data['dashscope-service-api']['app-type']}" + ) + + diff --git a/templates/provider.json b/templates/provider.json index 9db3c020..13044f1e 100644 --- a/templates/provider.json +++ b/templates/provider.json @@ -25,12 +25,6 @@ ], "siliconflow": [ "xxxxxxx" - ], - "dashscope": [ - "sk-1234567890" - ], - "qwen": [ - "sk-1234567890", ] }, "requester": { @@ -46,16 +40,6 @@ }, "timeout": 120 }, - "dashscope-chat-applications": { - "args": {}, - "base-url": "https://dashscope.aliyuncs.com/api/v1", - "timeout": 120 - }, - "qwen-chat-completions": { - "base-url": "https://dashscope.aliyuncs.com/compatible-mode/v1", - "args": {}, - "timeout": 120 - }, "moonshot-chat-completions": { "base-url": "https://api.moonshot.cn/v1", "args": {}, @@ -119,5 +103,22 @@ "output-key": "summary", "timeout": 120 } + }, + "dashscope-service-api": { + "agent": { + "api-key": "sk-1234567890", + "app-id": "Your_app_id", + "references_quote": "参考资料来自:" + }, + "app-type": "agent", + "workflow": { + "api-key": "sk-1234567890", + "app-id": "Your_app_id", + "references_quote": "参考资料来自:", + "biz_params": { + "city": "北京", + "date": "2023-08-10" + } + } } } \ No newline at end of file From e51950aa751b2e3ee7aebff5245f360bd24456de Mon Sep 17 00:00:00 2001 From: Civic_Crab <1308770968@qq.com> Date: Tue, 11 Feb 2025 18:50:56 +0800 Subject: [PATCH 22/33] =?UTF-8?q?=E4=BF=AE=E6=94=B9llm-model.json=EF=BC=8C?= =?UTF-8?q?=E5=8E=BB=E9=99=A4=E8=88=8D=E5=BC=83=E7=9A=84qwen=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/metadata/llm-models.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/templates/metadata/llm-models.json b/templates/metadata/llm-models.json index 0dfb2a43..88757e66 100644 --- a/templates/metadata/llm-models.json +++ b/templates/metadata/llm-models.json @@ -216,11 +216,6 @@ "name": "your-dashscope-app-id", "requester": "dashscope-chat-applications", "token_mgr": "dashscope", - }, - { - "name": "qwen-plus", - "requester": "qwen-chat-completions", - "token_mgr": "qwen", } ] } \ No newline at end of file From 51cca31f04e43df93a930bb14a1e5a1d354d714e Mon Sep 17 00:00:00 2001 From: Civic_Crab <1308770968@qq.com> Date: Tue, 11 Feb 2025 18:52:27 +0800 Subject: [PATCH 23/33] =?UTF-8?q?=E5=8E=BB=E9=99=A4qwen=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modelmgr/requesters/qwenchatcmpl.py | 21 ------------------- 1 file changed, 21 deletions(-) delete mode 100644 pkg/provider/modelmgr/requesters/qwenchatcmpl.py diff --git a/pkg/provider/modelmgr/requesters/qwenchatcmpl.py b/pkg/provider/modelmgr/requesters/qwenchatcmpl.py deleted file mode 100644 index 8dfbae8e..00000000 --- a/pkg/provider/modelmgr/requesters/qwenchatcmpl.py +++ /dev/null @@ -1,21 +0,0 @@ -from __future__ import annotations - -import openai - -from . import chatcmpl -from .. import requester -from ....core import app - - -@requester.requester_class("siliconflow-chat-completions") -class QwenChatCompletions(chatcmpl.OpenAIChatCompletions): - """Qwen ChatCompletion API 请求器""" - - client: openai.AsyncClient - - requester_cfg: dict - - def __init__(self, ap: app.Application): - self.ap = ap - - self.requester_cfg = self.ap.provider_cfg.data['requester']['qwen-chat-completions'] From e89c6b68c957371bfb3f1d53cdb43385099b3931 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Tue, 11 Feb 2025 21:19:15 +0800 Subject: [PATCH 24/33] fix: f the stream resp --- pkg/provider/modelmgr/requesters/chatcmpl.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/provider/modelmgr/requesters/chatcmpl.py b/pkg/provider/modelmgr/requesters/chatcmpl.py index 0b1c4cf2..c2edcf9a 100644 --- a/pkg/provider/modelmgr/requesters/chatcmpl.py +++ b/pkg/provider/modelmgr/requesters/chatcmpl.py @@ -61,7 +61,7 @@ class OpenAIChatCompletions(requester.LLMAPIRequester): async for chunk in resp_gen: # print(chunk) - if not 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: @@ -75,6 +75,9 @@ class OpenAIChatCompletions(requester.LLMAPIRequester): break else: tool_calls.append(tool_call) + + if chunk.choices[0].finish_reason is not None: + break real_tool_calls = [] From 2c3fdb4fdc4e7055ee4180867aa7e40ac5d27363 Mon Sep 17 00:00:00 2001 From: lipu Date: Tue, 11 Feb 2025 21:37:07 +0800 Subject: [PATCH 25/33] feat(lark):enable lark callback --- pkg/core/migrations/m021_lark_config.py | 3 +- pkg/platform/sources/lark.py | 72 +++++++++++++++++++++---- templates/platform.json | 3 +- templates/schema/platform.json | 4 ++ 4 files changed, 69 insertions(+), 13 deletions(-) diff --git a/pkg/core/migrations/m021_lark_config.py b/pkg/core/migrations/m021_lark_config.py index 04c83caf..09adbc42 100644 --- a/pkg/core/migrations/m021_lark_config.py +++ b/pkg/core/migrations/m021_lark_config.py @@ -23,7 +23,8 @@ class LarkConfigMigration(migration.Migration): "enable": False, "app_id": "cli_abcdefgh", "app_secret": "XXXXXXXXXX", - "bot_name": "LangBot" + "bot_name": "LangBot", + "port": None }) await self.ap.platform_cfg.dump_config() diff --git a/pkg/platform/sources/lark.py b/pkg/platform/sources/lark.py index b4b346db..eecf9342 100644 --- a/pkg/platform/sources/lark.py +++ b/pkg/platform/sources/lark.py @@ -14,7 +14,10 @@ import datetime import aiohttp import lark_oapi.ws.exception +import quart +from flask import jsonify from lark_oapi.api.im.v1 import * +from lark_oapi.api.verification.v1 import GetVerificationRequest from .. import adapter from ...pipeline.longtext.strategies import forward @@ -288,12 +291,47 @@ class LarkMessageSourceAdapter(adapter.MessageSourceAdapter): ] = {} config: dict - + quart_app: quart.Quart ap: app.Application def __init__(self, config: dict, ap: app.Application): self.config = config self.ap = ap + self.quart_app = quart.Quart(__name__) + + @self.quart_app.route('/lark/callback', methods=['POST']) + async def lark_callback(): + data = await quart.request.json + + type = data.get("type") + if type is None : + context = EventContext(data) + type = context.header.event_type + if type is None : + return 'ok' + + if 'url_verification' in type: + # todo 验证verification token + return data + context = EventContext(data) + type = context.header.event_type + p2v1 = P2ImMessageReceiveV1() + p2v1.header = context.header + event = P2ImMessageReceiveV1Data() + event.message = EventMessage(context.event['message']) + event.sender = EventSender(context.event['sender']) + p2v1.event = event + p2v1.schema = context.schema + if 'im.message.receive_v1' in type: + try: + event = await self.event_converter.target2yiri(p2v1, self.api_client) + except Exception as e: + traceback.print_exc() + + if event.__class__ in self.listeners: + await self.listeners[event.__class__](event, self) + + return 'ok' async def on_message(event: lark_oapi.im.v1.P2ImMessageReceiveV1): @@ -392,16 +430,28 @@ class LarkMessageSourceAdapter(adapter.MessageSourceAdapter): self.listeners.pop(event_type) async def run_async(self): - try: - await self.bot._connect() - except lark_oapi.ws.exception.ClientException as e: - raise e - except Exception as e: - await self.bot._disconnect() - if self.bot._auto_reconnect: - await self.bot._reconnect() - else: - raise e + port =self.config['port'] + if port is None or port == "": + try: + await self.bot._connect() + except lark_oapi.ws.exception.ClientException as e: + raise e + except Exception as e: + await self.bot._disconnect() + if self.bot._auto_reconnect: + await self.bot._reconnect() + else: + raise e + else: + async def shutdown_trigger_placeholder(): + while True: + await asyncio.sleep(1) + + await self.quart_app.run_task( + host='0.0.0.0', + port=self.config['port'], + shutdown_trigger=shutdown_trigger_placeholder, + ) async def kill(self) -> bool: return False diff --git a/templates/platform.json b/templates/platform.json index 58742b37..ed2bf571 100644 --- a/templates/platform.json +++ b/templates/platform.json @@ -49,7 +49,8 @@ "enable": false, "app_id": "cli_abcdefgh", "app_secret": "XXXXXXXXXX", - "bot_name": "LangBot" + "bot_name": "LangBot", + "port":"" }, { "adapter": "discord", diff --git a/templates/schema/platform.json b/templates/schema/platform.json index 9a5deeb0..bc5d9962 100644 --- a/templates/schema/platform.json +++ b/templates/schema/platform.json @@ -252,6 +252,10 @@ "type": "string", "default": "", "description": "飞书的bot_name" + }, + "port": { + "type": "integer", + "description": "设置监听的端口,开启callback event" } } }, From e17da4e2ee2074e156080b5004cdec965724ba24 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Wed, 12 Feb 2025 11:11:07 +0800 Subject: [PATCH 26/33] chore: remove models of MaaS from `llm-models.json` --- templates/metadata/llm-models.json | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/templates/metadata/llm-models.json b/templates/metadata/llm-models.json index 110c18da..b5c29cf3 100644 --- a/templates/metadata/llm-models.json +++ b/templates/metadata/llm-models.json @@ -211,30 +211,6 @@ "requester": "zhipuai-chat-completions", "token_mgr": "zhipuai", "vision_supported": true - }, - { - "name": "siliconflow-r1", - "model_name": "deepseek-ai/DeepSeek-R1", - "requester": "siliconflow-chat-completions", - "token_mgr": "siliconflow" - }, - { - "name": "siliconflow-v3", - "model_name": "deepseek-ai/DeepSeek-V3", - "requester": "siliconflow-chat-completions", - "token_mgr": "siliconflow" - }, - { - "name": "aliyun-r1", - "model_name": "deepseek-r1", - "requester": "aliyun-chat-completions", - "token_mgr": "aliyun" - }, - { - "name": "aliyun-v3", - "model_name": "deepseek-v3", - "requester": "aliyun-chat-completions", - "token_mgr": "aliyun" } ] } \ No newline at end of file From 191f8866ae11a3896271229e47782a7298155eae Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Wed, 12 Feb 2025 11:25:28 +0800 Subject: [PATCH 27/33] chore(bailian): related configuration --- .../m028_aliyun_requester_config.py | 27 ++++++++++++++++++ pkg/core/stages/migrate.py | 2 +- pkg/provider/modelmgr/modelmgr.py | 2 +- .../{aliyunchatcmpl.py => bailianchatcmpl.py} | 8 +++--- templates/provider.json | 6 ++-- templates/schema/provider.json | 28 +++++++++++++++++++ 6 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 pkg/core/migrations/m028_aliyun_requester_config.py rename pkg/provider/modelmgr/requesters/{aliyunchatcmpl.py => bailianchatcmpl.py} (58%) diff --git a/pkg/core/migrations/m028_aliyun_requester_config.py b/pkg/core/migrations/m028_aliyun_requester_config.py new file mode 100644 index 00000000..f28bc04f --- /dev/null +++ b/pkg/core/migrations/m028_aliyun_requester_config.py @@ -0,0 +1,27 @@ +from __future__ import annotations + +from .. import migration + + +@migration.migration_class("bailian-requester-config", 28) +class BailianRequesterConfigMigration(migration.Migration): + """迁移""" + + async def need_migrate(self) -> bool: + """判断当前环境是否需要运行此迁移""" + + return 'bailian-chat-completions' not in self.ap.provider_cfg.data['requester'] + + async def run(self): + """执行迁移""" + self.ap.provider_cfg.data['keys']['bailian'] = [ + "sk-xxxxxxx" + ] + + self.ap.provider_cfg.data['requester']['bailian-chat-completions'] = { + "base-url": "https://dashscope.aliyuncs.com/compatible-mode/v1", + "args": {}, + "timeout": 120 + } + + await self.ap.provider_cfg.dump_config() diff --git a/pkg/core/stages/migrate.py b/pkg/core/stages/migrate.py index 16faa53a..e9b0b4d7 100644 --- a/pkg/core/stages/migrate.py +++ b/pkg/core/stages/migrate.py @@ -9,7 +9,7 @@ from ..migrations import m005_deepseek_cfg_completion, m006_vision_config, m007_ from ..migrations import m010_ollama_requester_config, m011_command_prefix_config, m012_runner_config, m013_http_api_config, m014_force_delay_config from ..migrations import m015_gitee_ai_config, m016_dify_service_api, m017_dify_api_timeout_params, m018_xai_config, m019_zhipuai_config 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 +from ..migrations import m026_qqofficial_config, m028_aliyun_requester_config @stage.stage_class("MigrationStage") class MigrationStage(stage.BootingStage): diff --git a/pkg/provider/modelmgr/modelmgr.py b/pkg/provider/modelmgr/modelmgr.py index 6748291c..489a322f 100644 --- a/pkg/provider/modelmgr/modelmgr.py +++ b/pkg/provider/modelmgr/modelmgr.py @@ -6,7 +6,7 @@ from . import entities, requester from ...core import app from . import token -from .requesters import chatcmpl, anthropicmsgs, moonshotchatcmpl, deepseekchatcmpl, ollamachat, giteeaichatcmpl, xaichatcmpl, zhipuaichatcmpl, lmstudiochatcmpl, siliconflowchatcmpl, aliyunchatcmpl +from .requesters import bailianchatcmpl, chatcmpl, anthropicmsgs, moonshotchatcmpl, deepseekchatcmpl, ollamachat, giteeaichatcmpl, xaichatcmpl, zhipuaichatcmpl, lmstudiochatcmpl, siliconflowchatcmpl FETCH_MODEL_LIST_URL = "https://api.qchatgpt.rockchin.top/api/v2/fetch/model_list" diff --git a/pkg/provider/modelmgr/requesters/aliyunchatcmpl.py b/pkg/provider/modelmgr/requesters/bailianchatcmpl.py similarity index 58% rename from pkg/provider/modelmgr/requesters/aliyunchatcmpl.py rename to pkg/provider/modelmgr/requesters/bailianchatcmpl.py index 4f4900d7..cce003bd 100644 --- a/pkg/provider/modelmgr/requesters/aliyunchatcmpl.py +++ b/pkg/provider/modelmgr/requesters/bailianchatcmpl.py @@ -7,9 +7,9 @@ from .. import requester from ....core import app -@requester.requester_class("aliyun-chat-completions") -class AliyunChatCompletions(chatcmpl.OpenAIChatCompletions): - """Aliyun ChatCompletion API 请求器""" +@requester.requester_class("bailian-chat-completions") +class BailianChatCompletions(chatcmpl.OpenAIChatCompletions): + """阿里云百炼大模型平台 ChatCompletion API 请求器""" client: openai.AsyncClient @@ -18,4 +18,4 @@ class AliyunChatCompletions(chatcmpl.OpenAIChatCompletions): def __init__(self, ap: app.Application): self.ap = ap - self.requester_cfg = self.ap.provider_cfg.data['requester']['aliyun-chat-completions'] + self.requester_cfg = self.ap.provider_cfg.data['requester']['bailian-chat-completions'] diff --git a/templates/provider.json b/templates/provider.json index c22ba42c..551c7d88 100644 --- a/templates/provider.json +++ b/templates/provider.json @@ -26,8 +26,8 @@ "siliconflow": [ "xxxxxxx" ], - "aliyun": [ - "sk-aliyun_token" + "bailian": [ + "sk-xxxxxxx" ] }, "requester": { @@ -83,7 +83,7 @@ "args": {}, "timeout": 120 }, - "aliyun-chat-completions": { + "bailian-chat-completions": { "args": {}, "base-url": "https://dashscope.aliyuncs.com/compatible-mode/v1", "timeout": 120 diff --git a/templates/schema/provider.json b/templates/schema/provider.json index bd2084b8..e6f806c5 100644 --- a/templates/schema/provider.json +++ b/templates/schema/provider.json @@ -82,6 +82,14 @@ "type": "string" }, "default": [] + }, + "bailian": { + "type": "array", + "title": "阿里云百炼大模型平台 API 密钥", + "items": { + "type": "string" + }, + "default": [] } } }, @@ -288,6 +296,26 @@ "default": 120 } } + }, + "bailian-chat-completions": { + "type": "object", + "title": "阿里云百炼大模型平台 API 请求配置", + "description": "仅可编辑 URL 和 超时时间,额外请求参数不支持可视化编辑,请到编辑器编辑", + "properties": { + "base-url": { + "type": "string", + "title": "API URL" + }, + "args": { + "type": "object", + "default": {} + }, + "timeout": { + "type": "number", + "title": "API 请求超时时间", + "default": 120 + } + } } } }, From a90f996b249f80c0eed231870021201b35c28caa Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Wed, 12 Feb 2025 13:33:07 +0800 Subject: [PATCH 28/33] chore: related configuration of dashscope runner --- .../m029_dashscope_app_api_config.py | 33 ++++++++++++ pkg/core/stages/migrate.py | 2 + pkg/provider/runnermgr.py | 2 + pkg/provider/runners/dashscopeapi.py | 18 +++---- templates/metadata/llm-models.json | 5 -- templates/provider.json | 13 +++-- templates/schema/provider.json | 53 +++++++++++++++++++ 7 files changed, 105 insertions(+), 21 deletions(-) create mode 100644 pkg/core/migrations/m029_dashscope_app_api_config.py diff --git a/pkg/core/migrations/m029_dashscope_app_api_config.py b/pkg/core/migrations/m029_dashscope_app_api_config.py new file mode 100644 index 00000000..3a069bac --- /dev/null +++ b/pkg/core/migrations/m029_dashscope_app_api_config.py @@ -0,0 +1,33 @@ +from __future__ import annotations + +from .. import migration + + +@migration.migration_class("dashscope-app-api-config", 29) +class DashscopeAppAPICfgMigration(migration.Migration): + """迁移""" + + async def need_migrate(self) -> bool: + """判断当前环境是否需要运行此迁移""" + return 'dashscope-app-api' not in self.ap.provider_cfg.data + + async def run(self): + """执行迁移""" + self.ap.provider_cfg.data['dashscope-app-api'] = { + "app-type": "agent", + "api-key": "sk-1234567890", + "agent": { + "app-id": "Your_app_id", + "references_quote": "参考资料来自:" + }, + "workflow": { + "app-id": "Your_app_id", + "references_quote": "参考资料来自:", + "biz_params": { + "city": "北京", + "date": "2023-08-10" + } + } + } + + await self.ap.provider_cfg.dump_config() diff --git a/pkg/core/stages/migrate.py b/pkg/core/stages/migrate.py index a1983f0b..7be46b4c 100644 --- a/pkg/core/stages/migrate.py +++ b/pkg/core/stages/migrate.py @@ -11,6 +11,8 @@ 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 +from ..migrations import m029_dashscope_app_api_config + @stage.stage_class("MigrationStage") class MigrationStage(stage.BootingStage): """迁移阶段 diff --git a/pkg/provider/runnermgr.py b/pkg/provider/runnermgr.py index 4cd38c04..52e1d8d2 100644 --- a/pkg/provider/runnermgr.py +++ b/pkg/provider/runnermgr.py @@ -23,6 +23,8 @@ class RunnerManager: self.using_runner = r(self.ap) await self.using_runner.initialize() break + else: + raise ValueError(f"未找到请求运行器: {self.ap.provider_cfg.data['runner']}") def get_runner(self) -> runner.RequestRunner: return self.using_runner diff --git a/pkg/provider/runners/dashscopeapi.py b/pkg/provider/runners/dashscopeapi.py index 8a6aef9d..0201f35d 100644 --- a/pkg/provider/runners/dashscopeapi.py +++ b/pkg/provider/runners/dashscopeapi.py @@ -20,7 +20,7 @@ class DashscopeAPIError(Exception): super().__init__(self.message) -@runner.runner_class("dashscope-service-api") +@runner.runner_class("dashscope-app-api") class DashScopeAPIRunner(runner.RequestRunner): "阿里云百炼DashsscopeAPI对话请求器" @@ -34,7 +34,7 @@ class DashScopeAPIRunner(runner.RequestRunner): async def initialize(self): """初始化""" valid_app_types = ["agent", "workflow"] - self.app_type = self.ap.provider_cfg.data["dashscope-service-api"]["app-type"] + self.app_type = self.ap.provider_cfg.data["dashscope-app-api"]["app-type"] #检查配置文件中使用的应用类型是否支持 if (self.app_type not in valid_app_types): raise DashscopeAPIError( @@ -42,10 +42,10 @@ class DashScopeAPIRunner(runner.RequestRunner): ) #初始化Dashscope 参数配置 - self.app_id = self.ap.provider_cfg.data["dashscope-service-api"][self.app_type]["app-id"] - self.api_key = self.ap.provider_cfg.data["dashscope-service-api"][self.app_type]["api-key"] - self.references_quote = self.ap.provider_cfg.data["dashscope-service-api"][self.app_type]["references_quote"] - self.biz_params = self.ap.provider_cfg.data["dashscope-service-api"]["workflow"]["biz_params"] + self.app_id = self.ap.provider_cfg.data["dashscope-app-api"][self.app_type]["app-id"] + self.api_key = self.ap.provider_cfg.data["dashscope-app-api"]["api-key"] + self.references_quote = self.ap.provider_cfg.data["dashscope-app-api"][self.app_type]["references_quote"] + self.biz_params = self.ap.provider_cfg.data["dashscope-app-api"]["workflow"]["biz_params"] def _replace_references(self, text, references_dict): """阿里云百炼平台的自定义应用支持资料引用,此函数可以将引用标签替换为参考资料""" @@ -222,15 +222,15 @@ class DashScopeAPIRunner(runner.RequestRunner): self, query: core_entities.Query ) -> typing.AsyncGenerator[llm_entities.Message, None]: """运行""" - if self.ap.provider_cfg.data["dashscope-service-api"]["app-type"] == "agent": + if self.ap.provider_cfg.data["dashscope-app-api"]["app-type"] == "agent": async for msg in self._agent_messages(query): yield msg - elif self.ap.provider_cfg.data["dashscope-service-api"]["app-type"] == "workflow": + elif self.ap.provider_cfg.data["dashscope-app-api"]["app-type"] == "workflow": async for msg in self._workflow_messages(query): yield msg else: raise DashscopeAPIError( - f"不支持的 Dashscope 应用类型: {self.ap.provider_cfg.data['dashscope-service-api']['app-type']}" + f"不支持的 Dashscope 应用类型: {self.ap.provider_cfg.data['dashscope-app-api']['app-type']}" ) diff --git a/templates/metadata/llm-models.json b/templates/metadata/llm-models.json index 88757e66..b5c29cf3 100644 --- a/templates/metadata/llm-models.json +++ b/templates/metadata/llm-models.json @@ -211,11 +211,6 @@ "requester": "zhipuai-chat-completions", "token_mgr": "zhipuai", "vision_supported": true - }, - { - "name": "your-dashscope-app-id", - "requester": "dashscope-chat-applications", - "token_mgr": "dashscope", } ] } \ No newline at end of file diff --git a/templates/provider.json b/templates/provider.json index 13044f1e..e1eb2e9c 100644 --- a/templates/provider.json +++ b/templates/provider.json @@ -104,21 +104,20 @@ "timeout": 120 } }, - "dashscope-service-api": { + "dashscope-app-api": { + "app-type": "agent", + "api-key": "sk-1234567890", "agent": { - "api-key": "sk-1234567890", "app-id": "Your_app_id", "references_quote": "参考资料来自:" }, - "app-type": "agent", "workflow": { - "api-key": "sk-1234567890", "app-id": "Your_app_id", "references_quote": "参考资料来自:", "biz_params": { - "city": "北京", - "date": "2023-08-10" - } + "city": "北京", + "date": "2023-08-10" + } } } } \ No newline at end of file diff --git a/templates/schema/provider.json b/templates/schema/provider.json index bd2084b8..b4a72253 100644 --- a/templates/schema/provider.json +++ b/templates/schema/provider.json @@ -397,6 +397,59 @@ } } } + }, + "dashscope-app-api": { + "type": "object", + "title": "阿里百炼平台自建应用 API 配置", + "properties": { + "app-type": { + "type": "string", + "title": "应用类型", + "description": "支持 workflow 和 agent,workflow:智能体编排;agent:普通智能体;请填写下方对应的应用类型 API 参数", + "enum": ["workflow", "agent"], + "default": "agent" + }, + "api-key": { + "type": "string", + "title": "API 密钥" + }, + "agent": { + "type": "object", + "title": "Agent API 参数", + "properties": { + "app-id": { + "type": "string", + "title": "应用 ID" + }, + "references_quote": { + "type": "string", + "title": "参考资料引用", + "description": "设置参考资料引用,用于从 Dashscope App API 结束节点返回的 JSON 数据中提取引用内容", + "default": "参考资料来自:" + } + } + }, + "workflow": { + "type": "object", + "title": "工作流 API 参数", + "properties": { + "app-id": { + "type": "string", + "title": "应用 ID" + }, + "references_quote": { + "type": "string", + "title": "参考资料引用", + "default": "参考资料来自:" + }, + "biz_params": { + "type": "object", + "title": "传入参数", + "default": {} + } + } + } + } } } } \ No newline at end of file From 35721c134051361c765f1ce4d3c767733097ad9f Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Wed, 12 Feb 2025 13:47:01 +0800 Subject: [PATCH 29/33] doc(README): update comment of aliyun bailian --- README.md | 2 +- README_EN.md | 2 +- README_JP.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4cf10013..8fa57c20 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ | [LMStudio](https://lmstudio.ai/) | ✅ | 本地大模型运行平台 | | [GiteeAI](https://ai.gitee.com/) | ✅ | 大模型接口聚合平台 | | [SiliconFlow](https://siliconflow.cn/) | ✅ | 大模型聚合平台 | -| [阿里云百炼](https://bailian.console.aliyun.com/) | ✅ | 大模型聚合平台 | +| [阿里云百炼](https://bailian.console.aliyun.com/) | ✅ | 大模型聚合平台, LLMOps 平台 | ## 😘 社区贡献 diff --git a/README_EN.md b/README_EN.md index 691774ef..6684a9ae 100644 --- a/README_EN.md +++ b/README_EN.md @@ -110,7 +110,7 @@ Directly use the released version to run, see the [Manual Deployment](https://do | [LMStudio](https://lmstudio.ai/) | ✅ | Local LLM running platform | | [GiteeAI](https://ai.gitee.com/) | ✅ | LLM interface gateway(MaaS) | | [SiliconFlow](https://siliconflow.cn/) | ✅ | LLM gateway(MaaS) | -| [Aliyun Bailian](https://bailian.console.aliyun.com/) | ✅ | LLM gateway(MaaS) | +| [Aliyun Bailian](https://bailian.console.aliyun.com/) | ✅ | LLM gateway(MaaS), LLMOps platform | ## 🤝 Community Contribution diff --git a/README_JP.md b/README_JP.md index d2303fba..f328349c 100644 --- a/README_JP.md +++ b/README_JP.md @@ -108,7 +108,7 @@ LangBotはBTPanelにリストされています。BTPanelをインストール | [LMStudio](https://lmstudio.ai/) | ✅ | ローカルLLM実行プラットフォーム | | [GiteeAI](https://ai.gitee.com/) | ✅ | LLMインターフェースゲートウェイ(MaaS) | | [SiliconFlow](https://siliconflow.cn/) | ✅ | LLMゲートウェイ(MaaS) | -| [Aliyun Bailian](https://bailian.console.aliyun.com/) | ✅ | LLMゲートウェイ(MaaS) | +| [Aliyun Bailian](https://bailian.console.aliyun.com/) | ✅ | LLMゲートウェイ(MaaS), LLMOpsプラットフォーム | ## 🤝 コミュニティ貢献 From 5311e787766b100584166113a23a69344beb5e09 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Wed, 12 Feb 2025 15:16:02 +0800 Subject: [PATCH 30/33] chore: release v3.4.7.1 --- pkg/utils/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/utils/constants.py b/pkg/utils/constants.py index 7e582b0f..0b9966da 100644 --- a/pkg/utils/constants.py +++ b/pkg/utils/constants.py @@ -1,4 +1,4 @@ -semantic_version = "v3.4.7" +semantic_version = "v3.4.7.1" debug_mode = False From 6502a64cab043fa84bd1f5acabde7f671850c2b5 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Wed, 12 Feb 2025 21:12:53 +0800 Subject: [PATCH 31/33] feat(lark): supports for encrypted message --- pkg/core/migrations/m021_lark_config.py | 4 +- pkg/core/migrations/m030_lark_config_cmpl.py | 31 +++++++ pkg/core/stages/migrate.py | 2 +- pkg/platform/sources/lark.py | 94 ++++++++++++++------ templates/platform.json | 4 +- templates/schema/platform.json | 13 ++- 6 files changed, 115 insertions(+), 33 deletions(-) create mode 100644 pkg/core/migrations/m030_lark_config_cmpl.py diff --git a/pkg/core/migrations/m021_lark_config.py b/pkg/core/migrations/m021_lark_config.py index 09adbc42..03d3c9a5 100644 --- a/pkg/core/migrations/m021_lark_config.py +++ b/pkg/core/migrations/m021_lark_config.py @@ -24,7 +24,9 @@ class LarkConfigMigration(migration.Migration): "app_id": "cli_abcdefgh", "app_secret": "XXXXXXXXXX", "bot_name": "LangBot", - "port": None + "enable-webhook": False, + "port": 2285, + "encrypt-key": "xxxxxxxxx" }) await self.ap.platform_cfg.dump_config() diff --git a/pkg/core/migrations/m030_lark_config_cmpl.py b/pkg/core/migrations/m030_lark_config_cmpl.py new file mode 100644 index 00000000..e016af7b --- /dev/null +++ b/pkg/core/migrations/m030_lark_config_cmpl.py @@ -0,0 +1,31 @@ +from __future__ import annotations + +from .. import migration + + +@migration.migration_class("lark-config-cmpl", 30) +class LarkConfigCmplMigration(migration.Migration): + """迁移""" + + async def need_migrate(self) -> bool: + """判断当前环境是否需要运行此迁移""" + + for adapter in self.ap.platform_cfg.data['platform-adapters']: + if adapter['adapter'] == 'lark': + if 'enable-webhook' not in adapter: + return True + + return False + + async def run(self): + """执行迁移""" + for adapter in self.ap.platform_cfg.data['platform-adapters']: + if adapter['adapter'] == 'lark': + if 'enable-webhook' not in adapter: + adapter['enable-webhook'] = False + if 'port' not in adapter: + adapter['port'] = 2285 + if 'encrypt-key' not in adapter: + adapter['encrypt-key'] = "xxxxxxxxx" + + await self.ap.platform_cfg.dump_config() diff --git a/pkg/core/stages/migrate.py b/pkg/core/stages/migrate.py index a1983f0b..e144e83a 100644 --- a/pkg/core/stages/migrate.py +++ b/pkg/core/stages/migrate.py @@ -10,7 +10,7 @@ from ..migrations import m010_ollama_requester_config, m011_command_prefix_confi from ..migrations import m015_gitee_ai_config, m016_dify_service_api, m017_dify_api_timeout_params, m018_xai_config, m019_zhipuai_config 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 - +from ..migrations import m030_lark_config_cmpl @stage.stage_class("MigrationStage") class MigrationStage(stage.BootingStage): """迁移阶段 diff --git a/pkg/platform/sources/lark.py b/pkg/platform/sources/lark.py index eecf9342..81ae76c5 100644 --- a/pkg/platform/sources/lark.py +++ b/pkg/platform/sources/lark.py @@ -11,6 +11,9 @@ import base64 import uuid import json import datetime +import hashlib +import base64 +from Crypto.Cipher import AES import aiohttp import lark_oapi.ws.exception @@ -28,6 +31,28 @@ from ..types import entities as platform_entities from ...utils import image +class AESCipher(object): + def __init__(self, key): + self.bs = AES.block_size + self.key=hashlib.sha256(AESCipher.str_to_bytes(key)).digest() + @staticmethod + def str_to_bytes(data): + u_type = type(b"".decode('utf8')) + if isinstance(data, u_type): + return data.encode('utf8') + return data + @staticmethod + def _unpad(s): + return s[:-ord(s[len(s) - 1:])] + def decrypt(self, enc): + iv = enc[:AES.block_size] + cipher = AES.new(self.key, AES.MODE_CBC, iv) + return self._unpad(cipher.decrypt(enc[AES.block_size:])) + def decrypt_string(self, enc): + enc = base64.b64decode(enc) + return self.decrypt(enc).decode('utf8') + + class LarkMessageConverter(adapter.MessageConverter): @staticmethod @@ -301,37 +326,47 @@ class LarkMessageSourceAdapter(adapter.MessageSourceAdapter): @self.quart_app.route('/lark/callback', methods=['POST']) async def lark_callback(): - data = await quart.request.json + try: + data = await quart.request.json - type = data.get("type") - if type is None : + if 'encrypt' in data: + cipher = AESCipher(self.config['encrypt-key']) + data = cipher.decrypt_string(data['encrypt']) + data = json.loads(data) + + type = data.get("type") + if type is None : + context = EventContext(data) + type = context.header.event_type + + if 'url_verification' == type: + print(data.get("challenge")) + # todo 验证verification token + return { + "challenge": data.get("challenge") + } context = EventContext(data) type = context.header.event_type - if type is None : - return 'ok' + p2v1 = P2ImMessageReceiveV1() + p2v1.header = context.header + event = P2ImMessageReceiveV1Data() + event.message = EventMessage(context.event['message']) + event.sender = EventSender(context.event['sender']) + p2v1.event = event + p2v1.schema = context.schema + if 'im.message.receive_v1' == type: + try: + event = await self.event_converter.target2yiri(p2v1, self.api_client) + except Exception as e: + traceback.print_exc() - if 'url_verification' in type: - # todo 验证verification token - return data - context = EventContext(data) - type = context.header.event_type - p2v1 = P2ImMessageReceiveV1() - p2v1.header = context.header - event = P2ImMessageReceiveV1Data() - event.message = EventMessage(context.event['message']) - event.sender = EventSender(context.event['sender']) - p2v1.event = event - p2v1.schema = context.schema - if 'im.message.receive_v1' in type: - try: - event = await self.event_converter.target2yiri(p2v1, self.api_client) - except Exception as e: - traceback.print_exc() + if event.__class__ in self.listeners: + await self.listeners[event.__class__](event, self) - if event.__class__ in self.listeners: - await self.listeners[event.__class__](event, self) - - return 'ok' + return {"code": 200, "message": "ok"} + except Exception as e: + traceback.print_exc() + return {"code": 500, "message": "error"} async def on_message(event: lark_oapi.im.v1.P2ImMessageReceiveV1): @@ -430,9 +465,10 @@ class LarkMessageSourceAdapter(adapter.MessageSourceAdapter): self.listeners.pop(event_type) async def run_async(self): - port =self.config['port'] + port = self.config['port'] + enable_webhook = self.config['enable-webhook'] - if port is None or port == "": + if not enable_webhook: try: await self.bot._connect() except lark_oapi.ws.exception.ClientException as e: @@ -450,7 +486,7 @@ class LarkMessageSourceAdapter(adapter.MessageSourceAdapter): await self.quart_app.run_task( host='0.0.0.0', - port=self.config['port'], + port=port, shutdown_trigger=shutdown_trigger_placeholder, ) async def kill(self) -> bool: diff --git a/templates/platform.json b/templates/platform.json index ed2bf571..703a2ad4 100644 --- a/templates/platform.json +++ b/templates/platform.json @@ -50,7 +50,9 @@ "app_id": "cli_abcdefgh", "app_secret": "XXXXXXXXXX", "bot_name": "LangBot", - "port":"" + "enable-webhook": false, + "port": 2285, + "encrypt-key": "xxxxxxxxx" }, { "adapter": "discord", diff --git a/templates/schema/platform.json b/templates/schema/platform.json index bc5d9962..f0224e61 100644 --- a/templates/schema/platform.json +++ b/templates/schema/platform.json @@ -253,9 +253,20 @@ "default": "", "description": "飞书的bot_name" }, + "enable-webhook": { + "type": "boolean", + "default": false, + "description": "是否启用webhook模式" + }, "port": { "type": "integer", - "description": "设置监听的端口,开启callback event" + "description": "设置监听的端口,开启callback event时需要设置", + "default": 2285 + }, + "encrypt-key": { + "type": "string", + "default": "", + "description": "设置加密密钥" } } }, From 04dd4fce68e28d81af242de088f4c0a819d5179b Mon Sep 17 00:00:00 2001 From: WangCham Date: Wed, 12 Feb 2025 22:04:16 +0800 Subject: [PATCH 32/33] Update wecom.py fix the bug that wecom couldnt send message when accept an image. --- pkg/platform/sources/wecom.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/platform/sources/wecom.py b/pkg/platform/sources/wecom.py index 5d127958..24f22bd4 100644 --- a/pkg/platform/sources/wecom.py +++ b/pkg/platform/sources/wecom.py @@ -130,7 +130,7 @@ class WecomEventConverter: ) elif event.type == "image": friend = platform_entities.Friend( - id=event.user_id, + id=f"u{event.user_id}", nickname=str(event.agent_id), remark="", ) From 75af631c17863553b5f25ebf9c08d4a5a7080dda Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Thu, 13 Feb 2025 00:49:19 +0800 Subject: [PATCH 33/33] chore: release v3.4.7.2 --- pkg/utils/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/utils/constants.py b/pkg/utils/constants.py index 0b9966da..abb7d50c 100644 --- a/pkg/utils/constants.py +++ b/pkg/utils/constants.py @@ -1,4 +1,4 @@ -semantic_version = "v3.4.7.1" +semantic_version = "v3.4.7.2" debug_mode = False