Agents.js 介紹:使用 JavaScript 為您的 LLM 提供工具

釋出於 2023 年 7 月 24 日
在 GitHub 上更新

我們最近一直在 huggingface.js 上開發 Agents.js。這是一個新庫,用於在瀏覽器或伺服器中使用 JavaScript 為 LLM 提供工具訪問許可權。它開箱即用,帶有一些多模態工具,並且可以輕鬆擴充套件您自己的工具和語言模型。

安裝

入門非常簡單,您可以透過以下方式從 npm 獲取該庫

npm install @huggingface/agents

使用

該庫公開了 `HfAgent` 物件,它是該庫的入口點。您可以這樣例項化它

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

const HF_ACCESS_TOKEN = "hf_..."; // get your token at https://huggingface.co/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`,以便檢查您正在執行的程式碼。

自定義 LLM 💬

預設情況下,`HfAgent` 將使用託管的推理 API OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5 作為 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 模型提供支援,並使用從中心呼叫的工具。🚀

社群

註冊登入發表評論

© . This site is unofficial and not affiliated with Hugging Face, Inc.