跳到正文
原文
Hugging Face Blog·· 2023-07-24精选AI 评分60

Hugging Face 发布 Agents.js:用 JavaScript 为 LLM 提供工具能力

Introducing Agents.js: Give tools to your LLMs using JavaScript

AI 导读

Hugging Face 发布 Agents.js,一个在浏览器或 Node 中为 LLM 提供工具调用的 JavaScript 库,可通过 npm install @huggingface/agents 安装。

推荐理由

Hugging Face 官方发布 JS 智能体库,可了解其工具调用方式与自定义扩展路径。

正文 · AI 翻译

我们最近在 huggingface.js 上开发了 Agents.js。这是一个新的库,用于从浏览器或服务器端的 JavaScript 为大型语言模型提供工具访问。它自带一些多模态工具,并可以轻松扩展您自己的工具和语言模型。

安装

开始使用非常简单,您可以从 npm 获取该库,命令如下:

npm install @huggingface/agents

用法

该库暴露了 HfAgent 对象,这是库的入口点。您可以像这样实例化它:

import { HfAgent } from "@huggingface/agents";

const HF_ACCESS_TOKEN = "hf_..."; // get your token at https://hf-mirror.com/settings/tokens

const agent = new HfAgent(HF_ACCESS_TOKEN);

之后,使用代理就很简单了。您给它一个纯文本命令,它会返回一些消息。

const code = await agent.generateCode(
  "Draw a picture of a rubber duck with a top hat, then caption this picture."
);

在这个例子中,生成了以下代码

// code generated by the LLM
async function generate() {
  const output = await textToImage("rubber duck with a top hat");
  message("We generate the duck picture", output);
  const caption = await imageToText(output);
  message("Now we caption the image", caption);
  return output;
}

然后,代码可以这样评估:

const messages = await agent.evaluateCode(code);

代理返回的消息是具有以下形状的对象:

export interface Update {
    message: string;
    data: undefined | string | Blob;

其中 message 是信息文本,data 可以包含字符串或 blob。blob 可用于显示图像或音频。

如果您信任您的环境(见 警告),您也可以直接从提示符中使用 run 运行代码:

const messages = await agent.run(
  "Draw a picture of a rubber duck with a top hat, then caption this picture."
);

用法警告

目前使用这个库意味着在浏览器(或 Node)中评估任意代码。这是一种安全风险,不应在不受信任的环境中这样做。我们建议您使用 generateCode 和 evaluateCode 而不是 run,以检查您正在运行的代码。

自定义 LLMs 💬

默认情况下,HfAgent 将使用 OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5 托管的推理 API 作为 LLM。不过,这可以自定义。

在实例化 HfAgent 时,您可以传入自定义的 LLM。在这个上下文中,LLM 是任何接受字符串输入并返回字符串 promise 的异步函数。例如,如果您有 OpenAI API 密钥,可以这样使用:

import { Configuration, OpenAIApi } from "openai";

const HF_ACCESS_TOKEN = "hf_...";
const api = new OpenAIApi(new Configuration({ apiKey: "sk-..." }));

const llmOpenAI = async (prompt: string): Promise<string> => {
  return (
    (
      await api.createCompletion({
        model: "text-davinci-003",
        prompt: prompt,
        max_tokens: 1000,
      })
    ).data.choices[0].text ?? ""
  );
};

const agent = new HfAgent(HF_ACCESS_TOKEN, llmOpenAI);

自定义工具 🛠️

Agents.js 设计为易于通过自定义工具和示例进行扩展。例如,如果您想添加一个将文本从英语翻译成德语的工具,可以这样做:

import type { Tool } from "@huggingface/agents/src/types";

const englishToGermanTool: Tool = {
  name: "englishToGerman",
  description:
    "Takes an input string in english and returns a german translation. ",
  examples: [
    {
      prompt: "translate the string 'hello world' to german",
      code: `const output = englishToGerman("hello world")`,
      tools: ["englishToGerman"],
    },
    {
      prompt:
        "translate the string 'The quick brown fox jumps over the lazy dog` into german",
      code: `const output = englishToGerman("The quick brown fox jumps over the lazy dog")`,
      tools: ["englishToGerman"],
    },
  ],
  call: async (input, inference) => {
    const data = await input;
    if (typeof data !== "string") {
      throw new Error("Input must be a string");
    }
    const result = await inference.translation({
      model: "t5-base",
      inputs: input,
    });
    return result.translation_text;
  },
};

现在,在初始化代理时,可以将这个工具添加到工具列表中。

import { HfAgent, LLMFromHub, defaultTools } from "@huggingface/agents";

const HF_ACCESS_TOKEN = "hf_...";

const agent = new HfAgent(HF_ACCESS_TOKEN, LLMFromHub("hf_..."), [
  englishToGermanTool,
  ...defaultTools,
]);

将输入文件传递给代理 🖼️

代理也可以接收输入文件,传递给工具。您可以将可选的 FileList 传递给 generateCode 和 evaluateCode,像这样:

如果您有以下 HTML:

<input id="fileItem" type="file" />

然后您可以这样做:

const agent = new HfAgent(HF_ACCESS_TOKEN);
const files = document.getElementById("fileItem").files; // FileList type
const code = agent.generateCode(
  "Caption the image and then read the text out loud.",
  files
);

当传递图像时,生成了以下代码:

// code generated by the LLM
async function generate(image) {
  const caption = await imageToText(image);
  message("First we caption the image", caption);
  const output = await textToSpeech(caption);
  message("Then we read the caption out loud", output);
  return output;
}

演示 🎉

我们一直在为 Agents.js 开发一个演示,您可以在这里尝试。它由我们在 HuggingChat 上使用的同一个 Open Assistant 30B 模型驱动,并使用从 hub 调用的工具。🚀

来源:Hugging Face Blog · hf-mirror.com