RAG:将检索到的整个文档注入到提示中,而不是仅注入 page_content

问题描述 投票:0回答:1

当使用rag chain时,我意识到整个

Document
都被注入到提示符中。直接传递文档的
page_content
不是更有意义吗?我担心看到
[Document(page_content='
会以意想不到的方式影响结果。

from operator import itemgetter

from langchain_community.vectorstores import FAISS
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableLambda, RunnablePassthrough
from langchain_openai import ChatOpenAI, OpenAIEmbeddings

vectorstore = FAISS.from_texts(
    ["harrison worked at kensho", "harrison worked for 3 years"], embedding=OpenAIEmbeddings()
)
retriever = vectorstore.as_retriever()

template = """Answer the question based only on the following context:
{context}

Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)

model = ChatOpenAI()

chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
)

chain.invoke("where did harrison work?")
ChatPromptValue(messages=[HumanMessage(content="Answer the question based only on the following context:\n[Document(page_content='harrison worked for 3 years'), Document(page_content='harrison worked at kensho')]\n\nQuestion: where did harrison work?\n")])
nlp langchain
1个回答
0
投票

为了解决这个问题,我使用内置的 Jinja 2 模板渲染:

prompt = ChatPromptTemplate.from_template(template, template_format="jinja2")

然后在提示模板中:

CONTEXT: {% for doc in context %}
             {{ doc.page_content }}
         {% endfor %}

USER: {{ question }}
© www.soinside.com 2019 - 2024. All rights reserved.