当使用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")])
为了解决这个问题,我使用内置的 Jinja 2 模板渲染:
prompt = ChatPromptTemplate.from_template(template, template_format="jinja2")
然后在提示模板中:
CONTEXT: {% for doc in context %}
{{ doc.page_content }}
{% endfor %}
USER: {{ question }}