ValueError: 当不存在一个输入键时不支持 `run`,得到 ['question', 'documents']

2023-12-27

尝试运行 langchain 代码时出现错误。

ValueError: `run` not supported when there is not exactly one input key, got ['question', 'documents'].
Traceback:
File "c:\users\aviparna.biswas\appdata\local\programs\python\python37\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
File "D:\Python Projects\POC\Radium\Ana\app.py", line 49, in <module>
    answer = question_chain.run(formatted_prompt)
File "c:\users\aviparna.biswas\appdata\local\programs\python\python37\lib\site-packages\langchain\chains\base.py", line 106, in run
    f"`run` not supported when there is not exactly one input key, got ['question', 'documents']."

我的代码如下。

import os
from apikey import apikey

import streamlit as st
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SequentialChain
#from langchain.memory import ConversationBufferMemory
from docx import Document

os.environ['OPENAI_API_KEY'] = apikey

# App framework
st.title('???????? Colab Ana Answering Bot..')
prompt = st.text_input('Plug in your question here')


# Upload multiple documents
uploaded_files = st.file_uploader("Choose your documents (docx files)", accept_multiple_files=True, type=['docx'])
document_text = ""

# Read and combine Word documents
def read_docx(file):
    doc = Document(file)
    full_text = []
    for paragraph in doc.paragraphs:
        full_text.append(paragraph.text)
    return '\n'.join(full_text)

for file in uploaded_files:
    document_text += read_docx(file) + "\n\n"

with st.expander('Contextual Prompt'):
    st.write(document_text)

# Prompt template
question_template = PromptTemplate(
    input_variables=['question', 'documents'],
    template='Given the following documents: {documents}. Answer the question: {question}'
)

# Llms
llm = OpenAI(temperature=0.9)
question_chain = LLMChain(llm=llm, prompt=question_template, verbose=True, output_key='answer')

# Show answer if there's a prompt and documents are uploaded
if prompt and document_text:
    formatted_prompt = question_template.format(question=prompt, documents=document_text)
    answer = question_chain.run(formatted_prompt)
    st.write(answer['answer'])

我已经阅读了文档,但即使如此我也遇到了同样的错误。我已经看过 langchain 采取多个提示的演示。


对于具有多个输入的提示,请使用predict()代替run(),或者直接调用链。 (注:需要Python 3.8+)

prompt_template = "Tell me a {adjective} joke and make it include a {profession}"
llm_chain = LLMChain(
    llm=OpenAI(temperature=0.5),
    prompt=PromptTemplate.from_template(prompt_template)
)

# Option 1
llm_chain(inputs={"adjective": "corny", "profession": "plumber"})

# Option 2
llm_chain.predict(adjective="corny", profession="plumber")

另请注意,您只需分配PromptTemplate现在你正在实例化LLMChain- 之后你只需传入模板变量 - 在你的情况下,documents and question(而不是像您当前那样传递格式化模板)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ValueError: 当不存在一个输入键时不支持 `run`,得到 ['question', 'documents'] 的相关文章

  • pip 安装最新的依赖版本

    当我使用安装包时pip install e 它仅安装不满足的依赖项并忽略依赖项升级 如何在每次运行时安装最新的依赖版本pip install e 我尝试过使用pip install upgrade e 但是使用这个选项没有任何改变 我仍然得
  • 使用python编辑html,但是lxml将漂亮的html实体转换为奇怪的编码

    我正在尝试使用 python 带有 pyquery 和 lxml 来更改和清理一些 html Eg html div p It 146 s a spicy meatball p div lxml html clean 函数 clean ht
  • 在 ubuntu 中卸载 python 模块

    我必须删除一个名为 django 的 python 模块 一种流行的模块 因为我安装了错误的版本 1 3 py 2 6 中的 beta 如何卸载这个模块 请解释一下 因为我只在 Windows 中使用过 python 而从未在 Ubuntu
  • 如何检查给定的数字是否是2的幂?

    下面的代码不适用于某些输入 a i set 1 while i lt 10000 a add i i lt lt 1 N int input if N in a print True else print False 我最初的想法是检查每个
  • 清理 MongoDB 的输入

    我正在为 MongoDB 数据库程序编写 REST 接口 并尝试实现搜索功能 我想公开整个 MongoDB 接口 我确实有两个问题 但它们是相关的 所以我将它们放在一篇文章中 使用 Python json 模块解码不受信任的 JSON 是否
  • matplotlib - 将文本包装在图例中

    我目前正在尝试绘制一些pandas数据通过matplotlib seaborn 然而我的一个专栏标题特别长 拉长了情节 考虑以下示例 import random import pandas as pd import matplotlib p
  • 关于具有自定义损失的 3 输出 ANN 的加权

    我正在尝试定义一个自定义损失函数 它在回归模型中接收 3 个输出变量 def custom loss y true y pred y true c K cast y true float32 Shape batch size 3 y pre
  • Python正则表达式替换引号中的文本(引号本身除外)

    例如 我有一个测试字符串 content I opened my mouth Good morning I said cheerfully 我想使用正则表达式删除双语音标记之间的文本 但不删除语音标记本身 所以它会返回 I opened m
  • 如何使用 Python 在表单中选择选项?

    我想知道如何以格式如下的形式选择选项 td align left td
  • 如何停止 PythonShell

    如何终止 停止 Node js 中 PythonShell 执行的 Python 脚本的执行 我在交互模式下运行 输出通过 socket io 发送到给定的房间 如果没有更多的客户端连接到这个房间 我想停止 python 脚本的执行 这是我
  • 私有属性,但却是一个神秘的领域

    我想将属性设为私有 但带有 pydantic 字段 from pydantic import BaseModel Field PrivateAttr validator class A BaseModel a str I want a py
  • python os.fork 使用相同的 python 解释器吗?

    据我所知 Python 中的线程使用相同的 Python 解释器实例 我的问题是与创建的流程相同os fork 或者每个进程创建的os fork有自己的翻译吗 每当你 fork 时 整个 Python 进程都会在内存中复制 包括Python
  • 在添加数据之前使用 Python gdata 清除工作表中的行

    我有一个 Google 电子表格 我使用 python 脚本和 gdata 库填充值 如果我多次运行脚本 它会将新行附加到工作表中 我希望脚本在填充之前首先清除行中的所有数据 这样每次运行时我都会有一组新的数据脚本 我尝试过使用 Updat
  • 从 SUDS 中的 SOAP 响应中提取 Cookie

    我必须使用具有多种服务的 API 所有这些都需要来自下面的身份验证的 JSESSION cookie 然而 当我调用下一个服务时 它不会保留 cookie 因此会拒绝它们 from suds client import Client url
  • 如何在 Jupyter Notebook 中选择 conda 环境

    我安装了 Anaconda 5 3 和 Python 3 7 根环境 之后我使用 Python 3 6 创建了一个新环境 py36 我激活了新环境activate py36 conda env list表明环境是活跃的 但是当我启动 Jup
  • model.predict() 返回类而不是概率

    Hello 我是第一次使用 Keras 我训练并保存了一个模型 作为 json 文件及其权重 该模型旨在将图像分为 3 个类别 我的编译方法 model compile loss categorical crossentropy optim
  • 阻止 BeautifulSoup 将我的 XML 标签转换为小写

    我正在使用 BeautifulStoneSoup 来解析 XML 文档并更改一些属性 我注意到它会自动将所有 XML 标签转换为小写 例如我的源文件有
  • 如何使用 Matplotlib 可视化标量二维数据?

    所以我有一个网格网格 矩阵 X 和 Y 以及标量数据 矩阵 Z 我需要将其可视化 最好是一些 2D 图像 在各点处带有颜色 显示 Z 值 我做了一些研究 但没有找到任何能完全满足我想要的效果的东西 pyplot imshow Z 看起来不错
  • Python 子进程:无法转义引号

    我知道以前曾问过类似的问题 但它们似乎都是通过重新设计参数的传递方式 即使用列表等 来解决的 但是 我这里有一个问题 因为我没有这个选项 有一个特定的命令行程序 我使用的是 Bash shell 我必须向其传递带引号的字符串 它不能不被引用
  • python:日志记录:我们可以向记录器添加多个过滤器吗?考虑哪一个

    我试图了解 Python 日志记录中的多个过滤器 一个在配置中定义 另一个在代码中定义 如何工作 我正在开发一个 Django 项目 下面是我在 settings py 中的记录器配置 我的目标是switch on and switch o

随机推荐