suna/core/examples/example-agent/agent.py

94 lines
3.0 KiB
Python
Raw Normal View History

2024-10-17 19:44:10 +08:00
import asyncio
2024-10-28 05:04:42 +08:00
import json
2024-10-17 19:44:10 +08:00
from agentpress.thread_manager import ThreadManager
from tools.files_tool import FilesTool
2024-10-30 00:21:19 +08:00
from agentpress.state_manager import StateManager
from tools.terminal_tool import TerminalTool
2024-10-10 22:21:39 +08:00
2024-11-02 07:05:29 +08:00
async def run_agent(thread_id: str, max_iterations: int = 5):
2024-11-01 23:09:12 +08:00
# Initialize managers and tools
thread_manager = ThreadManager()
2024-11-02 07:05:29 +08:00
state_manager = StateManager()
2024-11-01 23:09:12 +08:00
thread_manager.add_tool(FilesTool)
thread_manager.add_tool(TerminalTool)
await thread_manager.add_message(
thread_id,
{
"role": "user",
2024-11-02 07:05:29 +08:00
"content": "Let's create a marketing website for my AI Agent 'Jarvis' using HTML, CSS, Javascript. Use images from pixabay, pexels, and co. Style it cyberpunk style. Make it like Ironmen Jarvis."
2024-11-01 23:09:12 +08:00
}
)
2024-10-23 09:42:38 +08:00
async def init():
pass
async def pre_iteration():
2024-10-30 00:21:19 +08:00
# Update files state
files_tool = FilesTool()
await files_tool._init_workspace_state()
2024-10-23 09:42:38 +08:00
async def after_iteration():
2024-11-02 07:05:29 +08:00
# Ask the user for a custom message or use the default
custom_message = input("Enter a message to send (or press Enter to use 'Continue!!!' as message): ")
message_content = custom_message if custom_message else "Continue!!!"
2024-10-30 00:21:19 +08:00
await thread_manager.add_message(thread_id, {
"role": "user",
2024-11-02 07:05:29 +08:00
"content": message_content
2024-10-30 00:21:19 +08:00
})
2024-10-23 09:42:38 +08:00
async def finalizer():
pass
await init()
iteration = 0
2024-11-01 23:09:12 +08:00
2024-10-23 09:42:38 +08:00
while iteration < max_iterations:
iteration += 1
await pre_iteration()
2024-10-28 05:04:42 +08:00
# Get entire state store
state = await state_manager.export_store()
2024-10-30 00:21:19 +08:00
state_info = f"Current workspace state:\n{json.dumps(state, indent=2)}"
2024-10-28 05:04:42 +08:00
system_message = {
"role": "system",
2024-10-30 00:21:19 +08:00
"content": f"""You are a web developer who can create, read, update, and delete files,
and execute terminal commands. You write clean, well-structured code and explain your changes.
2024-10-23 09:42:38 +08:00
2024-10-30 00:21:19 +08:00
Current workspace state:
{state_info}
Explain what you're doing before making changes."""
}
model_name = "anthropic/claude-3-5-sonnet-latest"
2024-10-28 05:04:42 +08:00
2024-10-23 09:42:38 +08:00
response = await thread_manager.run_thread(
thread_id=thread_id,
system_message=system_message,
model_name=model_name,
temperature=0.7,
2024-10-30 00:21:19 +08:00
max_tokens=4096,
2024-10-23 09:42:38 +08:00
tool_choice="auto",
execute_tools_async=False,
2024-10-23 10:43:57 +08:00
use_tools=True,
2024-10-23 09:42:38 +08:00
execute_model_tool_calls=True
)
2024-11-01 23:09:12 +08:00
print(response)
2024-10-23 09:42:38 +08:00
2024-11-02 07:05:29 +08:00
# Call after_iteration without arguments
2024-10-23 09:42:38 +08:00
await after_iteration()
await finalizer()
2024-10-17 19:44:10 +08:00
2024-10-17 04:08:46 +08:00
if __name__ == "__main__":
2024-10-23 09:42:38 +08:00
async def main():
2024-11-02 07:05:29 +08:00
thread_manager = ThreadManager()
thread_id = await thread_manager.create_thread()
await run_agent(thread_id)
asyncio.run(main())