用 100 行代码揭秘 LLM 集成工具 LangChain
发布时间:2023-05-15 08:50:39 所属栏目:外闻 来源:
导读:LangChain 是一个强大的程序框架,它旨在允许初学者用户围绕数以千计的大型开放式语言模型快速构建高性能的应用程序和管道。它直接与 OpenAI 的 GPT-3 和 GPT-3.5 模型以及 Hugging Face 的开源替代品(如 Google 的
LangChain 是一个强大的程序框架,它旨在允许初学者用户围绕数以千计的大型开放式语言模型快速构建高性能的应用程序和管道。它直接与 OpenAI 的 GPT-3 和 GPT-3.5 模型以及 Hugging Face 的开源替代品(如 Google 的 flan-t5 模型)集成。除此之外,它还提供了一套工具、组件和接口,可以简化以大型语言模型 ( LargeLanguageModel)和聊天模型为基础的构建应用程序的过程。开发者可以借助它轻松打造自己的 AI 知识库。 时至今日,LLMs 接口框架 LangChain 在 GitHub 上已经收获了 3万+ 个 Star,已经成为了当下非常流行的一个工具包。 Scott Logic 首席技术官 Colin Eberhardt 发表了一篇博文。他表示,自己用 100 行代码重新研究 LangChain,揭示了 LangChain 背后的工作原理。 LangChain 主要问题循环 Colin Eberhardt 表示,他最感兴趣的 LangChain 部分是Agent 模型。这个 API 允许你创建复杂的对话接口,并且利用各种工具(例如 Google 搜索、计算器)来回答问题。因此,数据库成功解决了在用 LLM 回答解决重要问题时,所遇到的产生令人不适的错误答案的自动化倾向和缺乏最新实时更新的数据等问题。 从广义上讲,使用 Agent 模型,让 LLM 成为了一个编排器。接受问题,将其分解为块,然后使用适当的工具来组合答案。 深入研究 LangChain 代码库,可以发现该流程是通过以下提示执行的: Answer the following questions as best you can. You have access to the following tools: search: a search engine. useful for when you need to answer questions about currentevents. input should be a search query.calculator: useful for getting the result of a math expression. The input to thistool should be a valid mathematical expression that could be executedby a simple calculator. Use the following format: Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [search, calculator]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can repeat N times)Thought: I now know the final answerFinal Answer: the final answer to the original input question 搜索工具 在正确的位置完成停止后,现在需要创建第一个“工具”,它执行 Google 搜索。Colin Eberhardt 将使用 SerpApi 来爬取 Google,并以简单的 SON 格式提供响应。 下面对工具进行定义,命名为:search const googleSearch = async (question) =>await fetch(`https://serpapi.com/search?api_key=${process.env.SERPAPI_API_KEY}&q=${question}`).then((res) => res.json).then((res) => res.answer_box?.answer || res.answer_box?.snippet); const tools = {search: {deion:`a search engine. useful for when you need to answer questions aboutcurrent events. input should be a search query.`,execute: googleSearch,},}; 该函数使用 SerpApi,在这种情况下,主要依赖通过页面的“答案框”组件可见的结果。这实际上是让谷歌搜索引擎提供可能的答案而不仅仅单单指的是网页搜索结果出现的列表的一种巧妙方法。 计算器工具 Colin Eberhardt 认为可以通过添加计算器工具来使其更强大: import { Parser } from "expr-eval"; const tools = {search: { ... },calculator: {deion:`Useful for getting the result of a math expression. The input to thistool should be a valid mathematical expression that could be executedby a simple calculator.`,execute: (input) => Parser.evaluate(input).toString,},}; 使用 expr-eval 模块完成所有复杂工作,这是一个简单的添加,现在可以做一些数学运算。同样,需要再次查看提示来了解内部工作原理,而不仅仅是查看结果: Question: what is the square root of 25?Thought: I need to use a calculator for thisAction: calculatorAction Input: 25^(1/2)Observation: 5Thought: I now know the final answerFinal Answer: The square root of 25 is 5. 在这里,LLM 已成功确定这个问题需要计算器。它还发现,对于计算器来说,“ 25 的平方根”通常表示为“25^(1/2)”,从而达到预期的结果。 当然,现在可以提出需要同时搜索网络和计算的问题。当被问及“昨天旧金山高温是多少华氏度?或者是多少摄氏度?“它能正确回答”昨天,旧金山的高温是 54°F 或 12.2° C。 对话界面 当前版本的代码只回答了一个问题。在上面的例子中,Colin Eberhardt 表示必须将两个问题绑定在一句话中。因此,更好的界面应该大多数时候是简单易用的对话形式的,这样能够允许用户在可以选择性地保留上下文的同时提出可能的后续提出的问题。 如何用 GPT 实现这一点并不明显,交互是无状态的,您提供提示,模型提供完成。创建一个长时间的对话需要一些非常聪明的提示工程。深入研究 LangChain 后,我发现它使用了一种有趣的技术。 (编辑:汽车网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐