跳到主要內容
Open In ColabOpen on GitHub

如何檢查可執行物件

先決條件

本指南假設您熟悉以下概念

一旦您使用 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()
)

取得圖形

您可以使用 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 鏈。

接下來,查看本節中關於可執行物件的其他操作指南,或關於偵錯您的鏈的相關操作指南。


此頁面是否有幫助?