langchain icon indicating copy to clipboard operation
langchain copied to clipboard

How do I customize the prompt for the zero shot agent ?

Open jiyer2016 opened this issue 2 years ago • 3 comments

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 ?

jiyer2016 avatar May 03 '23 13:05 jiyer2016

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

berkedilekoglu avatar May 03 '23 22:05 berkedilekoglu

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.

jiyer2016 avatar May 04 '23 09:05 jiyer2016

@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 avatar Jun 01 '23 11:06 s-1-n-t-h

@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 avatar Jun 06 '23 07:06 berkedilekoglu

@berkedilekoglu no problem, thanks for replying out !

s-1-n-t-h avatar Jun 06 '23 08:06 s-1-n-t-h

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
    }
)

felipeff avatar Jun 10 '23 23:06 felipeff

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//prompt.py

runninghare avatar Jun 14 '23 22:06 runninghare

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.",
    },

});

geriang avatar Jun 27 '23 01:06 geriang

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.

image image

jonra1993 avatar Aug 02 '23 15:08 jonra1993

@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.

Boopalanoptisol avatar Sep 01 '23 15:09 Boopalanoptisol

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,
    )

jonra1993 avatar Sep 02 '23 00:09 jonra1993

This is useful: https://python.langchain.com/docs/modules/agents/how_to/custom_llm_agent

aryansid avatar Sep 19 '23 10:09 aryansid

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!

dosubot[bot] avatar Dec 19 '23 00:12 dosubot[bot]

@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.

vishnua2j avatar Mar 21 '24 15:03 vishnua2j