langchain
langchain copied to clipboard
How do I customize the prompt for the zero shot agent ?
I create an agent using:
zero_shot_agent = initialize_agent(
agent="zero-shot-react-description",
tools=tools,
llm=llm,
verbose=True,
max_iterations=3
)
I now want to customize the content of the default prompt used by the agent. I wasn't able to locate any documented input parameters to initialize_agent() to do so. Is there a way to accomplish this ?
You can see the already defined prompt on your agent by using:
print(zero_shot_agent.agent.llm_chain.prompt.template)
Also you can define a message and change it like that:
sys_message = "YOUR NEW PROMPT IS HERE"
zero_shot_agent.agent.llm_chain.prompt.template = sys_message
Thank you @berkedilekoglu - I tried it and this works for me.
However, I suspect this solution bypasses the prompt validation that happens when the PromptTemplate is constructed. Do you agree ?
Was hoping for a solution where the custom prompt could be set via the call to initialize_agent() Let me know.
@berkedilekoglu hi, can u help me out on how to change prompt with lang chain JS, cuz it seems like not available on the documentation too!
@s-1-n-t-h hello, I apologize for my delayed response. I don't think I can be of assistance as my knowledge of JavaScript is at a very basic level.
@berkedilekoglu no problem, thanks for replying out !
Thank you @berkedilekoglu - I tried it and this works for me.
However, I suspect this solution bypasses the prompt validation that happens when the PromptTemplate is constructed. Do you agree ?
Was hoping for a solution where the custom prompt could be set via the call to initialize_agent() Let me know.
Old thread, but I found it while searching how to do the same thing, so here it goes:
PREFIX = """Answer the following questions as best you can. You have access to the following tools:"""
FORMAT_INSTRUCTIONS = """Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question"""
SUFFIX = """Begin!
Question: {input}
Thought:{agent_scratchpad}"""
agent = initialize_agent(
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
tools=tools,
llm=llm,
agent_kwargs={
'prefix':PREFIX,
'format_instructions':FORMAT_INSTRUCTIONS,
'suffix':SUFFIX
}
)
I was facing the same issue.
Love the answer about using agent_kwargs. In my case, replacing PREFIX is enough. FORMAT_INSTRUCTIONS requires duplicating a lot of existing contents and variables which are not necessary.
You can find the entire built-in prompts in /langchain/agents/
you can add prefix to executor agent in langchainjs like this:
const executor = await initializeAgentExecutorWithOptions(tools, llm, {
agentType: "structured-chat-zero-shot-react-description",
verbose: true,
memory: new BufferMemory({
chatHistory: new ChatMessageHistory(pastMessages),
returnMessages: true,
memoryKey: "chat_history"
}),
agentArgs: {
inputVariables: ["input", "agent_scratchpad", "chat_history"],
memoryPrompts: [new MessagesPlaceholder("chat_history")],
prefix: "you are a chat bot. Your priority is to chat with enquirers and use tools when necessary.",
},
});
Thanks @felipeff your recommendation was really helpful. Also If others are looking for how to customize prompts of other agents, it is a good idea to check these paths. So you can know which parameters use on agent_kwargs depending on the chosen agent.
@berkedilekoglu Is it possible to add memory in the chat-zero-shot-react-description agent. If yes Please Give me some explanation. I tried a lot can't able to add.
Hi @Boopalanoptisol you can do that with something like this on AgentExecutor
message_history = PostgresChatMessageHistory(
connection_string=f"postgresql://{settings.DATABASE_USER}:{settings.DATABASE_PASSWORD}@{settings.DATABASE_HOST}/chat"
session_id="foo",
)
memory = ConversationBufferMemory(
memory_key="chat_history", return_messages=True, chat_memory=message_history
)
######
#Agent code and tools
#######
agent_executor = AgentExecutor.from_agent_and_tools(
agent=agent,
tools=tools,
verbose=False,
handle_parsing_errors=True,
memory=memory,
)
This is useful: https://python.langchain.com/docs/modules/agents/how_to/custom_llm_agent
Hi, @jiyer2016
I'm helping the LangChain team manage their backlog and am marking this issue as stale. From what I understand, you opened this issue to seek guidance on customizing the prompt for the zero-shot agent created using the initialize_agent function. There were multiple solutions provided by the community, including using sys_message to change the prompt and using agent_kwargs to set a custom prompt via initialize_agent(). The issue has been resolved with these solutions.
Could you please confirm if this issue is still relevant to the latest version of the LangChain repository? If it is, please let the LangChain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.
Thank you for your understanding and contribution to LangChain!
@berkedilekoglu Is it possible to add memory in the chat-zero-shot-react-description agent. If yes Please Give me some explanation. I tried a lot can't able to add.
I am also facing the same issue ....Kindly help me if you were able to find the solution.