我是AI小火箭的HB,我摸索和写作人工智能和语言交叉点的所有事物,范畴从LLM,聊天机器人,语音机器人,开发框架,以数据为核心的潜在空间等。

介绍

LangChain是一个开源Python库,用于构建由大型语言模型(LLM)反对的应用程序。它提供了一个框架,将LLM与其余数据源(如互联网或集体文件)连接起来,容许开发人员将多个命令链接在一起,以创立更简单的应用程序。LangChain创立于2022年10月,是围绕LLMs(大语言模型)建设的一个框架,LLMs应用机器学习算法和海量数据来剖析和了解自然语言。LangChain本身并不开发LLMs,它的核心理念是为各种LLMs实现通用的接口,把LLMs相干的组件“链接”在一起,简化LLMs利用的开发难度,不便开发者疾速地开发简单的LLMs利用。

反对的语言

LangChain目前有两个语言的实现:Python和Node.js。

组件

LangChain的组件包含:

  • Models:模型,各种类型的模型和模型集成,比方GPT-4。
  • Prompts:提醒,包含提醒治理、提醒优化和提醒序列化。
  • Memory:记忆,用来保留和模型交互时的上下文状态。
  • Indexes:索引,用来结构化文档,以便和模型交互。
  • Chains:链,一系列对各种组件的调用。
  • Agents:代理,决定模型采取哪些口头,执行并且察看流程,直到实现为止。

应用场景

LangChain的应用场景包含:构建聊天机器人、文本生成、文本分类、问答零碎、语言翻译、语言模型微调等。

装置依赖库

npm install -S langchain

Hello World

首先,应用Langchain来调用OpenAI模型。

import { OpenAI } from "langchain/llms/openai";const model = new OpenAI({    openAIApiKey: 'sk-xxxx',//你的OpenAI API Key    temperature: 0.9});const res = await model.call(    "写一首诗,限度20个字");console.log(res);

输入

春风迎新年,喜气绕家园。祝愿短信语,友情永绵长。

替换提醒语中的参数

import { OpenAI } from "langchain/llms/openai";import { PromptTemplate } from "langchain/prompts";import { LLMChain } from "langchain/chains";const model = new OpenAI({    openAIApiKey: 'sk-xxxx',//你的OpenAI API Key    temperature: 0.9});const template = "What is a good name for a company that makes {product}?";const prompt = new PromptTemplate({    template: template,    inputVariables: ["product"],});const chain = new LLMChain({ llm: model, prompt: prompt });const res = await chain.call({ product: "colorful socks" });console.log(res);

开始见识Langchain的弱小

截止上个实例,你还没见识到Langchain的弱小。

接下来,你先注册一个SerpApi帐号,获取api key

点击这里注册

而后执行以下的代码,

import { OpenAI } from "langchain/llms/openai";import { initializeAgentExecutorWithOptions } from "langchain/agents";import { SerpAPI } from "langchain/tools";import { Calculator } from "langchain/tools/calculator";const model = new OpenAI({    streaming: true,    openAIApiKey: 'sk-xxxx',//你的OpenAI API Key    temperature: 0.9});const tools = [    new SerpAPI('你的SerpAPI的key', {        location: "Austin,Texas,United States",        hl: "en",        gl: "us",    }),    new Calculator(),];const executor = await initializeAgentExecutorWithOptions(tools, model, {    agentType: "zero-shot-react-description",});console.log("Loaded agent.");const input =    "谁是周杰伦的老婆?" +    "她的年纪加上10是多少?"console.log(`Executing with input "${input}"...`);const result = await executor.call({ input });console.log(`Got output ${result.output}`);

输入:

Loaded agent.Executing with input "谁是周杰伦的老婆?她的年纪加上10是多少?"...Got output Hannah Quinlivan is Zhou Jielun's wife and she is 39 years old.

执行后果做了两件事,

  1. 应用SerpAPI工具获取周杰伦的老婆的名字:Quinlivan
  2. 而后获取她的年龄:29岁
  3. 最初应用Calculator工具加上10:最终失去39岁的后果

这里引进了Langchainagents概念:代理。

决定模型采取哪些口头,执行并且察看流程,直到实现为止。

代码中引进了两个工具:SerpAPICalculator

const tools = [    new SerpAPI('你的SerpAPI的key', {        location: "Austin,Texas,United States",        hl: "en",        gl: "us",    }),    new Calculator(),];

AI小火箭

应用AI小火箭也能够间接应用OpenAI的接口,疾速应用,价格远低于OpenAI。

本文由博客一文多发平台 OpenWrite 公布!