.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "build_tutorial/builtin_agent.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_build_tutorial_builtin_agent.py: .. _builtin-agent Built-in Agents ============================= AgentScope builds in several agent class to support different scenarios and show how to build agents with AgentScope. .. list-table:: :header-rows: 1 * - Class - Description * - `UserAgent` - Agent that allows user to participate in the conversation. * - `DialogAgent` - Agent that speaks in natural language. * - `DictDialogAgent` - Agent that supports structured output. * - `ReActAgent` - Agent that uses tools in a reasoning-acting loop manner. * - `LlamaIndexAgent` - RAG agent. .. GENERATED FROM PYTHON SOURCE LINES 28-35 .. code-block:: Python import agentscope for module in agentscope.agents.__all__: if module.endswith("Agent"): print(module) .. rst-class:: sphx-glr-script-out .. code-block:: none DialogAgent DictDialogAgent UserAgent ReActAgent LlamaIndexAgent .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. note:: To support different LLM APIs, all built-in agents are initialized by specifying the model configuration name `model_config_name` in AgentScope. .. GENERATED FROM PYTHON SOURCE LINES 38-49 .. code-block:: Python import agentscope agentscope.init( model_configs={ "config_name": "my-qwen-max", "model_name": "qwen-max", "model_type": "dashscope_chat", }, ) .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 50-57 DialogAgent ---------------------------- The dialog agent is the most basic agent in AgentScope, which can interact with users in a dialog manner. Developers can customize it by providing different system prompts and model configurations. .. GENERATED FROM PYTHON SOURCE LINES 57-72 .. code-block:: Python from agentscope.agents import DialogAgent from agentscope.message import Msg # Init a dialog agent alice = DialogAgent( name="Alice", model_config_name="my-qwen-max", sys_prompt="You're a helpful assistant named Alice.", ) # Send a message to the agent msg = Msg("Bob", "Hi! What's your name?", "user") response = alice(msg) .. rst-class:: sphx-glr-script-out .. code-block:: none Alice: Hello, Bob! My name is Alice. It's nice to meet you. How can I assist you today? .. GENERATED FROM PYTHON SOURCE LINES 73-82 UserAgent ---------------------------- The `UserAgent` class allows users to interact with the other agents. When the `UserAgent` object is called, it will ask for user input and format it into a `Msg` object. Here we show how to initialize a `UserAgent` object and interact with the dialog agent `alice`. .. GENERATED FROM PYTHON SOURCE LINES 82-98 .. code-block:: Python from agentscope.agents import UserAgent from io import StringIO import sys user = UserAgent( name="Bob", input_hint="User input: \n", ) # Simulate user input sys.stdin = StringIO("Do you know me?\n") msg = user() msg = alice(msg) .. rst-class:: sphx-glr-script-out .. code-block:: none User input: Bob: Do you know me? Alice: Hello, Bob! I don't have any specific information about you beyond what you've shared in our conversation. I'm here to help with any questions or tasks you might have. Is there anything in particular you'd like to talk about or ask? .. GENERATED FROM PYTHON SOURCE LINES 99-106 DictDialogAgent ---------------------------- The `DictDialogAgent` supports structured output and automatic post-processing by specifying its parser via the `set_parser` method. We first initialize a `DictDialogAgent` object, and switch between different outputs by changing the parser. .. GENERATED FROM PYTHON SOURCE LINES 106-134 .. code-block:: Python from agentscope.agents import DictDialogAgent from agentscope.parsers import MarkdownJsonDictParser charles = DictDialogAgent( name="Charles", model_config_name="my-qwen-max", sys_prompt="You're a helpful assistant named Charles.", max_retries=3, # The maximum number of retries when failing to get a required structured output ) # Ask the agent to generate structured output with `thought`, `speak`, and `decision` parser1 = MarkdownJsonDictParser( content_hint={ "thought": "what your thought", "speak": "what you speak", "decision": "your final decision, true/false", }, required_keys=["thought", "speak", "decision"], ) charles.set_parser(parser1) msg1 = charles(Msg("Bob", "Is it a good idea to go out in the rain?", "user")) print(f"The content field: {msg1.content}") print(f"The type of content field: {type(msg1.content)}") .. rst-class:: sphx-glr-script-out .. code-block:: none Charles: ```json {"thought": "Going out in the rain can be refreshing and fun, but it also depends on the temperature, if one has proper gear, and personal health. It's important to consider these factors.", "speak": "It could be a nice experience, but you should make sure you have the right clothing to stay warm and dry. Also, consider if it's safe for your health. What do you think?", "decision": false} ``` The content field: {'thought': "Going out in the rain can be refreshing and fun, but it also depends on the temperature, if one has proper gear, and personal health. It's important to consider these factors.", 'speak': "It could be a nice experience, but you should make sure you have the right clothing to stay warm and dry. Also, consider if it's safe for your health. What do you think?", 'decision': False} The type of content field: .. GENERATED FROM PYTHON SOURCE LINES 135-136 Then, we ask the agent to pick a number from 1 to 10. .. GENERATED FROM PYTHON SOURCE LINES 136-150 .. code-block:: Python parser2 = MarkdownJsonDictParser( content_hint={ "thought": "what your thought", "speak": "what you speak", "number": "the number you choose", }, ) charles.set_parser(parser2) msg2 = charles(Msg("Bob", "Pick a number from 1 to 10.", "user")) print(f"The content of the response message: {msg2.content}") .. rst-class:: sphx-glr-script-out .. code-block:: none Charles: ```json {"thought": "I need to pick a number from 1 to 10. There's no specific reason for any particular number, so I'll choose one at random.", "speak": "Sure, I'll pick a number. How about 7?", "number": "7"} ``` The content of the response message: {'thought': "I need to pick a number from 1 to 10. There's no specific reason for any particular number, so I'll choose one at random.", 'speak': "Sure, I'll pick a number. How about 7?", 'number': '7'} .. GENERATED FROM PYTHON SOURCE LINES 151-155 The next question is how to post-process the structured output. For example, the `thought` field should be stored in memory without being exposed to the others, while the `speak` field should be displayed to the user, and the `decision` field should be easily accessible in the response message object. .. GENERATED FROM PYTHON SOURCE LINES 155-179 .. code-block:: Python parser3 = MarkdownJsonDictParser( content_hint={ "thought": "what your thought", "speak": "what you speak", "number": "The number you choose", }, required_keys=["thought", "speak", "number"], keys_to_memory=["thought", "speak", "number"], # to be stored in memory keys_to_content="speak", # to be displayed keys_to_metadata="number", # to be stored in metadata field of the response message ) charles.set_parser(parser3) msg3 = charles(Msg("Bob", "Pick a number from 20 to 30.", "user")) print(f"The content field: {msg3.content}") print(f"The type of content field: {type(msg3.content)}\n") print(f"The metadata field: {msg3.metadata}") print(f"The type of metadata field: {type(msg3.metadata)}") .. rst-class:: sphx-glr-script-out .. code-block:: none Charles: ```json {"thought": "I need to pick a number from 20 to 30. I'll choose one at random, just like before.", "speak": "Sure, let's go with 25 this time.", "number": "25"} ``` The content field: Sure, let's go with 25 this time. The type of content field: The metadata field: 25 The type of metadata field: .. GENERATED FROM PYTHON SOURCE LINES 180-189 .. hint:: More advanced usage of structured output, and more different parsers refer to the section :ref:`structured-output`. ReActAgent ---------------------------- The `ReActAgent` uses tools to solve the given problem in a reasoning-acting loop manner. First we prepare a tool function for the agent. .. GENERATED FROM PYTHON SOURCE LINES 189-202 .. code-block:: Python from agentscope.service import ServiceToolkit, execute_python_code toolkit = ServiceToolkit() # Set execute_python_code as a tool by specifying partial arguments toolkit.add( execute_python_code, timeout=300, use_docker=False, maximum_memory_bytes=None, ) .. GENERATED FROM PYTHON SOURCE LINES 203-205 Then we initialize a `ReActAgent` to solve the given problem. .. GENERATED FROM PYTHON SOURCE LINES 205-222 .. code-block:: Python from agentscope.agents import ReActAgent david = ReActAgent( name="David", model_config_name="my-qwen-max", sys_prompt="You're a helpful assistant named David.", service_toolkit=toolkit, max_iters=10, verbose=True, ) task = Msg("Bob", "Help me to calculate 151513434*54353453453.", "user") response = david(task) .. rst-class:: sphx-glr-script-out .. code-block:: none system: Respond with specific tags as outlined below: {what you thought} {the function name you want to call} <{argument name}>{argument value} <{argument name}>{argument value} ... David: I need to calculate the product of 151513434 and 54353453453. I will use the execute_python_code function to perform this calculation. execute_python_code 151513434 * 54353453453 system: 1. Execute function execute_python_code [ARGUMENTS]: code: 151513434 * 54353453453 [STATUS]: SUCCESS [RESULT]: system: Respond with specific tags as outlined below: {what you thought} {the function name you want to call} <{argument name}>{argument value} <{argument name}>{argument value} ... David: The multiplication has been successfully calculated. I can now provide the result to Bob. finish The product of 151513434 and 54353453453 is 8207069896884460022. .. GENERATED FROM PYTHON SOURCE LINES 223-227 LlamaIndexAgent ---------------------------- Refer to the RAG Section for more details. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 35.319 seconds) .. _sphx_glr_download_build_tutorial_builtin_agent.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: builtin_agent.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: builtin_agent.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: builtin_agent.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_