如何从 str 创建 langchain 文档?

Moh*_*ine 18 python nlp langchain large-language-model

我在他们的官方网站上搜索了所有 langchain 文档,但没有找到如何从 python 中的 str 变量创建 langchain 文档,所以我在他们的 GitHub 代码中搜索,发现了这个:

  doc=Document(
                page_content="text",
                metadata={"source": "local"}
            )

Run Code Online (Sandbox Code Playgroud)

PS:我添加了元数据属性
,然后尝试将该文档与我的链一起使用:
内存和链:

memory = ConversationBufferMemory(memory_key="chat_history", input_key="human_input")
chain = load_qa_chain(
    llm, chain_type="stuff", memory=memory, prompt=prompt
)

Run Code Online (Sandbox Code Playgroud)

调用方法:

  chain({"input_documents": doc, "human_input": query})
Run Code Online (Sandbox Code Playgroud)

提示模板:

template = """You are a senior financial analyst analyzing the below document and having a conversation with a human.
{context}
{chat_history}
Human: {human_input}
senior financial analyst:"""

prompt = PromptTemplate(
    input_variables=["chat_history", "human_input", "context"], template=template
)
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

AttributeError: 'tuple' object has no attribute 'page_content'

Run Code Online (Sandbox Code Playgroud)

当我在将 Document 对象与链一起使用之前尝试检查它的类型和页面内容时,我得到了这个

print(type(doc))
<class 'langchain.schema.Document'>
print(doc.page_content)
"text"


Run Code Online (Sandbox Code Playgroud)

小智 32

这对我有用:

from langchain.docstore.document import Document

doc =  Document(page_content="text", metadata={"source": "local"})

Run Code Online (Sandbox Code Playgroud)


小智 2

我遇到了类似的问题,我注意到 API 调用了列表,所以尝试

    doc = Document(page_content=input,
            metatdata={
                "source": "userinput"
            }
        )
    #db.add_documents(doc)
    db.add_documents([doc])
Run Code Online (Sandbox Code Playgroud)