langchain (LCEL) v0.2文档(11)如何检查可运行文件(中英对照)

60 阅读1分钟

How to inspect runnables

Once you create a runnable with LangChain Expression Language, you may often want to inspect it to get a better sense for what is going on. This notebook covers some methods for doing so.

使用 LangChain 表达式语言创建可运行程序后,您可能经常需要检查它以更好地了解正在发生的情况。本笔记本介绍了一些这样做的方法。

This guide shows some ways you can programmatically introspect the internal steps of chains. If you are instead interested in debugging issues in your chain, see this section instead.

本指南展示了一些可以通过编程方式内省链的内部步骤的方法。如果您对调试链中的问题感兴趣,请参阅本节。

First, let's create an example chain. We will create one that does retrieval:

%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 Reference: FAISS | StrOutputParser | ChatPromptTemplate | RunnablePassthrough | ChatOpenAI | OpenAIEmbeddings

Get a graph

You can use the get_graph() method to get a graph representation of the runnable:

您可以使用 get_graph() 方法来获取可运行程序的图形表示

chain.get_graph()

Print a graph

While that is not super legible, you can use the print_ascii() method to show that graph in a way that's easier to understand:

虽然这不是超级清晰,但您可以使用 print_ascii() 方法以更容易理解的方式显示该图表:

chain.get_graph().print_ascii()
           +---------------------------------+         
           | Parallel<context,question>Input |         
           +---------------------------------+         
                    **               **                
                 ***                   ***             
               **                         **           
+----------------------+              +-------------+  
| VectorStoreRetriever |              | Passthrough |  
+----------------------+              +-------------+  
                    **               **                
                      ***         ***                  
                         **     **                     
           +----------------------------------+        
           | Parallel<context,question>Output |        
           +----------------------------------+        
                             *                         
                             *                         
                             *                         
                  +--------------------+               
                  | ChatPromptTemplate |               
                  +--------------------+               
                             *                         
                             *                         
                             *                         
                      +------------+                   
                      | ChatOpenAI |                   
                      +------------+                   
                             *                         
                             *                         
                             *                         
                   +-----------------+                 
                   | StrOutputParser |                 
                   +-----------------+                 
                             *                         
                             *                         
                             *                         
                +-----------------------+              
                | StrOutputParserOutput |              
                +-----------------------+

Get the prompts

You may want to see just the prompts that are used in a chain with the get_prompts() method:

chain.get_prompts()