Reputation: 11
Previously I used initialize_agent
method by passing agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION
to initialize a conversation react agent in LangChain v0.0.
agent_executor = initialize_agent(tools=tools,
llm=llm,
memory=memory,
verbose=True,
max_iterations=3,
handle_parsing_errors=True,
agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
agent_kwargs=prompt
)
But in LangChain v1.0, it says that it will be deprecated and to use the create_react_agent
method. I want to initialize an CONVERSATIONAL_REACT_DESCRIPTION
agent.
# Construct the ReAct agent
agent = create_react_agent(llm=llm,
tools=tools,
prompt=template
)
# Create an agent executor by passing in the agent and tools
agent_executor = AgentExecutor(agent=agent,
tools=tools,
max_iterations=3,
handle_parsing_errors=True,
verbose=True
)
langchain 0.1.11, langchain-community 0.0.27, langchain-core 0.1.30, langchain-google-genai 0.0.9, langchain-mistralai 0.0.5, langchain-openai 0.0.8, langchain-text-splitters 0.0.1, langsmith 0.1.23
How to initiate a CONVERSATIONAL_REACT_DESCRIPTION
in LangChain v0.1?
Upvotes: 1
Views: 782
Reputation: 171
import os
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_core.prompts import MessagesPlaceholder
from langchain_openai import ChatOpenAI
from langchain.agents import create_openai_functions_agent
from langchain.agents import AgentExecutor
from langchain.tools.retriever import create_retriever_tool
vectordb = FAISS.load_local(faiss_path, instructor_embeddings)
retriever = vectordb.as_retriever(score_threshold=0.7)
retriever_tool = create_retriever_tool(
retriever,
"some_name",
"Retriver description",
)
prompt = ChatPromptTemplate.from_messages([
("system", prompt_template),
MessagesPlaceholder(variable_name="chat_history"),
("user", "{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
])
tools = [retriever_tool, <other_tools>]
llm = ChatOpenAI(temperature=0.0)
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)
data = {
"chat_history": <chat_history>,
"input": "<user prompt>"
}
response = agent_executor.invoke(data)
response = response['output']
Upvotes: 0