Skip to content

Commit 464f9e2

Browse files
authored
Merge pull request #912 from zhangairku/python-support-parameters-metadata
python支持parameters和CustomMetadata
2 parents 0f51404 + 4b8f760 commit 464f9e2

9 files changed

+421
-8
lines changed

python/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515

16-
__version__ = '1.1.0'
16+
__version__ = '1.1.2'
1717

1818
import os
1919
import sys

python/core/console/appbuilder_client/appbuilder_client.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""AppBuilderClient组件"""
1616
import os
1717
import json
18-
from typing import Optional, Union
18+
from typing import Optional, Union, Any
1919
from appbuilder.core.component import Message, Component
2020
from appbuilder.core.manifest.models import Manifest
2121
from appbuilder.core.console.appbuilder_client import data_class
@@ -318,6 +318,8 @@ def run(
318318
end_user_id: str = None,
319319
action: data_class.Action = None,
320320
mcp_authorization: list[dict] = None,
321+
parameters: dict[str, Any] = None,
322+
custom_metadata: data_class.CustomMetadata = None,
321323
**kwargs,
322324
) -> Message:
323325
r"""运行智能体应用
@@ -333,6 +335,12 @@ def run(
333335
end_user_id (str): 用户ID,用于区分不同用户
334336
action (data_class.Action): 对话时要进行的特殊操作。如回复工作流agent中“信息收集节点“的消息。
335337
mcp_authorization (list[dict]): mcp鉴权配置:目前仅适配百度网盘
338+
parameters: 用户在工作流Agent中自定义添加的参数,对应画布中开始节点用户新增的参数。例如:
339+
"parameters":
340+
{"custom_variable1": "abc",
341+
"custom_variable2": 1.23
342+
}
343+
custom_metadata: 自定义角色指令,适用于自主规划agent
336344
kwargs: 其他参数
337345
338346
Returns:
@@ -373,6 +381,8 @@ def run(
373381
end_user_id=end_user_id,
374382
action=action,
375383
mcp_authorization=mcp_authorization,
384+
parameters=parameters,
385+
custom_metadata=custom_metadata,
376386
)
377387

378388
headers = self.http_client.auth_header_v2(mcp_context=self._mcp_context)

python/core/console/appbuilder_client/async_appbuilder_client.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
import json
1515
import os
16-
from typing import Union
16+
from typing import Union, Any
1717
from aiohttp import FormData
1818
from appbuilder.core.component import Message, Component
1919
from appbuilder.core.console.appbuilder_client import data_class, AppBuilderClient
@@ -69,6 +69,8 @@ async def run(
6969
end_user_id: str = None,
7070
action: data_class.Action = None,
7171
mcp_authorization: list[dict] = None,
72+
parameters: dict[str, Any] = None,
73+
custom_metadata: data_class.CustomMetadata = None,
7274
**kwargs,
7375
) -> Message:
7476
r"""异步运行智能体应用
@@ -84,6 +86,12 @@ async def run(
8486
end_user_id (str): 用户ID,用于区分不同用户
8587
action (data_class.Action): 对话时要进行的特殊操作。如回复工作流agent中“信息收集节点“的消息。
8688
mcp_authorization (list[dict]): mcp鉴权配置:目前仅适配百度网盘
89+
parameters: 用户在工作流Agent中自定义添加的参数,对应画布中开始节点用户新增的参数。例如:
90+
"parameters":
91+
{"custom_variable1": "abc",
92+
"custom_variable2": 1.23
93+
}
94+
custom_metadata: 自定义角色指令,适用于自主规划agent
8795
kwargs: 其他参数
8896
8997
Returns:
@@ -124,6 +132,8 @@ async def run(
124132
end_user_id=end_user_id,
125133
action=action,
126134
mcp_authorization=mcp_authorization,
135+
parameters=parameters,
136+
conversation=custom_metadata,
127137
)
128138

129139
headers = self.http_client.auth_header_v2(mcp_context=self._mcp_context)

python/core/console/appbuilder_client/data_class.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from pydantic import BaseModel
1616
from pydantic import Field
17-
from typing import Union
17+
from typing import Union, Any
1818
from typing import Optional
1919
from appbuilder.core.manifest.models import Manifest
2020

@@ -40,7 +40,8 @@ def ToAppBuilderTool(tool):
4040
if "type" in tool and tool["type"]:
4141
return Tool(**tool), False
4242
if hasattr(tool, 'inputSchema') and hasattr(tool, 'inputSchema'):
43-
return Tool(type="function", function=Function(name=tool.name, description=tool.description, parameters=tool.inputSchema)), True
43+
return Tool(type="function",
44+
function=Function(name=tool.name, description=tool.description, parameters=tool.inputSchema)), True
4445
else:
4546
return tool, False
4647

@@ -93,6 +94,10 @@ class ActionParameters(BaseModel):
9394
..., description="要回复的'信息收集节点'中断事件")
9495

9596

97+
class CustomMetadata(BaseModel):
98+
override_role_instruction: str = Field(..., description="自定义角色指令,适用于自主规划agent")
99+
100+
96101
class Action(BaseModel):
97102
action_type: str = Field(...,
98103
description="action类型,目前可用值'resume', 用于回复信息收集节点的消息")
@@ -134,6 +139,8 @@ class AppBuilderClientRequest(BaseModel):
134139
end_user_id: Optional[str] = None
135140
action: Optional[Action] = None
136141
mcp_authorization: Optional[list[dict]] = None
142+
parameters: Optional[dict[str, Any]] = None
143+
custom_metadata: Optional[CustomMetadata] = None
137144

138145

139146
class Usage(BaseModel):
@@ -354,7 +361,8 @@ class AppBuilderClientAppListRequest(BaseModel):
354361
limit: int = Field(
355362
default=10, description="当次查询的数据大小,默认10,最大值100", le=100, ge=1)
356363
after: str = Field(
357-
default="", description="用于分页的游标。after 是一个应用的id,它定义了在列表中的位置。例如,如果你发出一个列表请求并收到 10个对象,以 app_id_123 结束,那么你后续的调用可以包含 after=app_id_123 以获取列表的下一页数据。")
364+
default="",
365+
description="用于分页的游标。after 是一个应用的id,它定义了在列表中的位置。例如,如果你发出一个列表请求并收到 10个对象,以 app_id_123 结束,那么你后续的调用可以包含 after=app_id_123 以获取列表的下一页数据。")
358366
before: str = Field(default="", description="用于分页的游标。与after相反,填写它将获取前一页数据")
359367

360368

@@ -380,7 +388,8 @@ class DescribeAppsRequest(BaseModel):
380388
maxKeys: int = Field(
381389
default=10, description="当次查询的数据大小,默认10,最大值100", le=100, ge=1)
382390
marker: str = Field(
383-
default=None, description="用于分页的游标。marker 是应用的id,它定义了在列表中的位置。例如,如果你发出一个列表请求并收到 10个对象,以 app_id_123 开始,那么可以使用 marker=app_id_123 来获取列表的下一页数据")
391+
default=None,
392+
description="用于分页的游标。marker 是应用的id,它定义了在列表中的位置。例如,如果你发出一个列表请求并收到 10个对象,以 app_id_123 开始,那么可以使用 marker=app_id_123 来获取列表的下一页数据")
384393

385394

386395
class DescribeAppsResponse(BaseModel):
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Copyright (c) 2025 Baidu, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import unittest
17+
18+
import appbuilder
19+
from appbuilder.core.console.appbuilder_client.data_class import CustomMetadata
20+
21+
22+
@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
23+
class TestAppBuilderClientFeedback(unittest.TestCase):
24+
def setUp(self):
25+
"""
26+
设置环境变量。
27+
28+
Args:
29+
无参数,默认值为空。
30+
31+
Returns:
32+
无返回值,方法中执行了环境变量的赋值操作。
33+
"""
34+
self.app_id = "a3654cd9-378a-4b46-a33b-2259ca3b304e"
35+
36+
def test_appbuilder_custom_metadata(self):
37+
# 如果app_id为空,则跳过单测执行, 避免单测因配置无效而失败
38+
"""
39+
如果app_id为空,则跳过单测执行, 避免单测因配置无效而失败
40+
41+
Args:
42+
self (unittest.TestCase): unittest的TestCase对象
43+
44+
Raises:
45+
None: 如果app_id不为空,则不会引发任何异常
46+
unittest.SkipTest (optional): 如果app_id为空,则跳过单测执行
47+
"""
48+
if len(self.app_id) == 0:
49+
self.skipTest("self.app_id is empty")
50+
appbuilder.logger.setLoglevel("ERROR")
51+
builder = appbuilder.AppBuilderClient(self.app_id)
52+
conversation_id = builder.create_conversation()
53+
msg = builder.run(conversation_id, "我要回老家相亲", stream=False, custom_metadata=CustomMetadata(
54+
override_role_instruction="# 角色任务\n" +
55+
"作为高情商大师,你的主要任务是根据提问,做出最佳的建议。\n" +
56+
"\n" +
57+
"# 工具能力\n" +
58+
"\n" +
59+
"无工具集提供\n" +
60+
"\n" +
61+
"# 要求与限制\n" +
62+
"\n" +
63+
"1. 输出内容的风格为幽默\n" +
64+
"2.输出的字数限制为100字以内",
65+
))
66+
67+
print(msg.content.answer)
68+
69+
def test_appbuilder_custom_metadata_stream(self):
70+
# 如果app_id为空,则跳过单测执行, 避免单测因配置无效而失败
71+
"""
72+
如果app_id为空,则跳过单测执行, 避免单测因配置无效而失败
73+
74+
Args:
75+
self (unittest.TestCase): unittest的TestCase对象
76+
77+
Raises:
78+
None: 如果app_id不为空,则不会引发任何异常
79+
unittest.SkipTest (optional): 如果app_id为空,则跳过单测执行
80+
"""
81+
if len(self.app_id) == 0:
82+
self.skipTest("self.app_id is empty")
83+
appbuilder.logger.setLoglevel("ERROR")
84+
builder = appbuilder.AppBuilderClient(self.app_id)
85+
conversation_id = builder.create_conversation()
86+
msg = builder.run(conversation_id, "我要回老家相亲", stream=True, custom_metadata=CustomMetadata(
87+
override_role_instruction="# 角色任务\n" +
88+
"作为高情商大师,你的主要任务是根据提问,做出最佳的建议。\n" +
89+
"\n" +
90+
"# 工具能力\n" +
91+
"\n" +
92+
"无工具集提供\n" +
93+
"\n" +
94+
"# 要求与限制\n" +
95+
"\n" +
96+
"1. 输出内容的风格为幽默\n" +
97+
"2.输出的字数限制为100字以内",
98+
))
99+
for content in msg.content:
100+
print(content.answer)
101+
102+
103+
if __name__ == "__main__":
104+
unittest.main()
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Copyright (c) 2025 Baidu, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
import os
17+
import appbuilder
18+
19+
20+
@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
21+
class TestAppBuilderClientFeedback(unittest.TestCase):
22+
def setUp(self):
23+
"""
24+
设置环境变量。
25+
26+
Args:
27+
无参数,默认值为空。
28+
29+
Returns:
30+
无返回值,方法中执行了环境变量的赋值操作。
31+
"""
32+
self.app_id = "2313e282-baa6-4db6-92dd-a21e99cfd59e"
33+
34+
def test_appbuilder_parameters(self):
35+
# 如果app_id为空,则跳过单测执行, 避免单测因配置无效而失败
36+
"""
37+
如果app_id为空,则跳过单测执行, 避免单测因配置无效而失败
38+
39+
Args:
40+
self (unittest.TestCase): unittest的TestCase对象
41+
42+
Raises:
43+
None: 如果app_id不为空,则不会引发任何异常
44+
unittest.SkipTest (optional): 如果app_id为空,则跳过单测执行
45+
"""
46+
if len(self.app_id) == 0:
47+
self.skipTest("self.app_id is empty")
48+
appbuilder.logger.setLoglevel("ERROR")
49+
builder = appbuilder.AppBuilderClient(self.app_id)
50+
conversation_id = builder.create_conversation()
51+
msg = builder.run(conversation_id, "国庆长假", stream=False, parameters={"city": "北京"})
52+
print(msg.content.answer)
53+
54+
def test_appbuilder_parameters_stream(self):
55+
# 如果app_id为空,则跳过单测执行, 避免单测因配置无效而失败
56+
"""
57+
如果app_id为空,则跳过单测执行, 避免单测因配置无效而失败
58+
59+
Args:
60+
self (unittest.TestCase): unittest的TestCase对象
61+
62+
Raises:
63+
None: 如果app_id不为空,则不会引发任何异常
64+
unittest.SkipTest (optional): 如果app_id为空,则跳过单测执行
65+
"""
66+
if len(self.app_id) == 0:
67+
self.skipTest("self.app_id is empty")
68+
appbuilder.logger.setLoglevel("ERROR")
69+
builder = appbuilder.AppBuilderClient(self.app_id)
70+
conversation_id = builder.create_conversation()
71+
msg = builder.run(conversation_id, "国庆长假", stream=True, parameters={"city": "北京"})
72+
73+
for content in msg.content:
74+
print(content.answer)
75+
76+
77+
if __name__ == "__main__":
78+
unittest.main()

0 commit comments

Comments
 (0)