langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
88.6k stars 13.92k forks source link

agent的memory如何添加?我尝试了许多方法,始终报错variable chat_history should be a list of base messages #23563

Open zhaoyuchen1128 opened 1 week ago

zhaoyuchen1128 commented 1 week ago

Checked other resources

Example Code

memory = ConversationBufferMemory(memory_key="chat_history") chat_history=[] if co.count_documents(query) != 0:

    for i in range(0, len(co.find(query)[0]["content"]), 1):
        if i % 2 == 0:

           chat_history.append(HumanMessage(content=co.find(query)[0]["content"][i]))

        else:
           chat_history.append(AIMessage(content=co.find(query)[0]["content"][i]))

memory.chat_memory=chat_history
llm = OLLAMA(model=language_model)
print(memory.chat_memory)

tools = load_tools(["google-serper"], llm=llm)

agent = initialize_agent(tools, llm, agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, verbose=True,memory=memory)
xx=agent.run(content)

Error Message and Stack Trace (if applicable)

ValueError: variable chat_history should be a list of base messages

Description

我就是想将memory加载到agent中,加载到conversationchain时都没问题

System Info

windows latest

zhaoyuchen1128 commented 1 week ago

memory如果什么都不操作也报同样的错误

mackong commented 1 week ago

@zhaoyuchen1128 please provide complete example code.