GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing doc.agentscope.io — Create ReAct Agent - AgentScope Note Go to the end to download the full example code. Create ReAct Agent¶AgentScope provides out-of-the-box ReAct agent It supports the following features at the same time:
Tip Refer to the Agent section for more details about these features. In quickstart, we focus on how to create a ReAct agent and run it. from agentscope.agent import ReActAgent, AgentBase from agentscope.formatter import DashScopeChatFormatter from agentscope.memory import InMemoryMemory from agentscope.message import Msg from agentscope.model import DashScopeChatModel import asyncio import os from agentscope.tool import Toolkit, execute_python_code Creating ReAct Agent¶To improve the flexibility, the
Taking DashScope API as example, we create an agent object as follows: async def creating_react_agent() -> None: """Create a ReAct agent and run a simple task.""" # Prepare tools toolkit = Toolkit() toolkit.register_tool_function(execute_python_code) jarvis = ReActAgent( name="Jarvis", sys_prompt="You're a helpful assistant named Jarvis", model=DashScopeChatModel( model_name="qwen-max", api_key=os.environ["DASHSCOPE_API_KEY"], stream=True, enable_thinking=False, ), formatter=DashScopeChatFormatter(), toolkit=toolkit, memory=InMemoryMemory(), ) msg = Msg( name="user", content="Hi! Jarvis, run Hello World in Python.", role="user", ) await jarvis(msg) asyncio.run(creating_react_agent()) Jarvis: {
"type": "tool_use",
"id": "call_0c0453fdf1f0428396e1d0",
"name": "execute_python_code",
"input": {
"code": "print('Hello World')",
"timeout": 300
},
"raw_input": "{\"code\": \"print('Hello World')\", \"timeout\": 300}"
}
system: {
"type": "tool_result",
"id": "call_0c0453fdf1f0428396e1d0",
"name": "execute_python_code",
"output": [
{
"type": "text",
"text": "<returncode>0</returncode><stdout>Hello World\n</stdout><stderr></stderr>"
}
]
}
Jarvis: The Python code has been executed successfully and it printed:
```
Hello World
```
Creating From Scratch¶You may want to create an agent from scratch, AgentScope provides two base classes for you to inherit from:
Please refer to the Agent section for more details about the agent class. Taking the class MyAgent(AgentBase): """A custom agent class""" def __init__(self) -> None: """Initialize the agent""" super().__init__() self.name = "Friday" self.sys_prompt = "You're a helpful assistant named Friday." self.model = DashScopeChatModel( model_name="qwen-max", api_key=os.environ["DASHSCOPE_API_KEY"], stream=False, ) self.formatter = DashScopeChatFormatter() self.memory = InMemoryMemory() async def reply(self, msg: Msg | list[Msg] | None) -> Msg: """Reply to the message.""" await self.memory.add(msg) # Prepare the prompt prompt = await self.formatter.format( [ Msg("system", self.sys_prompt, "system"), *await self.memory.get_memory(), ], ) # Call the model response = await self.model(prompt) msg = Msg( name=self.name, content=response.content, role="assistant", ) # Record the response in memory await self.memory.add(msg) # Print the message await self.print(msg) return msg async def observe(self, msg: Msg | list[Msg] | None) -> None: """Observe the message.""" # Store the message in memory await self.memory.add(msg) async def handle_interrupt(self) -> Msg: """Postprocess the interrupt.""" # Taking a fixed response as example return Msg( name=self.name, content="I noticed you interrupted me, how can I help you?", role="assistant", ) async def run_custom_agent() -> None: """Run the custom agent.""" agent = MyAgent() msg = Msg( name="user", content="Who are you?", role="user", ) await agent(msg) asyncio.run(run_custom_agent()) Friday: I'm Friday, your helpful assistant! How can I assist you today? Further Reading¶Total running time of the script: (0 minutes 6.564 seconds) | |||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |