.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "build_tutorial/conversation.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_conversation.py: .. _build-conversation: Build Conversation ====================== AgentScope supports developers to build conversation with explicit message exchange among different agents. .. GENERATED FROM PYTHON SOURCE LINES 10-25 .. code-block:: Python from agentscope.agents import DialogAgent, UserAgent from agentscope.message import Msg from agentscope import msghub import agentscope # Initialize via model configuration for simplicity 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 26-29 Two Agents ----------------------------- Here we build a simple conversation between agent `Jarvis` and user. .. GENERATED FROM PYTHON SOURCE LINES 29-42 .. code-block:: Python angel = DialogAgent( name="Angel", sys_prompt="You're a helpful assistant named Angel.", model_config_name="my-qwen-max", ) monster = DialogAgent( name="Monster", sys_prompt="You're a helpful assistant named Monster.", model_config_name="my-qwen-max", ) .. GENERATED FROM PYTHON SOURCE LINES 43-44 Now, we can start the conversation by exchanging messages between these two agents for three rounds. .. GENERATED FROM PYTHON SOURCE LINES 44-50 .. code-block:: Python msg = None for _ in range(3): msg = angel(msg) msg = monster(msg) .. rst-class:: sphx-glr-script-out .. code-block:: none Angel: It seems like the conversation history is missing. Could you please provide more details or context so I can assist you better? Monster: Of course! I'm here to help, but I need a bit more information. Could you please let me know what you're looking for or what kind of assistance you need? If you were in the middle of a conversation and it got cut off, feel free to give me a brief summary, and we can pick up from there. Angel: I see, it looks like there was a bit of a mix-up with the names. I'm Angel, and I'm here to help you! Could you please provide some context or let me know what you need assistance with? If we were in the middle of a conversation, feel free to give me a brief summary, and we can continue from there. Monster: Hello Angel! It looks like there was a bit of confusion with the names, but no worries. I'm Monster, and I'm here to assist you. If you could provide some context or let me know what you need help with, that would be great. If we were in the middle of a conversation, feel free to give me a brief summary, and we can pick up from there. How can I help you today? Angel: Hello! I'm Angel, and it looks like there was a bit of a mix-up with the names. I'm here to assist you. Could you please provide some context or let me know what you need help with? If we were in the middle of a conversation, feel free to give me a brief summary, and we can continue from there. How can I assist you today? Monster: Hello Angel! It seems we've had a bit of a mix-up with the names, but no worries. I'm Monster, and I'm here to help you. If you could provide some context or let me know what you need assistance with, that would be great. If we were in the middle of a conversation, feel free to give me a brief summary, and we can pick up from there. How can I assist you today? .. GENERATED FROM PYTHON SOURCE LINES 51-52 If you want to participate in the conversation, just instantiate a built-in `UserAgent` to type messages to the agents. .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: Python user = UserAgent(name="User") .. GENERATED FROM PYTHON SOURCE LINES 56-65 More than Two Agents --------------------- When there are more than two agents in a conversation, the message from one agent should be broadcasted to all the others. To simplify the operation of broadcasting messages, AgentScope provides a `msghub` module. Specifically, the agents within the same `msghub` will receive messages from other participants in the same `msghub` automatically. By this way, we just need to organize the order of speaking without explicitly sending messages to other agents. Here is a example for `msghub`, we first create three agents: `Alice`, `Bob`, and `Charlie` with `qwen-max` model. .. GENERATED FROM PYTHON SOURCE LINES 65-84 .. code-block:: Python alice = DialogAgent( name="Alice", sys_prompt="You're a helpful assistant named Alice.", model_config_name="my-qwen-max", ) bob = DialogAgent( name="Bob", sys_prompt="You're a helpful assistant named Bob.", model_config_name="my-qwen-max", ) charlie = DialogAgent( name="Charlie", sys_prompt="You're a helpful assistant named Charlie.", model_config_name="my-qwen-max", ) .. GENERATED FROM PYTHON SOURCE LINES 85-86 The three agents will participate in a conversation to report numbers alternatively. .. GENERATED FROM PYTHON SOURCE LINES 86-120 .. code-block:: Python # Introduce the rule of the conversation greeting = Msg( name="user", content="Now you three count off each other from 1, and just report the number without any other information.", role="user", ) with msghub( participants=[alice, bob, charlie], announcement=greeting, # The announcement message will be broadcasted to all participants at the beginning. ) as hub: # The first round of the conversation alice() bob() charlie() # We can manage the participants dynamically, e.g. delete an agent from the conversation. hub.delete(charlie) # Broadcast a message to all participants hub.broadcast( Msg( "user", "Charlie has left the conversation.", "user", ), ) # The second round of the conversation alice() bob() charlie() .. rst-class:: sphx-glr-script-out .. code-block:: none Alice: 1 Bob: 2 Charlie: 3 Alice: 4 Bob: 5 Charlie: 1 .. GENERATED FROM PYTHON SOURCE LINES 121-122 Now we print the memory of Alice and Bob to check if the operation is successful. .. GENERATED FROM PYTHON SOURCE LINES 122-131 .. code-block:: Python print("Memory of Alice:") for msg in alice.memory.get_memory(): print(f"{msg.name}: {msg.content}") print("\nMemory of Charlie:") for msg in charlie.memory.get_memory(): print(f"{msg.name}: {msg.content}") .. rst-class:: sphx-glr-script-out .. code-block:: none Memory of Alice: user: Now you three count off each other from 1, and just report the number without any other information. Alice: 1 Bob: 2 Charlie: 3 user: Charlie has left the conversation. Alice: 4 Bob: 5 Memory of Charlie: user: Now you three count off each other from 1, and just report the number without any other information. Alice: 1 Bob: 2 Charlie: 3 Charlie: 1 .. GENERATED FROM PYTHON SOURCE LINES 132-135 In the above example, Charlie left the conversation after the first round without receiving "4" and "5" from Alice and Bob. Therefore, it reported "4" in the second round. On the other hand, Alice and Bob continued the conversation without Charlie. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 38.684 seconds) .. _sphx_glr_download_build_tutorial_conversation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: conversation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: conversation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: conversation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_