langchain-ai / langchain

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

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

Open zhaoyuchen1128 opened 5 months ago

zhaoyuchen1128 commented 5 months 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 5 months ago

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

mackong commented 5 months ago

@zhaoyuchen1128 please provide complete example code.

SpiritCloude commented 3 months ago

https://python.langchain.com/v0.2/docs/how_to/agent_executor/#adding-in-memory

agent添加memory

`python from langchain_community.chat_message_histories import ChatMessageHistory from langchain_core.chat_history import BaseChatMessageHistory from langchain_core.runnables.history import RunnableWithMessageHistory

store = {}

def get_session_history(session_id: str) -> BaseChatMessageHistory: if session_id not in store: store[session_id] = ChatMessageHistory() return store[session_id]

agent_with_chat_history = RunnableWithMessageHistory( agent_executor, get_session_history, input_messages_key="input", history_messages_key="chat_history", )

`

dosubot[bot] commented 1 day ago

Hi, @zhaoyuchen1128. I'm Dosu, and I'm helping the LangChain team manage their backlog. I'm marking this issue as stale.

Issue Summary

Next Steps

Thank you for your understanding and contribution!