0
def create_pandas_dataframe_agent( 

    llm:llm, 

    df:df, 

    #callback_manager: Optional[BaseCallbackManager] = None, 

    prefix: str=PREFIX, 

    suffix: str= SUFFIX, 

    input_variables:str= None, 

    verbose:bool=False, 

    return_intermediate_steps:bool=False, 

    #max_iterations: Optional[int]=15, 

    #max_execution_time: Optional[float]= None, 

    early_stopping_method:str="generate", 

    **kwargs: Any, 

) -> AgentExecutor: 

    """Construct a pandas agent from an LLM and dataframe.""" 

    import pandas as pd 

  

    if not isinstance(df, pd.DataFrame): 

        raise ValueError(f"Expected pandas object, got {type(df)}") 

    if input_variables is None: 

        input_variables = ["df","input"] 

    tools = [PythonAstREPLTool(locals={"df": df})] 

     

    PREFIX = """ 

            You are working with a pandas dataframe in Python. The name of the dataframe is `df`. 

            You should use the tools below to answer the question posed of you:""" 

     

    SUFFIX = """ 

            This is the result of `print(df.head())`: 

            {df} 

            Begin! 

            {chat_history} 

            Question: {input}""" 

  

    prompt = ZeroShotAgent.create_prompt( 

        tools,  

        prefix=PREFIX, 

        suffix=SUFFIX,  

        input_variables=[ "df","input","chat_history"] 

    ) 

     

    print(prompt) 

     

    partial_prompt = prompt.partial(df=str(df.head())) 

     

    print(f"\n{partial_prompt}") 

     

    doc_chain = load_qa_chain(llm, chain_type="map_reduce") 

     

    llm_chain = LLMChain( 

        llm=llm, 

        prompt=partial_prompt, 

        #callback_manager=callback_manager, 

    ) 

     

    tool_names = [tool.name for tool in tools] 

     

    agent = ZeroShotAgent( 

        llm_chain=llm_chain, 

        allowed_tools=tool_names, 

         

        #callback_manager=callback_manager, 

        **kwargs, 

    ) 

      

   

       

    return AgentExecutor.from_agent_and_tools( 

        agent=agent, 

        tools=tools, 

        verbose=verbose, 

        return_intermediate_steps=return_intermediate_steps, 

        #max_iterations=max_iterations, 

        #max_execution_time=30, 

        #max_iterations=2, 

        early_stopping_method=early_stopping_method, 

        #callback_manager=callback_manager, 

        memory = memory 

         

    ) 

    chain=create_pandas_dataframe_agent(llm=llm, 

    df=df, 

    input_variables= None, 

    verbose=True, 

    return_intermediate_steps=False, 

    early_stopping_method="generate", 

    max_iterations=2) 

 

    from langchain.agents import Tool 

  

tools = [ 

    Tool( 

        name='Knowledge Base', 

        func=chain.run, 

        description=( 

            'use this tool when answering general knowledge queries to get ' 

            'more information about the topic' 

        ), 

         

    ) 

] 

 

from langchain.agents import initialize_agent 

  

agent = initialize_agent( 

    agent='conversational-react-description', 

    tools=tools, 

    llm=llm, 

    verbose=False, 

    max_iterations=2, 

    early_stopping_method='generate', 

    memory=memory, 

    handle_parsing_errors=_handle_error, 

#     max_execution_time=30, 

   # agent_kwargs={"format_instructions": format_instructions}, 

) 

 

query="give the names of in Zambia project" 

  

# agent(query) 

try: 

    response = agent(query) 

except ValueError as e: 

    response = str(e) 

    if not response.startswith("Could not parse LLM output: `"): 

        raise e 

    response = response.removeprefix("Could not parse LLM output: `").removesuffix("`") 

l 

 

Here, I have used the pandas dataframe agent with appropriate tools and the outcome is absolutely correct but the execution time is too high. But, the actual problem is when I restart my flask server again, my code gets executed quite fast. I am using AzureOpenAI library. Also, I am facing an inconsistent response issue as well. When I run the same query again, it gives me the right output.

I am expecting a consistent response which is right and the execution time is also low.

markalex
  • 8,623
  • 2
  • 7
  • 32

0 Answers0