-
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
|
Beta Was this translation helpful? Give feedback.
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.
Yes, you can add a prompt template to the
create_pandas_dataframe_agent
function. In the sample code you provided, you can define a prompt template and integrate it into the agent creation process. Here's an example of how you can do this: