Agents.js 介绍:使用 JavaScript 为您的 LLM 提供工具
我们最近一直在 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 模型提供支持,并使用从中心调用的工具。🚀