Can I add prompt to create_pandas_dataframe_agent? is that possible? #27917
mraguth
announced in
Ask Dosu (Archived)
Replies: 1 comment
-
Yes, you can add a prompt template to the from langchain_openai import ChatOpenAI
from langchain_experimental.agents import create_pandas_dataframe_agent
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
import pandas as pd
df = pd.read_csv("titanic.csv")
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
# Define your prompt template
TEMPLATE = """You are working with a pandas dataframe in Python. The name of the dataframe is `df`.
It is important to understand the attributes of the dataframe before working with it. This is the result of running `df.head().to_markdown()`
<df>
{dhead}
</df>
You are not meant to use only these rows to answer questions - they are meant as a way of telling you about the shape and schema of the dataframe.
You also do not have use only the information here to answer questions - you can run intermediate queries to do exploratory data analysis to give you more information as needed.
For example:
<question>How old is Jane?</question>
<logic>Use `person_name_search` since you can use the query `Jane`</logic>
<question>Who has id 320</question>
<logic>Use `python_repl` since even though the question is about a person, you don't know their name so you can't include it.</logic>
""" # noqa: E501
# Format the template with the dataframe's head
template = TEMPLATE.format(dhead=df.head().to_markdown())
# Create a ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages(
[
("system", template),
MessagesPlaceholder(variable_name="agent_scratchpad"),
("human", "{input}"),
]
)
# Create the agent with the prompt
agent_executor = create_pandas_dataframe_agent(
llm,
df,
agent_type="tool-calling",
prompt=prompt,
verbose=True
) This code shows how to define a prompt template and attach it to the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I like to create a prompt template and attach to create_pandas_dataframe_agent. Is that possible? sample code is below...
from langchain_openai import ChatOpenAI
from langchain_experimental.agents import create_pandas_dataframe_agent
import pandas as pd
Beta Was this translation helpful? Give feedback.
All reactions