Skip to content

Commit 0063302

Browse files
zzhangpurdue杨堃
and
杨堃
authored
Issues#450 (#451)
Co-authored-by: 杨堃 <[email protected]>
1 parent 6baff21 commit 0063302

File tree

2 files changed

+211
-1
lines changed

2 files changed

+211
-1
lines changed

modelscope_agent/agent.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
from modelscope_agent.llm import get_chat_model
55
from modelscope_agent.llm.base import BaseChatModel
6-
from modelscope_agent.tools.base import TOOL_REGISTRY, ToolServiceProxy
6+
from modelscope_agent.tools.base import (TOOL_REGISTRY, BaseTool,
7+
ToolServiceProxy)
78
from modelscope_agent.utils.logger import agent_logger as logger
89
from modelscope_agent.utils.utils import has_chinese_chars
910

@@ -122,6 +123,12 @@ def _register_tool(self,
122123

123124
tool_class_with_tenant = TOOL_REGISTRY[tool_name]
124125

126+
# adapt the TOOL_REGISTRY[tool_name] to origin tool class
127+
128+
if isinstance(tool_class_with_tenant, BaseTool):
129+
tool_class_with_tenant = {'class': TOOL_REGISTRY[tool_name]}
130+
TOOL_REGISTRY[tool_name] = tool_class_with_tenant
131+
125132
# check if the tenant_id of tool instance or tool service are exists
126133
# TODO: change from use_tool_api=True to False, to get the tenant_id of the tool changes to
127134
if tenant_id in tool_class_with_tenant and self.use_tool_api:

tests/tools/test_openapi_schema.py

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
import os
2+
3+
import pytest
4+
from modelscope_agent.agents import RolePlay
5+
from modelscope_agent.tools.base import TOOL_REGISTRY
6+
from modelscope_agent.tools.openapi_plugin import (OpenAPIPluginTool,
7+
openapi_schema_convert)
8+
9+
from modelscope.utils.config import Config
10+
11+
IS_FORKED_PR = os.getenv('IS_FORKED_PR', 'false') == 'true'
12+
schema_openAPI = {
13+
'schema': {
14+
'openapi': '3.1.0',
15+
'info': {
16+
'title': 'wanx-v1 Generation API',
17+
'description': 'API for generating image with wanx-v1',
18+
'version': 'v1.0.0'
19+
},
20+
'servers': [{
21+
'url': 'https://dashscope.aliyuncs.com'
22+
}],
23+
'paths': {
24+
'/api/v1/services/aigc/text2image/image-synthesis': {
25+
'post': {
26+
'summary': 'wanx-v1 text2image',
27+
'operationId': 'wanx_v1_text2image',
28+
'tags': ['wanx-v1 text2image'],
29+
'requestBody': {
30+
'required': True,
31+
'X-DashScope-Async': 'enable',
32+
'content': {
33+
'application/json': {
34+
'schema': {
35+
'$ref':
36+
'#/components/schemas/wanx_v1_text2imageRequest'
37+
}
38+
}
39+
}
40+
},
41+
'responses': {
42+
'200': {
43+
'description': 'Successful Response',
44+
'content': {
45+
'application/json': {
46+
'schema': {
47+
'$ref':
48+
'#/components/schemas/wanx_v1_text2imageResponse'
49+
}
50+
}
51+
}
52+
}
53+
},
54+
'security': [{
55+
'BearerAuth': []
56+
}]
57+
}
58+
},
59+
'/api/v1/tasks/{task_id}': {
60+
'get': {
61+
'summary':
62+
'Get Text2image Result',
63+
'operationId':
64+
'gettext2imageresult',
65+
'tags': ['Get Result'],
66+
'parameters': [{
67+
'name': 'task_id',
68+
'in': 'path',
69+
'required': True,
70+
'description':
71+
'The unique identifier of the Text2image generation task',
72+
'schema': {
73+
'type': 'string'
74+
}
75+
}],
76+
'security': [{
77+
'BearerAuth': []
78+
}]
79+
}
80+
}
81+
},
82+
'components': {
83+
'schemas': {
84+
'wanx_v1_text2imageRequest': {
85+
'type': 'object',
86+
'properties': {
87+
'model': {
88+
'type': 'string',
89+
'enum': ['wanx-v1']
90+
},
91+
'input': {
92+
'type': 'object',
93+
'properties': {
94+
'prompt': {
95+
'type': 'string',
96+
'example': '高清的,大师级的,4K,正面',
97+
'description': '描述画面的提示词信息',
98+
'required': True
99+
}
100+
}
101+
},
102+
'parameters': {
103+
'type': 'object',
104+
'properties': {
105+
'style': {
106+
'type':
107+
'string',
108+
'example':
109+
'<anime>',
110+
'description':
111+
'输出图像的风格',
112+
'required':
113+
True,
114+
'enum': [
115+
'<auto>', '<3d cartoon>', '<anime>',
116+
'<oil painting>', '<watercolor>',
117+
'<sketch>', '<chinese painting>',
118+
'<flat illustration>'
119+
]
120+
},
121+
'size': {
122+
'type': 'string',
123+
'example': '1024*1024',
124+
'description': '生成图像的分辨率,默认为1024*1024像素',
125+
'required': True,
126+
'enum':
127+
['1024*1024', '720*1280', '1280*720']
128+
},
129+
'n': {
130+
'type': 'integer',
131+
'example': 1,
132+
'description': '本次请求生成的图片数量',
133+
'required': True
134+
},
135+
'seed': {
136+
'type': 'integer',
137+
'example': 42,
138+
'description':
139+
'图片生成时候的种子值,取值范围为(0,4294967290)',
140+
'required': True
141+
}
142+
}
143+
}
144+
},
145+
'required': ['model', 'input', 'parameters']
146+
},
147+
'wanx_v1_text2imageResponse': {
148+
'type': 'object',
149+
'properties': {
150+
'output': {
151+
'type': 'string',
152+
'description': 'Generated image URL or data.'
153+
}
154+
}
155+
}
156+
},
157+
'securitySchemes': {
158+
'ApiKeyAuth': {
159+
'type': 'apiKey',
160+
'in': 'header',
161+
'name': 'Authorization'
162+
}
163+
}
164+
}
165+
},
166+
'auth': {
167+
'type': 'API Key',
168+
'apikey': 'test', # 这里填入API key
169+
'apikey_type': 'Bearer'
170+
},
171+
'privacy_policy': ''
172+
}
173+
174+
175+
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
176+
def test_openapi_schema_tool():
177+
schema_openAPI['auth']['apikey'] = os.getenv('DASHSCOPE_API_KEY')
178+
config_dict = openapi_schema_convert(
179+
schema=schema_openAPI['schema'], auth=schema_openAPI['auth'])
180+
plugin_cfg = Config(config_dict)
181+
182+
function_list = []
183+
184+
for name, _ in plugin_cfg.items():
185+
openapi_plugin_object = OpenAPIPluginTool(name=name, cfg=plugin_cfg)
186+
TOOL_REGISTRY[name] = openapi_plugin_object
187+
function_list.append(name)
188+
189+
role_template = '你扮演哆啦A梦小画家,你需要根据用户的要求用哆啦A梦的语气满足他们'
190+
llm_config = {
191+
'model': 'qwen-max',
192+
'model_server': 'dashscope',
193+
}
194+
195+
bot = RolePlay(
196+
function_list=function_list, llm=llm_config, instruction=role_template)
197+
198+
response = bot.run('哆啦A梦!帮我画一幅可爱的小女孩的照片', remote=False, print_info=True)
199+
text = ''
200+
for chunk in response:
201+
text += chunk
202+
print(text)
203+
assert isinstance(text, str)

0 commit comments

Comments
 (0)