问题背景:
I've searched all over langchain documentation on their official website but I didn't find how to create a langchain doc from a str variable in python so I searched in their GitHub code and I found this :
在 langchain 的官方文档中,我并没有直接找到如何从 Python 中的字符串变量创建 langchain 文档的具体指导。所以,我在 GitHub 代码库中找到了相关的线索。
doc=Document(
page_content="text",
metadata={"source": "local"}
)
PS: I added the metadata attribute
then I tried using that doc with my chain:
Memory and Chain:
PS: 我添加了元数据属性
然后我尝试使用那个文档与我的链进行交互:
内存和链:
memory = ConversationBufferMemory(memory_key="chat_history", input_key="human_input")
chain = load_qa_chain(
llm, chain_type="stuff", memory=memory, prompt=prompt
)
the call method:
chain({"input_documents": doc, "human_input": query})
prompt template: 提示模板
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
)
but I am getting the following error: 但我遇到了以下错误:
AttributeError: 'tuple' object has no attribute 'page_content'
when I tried to check the type and the page content of the Document object before using it with the chain I got this
当我试图在使用链之前检查Document对象的类型和页面内容时,我得到了这个
print(type(doc))
<class 'langchain.schema.Document'>
print(doc.page_content)
"text"
问题解决:
This worked for me: 我使用这种方法有效果
from langchain.docstore.document import Document
doc = Document(page_content="text", metadata={"source": "local"})