Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- examples/README.md +26 -0
- examples/__init__.py +219 -0
examples/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Apps
|
2 |
+
|
3 |
+
Each subdirectory contains an example provided by us. If you haven't set the LLM parameters as environment variables, please configure the AgentConfig before running.
|
4 |
+
|
5 |
+
## AgentConfig Parameters
|
6 |
+
|
7 |
+
When constructing the `AgentConfig`, you can set the parameters as follows:
|
8 |
+
|
9 |
+
- **llm_provider**: `str`
|
10 |
+
(Currently supports only "openai")
|
11 |
+
- **llm_model_name**: `str`
|
12 |
+
(Default: `"gpt-4o"`)
|
13 |
+
- **llm_temperature**: `float`
|
14 |
+
(Default: `1.0`)
|
15 |
+
- **llm_base_url**: `str` **(Required)**
|
16 |
+
(e.g., for OpenAI's official service, set to `https://api.openai.com/v1/`)
|
17 |
+
- **llm_api_key**: `str` **(Required)**
|
18 |
+
(Fill in with your API key)
|
19 |
+
|
20 |
+
## Writing an Agent and Tool
|
21 |
+
|
22 |
+
- **Agent**:
|
23 |
+
To write an agent, please refer to the [Agent README](../aworld/agents/README.md).
|
24 |
+
|
25 |
+
- **Tool in Environment**:
|
26 |
+
For instructions on writing a tool within the environment, please refer to the [Virtual Environments README](tools/README.md).
|
examples/__init__.py
ADDED
@@ -0,0 +1,219 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
import sys
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
from aworld.core.task import Task
|
7 |
+
from aworld.core.agent.base import AgentFactory
|
8 |
+
from aworld.core.agent.swarm import Swarm
|
9 |
+
from aworld.runner import Runners
|
10 |
+
from aworld.agents.llm_agent import Agent
|
11 |
+
from aworld.config.conf import AgentConfig, ContextRuleConfig, ModelConfig, OptimizationConfig, LlmCompressionConfig
|
12 |
+
from aworld.core.context.base import Context
|
13 |
+
from aworld.core.event.base import Message
|
14 |
+
from aworld.runners.hook.hooks import PreLLMCallHook, PostLLMCallHook
|
15 |
+
from aworld.runners.hook.hook_factory import HookFactory
|
16 |
+
from aworld.utils.common import convert_to_snake
|
17 |
+
|
18 |
+
|
19 |
+
class ContextManagement():
|
20 |
+
"""Test cases for Context Management system based on README examples"""
|
21 |
+
|
22 |
+
def init_agent(self, config_type: str = "1", context_rule: ContextRuleConfig = None):
|
23 |
+
if config_type == "1":
|
24 |
+
conf = AgentConfig(
|
25 |
+
llm_model_name=self.mock_model_name,
|
26 |
+
llm_base_url=self.mock_base_url,
|
27 |
+
llm_api_key=self.mock_api_key
|
28 |
+
)
|
29 |
+
else:
|
30 |
+
conf = AgentConfig(
|
31 |
+
llm_config=ModelConfig(
|
32 |
+
llm_model_name=self.mock_model_name,
|
33 |
+
llm_base_url=self.mock_base_url,
|
34 |
+
llm_api_key=self.mock_api_key
|
35 |
+
)
|
36 |
+
)
|
37 |
+
return Agent(
|
38 |
+
conf=conf,
|
39 |
+
name="my_agent" + str(random.randint(0, 1000000)),
|
40 |
+
system_prompt="You are a helpful assistant.",
|
41 |
+
agent_prompt="You are a helpful assistant.",
|
42 |
+
context_rule=context_rule
|
43 |
+
)
|
44 |
+
|
45 |
+
def __init__(self):
|
46 |
+
"""Set up test fixtures"""
|
47 |
+
self.mock_model_name = "gpt-4o"
|
48 |
+
self.mock_base_url = "http://localhost:34567"
|
49 |
+
self.mock_api_key = "lm-studio"
|
50 |
+
os.environ["LLM_API_KEY"] = self.mock_api_key
|
51 |
+
os.environ["LLM_BASE_URL"] = self.mock_base_url
|
52 |
+
os.environ["LLM_MODEL_NAME"] = self.mock_model_name
|
53 |
+
|
54 |
+
class _AssertRaisesContext:
|
55 |
+
"""Context manager for assertRaises"""
|
56 |
+
|
57 |
+
def __init__(self, expected_exception):
|
58 |
+
self.expected_exception = expected_exception
|
59 |
+
|
60 |
+
def __enter__(self):
|
61 |
+
return self
|
62 |
+
|
63 |
+
def __exit__(self, exc_type, exc_value, traceback):
|
64 |
+
if exc_type is None:
|
65 |
+
raise AssertionError(
|
66 |
+
f"Expected {self.expected_exception.__name__} to be raised, but no exception was raised")
|
67 |
+
if not issubclass(exc_type, self.expected_exception):
|
68 |
+
raise AssertionError(
|
69 |
+
f"Expected {self.expected_exception.__name__} to be raised, but got {exc_type.__name__}: {exc_value}")
|
70 |
+
return True # Suppress the exception
|
71 |
+
|
72 |
+
def fail(self, msg=None):
|
73 |
+
"""Fail immediately with the given message"""
|
74 |
+
raise AssertionError(msg or "Test failed")
|
75 |
+
|
76 |
+
def run_agent(self, input, agent: Agent):
|
77 |
+
swarm = Swarm(agent, max_steps=1)
|
78 |
+
print('swarm ', swarm)
|
79 |
+
return Runners.sync_run(
|
80 |
+
input=input,
|
81 |
+
swarm=swarm
|
82 |
+
)
|
83 |
+
|
84 |
+
def run_multi_agent(self, input, agent1: Agent, agent2: Agent):
|
85 |
+
swarm = Swarm(agent1, agent2, max_steps=1)
|
86 |
+
return Runners.sync_run(
|
87 |
+
input=input,
|
88 |
+
swarm=swarm
|
89 |
+
)
|
90 |
+
|
91 |
+
def run_task(self, context: Context, agent: Agent):
|
92 |
+
swarm = Swarm(agent, max_steps=1)
|
93 |
+
task = Task(input="""What is an agent.""", swarm=swarm, context=context)
|
94 |
+
result = Runners.sync_run_task(task)
|
95 |
+
print("----------------------------------------------------------------------------------------------")
|
96 |
+
print(result)
|
97 |
+
|
98 |
+
def default_context_configuration(self):
|
99 |
+
|
100 |
+
# No need to explicitly configure context_rule, system automatically uses default configuration
|
101 |
+
# Default configuration is equivalent to:
|
102 |
+
# context_rule=ContextRuleConfig(
|
103 |
+
# optimization_config=OptimizationConfig(
|
104 |
+
# enabled=True,
|
105 |
+
# max_token_budget_ratio=1.0 # Use 100% of context window
|
106 |
+
# ),
|
107 |
+
# llm_compression_config=LlmCompressionConfig(
|
108 |
+
# enabled=False # Compression disabled by default
|
109 |
+
# )
|
110 |
+
# )
|
111 |
+
mock_agent = self.init_agent("1")
|
112 |
+
response = self.run_agent(input="""What is an agent. describe within 20 words""", agent=mock_agent)
|
113 |
+
|
114 |
+
print(response.answer)
|
115 |
+
|
116 |
+
def custom_context_configuration(self):
|
117 |
+
"""Test custom context configuration (README Configuration example)"""
|
118 |
+
# Create custom context rules
|
119 |
+
mock_agent = self.init_agent(context_rule=ContextRuleConfig(
|
120 |
+
optimization_config=OptimizationConfig(
|
121 |
+
enabled=True,
|
122 |
+
max_token_budget_ratio=0.00015
|
123 |
+
),
|
124 |
+
llm_compression_config=LlmCompressionConfig(
|
125 |
+
enabled=True,
|
126 |
+
trigger_compress_token_length=100,
|
127 |
+
compress_model=ModelConfig(
|
128 |
+
llm_model_name=self.mock_model_name,
|
129 |
+
llm_base_url=self.mock_base_url,
|
130 |
+
llm_api_key=self.mock_api_key,
|
131 |
+
)
|
132 |
+
)
|
133 |
+
))
|
134 |
+
|
135 |
+
response = self.run_agent(input="""describe What is an agent in details""", agent=mock_agent)
|
136 |
+
print(response.answer)
|
137 |
+
|
138 |
+
|
139 |
+
def state_management_and_recovery(self):
|
140 |
+
class StateModifyAgent(Agent):
|
141 |
+
async def async_policy(self, observation, info=None, **kwargs):
|
142 |
+
result = await super().async_policy(observation, info, **kwargs)
|
143 |
+
self.context.state['policy_executed'] = True
|
144 |
+
return result
|
145 |
+
|
146 |
+
class StateTrackingAgent(Agent):
|
147 |
+
async def async_policy(self, observation, info=None, **kwargs):
|
148 |
+
result = await super().async_policy(observation, info, **kwargs)
|
149 |
+
assert self.context.state['policy_executed'] == True
|
150 |
+
return result
|
151 |
+
|
152 |
+
# Create custom agent instance
|
153 |
+
custom_agent = StateModifyAgent(
|
154 |
+
conf=AgentConfig(
|
155 |
+
llm_model_name=self.mock_model_name,
|
156 |
+
llm_base_url=self.mock_base_url,
|
157 |
+
llm_api_key=self.mock_api_key
|
158 |
+
),
|
159 |
+
name="state_modify_agent",
|
160 |
+
system_prompt="You are a Python expert who provides detailed and practical answers.",
|
161 |
+
agent_prompt="You are a Python expert who provides detailed and practical answers.",
|
162 |
+
)
|
163 |
+
|
164 |
+
# Create a second agent for multi-agent testing
|
165 |
+
second_agent = StateTrackingAgent(
|
166 |
+
conf=AgentConfig(
|
167 |
+
llm_model_name=self.mock_model_name,
|
168 |
+
llm_base_url=self.mock_base_url,
|
169 |
+
llm_api_key=self.mock_api_key
|
170 |
+
),
|
171 |
+
name="state_tracking_agent",
|
172 |
+
system_prompt="You are a helpful assistant.",
|
173 |
+
agent_prompt="You are a helpful assistant.",
|
174 |
+
)
|
175 |
+
|
176 |
+
response = self.run_multi_agent(
|
177 |
+
input="What is an agent. describe within 20 words",
|
178 |
+
agent1=custom_agent,
|
179 |
+
agent2=second_agent
|
180 |
+
)
|
181 |
+
print(response.answer)
|
182 |
+
|
183 |
+
|
184 |
+
class TestHookSystem(ContextManagement):
|
185 |
+
|
186 |
+
def __init__(self):
|
187 |
+
super().__init__()
|
188 |
+
|
189 |
+
def hook_registration(self):
|
190 |
+
"""Test hook registration and retrieval"""
|
191 |
+
# Test that hooks are registered in _cls attribute
|
192 |
+
# Test hook creation using __call__ method
|
193 |
+
pre_hook = HookFactory("TestPreLLMHook")
|
194 |
+
post_hook = HookFactory("TestPostLLMHook")
|
195 |
+
|
196 |
+
def hook_execution(self):
|
197 |
+
mock_agent = self.init_agent("1")
|
198 |
+
response = self.run_agent(input="""What is an agent. describe within 20 words""", agent=mock_agent)
|
199 |
+
print(response.answer)
|
200 |
+
|
201 |
+
def task_context_transfer(self):
|
202 |
+
|
203 |
+
mock_agent = self.init_agent("1")
|
204 |
+
context = Context.instance()
|
205 |
+
context.state.update({"task": "What is an agent."})
|
206 |
+
self.run_task(context=context, agent=mock_agent)
|
207 |
+
|
208 |
+
|
209 |
+
if __name__ == '__main__':
|
210 |
+
testContextManagement = ContextManagement()
|
211 |
+
testContextManagement.default_context_configuration()
|
212 |
+
testContextManagement.custom_context_configuration()
|
213 |
+
testContextManagement.state_management_and_recovery()
|
214 |
+
# testHookSystem = TestHookSystem()
|
215 |
+
# testHookSystem.hook_registration()
|
216 |
+
# testHookSystem = TestHookSystem()
|
217 |
+
# testHookSystem.hook_execution()
|
218 |
+
# testHookSystem = TestHookSystem()
|
219 |
+
# testHookSystem.task_context_transfer()
|