.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorial/workflow_routing.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_tutorial_workflow_routing.py: .. _routing: Routing ========================== There are two ways to implement routing in AgentScope, both simple and easy to implement: - Routing by structured output - Routing by tool calls .. tip:: Considering there is no unified standard/definition for agent routing, we follow the setting in `Building effective agents `_ Routing by Structured Output ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ By this way, we can directly use the structured output of the agent to determine which agent to route the message to. Initialize a routing agent .. GENERATED FROM PYTHON SOURCE LINES 20-83 .. code-block:: Python import asyncio import json import os from typing import Literal from pydantic import BaseModel, Field from agentscope.agent import ReActAgent from agentscope.formatter import DashScopeChatFormatter from agentscope.memory import InMemoryMemory from agentscope.message import Msg from agentscope.model import DashScopeChatModel from agentscope.tool import Toolkit, ToolResponse router = ReActAgent( name="Router", sys_prompt="You're a routing agent. Your target is to route the user query to the right follow-up task.", model=DashScopeChatModel( model_name="qwen-max", api_key=os.environ["DASHSCOPE_API_KEY"], stream=False, ), formatter=DashScopeChatFormatter(), ) # Use structured output to specify the routing task class RoutingChoice(BaseModel): your_choice: Literal[ "Content Generation", "Programming", "Information Retrieval", None, ] = Field( description="Choose the right follow-up task, and choose ``None`` if the task is too simple or no suitable task", ) task_description: str | None = Field( description="The task description", default=None, ) async def example_router_explicit() -> None: """Example of explicit routing with structured output.""" msg_user = Msg( "user", "Help me to write a poem", "user", ) # Route the query msg_res = await router( msg_user, structured_model=RoutingChoice, ) # The structured output is stored in the metadata field print("The structured output:") print(json.dumps(msg_res.metadata, indent=4, ensure_ascii=False)) asyncio.run(example_router_explicit()) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/agentscope/agentscope/src/agentscope/model/_dashscope_model.py:232: DeprecationWarning: 'required' is not supported by DashScope API. It will be converted to 'auto'. warnings.warn( Router: { "type": "tool_use", "name": "generate_response", "input": { "your_choice": "Content Generation", "task_description": "Help me to write a poem" }, "id": "call_d491224a69244b1a99d5a3" } system: { "type": "tool_result", "id": "call_d491224a69244b1a99d5a3", "name": "generate_response", "output": [ { "type": "text", "text": "Successfully generated response." } ] } Router: Sure, I can help with that! Could you please share some more details? Like, what would you like the poem to be about? Any specific style or length in mind? The structured output: { "your_choice": "Content Generation", "task_description": "Help me to write a poem" } .. GENERATED FROM PYTHON SOURCE LINES 84-90 Routing by Tool Calls ~~~~~~~~~~~~~~~~~~~~~~~~~ Another way is to wrap the downstream agents into a tool function, so that the routing agent decides which tool to call based on the user query. We first define several tool functions: .. GENERATED FROM PYTHON SOURCE LINES 90-140 .. code-block:: Python async def generate_python(demand: str) -> ToolResponse: """Generate Python code based on the demand. Args: demand (``str``): The demand for the Python code. """ # An example demand agent python_agent = ReActAgent( name="PythonAgent", sys_prompt="You're a Python expert, your target is to generate Python code based on the demand.", model=DashScopeChatModel( model_name="qwen-max", api_key=os.environ["DASHSCOPE_API_KEY"], stream=False, ), memory=InMemoryMemory(), formatter=DashScopeChatFormatter(), toolkit=Toolkit(), ) msg_res = await python_agent(Msg("user", demand, "user")) return ToolResponse( content=msg_res.get_content_blocks("text"), ) # Fake some other tool functions for demonstration purposes async def generate_poem(demand: str) -> ToolResponse: """Generate a poem based on the demand. Args: demand (``str``): The demand for the poem. """ pass async def web_search(query: str) -> ToolResponse: """Search the web for the query. Args: query (``str``): The query to search. """ pass .. GENERATED FROM PYTHON SOURCE LINES 141-143 After that, we define a routing agent and equip it with the above tool functions. .. GENERATED FROM PYTHON SOURCE LINES 143-177 .. code-block:: Python toolkit = Toolkit() toolkit.register_tool_function(generate_python) toolkit.register_tool_function(generate_poem) toolkit.register_tool_function(web_search) # Initialize the routing agent with the toolkit router_implicit = ReActAgent( name="Router", sys_prompt="You're a routing agent. Your target is to route the user query to the right follow-up task.", model=DashScopeChatModel( model_name="qwen-max", api_key=os.environ["DASHSCOPE_API_KEY"], stream=False, ), formatter=DashScopeChatFormatter(), toolkit=toolkit, memory=InMemoryMemory(), ) async def example_router_implicit() -> None: """Example of implicit routing with tool calls.""" msg_user = Msg( "user", "Help me to generate a quick sort function in Python", "user", ) # Route the query await router_implicit(msg_user) asyncio.run(example_router_implicit()) .. rst-class:: sphx-glr-script-out .. code-block:: none Router: { "type": "tool_use", "name": "generate_python", "input": { "demand": "a quick sort function" }, "id": "call_0ab60508e57441c0be0243" } PythonAgent: Certainly! Here's a Python implementation of the Quick Sort algorithm: ```python def quick_sort(arr): if len(arr) <= 1: return arr else: pivot = arr[len(arr) // 2] left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quick_sort(left) + middle + quick_sort(right) # Example usage: arr = [3, 6, 8, 10, 1, 2, 1] sorted_arr = quick_sort(arr) print("Sorted array:", sorted_arr) ``` This function works as follows: 1. If the array has 1 or 0 elements, it is already sorted, so we return it as is. 2. Otherwise, we choose a pivot element from the array (in this case, the middle element). 3. We then partition the array into three sub-arrays: - `left` contains elements less than the pivot. - `middle` contains elements equal to the pivot. - `right` contains elements greater than the pivot. 4. We recursively apply the `quick_sort` function to the `left` and `right` sub-arrays and concatenate the results with the `middle` array to get the final sorted array. You can test this function with different arrays to see how it works. system: { "type": "tool_result", "id": "call_0ab60508e57441c0be0243", "name": "generate_python", "output": [ { "type": "text", "text": "Certainly! Here's a Python implementation of the Quick Sort algorithm:\n\n```python\ndef quick_sort(arr):\n if len(arr) <= 1:\n return arr\n else:\n pivot = arr[len(arr) // 2]\n left = [x for x in arr if x < pivot]\n middle = [x for x in arr if x == pivot]\n right = [x for x in arr if x > pivot]\n return quick_sort(left) + middle + quick_sort(right)\n\n# Example usage:\narr = [3, 6, 8, 10, 1, 2, 1]\nsorted_arr = quick_sort(arr)\nprint(\"Sorted array:\", sorted_arr)\n```\n\nThis function works as follows:\n1. If the array has 1 or 0 elements, it is already sorted, so we return it as is.\n2. Otherwise, we choose a pivot element from the array (in this case, the middle element).\n3. We then partition the array into three sub-arrays:\n - `left` contains elements less than the pivot.\n - `middle` contains elements equal to the pivot.\n - `right` contains elements greater than the pivot.\n4. We recursively apply the `quick_sort` function to the `left` and `right` sub-arrays and concatenate the results with the `middle` array to get the final sorted array.\n\nYou can test this function with different arrays to see how it works." } ] } Router: The Python implementation of the Quick Sort algorithm has been generated for you. Here it is: ```python def quick_sort(arr): if len(arr) <= 1: return arr else: pivot = arr[len(arr) // 2] left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quick_sort(left) + middle + quick_sort(right) # Example usage: arr = [3, 6, 8, 10, 1, 2, 1] sorted_arr = quick_sort(arr) print("Sorted array:", sorted_arr) ``` You can use this function to sort an array of numbers. The example usage provided will sort the array `[3, 6, 8, 10, 1, 2, 1]` and print the sorted result. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 47.336 seconds) .. _sphx_glr_download_tutorial_workflow_routing.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: workflow_routing.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: workflow_routing.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: workflow_routing.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_