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_chain
chain = load_chain("lc://chains/llm-math/chain.json")
chain.run("whats 2 raised to .12")
结果:
> Entering new LLMMathChain chain...
whats 2 raised to .12
Answer: 1.0791812460476249
> Finished chain.
'Answer: 1.0791812460476249'
有的时候,我们需要使用一些专门的库。比如:需要回答向量相关问题时,我们需要向量知识库。
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import CharacterTextSplitter
from langchain import OpenAI, VectorDBQA
from langchain.document_loaders import TextLoader
# 加载制定的知识库
loader = TextLoader("../../state_of_the_union.txt")
# 转成Document
documents = 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中加载文件
# 里面配置的是llm
chain = 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
参考地址:文章来源:https://www.toymoban.com/news/detail-539150.html
Loading from LangChainHub文章来源地址https://www.toymoban.com/news/detail-539150.html
到了这里,关于【LangChain】从LangChainHub加载(Loading from LangChainHub)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!