curiousily / Get-Things-Done-with-Prompt-Engineering-and-LangChain

LangChain & Prompt Engineering tutorials on Large Language Models (LLMs) such as ChatGPT with custom data. Jupyter notebooks on loading and indexing data, creating prompt templates, CSV agents, and using retrieval QA chains to query the custom data. Projects for using a private LLM (Llama 2) for chat with PDF files, tweets sentiment analysis.
https://mlexpert.io
Apache License 2.0
1.1k stars 347 forks source link

Changing the template and adding customzation in pandas_dataframe_agent/csv agent #1

Open m-ali-awan opened 1 year ago

m-ali-awan commented 1 year ago

Hi , Thanks for veery helping notebooks

Hope all of you are doing great.

I am developing a chatbot/question-answer agent using GPT-4 on pandas dataframe in langchain. I am able to make it work with following code:

gpt4_agent = create_pandas_dataframe_agent(
    ChatOpenAI(temperature=0,
               openai_api_key=OPENAI_API_KEY,
               model_name = 'gpt-4'),[sub_listing_df,company_df], verbose=True
)

But, I think that I can make it more optimized by changing some paramters, and especially prompt template: Here is the prompt template being used by default:

display(Markdown(gpt4_agent.agent.llm_chain.prompt.template))
----------------------
You are working with {num_dfs} pandas dataframes in Python named df1, df2, etc. You should use the tools below to answer the question posed of you:

python_repl_ast: A Python shell. Use this to execute python commands. Input should be a valid python command. When using this tool, sometimes output is abbreviated - make sure it does not look abbreviated before using it in your answer.

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 [python_repl_ast] 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

This is the result of print(df.head()) for each dataframe: {dfs_head}

Begin! Question: {input} {agent_scratchpad}

How can I change the prompt template?

Thanks for any help

m-ali-awan commented 1 year ago

I have tried following:

template = """We are scraping data from a Vehicle Damage Estimating Software. First input is Vehicle Make Model Year, Type, and respective Part and Operation.
            Then we have to choose from possible options to reach that specific part. I will provide you with received Options, you have to tell me which to choose, 
            So we can reach to respective part info
Just output single option, and not folllowing sequence.

Begin! Question: {input} {agent_scratchpad}"""

gpt4_agent = create_pandas_dataframe_agent(
    ChatOpenAI(temperature=0,
               openai_api_key=OPENAI_API_KEY,
               model_name = 'gpt-4'),
    [sub_listing_df,company_df], verbose=True,
    chain_type_kwargs={
        'prompt': PromptTemplate(template=template,input_variables=['input','agent_scratchpad'])
    }
    )

But it is not changing the agent prompt template