LangChain学习文档

  • Chains(链)
    • 【LangChain】不同的调用方式(Different call methods)
    • 【LangChain】自定义chain
    • 【LangChain】调试Chain(Debugging chains)
    • 【LangChain】从LangChainHub加载(Loading from LangChainHub)

概述

LangChainHub 可以理解为LangChain 工具包或者说组件中心。里面提供了高质量的组件方便使用。

他的笔记本介绍了如何从 LangChainHub 加载Chain。

内容

from langchain.chains import load_chainchain = load_chain("lc://chains/llm-math/chain.json")chain.run("whats 2 raised to .12")

结果:

> Entering new LLMMathChain chain...whats 2 raised to .12Answer: 1.0791812460476249> Finished chain.'Answer: 1.0791812460476249'

有的时候,我们需要使用一些专门的库。比如:需要回答向量相关问题时,我们需要向量知识库。

from langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.vectorstores import Chromafrom langchain.text_splitter import CharacterTextSplitterfrom langchain import OpenAI, VectorDBQAfrom langchain.document_loaders import TextLoader# 加载制定的知识库loader = TextLoader("../../state_of_the_union.txt")# 转成Documentdocuments = loader.load()# 文本序列化对象text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)# 对文档(知识库)进行序列化texts = text_splitter.split_documents(documents)# 指定向量嵌入embeddings = OpenAIEmbeddings()# 转成可被搜索的对象vectorstore = Chroma.from_documents(texts, embeddings)# 第一个参数是lc://chains/vector-db-qa/stuff/chain.json,就是从LangChainHub中加载文件# 里面配置的是llmchain = load_chain("lc://chains/vector-db-qa/stuff/chain.json", vectorstore=vectorstore)query = "What did the president say about Ketanji Brown Jackson"chain.run(query)

结果:

" The president said that Ketanji Brown Jackson is a Circuit Court of Appeals Judge, one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans, and will continue Justice Breyer's legacy of excellence."

总结

这篇主要就是告诉我们,lc://chains/llm-math/chain.json,这个可以从LangChainHub中加载数据。

lc://chains/llm-math/chain.json 实际地址:https://github.com/hwchase17/langchain-hub/blob/master/chains/llm-math/chain.json

lc://chains/vector-db-qa/stuff/chain.json 的实际地址:https://github.com/hwchase17/langchain-hub/blob/master/chains/vector-db-qa/stuff/chain.json

参考地址:

Loading from LangChainHub