我在官方网站的langchain文档中进行了全面搜索,但没有找到如何在Python中使用一个str变量创建langchain文档的方法,所以我在他们的GitHub代码中进行了搜索,我找到了这个:
doc=Document(
page_content="text",
metadata={"source": "local"}
)
PS:我添加了元数据属性
然后我尝试使用我的链条来处理该文档:
内存和链条:
memory = ConversationBufferMemory(memory_key="chat_history", input_key="human_input")
chain = load_qa_chain(
llm, chain_type="stuff", memory=memory, prompt=prompt
)
调用方法:
chain({"input_documents": doc, "human_input": query})
提示模板:
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
)
但是我遇到了以下错误:
AttributeError: 'tuple' object has no attribute 'page_content'
当我在使用链式操作之前尝试检查文档对象的类型和页面内容时,我得到了这个结果。
print(type(doc))
<class 'langchain.schema.Document'>
print(doc.page_content)
"text"
langchain.schema.document
导入也是可行的,而且后者模块是在API文档中指定的那个。 - Sergii Volchkov