1- # Building and Running Agents
2-
3- In AWorld's design, both Workflows and Multi-Agent Systems (MAS) are complex systems built around Agents as the core
4- component. Using the most common llm_agent as an example, this tutorial provides detailed guidance on:
1+ <h1 id =" BKFxm " >Building and Running Agents</h1 >
2+ Using the most common llm_agent as an example, this tutorial provides detailed guidance on:
53
641 . How to quickly build an Agent
7- 2 . How to customize an Agent
8- This document is divided into two parts to explain AWorld's design philosophy.
9-
10- ## Part 1: Quick Agent Setup
11-
12- ### Declaring an Agent
5+ 2 . How to customize an Agent
6+ This document is divided into two parts to explain AWorld's design philosophy.
137
8+ <h2 id =" iFKMm " >Quick Agent Setup</h2 >
9+ <h3 id =" A1BVO " >Declaring an Agent</h3 >
1410``` python
1511from aworld.agents.llm_agent import Agent
1612
1713# Assign a name to your agent
1814agent = Agent(name = " my_agent" )
1915```
2016
21- ### Configuring LLM
22-
23- #### Method 1: Using Environment Variables
24-
17+ <h3 id =" SnC96 " >Configuring LLM</h3 >
18+ <h4 id =" obpm9 " >Method 1: Using Environment Variables</h4 >
2519``` python
2620import os
2721
@@ -32,8 +26,7 @@ os.environ["LLM_API_KEY"] = "your-api-key"
3226os.environ[" LLM_BASE_URL" ] = " https://api.openai.com/v1" # Optional for OpenAI
3327```
3428
35- #### Method 2: Using AgentConfig
36-
29+ <h4 id =" wRp8S " >Method 2: Using AgentConfig</h4 >
3730``` python
3831import os
3932from aworld.agents.llm_agent import Agent
@@ -49,8 +42,7 @@ agent_config = AgentConfig(
4942agent = Agent(name = " my_agent" , conf = agent_config)
5043```
5144
52- #### Method 3: Using Shared ModelConfig
53-
45+ <h4 id =" NDNsw " >Method 3: Using Shared ModelConfig</h4 >
5446When multiple agents use the same LLM service, you can specify a shared ModelConfig:
5547
5648``` python
@@ -74,8 +66,7 @@ agent_config = AgentConfig(
7466agent = Agent(name = " my_agent" , conf = agent_config)
7567```
7668
77- ### Configuring Prompts
78-
69+ <h3 id =" i8Rp7 " >Configuring Prompts</h3 >
7970``` python
8071from aworld.agents.llm_agent import Agent
8172import os
@@ -103,10 +94,8 @@ agent = Agent(
10394)
10495```
10596
106- ### Configuring Tools
107-
108- #### Local Tools
109-
97+ <h3 id =" aWS23 " >Configuring Tools</h3 >
98+ <h4 id =" mvy8i " >Local Tools</h4 >
11099``` python
111100from aworld.agents.llm_agent import Agent
112101import os
@@ -142,8 +131,7 @@ agent = Agent(
142131)
143132```
144133
145- #### MCP (Model Context Protocol) Tools
146-
134+ <h4 id =" RpDnK " >MCP (Model Context Protocol) Tools</h4 >
147135``` python
148136from aworld.agents.llm_agent import Agent
149137import os
@@ -183,8 +171,7 @@ agent = Agent(
183171)
184172```
185173
186- #### Agent as Tool
187-
174+ <h4 id =" XXwHB " >Agent as Tool</h4 >
188175``` python
189176from aworld.agents.llm_agent import Agent
190177import os
@@ -215,10 +202,8 @@ agent = Agent(
215202)
216203```
217204
218- ## Part 2: Customizing Agents
219-
220- ### Customizing Agent Input
221-
205+ <h2 id =" Zvu7e " >Customizing Agents</h2 >
206+ <h3 id =" lcJ3t " >Customizing Agent Input</h3 >
222207Override the ` init_observation() ` function to customize how your agent processes initial observations:
223208
224209``` python
@@ -229,9 +214,8 @@ async def init_observation(self, observation: Observation) -> Observation:
229214 return observation
230215```
231216
232- ### Customizing Model Input
233-
234- Override the ` async_messages_transform() ` function to customize how messages are transformed before being sent to the
217+ <h3 id =" LziMs " >Customizing Model Input</h3 >
218+ Override the ` async_messages_transform() ` function to customize how messages are transformed before being sent to the
235219model:
236220
237221``` python
@@ -269,8 +253,9 @@ async def async_messages_transform(self,
269253 return messages
270254```
271255
272- ### Customizing Model Logic
256+ < h3 id = " memVz " > Customizing Model Logic</ h3 >
273257Override the ` invoke_model() ` function to implement custom model logic:
258+
274259``` python
275260async def invoke_model (self ,
276261 messages : List[Dict[str , str ]] = [],
@@ -296,8 +281,9 @@ async def invoke_model(self,
296281 )
297282```
298283
299- ### Customizing Model Output
284+ < h3 id = " Q0Dah " > Customizing Model Output</ h3 >
300285Create a custom ` ModelOutputParser ` class and specify it using the ` model_output_parser ` parameter:
286+
301287``` python
302288from aworld.models.model_output_parser import ModelOutputParser
303289
@@ -327,8 +313,10 @@ agent = Agent(
327313 model_output_parser = CustomOutputParser()
328314)
329315```
330- ### Customizing Agent Response
316+
317+ <h3 id =" VSib1 " >Customizing Agent Response</h3 >
331318Override the ` async_post_run() ` function to customize how your agent responds:
319+
332320``` python
333321from aworld.core.message import Message
334322
@@ -355,8 +343,29 @@ async def async_post_run(self,
355343 )
356344```
357345
358- ### Custom Response Parsing
359- If the framework doesn't support your response structure, you can create a custom response parser:
346+ <h3 id =" aBJ4H " >Custom Agent Policy</h3 >
347+ Override the ` async_policy() ` function to customize your agent policy logic:
348+
349+ ``` python
350+ async def async_policy (self , observation : Observation, info : Dict[str , Any] = {}, message : Message = None ,
351+ ** kwargs ) -> List[ActionModel]:
352+ self ._finished = False
353+ # build model input messages
354+ messages = await self .build_llm_input(observation, info, message)
355+ # call model
356+ llm_response = await self .invoke_model(messages, message = message, ** kwargs)
357+ # parse model response
358+ agent_result = await self .model_output_parser.parse(llm_response,
359+ agent_id = self .id(),
360+ use_tools_in_prompt = self .use_tools_in_prompt)
361+
362+ self ._finished = True
363+ return agent_result.actions
364+ ```
365+
366+ <h3 id =" FFSXC " >Custom Agent Event Parsing</h3 >
367+ If the framework still does not support the response structure you want, or if there is special logic processing (such as triggering multiple downstream agents based on Agent response), you can create a custom agent response event handler:
368+
360369``` python
361370from aworld.runners import HandlerFactory
362371from aworld.runners.default_handler import DefaultHandler
@@ -389,5 +398,6 @@ agent = Agent(
389398 event_handler_name = custom_name
390399)
391400```
392- ** Important Note:** The ` custom_name ` variable value must remain consistent across your handler registration and agent
393- configuration.
401+
402+ ** Important Note:** The ` custom_name ` variable value must remain consistent across your handler registration and agent configuration.
403+
0 commit comments