ValueError: 一个输入键预期在具有内存和多个输入的 LangChain 中得到 ['text_one', 'text_two']

pva*_*v16 4 python langchain

我正在尝试使用内存和多个输入在 LangChain 中运行一条链。我能找到的最接近的错误发布在此处,但在那一错误中,他们仅传递一个输入。

这是设置:

from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.memory import ConversationBufferMemory

llm = OpenAI(
    model="text-davinci-003",
    openai_api_key=environment_values["OPEN_AI_KEY"], # Used dotenv to store API key
    temperature=0.9,
    client="",
)

memory = ConversationBufferMemory(memory_key="chat_history")

prompt = PromptTemplate(
    input_variables=[
        "text_one",
        "text_two",
        "chat_history"
    ],
    template=(
        """You are an AI talking to a huamn. Here is the chat
        history so far:

        {chat_history}

        Here is some more text:

        {text_one}

        and here is a even more text:

        {text_two}
        """
    )
)

chain = LLMChain(
    llm=llm,
    prompt=prompt,
    memory=memory,
    verbose=False
)
Run Code Online (Sandbox Code Playgroud)

当我跑步时

output = chain.predict(
    text_one="Hello",
    text_two="World"
)
Run Code Online (Sandbox Code Playgroud)

我明白了ValueError: One input key expected got ['text_one', 'text_two']

我看过这个 stackoverflow 帖子,建议尝试:

output = chain(
    inputs={
        "text_one" : "Hello",
        "text_two" : "World"
    }
)
Run Code Online (Sandbox Code Playgroud)

这给出了完全相同的错误。本着尝试不同事物的精神,我也尝试过:

output = chain.predict( # Also tried .run() here
    inputs={
        "text_one" : "Hello",
        "text_two" : "World"
    }
)
Run Code Online (Sandbox Code Playgroud)

这使Missing some input keys: {'text_one', 'text_two'}

我还在langchain GitHub 上查看了这个问题,它建议将 传递到llm内存中,即

# Everything the same except...
memory = ConversationBufferMemory(llm=llm, memory_key="chat_history") # Note the llm here
Run Code Online (Sandbox Code Playgroud)

我仍然遇到同样的错误。如果有人知道解决此错误的方法,请告诉我。谢谢。

pva*_*v16 9

在起草这个问题时,我找到了答案。

定义memory变量时,传递一个input_key="human_input"并确保每个提示都已human_input定义。

memory=ConversationBufferMemory(
    memory_key="chat_history",
    input_key="human_input"
)
Run Code Online (Sandbox Code Playgroud)

然后,在每个提示中,确保有输入human_input

prompt = PromptTemplate(
    input_variables=[
        "text_one",
        "text_two",
        "chat_history",
        "human_input", # Even if it's blank

    ],
    template=(
        """You are an AI talking to a huamn. Here is the chat
        history so far:

        {chat_history}

        Here is some more text:

        {text_one}

        and here is a even more text:

        {text_two}

        {human_input}
        """
    )
)
Run Code Online (Sandbox Code Playgroud)

然后,构建你的链:

chain = LLMChain(
    llm=llm,
    prompt=prompt,
    memory=memory, # Contains the input_key
    verbose=False
)
Run Code Online (Sandbox Code Playgroud)

然后运行它:

output = chain.predict(
    human_input="", # or whatever you want
    text_one="Hello",
    text_two="World"
)
print(output)
# On my machine, it outputs: '\nAI: Hi there! How can I help you?'
Run Code Online (Sandbox Code Playgroud)