LangChain 入门:构建 LLM 支持的应用程序的初学者指南
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/a77fabbad09941cebfc38ac00434cdce~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723894067&x-signature=2wHLwsMz2kYHIMKJF2mc5Ux4%2F18%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">自 ChatGPT 发布<span style="color: black;">败兴</span>,大型语言模型 (LLM) <span style="color: black;">得到</span>了广泛的欢迎。 尽管您可能<span style="color: black;">无</span>足够的资金和计算资源来在地下室从头<span style="color: black;">起始</span>训练法学硕士,但您仍然<span style="color: black;">能够</span><span style="color: black;">运用</span>预先训练的法学硕士来构建<span style="color: black;">有些</span>很酷的东西,例如:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">能够</span><span style="color: black;">按照</span>您的数据与外界互动的个人助理</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">按照</span>您的目的定制的聊天<span style="color: black;">设备</span>人</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">对您的文档或代码的分析或总结</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">法学硕士正在改变<span style="color: black;">咱们</span>构建人工智能<span style="color: black;">制品</span>的方式</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">凭借其奇怪的 API 和快速的工程,法学硕士正在改变<span style="color: black;">咱们</span>构建人工智能<span style="color: black;">制品</span>的方式。 这<span style="color: black;">便是</span><span style="color: black;">为何</span>新的<span style="color: black;">研发</span>工具以“LLMOps”一词随处可见。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">LangChain<span style="color: black;">便是</span>这些新工具之一。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">什么是LangChain?</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">LangChain 是一个框架,旨在<span style="color: black;">帮忙</span>您更<span style="color: black;">容易</span>地构建 LLM 支持的应用程序,为您<span style="color: black;">供给</span>以下功能:</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">各样</span><span style="color: black;">区别</span><span style="color: black;">基本</span>模型的通用接口(参见模型),</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">帮忙</span>您管理提示的框架(请参阅提示),以及</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">长时间</span>记忆(参见内存)、<span style="color: black;">外边</span>数据(参见索引)、其他法学硕士(参见链)和其他代理的中央接口,用于处理法学硕士<span style="color: black;">没法</span>处理的任务(例如,计算或搜索)(参见代理) 。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">它<span style="color: black;">是由于</span> Harrison Chase 创建的开源项目(GitHub 存储库)。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">因为</span> LangChain <span style="color: black;">拥有</span>许多<span style="color: black;">区别</span>的功能,<span style="color: black;">因此呢</span>一<span style="color: black;">起始</span>理解它的功能可能会很困难。 <span style="color: black;">因此呢</span>,<span style="color: black;">咱们</span>将在本文<span style="color: black;">中间商</span>绍 LangChain(<span style="color: black;">日前</span>)的六个关键模块,以便您更好地<span style="color: black;">认识</span>其功能。</span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">先决<span style="color: black;">要求</span></h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">要<span style="color: black;">根据</span>本教程进行操作,您<span style="color: black;">必须</span>安装 langchain Python 包并准备好<span style="color: black;">运用</span>所有<span style="color: black;">关联</span>的 API 密钥。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">安装LangChain</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在安装 langchain 包之前,请<span style="color: black;">保证</span>您的 Python 版本≥ 3.8.1 且 <4.0。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">要安装 langchain Python 包,您<span style="color: black;">能够</span> pip install 它。</p><span style="color: black;">pip</span> install langchain<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在本教程中,<span style="color: black;">咱们</span>使用版本 0.0.147。 GitHub 存储库非常活跃; <span style="color: black;">因此呢</span>,请<span style="color: black;">保证</span>您<span style="color: black;">持有</span>最新版本。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">完成所有设置后,导入 langchain Python 包。</span></p><span style="color: black;">import</span> langchain<h1 style="color: black; text-align: left; margin-bottom: 10px;">API 密钥</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>法学硕士构建应用程序<span style="color: black;">必须</span>您想要<span style="color: black;">运用</span>的某些服务的 API 密钥,并且某些 API <span style="color: black;">拥有</span><span style="color: black;">关联</span>成本。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LLM <span style="color: black;">供给</span>商(必需)— 您<span style="color: black;">首要</span><span style="color: black;">必须</span>您想要<span style="color: black;">运用</span>的 LLM <span style="color: black;">供给</span>商的 API 密钥。 <span style="color: black;">咱们</span><span style="color: black;">日前</span>正在经历“AI 的 Linux 时刻”,<span style="color: black;">研发</span>人员<span style="color: black;">必要</span><span style="color: black;">重点</span>基于性能和成本之间的权衡,在专有或开源<span style="color: black;">基本</span>模型之间进行<span style="color: black;">选取</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">专有模型是闭源<span style="color: black;">基本</span>模型,由<span style="color: black;">持有</span>大型专家团队和<span style="color: black;">海量</span>人工智能预算的<span style="color: black;">机构</span><span style="color: black;">持有</span>。 它们<span style="color: black;">一般</span>比开源模型更大,<span style="color: black;">因此呢</span><span style="color: black;">拥有</span>更好的性能,但它们<span style="color: black;">亦</span>有昂贵的 API。 专有模型<span style="color: black;">供给</span>商的示例<span style="color: black;">包含</span> OpenAI、co:here、AI21 Labs 或 Anthropic。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">大<span style="color: black;">都数</span>可用的 LangChain 教程都<span style="color: black;">运用</span> OpenAI,但请<span style="color: black;">重视</span>,OpenAI API(<span style="color: black;">针对</span>实验<span style="color: black;">来讲</span>并不昂贵,但它)不是免费的。 要获取 OpenAI API 密钥,您<span style="color: black;">必须</span>一个 OpenAI 帐户,<span style="color: black;">而后</span>在 API 密钥下“创建新密钥”。</p><span style="color: black;">import</span> os
os.environ[<span style="color: black;">"OPENAI_API_KEY"</span>] = ... <span style="color: black;"># insert your API_TOKEN here</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">开源模型</span></strong><span style="color: black;"><span style="color: black;">一般</span>是较小的模型,其功能比专有模型低,但它们比专有模型更具成本效益。 开源模型的示例有:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">BLOOM 由 BigScience <span style="color: black;">供给</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Meta AI 的 LLaMA</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Google 的果馅饼 T5</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Eleuther AI 的 GPT-J</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">许多开源模型都在 Hugging Face 上<span style="color: black;">做为</span>社区中心进行组织和托管。 要获取 Hugging Face API 密钥,您<span style="color: black;">必须</span>一个 Hugging Face 帐户并在<span style="color: black;">拜访</span>令牌下创建一个“新令牌”。</span></p><span style="color: black;">import</span> os
os.environ[<span style="color: black;">"HUGGINGFACEHUB_API_TOKEN"</span>] = ... <span style="color: black;"># insert your API_TOKEN here</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">您可<span style="color: black;">以避免</span>费为开源LLM<span style="color: black;">运用</span> Hugging Face,但您将仅限于性能较低的小型LLM。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">个人说明 - 让<span style="color: black;">咱们</span>老实说:当然,您<span style="color: black;">能够</span>在<span style="color: black;">这儿</span>尝试开源<span style="color: black;">基本</span>模型。 我尝试仅<span style="color: black;">运用</span> Hugging Face 上托管的开源模型(可<span style="color: black;">经过</span>常规帐户(google/flan-t5-xl 和句子转换器/all-MiniLM-L6-v2)<span style="color: black;">运用</span>本教程)。 它适用于大<span style="color: black;">都数</span>示例,但让某些示例发挥<span style="color: black;">功效</span><span style="color: black;">亦</span>很痛苦。 最后,我扣动扳机并为 OpenAI 设置了一个付费帐户,<span style="color: black;">由于</span> LangChain 的大<span style="color: black;">都数</span>示例似乎都针对 OpenAI 的 API 进行了优化。 为本教程运行<span style="color: black;">有些</span>实验总共花费了我大约 1 美元。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">矢量数据库(可选)- <span style="color: black;">倘若</span>您想<span style="color: black;">运用</span>特定的矢量数据库,例如 Pinecone、Weaviate 或 Milvus,您<span style="color: black;">必须</span>向<span style="color: black;">她们</span>注册以<span style="color: black;">得到</span> API 密钥并<span style="color: black;">检测</span>其定价。 在本教程中,<span style="color: black;">咱们</span><span style="color: black;">运用</span> Faiss,它不<span style="color: black;">必须</span>注册。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">工具(可选)- <span style="color: black;">按照</span>您<span style="color: black;">期盼</span> LLM 与之交互的工具(例如 OpenWeatherMap 或 SerpAPI),您可能<span style="color: black;">必须</span>向它们注册以<span style="color: black;">得到</span> API 密钥并<span style="color: black;">检测</span>其定价。 在本教程中,<span style="color: black;">咱们</span>仅<span style="color: black;">运用</span>不<span style="color: black;">必须</span> API 密钥的工具。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">LangChain<span style="color: black;">能够</span>做什么?</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">该包为许多<span style="color: black;">基本</span>模型<span style="color: black;">供给</span>了通用接口,支持提示管理,并<span style="color: black;">经过</span>代理充当其他组件(如提示模板、其他 LLM、<span style="color: black;">外边</span>数据和其他工具)的中央接口。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">截止</span>撰写本文时,LangChain(版本0.0.147)涵盖六个模块:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">模型:从<span style="color: black;">区别</span>的法学硕士和嵌入模型中进行<span style="color: black;">选取</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">提示:管理 LLM 输入</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">链:将LLM与其他组件相结合</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">索引:<span style="color: black;">拜访</span><span style="color: black;">外边</span>数据</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">记忆:记住以前的对话</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代理:<span style="color: black;">拜访</span>其他工具</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以下部分的代码示例是从 LangChain 文档中复制和修改的。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">模型:从<span style="color: black;">区别</span>的LLM和嵌入模型中进行<span style="color: black;">选取</span></h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">日前</span>,许多<span style="color: black;">区别</span>的法学硕士正在涌现。 LangChain <span style="color: black;">供给</span>了与多种模型的集成以及所有模型的简化界面。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LangChain 区分了三种类型的模型,它们的输入和输出有所<span style="color: black;">区别</span>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LLM 将字符串<span style="color: black;">做为</span>输入(提示)并输出字符串(完成)。</p><span style="color: black;"># Proprietary LLM from e.g. OpenAI</span>
<span style="color: black;"># pip install openai</span>
<span style="color: black;">from</span> langchain.llms <span style="color: black;">import</span> OpenAI
llm = OpenAI(model_name=<span style="color: black;">"text-davinci-003"</span>)
<span style="color: black;"># Alternatively, open-source LLM hosted on Hugging Face</span>
<span style="color: black;"># pip install huggingface_hub</span>
<span style="color: black;">from</span>langchain<span style="color: black;">import</span> HuggingFaceHub
llm = HuggingFaceHub(repo_id = <span style="color: black;">"google/flan-t5-xl"</span>)
<span style="color: black;"># The LLM takes a prompt as an input and outputs a completion</span>
prompt = <span style="color: black;">"Alice has a parrot. What animal is Alices pet?"</span>
completion = llm(prompt)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">聊天模型与LLM类似。 <span style="color: black;">她们</span>将聊天<span style="color: black;">信息</span>列表<span style="color: black;">做为</span>输入并返回聊天<span style="color: black;">信息</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">文本嵌入模型接受文本输入并返回浮点数(嵌入)列表,它们是输入文本的数字<span style="color: black;">暗示</span>形式。 嵌入有助于从文本中提取信息。 <span style="color: black;">而后</span><span style="color: black;">能够</span>稍后<span style="color: black;">运用</span>该信息,例如,用于计算文本之间的<span style="color: black;">类似</span>性(例如,电影摘要)。</span></p><span style="color: black;"># Proprietary text embedding model from e.g. OpenAI</span>
<span style="color: black;"># pip install tiktoken</span>
<span style="color: black;">from</span> <span style="color: black;">langchain.embeddings import OpenAIEmbeddings</span>
<span style="color: black;">embeddings</span> = <span style="color: black;">OpenAIEmbeddings()</span>
<span style="color: black;">
# Alternatively, open-source text embedding model hosted on Hugging Face</span>
<span style="color: black;"># pip install sentence_transformers</span>
<span style="color: black;">from</span> <span style="color: black;">langchain.embeddings import HuggingFaceEmbeddings</span>
<span style="color: black;">embeddings</span> = <span style="color: black;">HuggingFaceEmbeddings(model_name = "sentence-transformers/all-MiniLM-L6-v2")</span>
<span style="color: black;"># The embeddings model takes a text as an input and outputs a list of floats</span>
<span style="color: black;">text</span> = <span style="color: black;">"Alice has a parrot. What animal is Alices pet?"</span>
<span style="color: black;">text_embedding</span> = <span style="color: black;">embeddings.embed_query(text)</span>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">提示:管理 LLM 输入</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LLM 有奇怪的 API。 尽管用自然语言向法学硕士输入提示应该很直观,但<span style="color: black;">必须</span>对提示进行<span style="color: black;">海量</span><span style="color: black;">调节</span><span style="color: black;">才可</span>从法学硕士<span style="color: black;">得到</span>所需的输出。 这个过程<span style="color: black;">叫作</span>为即时工程。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">一旦有了好的提示,您可能想将其用作其他目的的模板。 <span style="color: black;">因此呢</span>,LangChain 为您<span style="color: black;">供给</span>了<span style="color: black;">所说</span>的 PromptTemplates,它<span style="color: black;">能够</span><span style="color: black;">帮忙</span>您从多个组件构建提示。</p>from langchain <span style="color: black;">import</span> PromptTemplate
<span style="color: black;">template</span> = <span style="color: black;">"What is a good name for a company that makes {product}?"</span>prompt = PromptTemplate(
input_variables=[<span style="color: black;">"product"</span>],
<span style="color: black;">template</span>=<span style="color: black;">template</span>,
)
prompt.format(product=<span style="color: black;">"colorful socks"</span>)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">以上</span>提示<span style="color: black;">能够</span>被视为零样本问题设置,您<span style="color: black;">期盼</span>法学硕士接受足够<span style="color: black;">关联</span>数据的培训以<span style="color: black;">供给</span>令人满意的响应。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">加强</span> LLM 输出的另一个技巧是在提示中添加<span style="color: black;">有些</span>示例,并使其<span style="color: black;">作为</span><span style="color: black;">少许</span>问题设置。</span></p><span style="color: black;">from</span> langchain <span style="color: black;">import</span> PromptTemplate, FewShotPromptTemplate
examples = [
{<span style="color: black;">"word"</span>: <span style="color: black;">"happy"</span>, <span style="color: black;">"antonym"</span>: <span style="color: black;">"sad"</span>},
{<span style="color: black;">"word"</span>: <span style="color: black;">"tall"</span>, <span style="color: black;">"antonym"</span>: <span style="color: black;">"short"</span>},
]
example_template = <span style="color: black;">"""
Word: {word}
Antonym: {antonym}\n
"""</span>example_prompt = PromptTemplate(
input_variables=[<span style="color: black;">"word"</span>, <span style="color: black;">"antonym"</span>],
template=example_template,
)
few_shot_prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
prefix=<span style="color: black;">"Give the antonym of every input"</span>,
suffix=<span style="color: black;">"Word: {input}\nAntonym:"</span>,
input_variables=[<span style="color: black;">"input"</span>],
example_separator=<span style="color: black;">"\n"</span>,
)
few_shot_prompt.format(input=<span style="color: black;">"big"</span>)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">上面的代码将生成一个提示模板,并<span style="color: black;">按照</span><span style="color: black;">供给</span>的示例和输入<span style="color: black;">构成</span>以下提示:</span></p><span style="color: black;">Give</span> <span style="color: black;">the antonym of every input</span>
<span style="color: black;">Word</span>: <span style="color: black;">happy</span>
<span style="color: black;">Antonym</span>: <span style="color: black;">sad</span>
<span style="color: black;">Word</span>: <span style="color: black;">tall</span>
<span style="color: black;">Antonym</span>: <span style="color: black;">short</span>
<span style="color: black;">Word</span>: <span style="color: black;">big</span>
<span style="color: black;">Antonym</span>:<h1 style="color: black; text-align: left; margin-bottom: 10px;">链:将LLM与其他组件相结合</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LangChain中的链接简单地描述了将LLM与其他组件结合起来创建应用程序的过程。 <span style="color: black;">有些</span>例子是:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">将 LLM 与提示模板相结合(请参阅本节)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>将第一个 LLM 的输出<span style="color: black;">做为</span>第二个 LLM 的输入,按<span style="color: black;">次序</span>组合多个 LLM(请参阅本节)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">将LLM与<span style="color: black;">外边</span>数据相结合,例如用于回答问题(参见索引)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">将LLM与<span style="color: black;">长时间</span>记忆相结合,例如聊天记录(请参阅记忆)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在上一节中,<span style="color: black;">咱们</span>创建了一个提示模板。 当<span style="color: black;">咱们</span>想将其与 LLM <span style="color: black;">一块</span><span style="color: black;">运用</span>时,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span> LLMChain,如下所示:</p><span style="color: black;">from</span> langchain.chains <span style="color: black;">import</span> LLMChain
chain = LLMChain(llm = llm,
prompt = prompt)
<span style="color: black;"># Run the chain only specifying the input variable.</span>chain.run(<span style="color: black;">"colorful socks"</span>)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span><span style="color: black;">咱们</span>想<span style="color: black;">运用</span><span style="color: black;">第1</span>个 LLM 的输出<span style="color: black;">做为</span>第二个 LLM 的输入,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span> SimpleSequentialChain:</span></p>from langchain.chains import LLMChain, SimpleSequentialChain
<span style="color: black;"># Define the first chain as in the previous code example</span>
<span style="color: black;"># ...</span>
<span style="color: black;"># Create a second chain with a prompt template and an LLM</span>second_prompt = PromptTemplate(
input_variables=[<span style="color: black;">"company_name"</span>],
template=<span style="color: black;">"Write a catchphrase for the following company: {company_name}"</span>,
)
chain_two = LLMChain(llm=llm, prompt=second_prompt)<span style="color: black;"># Combine the first and the second chain </span>
overall_chain = SimpleSequentialChain(chains=, verbose=True)
<span style="color: black;"># Run the chain specifying only the input variable for the first chain.</span>
catchphrase = overall_chain.run(<span style="color: black;">"colorful socks"</span>)<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/96fec699208548b083f443d7f70a0f04~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723894067&x-signature=0gP%2FdL3WHd4iBsHPPnwYo3Es7zE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">索引:<span style="color: black;">拜访</span><span style="color: black;">外边</span>数据</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LLM的局限性之一是缺乏上下文信息(例如,<span style="color: black;">拜访</span>某些特定文档或电子邮件)。 您<span style="color: black;">能够</span><span style="color: black;">经过</span>让LLM<span style="color: black;">拜访</span>特定的<span style="color: black;">外边</span>数据来<span style="color: black;">处理</span>这个问题。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">为此,您<span style="color: black;">首要</span><span style="color: black;">必须</span><span style="color: black;">运用</span>文档加载器加载<span style="color: black;">外边</span>数据。 LangChain 为<span style="color: black;">区别</span>类型的文档<span style="color: black;">供给</span>了多种加载器,从 PDF 和电子邮件到网站和 YouTube 视频。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">让<span style="color: black;">咱们</span>从 YouTube 视频加载<span style="color: black;">有些</span><span style="color: black;">外边</span>数据。 <span style="color: black;">倘若</span>你想加载一个大文本文档并<span style="color: black;">运用</span>文本分割器分割它,<span style="color: black;">能够</span>参考官方文档。</p><span style="color: black;"># pip install youtube-transcript-api</span>
<span style="color: black;"># pip install pytube</span>
<span style="color: black;">from</span> langchain.document_loaders <span style="color: black;">import</span>YoutubeLoader
loader = YoutubeLoader.from_youtube_url(<span style="color: black;">"https://www.youtube.com/watch?v=dQw4w9WgXcQ"</span>)
documents = loader.load()<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">此刻</span>您已准备好将<span style="color: black;">外边</span>数据<span style="color: black;">做为</span>文档<span style="color: black;">运用</span>,您<span style="color: black;">能够</span><span style="color: black;">运用</span>矢量数据库(VectorStore)中的文本嵌入模型(请参阅模型)对其进行索引。 流行的载体数据库<span style="color: black;">包含</span> Pinecone、Weaviate 和 Milvus。 在本文中,<span style="color: black;">咱们</span><span style="color: black;">运用</span> Faiss,<span style="color: black;">由于</span>它不<span style="color: black;">必须</span> API 密钥。</span></p><span style="color: black;"># pip install faiss-cpu</span>
<span style="color: black;">from</span> langchain.vectorstores <span style="color: black;">import</span> FAISS
<span style="color: black;"># create the vectorestore to use as the index</span>
db = FAISS.from_documents(documents, embeddings)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">您的文档(在本例中为视频)<span style="color: black;">此刻</span><span style="color: black;">做为</span>嵌入存储在矢量存储中。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">此刻</span>您<span style="color: black;">能够</span><span style="color: black;">运用</span>这些<span style="color: black;">外边</span>数据执行多种操作。 让<span style="color: black;">咱们</span>将它用于信息检索器的问答任务:</span></p><span style="color: black;">from</span> <span style="color: black;">langchain.chains import RetrievalQA</span>
<span style="color: black;">retriever</span> = <span style="color: black;">db.as_retriever()</span>
<span style="color: black;">qa</span> = <span style="color: black;">RetrievalQA.from_chain_type(</span>
<span style="color: black;">llm</span>=<span style="color: black;">llm, </span>
<span style="color: black;">chain_type</span>=<span style="color: black;">"stuff", </span>
<span style="color: black;">retriever</span>=<span style="color: black;">retriever, </span>
<span style="color: black;">return_source_documents</span>=<span style="color: black;">True)</span>
<span style="color: black;">query</span> = <span style="color: black;">"What am I never going to do?"</span>
<span style="color: black;">result</span> = <span style="color: black;">qa({"query": query})</span>
<span style="color: black;">print(result)</span>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/d95bbdbeb30e4781874af82bff6324af~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723894067&x-signature=71bQfVT58fo5AeS2LvJqd%2FZVWFE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">RetrievalQA 的输出</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">等一下——你<span style="color: black;">刚才</span>被滚了吗? 是的,你做到了。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">记忆:记住以前的对话</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">针对</span>聊天<span style="color: black;">设备</span>人等应用程序<span style="color: black;">来讲</span>,它们能够记住之前的对话至关重要。 但默认<span style="color: black;">状况</span>下,LLM<span style="color: black;">无</span>任何<span style="color: black;">长时间</span>记忆,除非你输入聊天记录。</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/3f656afad1514f9f8d76f1ad2a3ff07d~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723894067&x-signature=pdJ0IgVdakWAJmcUbNiSMGXgeg0%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">LangChain <span style="color: black;">经过</span><span style="color: black;">供给</span>几种<span style="color: black;">区别</span>的选项来处理聊天记录来<span style="color: black;">处理</span>这个问题:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">保存</span>所有对话,</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">保存</span>最新的k条对话,</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">总结谈话内容。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这里</span>示例中,<span style="color: black;">咱们</span>将<span style="color: black;">运用</span> ConversationChain 为该应用程序<span style="color: black;">供给</span>会话内存。</span></p>from langchain import ConversationChain
conversation = ConversationChain(llm=llm, verbose=True)
conversation.predict(<span style="color: black;">input</span>=<span style="color: black;">"Alice has a parrot."</span>)
conversation.predict(<span style="color: black;">input</span>=<span style="color: black;">"Bob has two cats."</span>)
conversation.predict(<span style="color: black;">input</span>=<span style="color: black;">"How many pets do Alice and Bob have?"</span>)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这将<span style="color: black;">引起</span>上图中右侧的对话。 <span style="color: black;">倘若</span><span style="color: black;">无</span> ConversationChain 来<span style="color: black;">保留</span>对话记忆,对话将类似于上图左侧的对话。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">代理:<span style="color: black;">拜访</span>其他工具</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">尽管LLM非常强大,但<span style="color: black;">亦</span>有<span style="color: black;">有些</span>局限性:它们缺乏上下文信息(例如,<span style="color: black;">没法</span><span style="color: black;">拜访</span>训练数据中未<span style="color: black;">包括</span>的特定知识),它们可能<span style="color: black;">火速</span>就会过时(例如,GPT-4 在 2021 年 9 月之前接受过数据训练),并且它们可能会<span style="color: black;">火速</span>过时。 数学<span style="color: black;">欠好</span>。</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/58dd4832d3e040109ada0b6a0a9c7272~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723894067&x-signature=yOY7ottN6aKDmwXxSgZVaKjDDUQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因为</span>LLM可能会幻想自己<span style="color: black;">没法</span>完成的任务,<span style="color: black;">因此呢</span><span style="color: black;">咱们</span><span style="color: black;">必须</span>为<span style="color: black;">她们</span><span style="color: black;">供给</span><span style="color: black;">弥补</span>工具,例如搜索(例如 Google 搜索)、计算器(例如 Python REPL 或 Wolfram Alpha)和<span style="color: black;">查询</span>(例如, 维基百科)。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">另外</span>,<span style="color: black;">咱们</span><span style="color: black;">必须</span>代理<span style="color: black;">按照</span>LLM的输出来决定<span style="color: black;">运用</span><span style="color: black;">那些</span>工具来完成任务。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">请<span style="color: black;">重视</span>,某些法学硕士(例如 google/flan-t5-xl)不适合以下示例,<span style="color: black;">由于</span>它们不遵循</p>conversational-react-description 模板。 对我<span style="color: black;">来讲</span>,这<span style="color: black;">便是</span>我在 OpenAI 上设置付费帐户并切换到 OpenAI API 的时刻。
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下面是一个例子,代理首先在维基百科上<span style="color: black;">查询</span>巴拉克·奥巴马的出生日期,<span style="color: black;">而后</span>用计算器计算出他在 2022 年的年龄。</span></p><span style="color: black;"># pip install wikipedia</span>
<span style="color: black;">from</span> langchain.agents <span style="color: black;">import</span> load_tools
<span style="color: black;">from</span> langchain.agents <span style="color: black;">import</span> initialize_agent
<span style="color: black;">from</span>langchain.agents<span style="color: black;">import</span> AgentType
tools = load_tools([<span style="color: black;">"wikipedia"</span>, <span style="color: black;">"llm-math"</span>], llm=llm)
agent = initialize_agent(tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=<span style="color: black;">True</span>)
agent.run(<span style="color: black;">"When was Barack Obama born? How old was he in 2022?"</span>)<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/2ce5563b4f144e66b2788f3b8f7e9c9f~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723894067&x-signature=Nu094%2BuXdvmayMgTF2WWi770Hh0%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">概括</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">就在几个月前,<span style="color: black;">咱们</span>所有人(或<span style="color: black;">最少</span><span style="color: black;">咱们</span>大<span style="color: black;">都数</span>人)都对 ChatGPT 的功能印象深刻。 <span style="color: black;">此刻</span>,像 LangChain <span style="color: black;">这般</span>的新<span style="color: black;">研发</span>工具使<span style="color: black;">咱们</span>能够在几个小时内在笔记本电脑上构建<span style="color: black;">一样</span>令人印象深刻的原型 - 这是<span style="color: black;">有些</span>真正令人兴奋的时刻!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LangChain 是一个开源 Python 库,任何人都<span style="color: black;">能够</span>编写代码来构建由 LLM 驱动的应用程序。 该包为许多<span style="color: black;">基本</span>模型<span style="color: black;">供给</span>了通用接口,支持提示管理,并充当其他组件(如提示模板、其他 LLM、<span style="color: black;">外边</span>数据和<span style="color: black;">经过</span>代理的其他工具)的中央接口(在撰写本文时)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">该库<span style="color: black;">供给</span>的功能比本文<span style="color: black;">说到</span>的要多得多。 以<span style="color: black;">日前</span>的发展速度,这篇<span style="color: black;">文案</span><span style="color: black;">亦</span>可能在<span style="color: black;">一月</span>内就过时了。</p>
外链发布社区 http://www.fok120.com/ i免费外链发布平台 http://www.fok120.com/ 我完全同意你的观点,说得太对了。
页:
[1]