|
1 | 1 | import asyncio |
2 | 2 | import os |
3 | 3 |
|
4 | | -from pydantic import Field |
5 | | - |
6 | | -from oxygent import MAS, Config, OxyRequest, OxyResponse, oxy |
7 | | -from oxygent.prompts import INTENTION_PROMPT |
| 4 | +from oxygent import MAS, Config, oxy, preset_tools |
8 | 5 |
|
9 | 6 | Config.set_agent_llm_model("default_llm") |
10 | 7 |
|
11 | 8 |
|
12 | | -async def workflow(oxy_request: OxyRequest): |
13 | | - short_memory = oxy_request.get_short_memory() |
14 | | - print("--- History record --- :", short_memory) |
15 | | - master_short_memory = oxy_request.get_short_memory(master_level=True) |
16 | | - print("--- History record-User layer --- :", master_short_memory) |
17 | | - print("user query:", oxy_request.get_query(master_level=True)) |
18 | | - await oxy_request.send_message({"type": "msg_type", "content": "msg_content"}) |
19 | | - oxy_response = await oxy_request.call( |
20 | | - callee="time_agent", |
21 | | - arguments={"query": "What time is it now in Asia/Shanghai?"}, |
22 | | - ) |
23 | | - print("--- Current time --- :", oxy_response.output) |
24 | | - oxy_response = await oxy_request.call( |
25 | | - callee="default_llm", |
26 | | - arguments={ |
27 | | - "messages": [ |
28 | | - {"role": "system", "content": "You are a helpful assistant."}, |
29 | | - {"role": "user", "content": "Hello!"}, |
30 | | - ], |
31 | | - "llm_params": {"temperature": 0.6}, |
32 | | - }, |
33 | | - ) |
34 | | - print(oxy_response.output) |
35 | | - import re |
36 | | - |
37 | | - numbers = re.findall(r"\d+", oxy_request.get_query()) |
38 | | - if numbers: |
39 | | - n = numbers[-1] |
40 | | - oxy_response = await oxy_request.call(callee="calc_pi", arguments={"prec": n}) |
41 | | - return f"Save {n} positions: {oxy_response.output}" |
42 | | - else: |
43 | | - return "Save 2 positions: 3.14, or you could ask me to save how many positions you want." |
44 | | - |
45 | | - |
46 | | -fh = oxy.FunctionHub(name="joke_tools") |
47 | | - |
48 | | - |
49 | | -@fh.tool(description="a tool for telling jokes") |
50 | | -async def joke_tool(joke_type: str = Field(description="The type of the jokes")): |
51 | | - import random |
52 | | - |
53 | | - jokes = [ |
54 | | - "Teacher: Can you use the word 'because' in a sentence? \n Student: I didn't do my homework because… because I didn't do my homework.", |
55 | | - "Patient: Doctor, I feel like a pair of curtains.\nDoctor: Pull yourself together!", |
56 | | - "How many software engineers does it take to change a light bulb?\nNone. That's a hardware problem.", |
57 | | - ] |
58 | | - print("The type of the jokes", joke_type) |
59 | | - return random.choice(jokes) |
60 | | - |
61 | | - |
62 | | -def update_query(oxy_request: OxyRequest) -> OxyRequest: |
63 | | - print(oxy_request.shared_data) |
64 | | - user_query = oxy_request.get_query(master_level=True) |
65 | | - current_query = oxy_request.get_query() |
66 | | - print(user_query + "\n" + current_query) |
67 | | - oxy_request.arguments["who"] = oxy_request.callee |
68 | | - return oxy_request |
69 | | - |
70 | | - |
71 | | -def format_output(oxy_response: OxyResponse) -> OxyResponse: |
72 | | - oxy_response.output = "Answer: " + oxy_response.output |
73 | | - return oxy_response |
74 | | - |
75 | | - |
76 | 9 | oxy_space = [ |
77 | 10 | oxy.HttpLLM( |
78 | 11 | name="default_llm", |
79 | 12 | api_key=os.getenv("DEFAULT_LLM_API_KEY"), |
80 | 13 | base_url=os.getenv("DEFAULT_LLM_BASE_URL"), |
81 | 14 | model_name=os.getenv("DEFAULT_LLM_MODEL_NAME"), |
82 | | - llm_params={"temperature": 0.01}, |
83 | | - semaphore=4, |
84 | | - ), |
85 | | - oxy.ChatAgent(name="intent_agent", prompt=INTENTION_PROMPT), |
86 | | - fh, |
87 | | - oxy.StdioMCPClient( |
88 | | - name="time_tools", |
89 | | - params={ |
90 | | - "command": "uvx", |
91 | | - "args": ["mcp-server-time", "--local-timezone=Asia/Shanghai"], |
92 | | - }, |
93 | | - ), |
94 | | - oxy.StdioMCPClient( |
95 | | - name="file_tools", |
96 | | - params={ |
97 | | - "command": "npx", |
98 | | - "args": ["-y", "@modelcontextprotocol/server-filesystem", "./local_file"], |
99 | | - }, |
100 | | - ), |
101 | | - oxy.StdioMCPClient( |
102 | | - name="my_tools", |
103 | | - params={ |
104 | | - "command": "uv", |
105 | | - "args": ["--directory", "./mcp_servers", "run", "my_tools.py"], |
106 | | - }, |
107 | | - ), |
108 | | - oxy.ReActAgent( |
109 | | - name="master_agent", |
110 | | - sub_agents=["time_agent", "file_agent", "math_agent"], |
111 | | - additional_prompt="You may get several types of tasks, please choose correct tools to finish tasks.", |
112 | | - is_master=True, |
113 | | - func_format_output=format_output, |
114 | | - timeout=100, |
115 | | - llm_model="default_llm", |
116 | 15 | ), |
| 16 | + preset_tools.time_tools, |
117 | 17 | oxy.ReActAgent( |
118 | 18 | name="time_agent", |
119 | | - desc="A tool for time query.", |
120 | | - additional_prompt="Do not send other information except time.", |
| 19 | + desc="A tool that can query the time", |
121 | 20 | tools=["time_tools"], |
122 | | - func_process_input=update_query, |
123 | | - trust_mode=False, |
124 | | - timeout=10, |
125 | 21 | ), |
| 22 | + preset_tools.file_tools, |
126 | 23 | oxy.ReActAgent( |
127 | 24 | name="file_agent", |
128 | | - desc="A tool for file operation.", |
| 25 | + desc="A tool that can operate the file system", |
129 | 26 | tools=["file_tools"], |
130 | 27 | ), |
131 | | - oxy.WorkflowAgent( |
| 28 | + preset_tools.math_tools, |
| 29 | + oxy.ReActAgent( |
132 | 30 | name="math_agent", |
133 | | - desc="A tool for pi query", |
134 | | - sub_agents=["time_agent"], |
135 | | - tools=["my_tools"], |
136 | | - func_workflow=workflow, |
137 | | - is_retain_master_short_memory=True, |
| 31 | + desc="A tool that can perform mathematical calculations.", |
| 32 | + tools=["math_tools"], |
| 33 | + ), |
| 34 | + oxy.ReActAgent( |
| 35 | + is_master=True, |
| 36 | + name="master_agent", |
| 37 | + sub_agents=["time_agent", "file_agent", "math_agent"], |
138 | 38 | ), |
139 | | - # oxy.StreamableMCPClient(name="test_stream", server_url="http://127.0.0.1:9000/mcp"), |
140 | 39 | ] |
141 | 40 |
|
142 | 41 |
|
143 | 42 | async def main(): |
144 | | - """ |
145 | | - new instance: |
146 | | - Method 1: async with MAS(oxy_space=oxy_space) as mas: |
147 | | - Method 2: mas = await MAS.create(oxy_space=oxy_space) |
148 | | - call directly: |
149 | | - await mas.call(callee="joke_tool", arguments={"joke_type": "comic"}) |
150 | | - start: |
151 | | - await mas.start_cli_mode(first_query="Get what time it is and save in `log.txt` under `/local_file`") |
152 | | - await mas.start_web_service(first_query="Get what time it is and save in `log.txt` under `/local_file`") |
153 | | - await mas.start_batch_processing(["Hello", "What time is it now", "200 positions of Pi"]) |
154 | | - """ |
155 | 43 | async with MAS(oxy_space=oxy_space) as mas: |
156 | 44 | await mas.start_web_service( |
157 | | - first_query="Please calculate the 20 positions of Pi", |
158 | | - welcome_message="Hi, I’m OxyGent. How can I assist you?", |
| 45 | + first_query="What time is it now? Please save it into time.txt." |
159 | 46 | ) |
160 | 47 |
|
161 | 48 |
|
|
0 commit comments