chore: related configuration of dashscope runner

This commit is contained in:
Junyan Qin
2025-02-12 13:33:07 +08:00
parent 51cca31f04
commit a90f996b24
7 changed files with 105 additions and 21 deletions

View File

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

View File

@@ -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):
"""迁移阶段

View File

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

View File

@@ -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']}"
)

View File

@@ -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",
}
]
}

View File

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

View File

@@ -397,6 +397,59 @@
}
}
}
},
"dashscope-app-api": {
"type": "object",
"title": "阿里百炼平台自建应用 API 配置",
"properties": {
"app-type": {
"type": "string",
"title": "应用类型",
"description": "支持 workflow 和 agentworkflow智能体编排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": {}
}
}
}
}
}
}
}