如何检查可运行对象
先决条件
本指南假定读者熟悉以下概念:
一旦你使用 LangChain 表达式语言 创建了一个可运行的程序,你通常可能希望对其进行检查,以更好地了解其内部运作。本笔记本介绍了几种实现此目的的方法。
本指南展示了一些可以编程方式检查链内部步骤的方法。如果您更关注调试链中的问题,请参阅 此部分。
首先,让我们创建一个示例链。我们将创建一个执行检索的链:
%pip install -qU langchain langchain-openai faiss-cpu tiktoken
from langchain_community.vectorstores import FAISS
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
vectorstore = FAISS.from_texts(
["harrison worked at kensho"], 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
| model
| StrOutputParser()
)
API 参考:FAISS | StrOutputParser | ChatPromptTemplate | RunnablePassthrough | ChatOpenAI | OpenAIEmbeddings
获取图表
您可以使用 get_graph() 方法获取可运行对象的图表示:
chain.get_graph()
打印图表
虽然这不太容易理解,但你可以使用 print_ascii() 方法以更易于理解的方式显示该图表:
chain.get_graph().print_ascii()
+---------------------------------+
| Parallel<context,question>Input |
+---------------------------------+
** **
*** ***
** **
+----------------------+ +-------------+
| VectorStoreRetriever | | Passthrough |
+----------------------+ +-------------+
** **
*** ***
** **
+----------------------------------+
| Parallel<context,question>Output |
+----------------------------------+
*
*
*
+--------------------+
| ChatPromptTemplate |
+--------------------+
*
*
*
+------------+
| ChatOpenAI |
+------------+
*
*
*
+-----------------+
| StrOutputParser |
+-----------------+
*
*
*
+-----------------------+
| StrOutputParserOutput |
+-----------------------+
获取提示
您可能只想查看链中使用的提示,可以使用 get_prompts() 方法:
chain.get_prompts()
[ChatPromptTemplate(input_variables=['context', 'question'], messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['context', 'question'], template='Answer the question based only on the following context:\n{context}\n\nQuestion: {question}\n'))])]
下一步
你现在已了解如何对组合的 LCEL 链进行自我检查。
接下来,查看本部分中有关可运行对象的其他操操作指南,或相关操操作指南中的 调试你的链。