Skip to content

Commit 41ed6ce

Browse files
authored
change openapi schema tool demo (#602)
1 parent f9a4e86 commit 41ed6ce

File tree

1 file changed

+29
-37
lines changed

1 file changed

+29
-37
lines changed

examples/tools/openapi_schema_tool.ipynb

+29-37
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
" \"schemas\":{\n",
174174
" \"wanx_v1_text2imageRequest\":{\n",
175175
" \"type\":\"object\",\n",
176-
" \"properties\":{\n",
176+
" \"properties\":{\n",
177177
" \"model\":{\n",
178178
" \"type\":\"string\",\n",
179179
" \"enum\":[\n",
@@ -283,24 +283,28 @@
283283
"metadata": {},
284284
"outputs": [],
285285
"source": [
286-
"from modelscope_agent.tools.openapi_plugin import openapi_schema_convert\n",
287-
"from modelscope.utils.config import Config\n",
286+
"import copy\n",
287+
"from modelscope_agent.tools.base import OpenapiServiceProxy\n",
288288
"from modelscope_agent.tools.base import TOOL_REGISTRY\n",
289-
"from modelscope_agent.tools.openapi_plugin import OpenAPIPluginTool\n",
290-
"\n",
291-
"\n",
292-
"config_dict = openapi_schema_convert(\n",
293-
" schema=schema_openAPI[\"schema\"],\n",
294-
" auth=schema_openAPI[\"auth\"])\n",
295-
"\n",
296-
"plugin_cfg = Config(config_dict)\n",
297289
"\n",
290+
"openapi_instance = OpenapiServiceProxy(openapi=schema_openAPI['schema'])\n",
291+
"schema_info = copy.deepcopy(openapi_instance.api_info_dict)\n",
292+
"for item in schema_info:\n",
293+
" schema_info[item].pop('is_active')\n",
294+
" schema_info[item].pop('is_remote_tool')\n",
295+
" schema_info[item].pop('details')\n",
296+
"print(schema_info)\n",
297+
"print(openapi_instance.api_info_dict)\n",
298298
"function_list = []\n",
299-
"\n",
300-
"for name, _ in plugin_cfg.items():\n",
301-
" openapi_plugin_object = OpenAPIPluginTool(name=name, cfg=plugin_cfg)\n",
302-
" TOOL_REGISTRY[name] = openapi_plugin_object\n",
303-
" function_list.append(name)"
299+
"tool_names = openapi_instance.tool_names\n",
300+
"for tool_name in tool_names:\n",
301+
" openapi_instance_for_specific_tool = copy.deepcopy(openapi_instance)\n",
302+
" openapi_instance_for_specific_tool.name = tool_name\n",
303+
" function_plain_text = openapi_instance_for_specific_tool.parser_function_by_tool_name(\n",
304+
" tool_name)\n",
305+
" openapi_instance_for_specific_tool.function_plain_text = function_plain_text\n",
306+
" TOOL_REGISTRY[tool_name] = openapi_instance_for_specific_tool\n",
307+
" function_list.append(tool_name)"
304308
]
305309
},
306310
{
@@ -320,9 +324,16 @@
320324
"\n",
321325
"role_template = '你扮演哆啦A梦小画家,你需要根据用户的要求用哆啦A梦的语气满足他们'\n",
322326
"llm_config = {\n",
323-
" 'model': 'qwen-max', \n",
327+
" 'model': 'qwen-max',\n",
324328
" 'model_server': 'dashscope',\n",
325329
" }\n",
330+
"kwargs = {}\n",
331+
"kwargs[\"credentials\"] = \\\n",
332+
"{\n",
333+
" \"auth_type\": \"api_key\",\n",
334+
" \"api_key_header\": \"Authorization\",\n",
335+
" \"api_key_value\": \"\", # 这里填入API key\n",
336+
"}\n",
326337
"\n",
327338
"bot = RolePlay(function_list=function_list,llm=llm_config, instruction=role_template)"
328339
]
@@ -340,31 +351,12 @@
340351
"metadata": {},
341352
"outputs": [],
342353
"source": [
343-
"response = bot.run(\"哆啦A梦!帮我画一幅可爱的小女孩的照片\", remote=False, print_info=True)\n",
354+
"response = bot.run(\"哆啦A梦!帮我画一幅可爱的小女孩的照片\", remote=False, print_info=True, **kwargs)\n",
344355
"text = ''\n",
345356
"for chunk in response:\n",
346357
" text += chunk\n",
347358
"print(text)"
348359
]
349-
},
350-
{
351-
"cell_type": "code",
352-
"execution_count": null,
353-
"metadata": {},
354-
"outputs": [],
355-
"source": [
356-
"import re\n",
357-
"from IPython.display import Image, display\n",
358-
"\n",
359-
"# 使用正则表达式提取音频文件路径\n",
360-
"match = re.search(r'!\\[.*?\\]\\((.*?)\\)', text)\n",
361-
"if match:\n",
362-
" audio_file_path = match.group(1)\n",
363-
" audio = Image(url=audio_file_path)\n",
364-
" display(audio)\n",
365-
"else:\n",
366-
" print(\"No audio file found in the observation string.\")"
367-
]
368360
}
369361
],
370362
"metadata": {

0 commit comments

Comments
 (0)