10. Discussion agent with tools (Two Agent Debates with Tools)

Discussion agents using tools (Agent Debates with Tools)

This example shows how to simulate a multi-agent conversation that allows agents to access the tool.

Initialize for LangSmith tracking.

import os

# Enter the project name for debugging.
os.environ["LANGCHAIN_PROJECT"] = "DEBATE AGENT"

DialogueAgent and DialogueSimulator

In this laptop, we will show you how to implement multi-agent simulations where authorized agents determine who to speak to. This follows the opposite selection method of multi-agent decentralized speaker selection.

Multi-Player Authoritarian Speaker Selection Same as defined in DialogueAgent Wow DialogueSimulator I will use the class.

DialogueAgent

  • send The method uses the conversation history to date and the agent's prefix to deliver a message to the chat model and return a response.

  • receive The method adds messages from other agents to the conversation record.

from typing import Callable, List


from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage,
)
from langchain_openai import ChatOpenAI


class DialogueAgent:
    def __init__(
        self,
        name: str,
        system_message: SystemMessage,
        model: ChatOpenAI,
    ) -> None:
        # Sets the name of the agent.
        self.name = name
        # Sets the system message.
        self.system_message = system_message
        # Set up the LLM model.
        self.model = model
        # Specify the agent name.
        self.prefix = f"{self.name}: "
        # Initializes the agent.
        self.reset()

    def reset(self):
        """
        Resets the conversation history.
        """
        self.message_history = ["Here is the conversation so far."]

    def send(self) -> str:
        """
        Add a system message + conversation content and finally the agent's name to the message.
        """
        message = self.model(
            [
                self.system_message,
                HumanMessage(content="\n".join(
                    [self.prefix] + self.message_history)),
            ]
        )
        return message.content

    def receive(self, name: str, message: str) -> None:
        """
        name 이 말한 message 를 메시지 내역에 추가합니다.
        """
        self.message_history.append(f"{name}: {message}")

DialogueSimulator

  • inject The method is given name ( name ) And message ( message Start a conversation with ), and have all agents receive that message.

  • step The method selects the next speaker, and when that speaker sends a message, all agents receive the message. And it increases the current stage.

Provides the ability to simulate conversations between multiple agents.

DialogueAgent Represents individual agents, DialogueSimulator Coordinates and manages conversations between agents.

DialogueAgentWithTools

DialogueAgent Expand to make the tool available DialogueAgentWithTools Define the class.

  • DialogueAgentWithTools Class DialogueAgent Classes were inherited.

  • send The method serves to create and return messages by the agent.

  • create_openai_tools_agent Initialize the agent chain using functions.

  • Defines the tools (tools) that the agent will use when initializing.

Tool settings

Defines a document search tool (Retrieval Tool).

Internet search tools

Generate tools that can be searched on the Internet.

Set up the tools each agent can utilize.

  • names The dictionary defines the name of the debater (prefix name) and the tools that each discussion agent can utilize.

  • topic Select the subject of the discussion.

① Document-based tools

LLM to add details to topic description

You can use the Large Language Model (LLM) to make the description of a given topic more detailed.

To do this, first provide a brief description or overview of the subject as input to the LLM. Then I ask LLM to elaborate on the subject.

Since LLM has learned a huge amount of text data, it can generate additional information and details related to a given topic. This allows you to extend the initial brief description to richer and more detailed content.

  • Description of the conversation based on the given conversation topic (topic) and participants (names) conversation_description ) To generate.

  • agent_descriptor_system_message Is a SystemMessage that states that you can add a description of the participants in the conversation.

  • generate_agent_description The function generates a description generated by LLM for each participant (name).

  • agent_specifier_prompt Conversation description and participant name, word restrictions ( word_limit ) Is composed of HumanMessage.

  • Using the ChatOpenAI model agent_specifier_prompt Generate a description of the participant (agent_description) based on.

You can write phrases that describe each panelist's brief position.

Last updated