langchain 变量`chat_history`应该是一个基本消息的列表,但是得到的是

wooyq4lh  于 3个月前  发布在  其他
关注(0)|答案(2)|浏览(32)

从描述来看,这个问题可能是由于chatbot_data['activeModel']的值不正确导致的。请检查chatbot_data字典中的activeModel键是否包含正确的模型名称。如果模型名称正确,但问题仍然存在,请尝试更新LangChain到最新版本,因为可能存在已知的问题和修复。

要更新LangChain,您可以运行以下命令:

pip install --upgrade langchain

如果问题仍然存在,建议在GitHub上查找类似的问题或提交一个新的issue,以便开发人员可以解决这个问题。

5fjcxozz

5fjcxozz2#

错误 "变量 chat_history 应该是一个基本消息的列表" 表示 chat_history 变量没有正确地初始化为 BaseMessage 对象的列表。以下是解决此问题的方法:

  1. 确保 chat_historyBaseMessage 对象的列表:
    确保 chat_history 变量已正确填充了 BaseMessage 或其子类( HumanMessageAIMessage )的示例。

  2. 更正 ConversationBufferWindowMemory 的初始化:
    ConversationBufferWindowMemory 应使用正确的参数进行初始化。

这是您代码的修正版本:

from langchain_core.messages import HumanMessage, AIMessage, BaseMessage
from langchain.memory import ConversationBufferWindowMemory
from langchain_community.chat_message_histories.firestore import FirestoreChatMessageHistory
from langchain.llms import ChatOpenAI
from langchain.agents import initialize_agent
from langchain.tools import Tool
from langchain.chains import RetrievalQA

async def run_chatbot(vectorstore, session_id, uid, chatbot_data):
    try:
        openai_api_key = os.getenv("OPENAI_API_KEY")
        if not openai_api_key:
            raise ValueError("Missing OpenAI API key in environment variables")
        print(chatbot_data['activeModel'])
        model = ChatOpenAI(
            temperature=0.5,
            model_name=chatbot_data['activeModel'],
            openai_api_key=openai_api_key,
        )

        firestore_config = {
            "collection_name": "chathistory",
            "session_id": session_id,
            "user_id": uid,
        }

        chat_history = FirestoreChatMessageHistory(**firestore_config)

        memory = ConversationBufferWindowMemory(
            chat_memory=chat_history,
            memory_key="chat_history",
            return_messages=True
        )

        # retrieval qa chain
        qa = RetrievalQA.from_chain_type(
            llm=model,
            chain_type="stuff",
            retriever=vectorstore.as_retriever()
        )

        qa_tool = Tool(
            name='Knowledge Base',
            func=qa.run,
            description=(
                'use this tool when answering general knowledge queries to get '
                'more information about the topic'
            )
        )

        tools = [qa_tool]

        agent = initialize_agent(
            agent='chat-conversational-react-description',
            tools=tools,
            llm=model,
            verbose=True,
            max_iterations=3,
            early_stopping_method='generate',
            memory=memory,
        )
    except Exception as e:
        print(f"An error occurred: {e}")

此代码确保 chat_history 已正确初始化并在 ConversationBufferWindowMemory1(https://github.com/langchain-ai/langchain/issues/23563)2(https://github.com/langchain-ai/langchain/blob/master/libs/community/tests/integration_tests/memory/test_firestore.py) 中使用。

  • 要继续与 Dosu 对话,请提及 @dosu。*

相关问题