关于机器学习:Javascript版Langchain入门

9次阅读

共计 2639 个字符,预计需要花费 7 分钟才能阅读完成。

我是 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 公布!

正文完
 0