Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- examples/callback/__init__.py +0 -0
- examples/callback/mcp.json +85 -0
- examples/callback/run-test.py +122 -0
- examples/callback/simple_callback.py +59 -0
- examples/callback/test_env.py +12 -0
examples/callback/__init__.py
ADDED
File without changes
|
examples/callback/mcp.json
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"mcpServers": {
|
3 |
+
"amap-amap-sse": {
|
4 |
+
"type": "sse",
|
5 |
+
"url": "https://mcp.amap.com/sse?key=${AMAP_AMAP_SSE_KEY}",
|
6 |
+
"timeout": 5.0,
|
7 |
+
"sse_read_timeout": 300.0
|
8 |
+
},
|
9 |
+
"tavily-mcp": {
|
10 |
+
"type": "stdio",
|
11 |
+
"command": "npx",
|
12 |
+
"args": ["-y", "[email protected]"],
|
13 |
+
"env": {
|
14 |
+
"TAVILY_API_KEY": "tvly-dev-"
|
15 |
+
}
|
16 |
+
},
|
17 |
+
"aworldsearch_server": {
|
18 |
+
"command": "python",
|
19 |
+
"args": [
|
20 |
+
"-m",
|
21 |
+
"mcp_servers.aworldsearch_server"
|
22 |
+
],
|
23 |
+
"env": {
|
24 |
+
"AWORLD_SEARCH_URL": "${AWORLD_SEARCH_URL}",
|
25 |
+
"AWORLD_SEARCH_TOTAL_NUM": "${AWORLD_SEARCH_TOTAL_NUM}",
|
26 |
+
"AWORLD_SEARCH_SLICE_NUM": "${AWORLD_SEARCH_SLICE_NUM}",
|
27 |
+
"AWORLD_SEARCH_DOMAIN": "${AWORLD_SEARCH_DOMAIN}",
|
28 |
+
"AWORLD_SEARCH_SEARCHMODE": "${AWORLD_SEARCH_SEARCHMODE}",
|
29 |
+
"AWORLD_SEARCH_SOURCE": "${AWORLD_SEARCH_SOURCE}",
|
30 |
+
"AWORLD_SEARCH_UID": "${AWORLD_SEARCH_UID}"
|
31 |
+
}
|
32 |
+
},
|
33 |
+
"picsearch_server": {
|
34 |
+
"command": "python",
|
35 |
+
"args": [
|
36 |
+
"-m",
|
37 |
+
"mcp_servers.picsearch_server"
|
38 |
+
],
|
39 |
+
"env": {
|
40 |
+
"PIC_SEARCH_URL": "${PIC_SEARCH_URL}",
|
41 |
+
"PIC_SEARCH_TOTAL_NUM": "${PIC_SEARCH_TOTAL_NUM}",
|
42 |
+
"PIC_SEARCH_SLICE_NUM": "${PIC_SEARCH_SLICE_NUM}",
|
43 |
+
"PIC_SEARCH_DOMAIN": "${PIC_SEARCH_DOMAIN}",
|
44 |
+
"PIC_SEARCH_SEARCHMODE": "${PIC_SEARCH_SEARCHMODE}",
|
45 |
+
"PIC_SEARCH_SOURCE": "${PIC_SEARCH_SOURCE}"
|
46 |
+
}
|
47 |
+
},
|
48 |
+
"gen_audio_server": {
|
49 |
+
"command": "python",
|
50 |
+
"args": [
|
51 |
+
"-m",
|
52 |
+
"mcp_servers.gen_audio_server"
|
53 |
+
],
|
54 |
+
"env": {
|
55 |
+
"AUDIO_TASK_URL": "${AUDIO_TASK_URL}",
|
56 |
+
"AUDIO_QUERY_URL": "${AUDIO_QUERY_URL}",
|
57 |
+
"AUDIO_APP_KEY": "${AUDIO_APP_KEY}",
|
58 |
+
"AUDIO_SECRET": "${AUDIO_SECRET}",
|
59 |
+
"AUDIO_SAMPLE_RATE": "${AUDIO_SAMPLE_RATE}",
|
60 |
+
"AUDIO_AUDIO_FORMAT": "${AUDIO_AUDIO_FORMAT}",
|
61 |
+
"AUDIO_TTS_VOICE": "${AUDIO_TTS_VOICE}",
|
62 |
+
"AUDIO_TTS_SPEECH_RATE": "${AUDIO_TTS_SPEECH_RATE}",
|
63 |
+
"AUDIO_TTS_VOLUME": "${AUDIO_TTS_VOLUME}",
|
64 |
+
"AUDIO_TTS_PITCH": "${AUDIO_TTS_PITCH}",
|
65 |
+
"AUDIO_VOICE_TYPE": "${AUDIO_VOICE_TYPE}"
|
66 |
+
}
|
67 |
+
},
|
68 |
+
"gen_video_server": {
|
69 |
+
"command": "python",
|
70 |
+
"args": [
|
71 |
+
"-m",
|
72 |
+
"mcp_servers.gen_video_server"
|
73 |
+
],
|
74 |
+
"env": {
|
75 |
+
"DASHSCOPE_API_KEY": "${DASHSCOPE_API_KEY}",
|
76 |
+
"DASHSCOPE_VIDEO_SUBMIT_URL": "${DASHSCOPE_VIDEO_SUBMIT_URL}",
|
77 |
+
"DASHSCOPE_QUERY_BASE_URL": "${DASHSCOPE_QUERY_BASE_URL}",
|
78 |
+
"DASHSCOPE_VIDEO_MODEL": "${DASHSCOPE_VIDEO_MODEL}",
|
79 |
+
"DASHSCOPE_VIDEO_SIZE": "${DASHSCOPE_VIDEO_SIZE}",
|
80 |
+
"DASHSCOPE_VIDEO_SLEEP_TIME": "${DASHSCOPE_VIDEO_SLEEP_TIME}",
|
81 |
+
"DASHSCOPE_VIDEO_RETRY_TIMES": "${DASHSCOPE_VIDEO_RETRY_TIMES}"
|
82 |
+
}
|
83 |
+
}
|
84 |
+
}
|
85 |
+
}
|
examples/callback/run-test.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding: utf-8
|
2 |
+
# Copyright (c) 2025 inclusionAI.
|
3 |
+
import asyncio
|
4 |
+
import json
|
5 |
+
import logging
|
6 |
+
import os
|
7 |
+
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
from aworld.agents.llm_agent import Agent
|
11 |
+
from aworld.config.conf import AgentConfig, TaskConfig
|
12 |
+
#from aworld.core.agent.llm_agent import Agent
|
13 |
+
from aworld.core.task import Task
|
14 |
+
|
15 |
+
from aworld.runner import Runners
|
16 |
+
from aworld.runners.callback.decorator import reg_callback
|
17 |
+
from aworld.tools.mcp_tool import async_mcp_tool
|
18 |
+
|
19 |
+
#import aworld.tools.examples.aworldsearch_function_tools
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
@reg_callback("print_content")
|
25 |
+
def simple_callback(content):
|
26 |
+
"""Simple callback function, prints content and returns it
|
27 |
+
|
28 |
+
Args:
|
29 |
+
content: Content to print
|
30 |
+
|
31 |
+
Returns:
|
32 |
+
The input content
|
33 |
+
"""
|
34 |
+
print(f"callback content: {content}")
|
35 |
+
return content
|
36 |
+
|
37 |
+
async def run():
|
38 |
+
load_dotenv()
|
39 |
+
llm_provider = os.getenv("LLM_PROVIDER_WEATHER", "openai")
|
40 |
+
llm_model_name = os.getenv("LLM_MODEL_NAME_WEATHER")
|
41 |
+
llm_api_key = os.getenv("LLM_API_KEY_WEATHER")
|
42 |
+
llm_base_url = os.getenv("LLM_BASE_URL_WEATHER")
|
43 |
+
llm_temperature = os.getenv("LLM_TEMPERATURE_WEATHER", 0.0)
|
44 |
+
|
45 |
+
agent_config = AgentConfig(
|
46 |
+
llm_provider=llm_provider,
|
47 |
+
llm_model_name=llm_model_name,
|
48 |
+
llm_api_key=llm_api_key,
|
49 |
+
llm_base_url=llm_base_url,
|
50 |
+
llm_temperature=llm_temperature,
|
51 |
+
)
|
52 |
+
#mcp_servers = ["filewrite_server", "fileread_server"]
|
53 |
+
#mcp_servers = ["amap-amap-sse","filewrite_server", "fileread_server"]
|
54 |
+
#mcp_servers = ["file_server"]
|
55 |
+
#mcp_servers = ["amap-amap-sse"]
|
56 |
+
mcp_servers = ["aworldsearch_server"]
|
57 |
+
#mcp_servers = ["gen_video_server"]
|
58 |
+
# mcp_servers = ["picsearch_server"]
|
59 |
+
#mcp_servers = ["gen_audio_server"]
|
60 |
+
#mcp_servers = ["playwright"]
|
61 |
+
#mcp_servers = ["tavily-mcp"]
|
62 |
+
|
63 |
+
path_cwd = os.path.dirname(os.path.abspath(__file__))
|
64 |
+
mcp_path = os.path.join(path_cwd, "mcp.json")
|
65 |
+
with open(mcp_path, "r") as f:
|
66 |
+
mcp_config = json.load(f)
|
67 |
+
|
68 |
+
print("-------------------mcp_config--------------",mcp_config)
|
69 |
+
|
70 |
+
#sand_box = Sandbox(mcp_servers=mcp_servers,mcp_config=mcp_config)
|
71 |
+
# You can specify sandbox
|
72 |
+
#sand_box = Sandbox(mcp_servers=mcp_servers, mcp_config=mcp_config,env_type=SandboxEnvType.K8S)
|
73 |
+
#sand_box = Sandbox(mcp_servers=mcp_servers, mcp_config=mcp_config,env_type=SandboxEnvType.SUPERCOMPUTER)
|
74 |
+
|
75 |
+
search_sys_prompt = "You are a versatile assistant"
|
76 |
+
search = Agent(
|
77 |
+
conf=agent_config,
|
78 |
+
name="search_agent",
|
79 |
+
system_prompt=search_sys_prompt,
|
80 |
+
mcp_config=mcp_config,
|
81 |
+
mcp_servers=mcp_servers,
|
82 |
+
#sandbox=sand_box,
|
83 |
+
)
|
84 |
+
|
85 |
+
# Run agent
|
86 |
+
# Runners.sync_run(input="Use tavily-mcp to check what tourist attractions are in Hangzhou", agent=search)
|
87 |
+
task = Task(
|
88 |
+
# input="Use tavily-mcp to check what tourist attractions are in Hangzhou",
|
89 |
+
# input="Use the file_server tool to analyze this audio link: https://amap-aibox-data.oss-cn-zhangjiakou.aliyuncs.com/.mp3",
|
90 |
+
# input="Use the amap-amap-sse tool to find hotels within one kilometer of West Lake in Hangzhou",
|
91 |
+
input="Use the aworldsearch_server tool to search for the origin of the Dragon Boat Festival",
|
92 |
+
# input="Use the picsearch_server tool to search for Captain America",
|
93 |
+
# input="Make sure to use the human_confirm tool to let the user confirm this message: 'Do you want to make a payment to this customer'",
|
94 |
+
# input="Use the gen_audio_server tool to convert this sentence to audio: 'Nice to meet you'",
|
95 |
+
#input="Use the gen_video_server tool to generate a video of this description: 'A cat walking alone on a snowy day'",
|
96 |
+
#input="现在纽约、上海、北京的天气怎么样?这里是三个城市,希望大模型识别调用工具的时候返回三个工具",
|
97 |
+
# input="First call the filewrite_server tool, then call the fileread_server tool",
|
98 |
+
# input="Use the playwright tool, with Google browser, search for the latest news about the Trump administration on www.baidu.com",
|
99 |
+
# input="Use tavily-mcp",
|
100 |
+
agent=search,
|
101 |
+
conf=TaskConfig(),
|
102 |
+
event_driven=True
|
103 |
+
)
|
104 |
+
|
105 |
+
#result = Runners.sync_run_task(task)
|
106 |
+
#result = Runners.sync_run_task(task)
|
107 |
+
#result = await Runners.streamed_run_task(task)
|
108 |
+
# result = await Runners.run_task(task)
|
109 |
+
# print(
|
110 |
+
# "----------------------------------------------------------------------------------------------"
|
111 |
+
# )
|
112 |
+
# print(result)
|
113 |
+
# async for chunk in Runners.streamed_run_task(task).stream_events():
|
114 |
+
# print(chunk, end="", flush=True)
|
115 |
+
|
116 |
+
async for output in Runners.streamed_run_task(task).stream_events():
|
117 |
+
print(f"Agent Ouput: {output}")
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
if __name__ == "__main__":
|
122 |
+
asyncio.run(run())
|
examples/callback/simple_callback.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding: utf-8
|
2 |
+
# Copyright (c) 2025 inclusionAI.
|
3 |
+
|
4 |
+
"""
|
5 |
+
Simple tool callback example, demonstrating the basic functionality of callback registration and execution.
|
6 |
+
"""
|
7 |
+
|
8 |
+
# Import business package, its __init__.py will automatically import and register callback functions
|
9 |
+
import business
|
10 |
+
from aworld.runners.callback.decorator import reg_callback, CallbackRegistry
|
11 |
+
|
12 |
+
|
13 |
+
# Import CallbackRegistry
|
14 |
+
|
15 |
+
|
16 |
+
@reg_callback("mcp_server__action")
|
17 |
+
def simple_callback(content):
|
18 |
+
"""Simple callback function, prints content and returns it
|
19 |
+
|
20 |
+
Args:
|
21 |
+
content: Content to print
|
22 |
+
|
23 |
+
Returns:
|
24 |
+
The input content
|
25 |
+
"""
|
26 |
+
print(f"Callback function received content: {content}")
|
27 |
+
return content
|
28 |
+
|
29 |
+
def main():
|
30 |
+
"""Main function, demonstrating how to get and execute callback functions"""
|
31 |
+
# List all registered callback functions
|
32 |
+
# print("\n===== Registered Callback Functions =====")
|
33 |
+
# business.list_all_callbacks()
|
34 |
+
|
35 |
+
# Get and execute print_content callback function
|
36 |
+
print("\n===== Execute print_content Callback Function =====")
|
37 |
+
callback_func = CallbackRegistry.get("mcp_server__action")
|
38 |
+
|
39 |
+
if callback_func:
|
40 |
+
print("Callback function found, executing...")
|
41 |
+
result = callback_func("Hello, Callback!!!!!")
|
42 |
+
print(f"Callback function execution result: {result}")
|
43 |
+
else:
|
44 |
+
print("print_content callback function not found")
|
45 |
+
|
46 |
+
# # Get and execute uppercase_content callback function
|
47 |
+
# print("\n===== Execute uppercase_content Callback Function =====")
|
48 |
+
# uppercase_func = CallbackRegistry.get("uppercase_content")
|
49 |
+
#
|
50 |
+
# if uppercase_func:
|
51 |
+
# print("Callback function found, executing...")
|
52 |
+
# result = uppercase_func("Hello, Uppercase Callback!")
|
53 |
+
# print(f"Callback function execution result: {result}")
|
54 |
+
# else:
|
55 |
+
# print("uppercase_content callback function not found")
|
56 |
+
|
57 |
+
|
58 |
+
if __name__ == "__main__":
|
59 |
+
main()
|
examples/callback/test_env.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
|
4 |
+
def main():
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
load_dotenv()
|
7 |
+
print(os.environ)
|
8 |
+
uid = os.getenv('AWORLD_SEARCH_UID')
|
9 |
+
print(uid)
|
10 |
+
|
11 |
+
if __name__ == "__main__":
|
12 |
+
main()
|