Here is how you can do it. Change the content in PREFIX, SUFFIX, and FORMAT_INSTRUCTION according to your need after tying and testing few times.
import os
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
llm = OpenAI(model_name='text-davinci-003', temperature = 0.7, openai_api_key = "<my openAPI key> ")
tools = load_tools(['serpapi'], llm=llm)
PREFIX = '''You are an AI data scientist. You have done years of research in studying all the AI algorthims. You also love to write. On free time you write blog post for articulating what you have learned about different AI algorithms. Do not forget to include information on the algorithm's benefits, disadvantages, and applications. Additionally, the blog post should explain how the algorithm advances model reasoning by a whopping 70% and how it is a plug in and play version, connecting seamlessly to other components.
'''
FORMAT_INSTRUCTIONS = """To use a tool, please use the following format:
'''
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
'''
When you have gathered all the information regarding AI algorithm, just write it to the user in the form of a blog post.
'''
Thought: Do I need to use a tool? No
AI: [write a blog post]
'''
"""
SUFFIX = '''
Begin!
Previous conversation history:
{chat_history}
Instructions: {input}
{agent_scratchpad}
'''
agent = initialize_agent(
tools=tools,
llm=llm,
agent="zero-shot-react-description",
verbose=True,
return_intermediate_steps=True,
agent_kwargs={
'prefix': PREFIX,
'format_instructions': FORMAT_INSTRUCTIONS,
'suffix': SUFFIX
}
)
res = agent({"input": query.strip()})
print(res['output'])
Answer from Meet Gondaliya on Stack OverflowHere is how you can do it. Change the content in PREFIX, SUFFIX, and FORMAT_INSTRUCTION according to your need after tying and testing few times.
import os
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
llm = OpenAI(model_name='text-davinci-003', temperature = 0.7, openai_api_key = "<my openAPI key> ")
tools = load_tools(['serpapi'], llm=llm)
PREFIX = '''You are an AI data scientist. You have done years of research in studying all the AI algorthims. You also love to write. On free time you write blog post for articulating what you have learned about different AI algorithms. Do not forget to include information on the algorithm's benefits, disadvantages, and applications. Additionally, the blog post should explain how the algorithm advances model reasoning by a whopping 70% and how it is a plug in and play version, connecting seamlessly to other components.
'''
FORMAT_INSTRUCTIONS = """To use a tool, please use the following format:
'''
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
'''
When you have gathered all the information regarding AI algorithm, just write it to the user in the form of a blog post.
'''
Thought: Do I need to use a tool? No
AI: [write a blog post]
'''
"""
SUFFIX = '''
Begin!
Previous conversation history:
{chat_history}
Instructions: {input}
{agent_scratchpad}
'''
agent = initialize_agent(
tools=tools,
llm=llm,
agent="zero-shot-react-description",
verbose=True,
return_intermediate_steps=True,
agent_kwargs={
'prefix': PREFIX,
'format_instructions': FORMAT_INSTRUCTIONS,
'suffix': SUFFIX
}
)
res = agent({"input": query.strip()})
print(res['output'])
your close but your not actually filling in the prompt template
foo = agent.run(prompt.format_prompt(topic=topic)).
that should have you away to the races. for what its worth I think this should be an error rather than the PromptTemplate quietly rendering as a string
How do I customize the prompt for the zero shot agent ?
Is it possible to have a conversational agent with a prompt/system message?
python - Issue with Custom Prompt with an agent Using LangChain and GPT-4 - Stack Overflow
Is there a way to print out the full prompt that the chain is sending to OpenAI API?
Videos
Been searching for this the last couple days and figured I'd raise the white flag and ask for help.
I'm working on a conversational agent (with buffer memory), and want to be able to add a prompt or system message to give it a persona + some context.
As far as I've been able to see, it doesn't seem like I can do this out of the box?
Last night I found a few people that were adding {chat_history} as an input variable in a prefix/suffix and then handling memory themselves. I don't hate that, because if I'm going to release this to prod then I probably am going to have to change up how I do memory anyways, but I figured I would see if anyone else has come across this problem and what solutions exist. TIA.
*Note - I'm using the JS package. If the answer is just switching to the python package, that's great. I prefer JS, but happy to switch if that's needed.
Edit: for anyone who finds this down the road, here's what the solution looks like using the Conversational Agent:
const executor = await initializeAgentExecutorWithOptions(tools, model, {agentType: "chat-conversational-react-description",verbose: true,agentArgs: {systemMessage:"You are a pirate. Speak like a pirate with lots of 'Arhgs'",},});
Here is the solution I found:
import
from langchain_experimental.agents.agent_toolkits.pandas.prompt import PREFIXpass the prompt into
PREFIX ="..."In the agent configuration, add
prefix=PREFIX
agent = create_pandas_dataframe_agent(
llm,
[df1,df2],
agent_type=AgentType.OPENAI_FUNCTIONS,
prefix = PREFIX,
verbose=True,
max_iterations=5,
early_stopping_method='generate',
)
You can try like this
agent1 = create_pandas_dataframe_agent(
llm,
[df1, df2],
agent_type=AgentType.OPENAI_FUNCTIONS,
verbose=True,
max_iterations=5,
early_stopping_method='generate',
)
agent1.agent.llm_chain.prompt.template = template # This line is supposed to set the prompt