TwoAgentDebateWithTools
Author: Suhyun Lee
Peer Review:
Proofread : BokyungisaGod
This is a part of LangChain Open Tutorial
Overview
This example demonstrates how to simulate multi-agent conversations where agents have access to tools. The agents interact with each other to engage in logical debates on a given topic, utilizing tools to search for information or perform calculations as needed. Through this, you can gain a practical understanding of integrating agents and tools within LangChain.
Table of Contents
References
Environment Setup
Setting up your environment is the first step. See the Environment Setup guide for more details.
[Note]
The langchain-opentutorial is a package of easy-to-use environment setup guidance, useful functions and utilities for tutorials.
Check out the langchain-opentutorial for more details.
You can set API keys in a .env file or set them manually.
[Note] If you’re not using the .env file, no worries! Just enter the keys directly in the cell below, and you’re good to go.
How to Set Up Tavily Search
DialogueAgent and DialogueSimulator
DialogueAgent and DialogueSimulatorIn this simulation, the Moderator Agent and Participant Agents interact to operate effectively.
Role Descriptions
Moderator Agent (Agent with authority)
Primary Role: Manages speaking turns and coordinates interactions.
Characteristics:
A central management agent with special authority.
Decides when participant agents can speak or act.
Example: Similar to a moderator in a meeting assigning speaking turns to participants.
Participant Agents
Primary Role: Act or speak according to the instructions from the moderator agent.
Characteristics:
Do not decide speaking turns independently.
Collaborate and participate in activities as directed by the moderator.
System Features
Centralized Management:
The moderator agent coordinates all speaking turns and actions.
In contrast to a decentralized system, where all agents self-coordinate.
This simulation exemplifies a centrally managed approach to speaking and action coordination.
DialogueAgent
DialogueAgentThe DialogueAgent class manages conversations by setting the agent's name, system message, and language model (ChatOpenAI). The primary methods of the class are as follows:
sendMethodRole:
Constructs messages using the current conversation history (
message_history) and the agent's name prefix (prefix).The prefix serves as an identifier that includes the agent's name, helping to structure conversation history and organize input formatting for the model.
Sends the constructed message to the language model (
ChatOpenAI) and returns the generated response.
How it works:
Combines the current conversation history (
message_history) with the prefix (prefix) to create a single message.Sends the constructed message to the language model (
ChatOpenAI).Returns the response message generated by the language model.
receiveMethodRole:
Adds a message sent by another agent (or user) and the speaker's name to the conversation history.
This conversation history is used when the
sendmethod is called later.
How it works:
Combines the speaker's name (
name) and the message (message) into a single line of conversation.Adds the combined message to the conversation history (
message_history).
resetMethodRole:
Resets the conversation history.
When reset, it is initialized with a default message:
"Here is the conversation so far."
How it works:
Resets the conversation history (
message_history) to an empty list.Adds the default message
"Here is the conversation so far."to the conversation history.
DialogueSimulator
DialogueSimulatorThe DialogueSimulator class coordinates and manages conversations among multiple agents.
It simulates interactions between individual DialogueAgent instances, controlling the flow of dialogue and message delivery.
Methods
injectMethodPurpose:
Initiates a conversation with a given
nameandmessage.Ensures all agents receive the message.
Typically used to set the initial message of a dialogue.
How it works:
Delivers the
nameandmessageto all agents.Increments the simulation step (
_step) by 1.
stepMethodPurpose:
Progresses the simulation by selecting the next speaker and continuing the conversation.
The selected speaker generates a message, which is then distributed to all other agents.
How it works:
Uses
selection_functionto determine the next speaker.The chosen speaker (
speaker) generates a message by calling itssendmethod.All agents receive the speaker's message.
Increments the simulation step (
_step) by 1.Returns the speaker's name and the generated message.
DialogueAgentWithTools
DialogueAgentWithToolsDialogueAgentWithTools extends the DialogueAgent class, adding support for external tools.
This class integrates an OpenAI model with external tools to handle both conversational and task-processing functionalities.
Methods
__init__MethodPurpose:
Initializes the agent with its name, system message, model, and a list of external tools.
How it works:
Calls
super().__init__to initialize the base settings (name, system message, model).Stores the list of tools in
self.tools.
sendMethodPurpose:
Processes messages using an agent that integrates the OpenAI model and external tools.
Generates a response by leveraging the conversation history and available tools.
How it works:
Uses
hub.pullto retrieve the required prompt for agent execution.Calls
create_openai_tools_agentto initialize an agent that integrates the OpenAI model and tools.Executes the agent using
AgentExecutorto process the input message.Combines the system message, prefix, and conversation history (
message_history) into an input message.Extracts the
outputfrom the execution result and creates anAIMessageobject to return the content.
create_openai_tools_agentFunctionPurpose:
Combines the OpenAI model and external tools to create a functional agent.
The agent is capable of leveraging tools for performing tasks.
How it works:
Initializes the agent by integrating the OpenAI model with the provided tools.
Configures the agent's behavior and rules using a supplied prompt.
Ensures the agent can call tools and handle their results as part of its operation.
Tool Configuration
Document Search Tool Setup
This code demonstrates the process of setting up a tool to search for documents containing arguments for and against medical school expansion.
Using the langchain package, it loads, splits, vectorizes documents, and creates a search tool.
Document Loading and Splitting
Role:
Loads text files and splits the documents into manageable chunks.
How it works:
Uses
TextLoaderto load text files:data/Opposition_to_Medical_School_Expansion.txt: Document with opposing arguments.data/Support_for_Medical_School_Expansion.txt: Document with supporting arguments.
Splits the documents into chunks of 1000 characters with an overlap of 100 characters using
RecursiveCharacterTextSplitter.
Creating a VectorStore
Role:
Vectorizes document content for searchability.
How it works:
Uses
OpenAIEmbeddingsto embed the document text.Creates a vector store using
FAISS:vector1: Based on the opposing arguments document.vector2: Based on the supporting arguments document.
Creating Retrievers
Role:
Provides functionality to search for similar documents using the vector store.
How it works:
Calls
vector1.as_retriever()andvector2.as_retriever()to create retrievers.Configures each retriever to return the top 5 most similar documents (
k=5).
Creating Search Tools
Role:
Defines search retrievers as tools for external use.
How it works:
Uses
create_retriever_toolto create search tools:doctor_retriever_tool: Tool for searching opposing argument documents.gov_retriever_tool: Tool for searching supporting argument documents.
Adds a name and description to each tool to clarify its purpose.
Internet Search Tool
Web Search Tool: Tavily Search
LangChain provides a built-in tool to easily use the Tavily search engine. Tavily Search is a powerful feature for searching relevant data on the web and returning search results.
Document-Based Tool Setup
names: Defines the names (prefixes) of each debater and the tools they can use."Doctor Union": Tools available to the Doctor Union agent (e.g.,doctor_retriever_tool)."Government": Tools available to the Government agent (e.g.,gov_retriever_tool).
topic: Specifies the debate topic.Example:
"As of 2024, is expanding medical school enrollment in South Korea necessary?"
word_limit: Sets a word limit for descriptions used by the agents.
Search-Based Tool Setup
names_search: Assigns search-based tools to debaters."Doctor Union"and"Government"are configured to use the search tool (search).
topicandword_limitare set the same as in the document-based setup.
Generating Participant Descriptions Using LLM
This code utilizes an LLM (Large Language Model) to create detailed descriptions for participants in a conversation.
Based on the given topic and participant information, the LLM generates descriptions that include each participant's perspective and role.
conversation_descriptionPurpose:
Creates a conversation description based on the discussion topic (
topic) and participant names (names).Serves as input text to provide the initial settings of the conversation to the LLM.
How it works:
Combines the topic and participant names into a description string.
Example:
Here is the topic of conversation: [topic]. The participants are: [Participant1, Participant2, ...].
agent_descriptor_system_messagePurpose:
Provides instructions to the LLM to "add detailed descriptions for participants."
A system message used as a guide when generating participant descriptions.
generate_agent_descriptionFunctionPurpose:
Generates a description for a specific participant (
name) using the LLM.Creates a tailored description that includes the participant's perspective and role.
How it works:
Creates the
agent_specifier_prompt:Includes the conversation description (
conversation_description), participant name (name), and word limit (word_limit).Asks the LLM to generate a professional and concise description.
Calls the
ChatOpenAImodel to generate the description.Returns the generated description (
agent_description).
agent_descriptionsPurpose:
A dictionary that stores descriptions for all participants.
How it works:
Calls the
generate_agent_descriptionfunction for each participant name.Stores the generated description along with the name in the dictionary.
Result format:
{'Participant1': 'Description1', 'Participant2': 'Description2', ...}.
You can write a brief statement explaining the stance of each debater directly.
Global System Message Configuration
The System Message defines the roles and conversational rules for each agent in an interactive AI system.
This code clarifies the behavior guidelines and goals that agents must follow during the conversation.
Components
generate_system_messageFunctionPurpose:
Creates a system message defining an agent's behavior guidelines based on its name (
name), description (description), and tools (tools).
How it works:
Composes a basic conversation setup, including the agent's name and description.
Specifies rules for the agent:
DO:
Use tools to retrieve information.
Counter arguments from the opposing agent and cite sources.
DO NOT:
Generate fake citations or reference unverified sources.
Repeat points already mentioned.
Instructs the agent to respond in Korean and stop speaking after completing its point of view.
Returns the final system message.
agent_system_messagesDictionaryPurpose:
Stores the system messages generated for all agents.
How it works:
Combines
names(agent names and tools) withagent_descriptions(agent descriptions).Calls
generate_system_messagefor each agent to create a system message.Stores the resulting messages in a dictionary, using agent names as keys.
System Message Output
Purpose:
Displays the generated system messages for verification.
How it works:
Iterates through the
agent_system_messagesdictionary.Prints each agent's name and its corresponding system message.
topic_specifier_prompt
The topic_specifier_prompt is code that generates a prompt to make the given conversation topic more specific.
It utilizes an LLM (Large Language Model) to refine the initial topic and create a clear topic to be conveyed to the conversation participants.
topic_specifier_promptRole:
Generates a prompt that includes the necessary instructions to specify the topic.
Composition:
SystemMessage:Provides the LLM with instructions to "make the topic more specific."
HumanMessage:Includes the initial topic (
topic) and participant names (names), requesting the topic to be specified within 100 words.Requires the response to be written in Korean.
ChatOpenAIRole:
Takes the
topic_specifier_promptas input and generates a more detailed topic.
Parameters:
temperature=1.0:Configures the model to generate more creative and varied topics.
Topic Specification Results
Original topic: Outputs the initial topic.
Detailed topic: Outputs the topic refined by the LLM.
Or, you can specify it directly as follows.
Agent Creation and Integration
This code creates and integrates agents to be used in the debate simulation.
Each agent is based on the DialogueAgentWithTools class and is equipped to explore evidence and present counterarguments using tools.
Components
agentsPurpose:
Creates the base agents for the simulation.
Each agent is configured with a name, system message, model, and tools.
How it works:
Iterates through
namesandagent_system_messages.Initializes an instance of
DialogueAgentWithToolsfor each participant.Adds the created agents to the
agentslist.
agents_with_searchPurpose:
Creates additional agents equipped with search tools.
How it works:
Iterates through
names_searchandagent_system_messages.Initializes
DialogueAgentWithToolsinstances with search functionality.Adds the created agents to the
agents_with_searchlist.
Agent Integration
Purpose:
Combines
agentsandagents_with_searchinto a unified list for the simulation.
How it works:
Calls
agents.extend(agents_with_search)to merge the two lists.The unified
agentslist contains all participants, equipped with the required tools and functionalities.
The select_next_speaker function is responsible for selecting the next speaker.
Debate Execution
This code runs and manages a conversation between agents using the DialogueSimulator. The debate is based on a specified topic and participating agents, with each step outputting the speaker and their message.
Components
max_itersPurpose:
Sets the maximum number of dialogue iterations.
Here,
max_iters=3limits the conversation to 3 exchanges.
simulatorPurpose:
An instance of the DialogueSimulator class that manages the flow of dialogue and message delivery.
Initialization:
agents: The list of agents participating in the conversation.select_next_speaker: A function to select the next speaker.
injectMethodPurpose:
Starts the conversation by injecting the specified topic through the "Moderator" agent.
How it works:
Calls
simulator.inject("Moderator", specified_topic)to inject the topic.The "Moderator" presents the topic, initiating the dialogue.
Outputs the topic:
print(f"(Moderator): {specified_topic}").
stepMethodPurpose:
Executes one step of the simulation, generating a speaker and their message.
How it works:
Calls
simulator.step()to select the next speaker.The selected speaker generates a message, which all agents receive.
Returns the speaker's name (
name) and message (message).Outputs the speaker and message:
print(f"({name}): {message}").
Repeat Loop
Purpose:
Uses a
whileloop to continue the conversation up tomax_iterstimes.
How it works:
Initializes
n = 0and incrementsnwith each iteration.While
n < max_iters, callssimulator.step()to continue the conversation.Outputs the speaker and message at each step.
Last updated