3bs
3bs

Reputation: 11

Langchain CSV agent

i have this lines to create the Langchain csv agent with the memory or a chat history added to itiwan to make the agent have access to the user questions and the responses and consider them in the actions but the agent doesn't recognize the memory at all here is my code >>

memory_x = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
agent = create_csv_agent(OpenAI(temperature=0, openai_api_key=os.environ["OPENAI_API_KEY"]),filepath,agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
`
# and here is the post function in the route that handles the the user input -----
    if request.method == 'POST':
          question = request.form['question']
          response = agent.run(question)  # Use user input from the web page


    

could someone plese help me do it

i have tried al the ways for adding the memory but i can't make this correct

Upvotes: 0

Views: 747

Answers (1)

ZKS
ZKS

Reputation: 2866

This is one way you can do it

        from langchain.agents import create_csv_agent
        from langchain.llms import OpenAI
        from langchain.agents.agent_types import AgentType

        class ChatWithCSVAgent:
            def __init__(self):
                self.memory = []
                self.agent = create_csv_agent(
                    OpenAI(temperature=0),
                    "your csv path",
                    verbose=True,
                    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
                )

            def run(self, user_input):
                
                response = self.agent.run(user_input)

                # Update memory
                self.memory.append({"user": user_input, "AI": response})

                for interaction in self.memory:
                    print("User:", interaction["user"])
                    print("AI Assistant:", interaction["AI"])

                return response


        #Run
        conversational_agent = ChatWithCSVAgent()
        response = conversational_agent.run("how many rows are there?")
        print(response)

Upvotes: -1

Related Questions