Hugging Face 发布 Agents.js:用 JavaScript 给大语言模型接入工具
Introducing Agents.js: Give tools to your LLMs using JavaScript
Hugging Face 在 huggingface.js 下推出 Agents.js,一个让浏览器或服务端 JavaScript 为大语言模型提供工具访问的新库,可通过 npm 安装(npm install @huggingface/agents)。
原文给出安装方式、代码生成与执行流程、自定义 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 将使用 OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5 托管的推理 API 作为 LLM。但这可以自定义。
在实例化你的 HfAgent 时,你可以传入一个自定义 LLM。在此上下文中,LLM 是任何接受字符串输入并返回字符串承诺的异步函数。例如,如果你有 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,
]);
将输入文件传递给代理 🖼️
代理还可以接受输入文件以传递给工具。你可以像这样向 generateCode 和 evaluateCode 传递一个可选的 FileList:
如果你有以下 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(RSS) · huggingface.co