Langchain CSV 代理

Langchain CSV agent

提问人:3bs 提问时间:9/22/2023 最后编辑:Yilmaz3bs 更新时间:9/26/2023 访问量:163

问:

我有这行来创建 Langchain csv 代理,并将内存或聊天记录添加到 itiwan,以使代理可以访问用户问题和响应并在操作中考虑它们,但代理根本不识别内存 这是我的代码>>

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


    

有人可以帮我做吗

我已经尝试了所有添加内存的方法,但我无法正确做到这一点

内存 openai-api history langchain

评论


答:

0赞 ZKS 9/26/2023 #1

这是您可以做到的一种方式

        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)

评论

0赞 3bs 9/27/2023
但这样我只保存内存,我需要做的是代理可以在生成我问题的答案时使用内存,因此它将是对话式的