创建 TypeScript 类,然后将这些类转换成 AI 聊天机器人。
从现在起,每位 TypeScript 开发者都能成为 AI 开发者了。
TypeScript 同志们,让我们用 @agentica
一起成为 AI 开发者。
- Github 仓库页面: https://github.com/wrtnlabs/agentica
- 文档指南: https://wrtnlabs.io/agentica
import { Agentica } from "@agentica/core";
import OpenAI from "openai";
import typia from "typia";
const agent = new Agentica({
vendor: {
model: "gpt-4o-mini",
api: new OpenAI({ apiKey: "********" }),
},
controllers: [
{
protocol: "class",
application: typia.llm.application<BbsArticleService, "chatgpt">(),
execute: new BbsArticleService(),
},
],
});
await agent.conversate("我想写一篇文章,你有什么建议吗?"); // 等待代理开始对话
进入全屏 退出全屏
2. Agentica 架构.@agentica
是专门用于大型语言模型函数调用的框架。
你可以通过 TypeScript 类型定义来提供功能。如果你创建了如下 FileSystem
类,并将其与@agentica
集成,它就会成为一个可以管理文件系统的聊天机器人。你可以通过与聊天机器人的对话来管理你的文件系统。
如果你同时提供了多个类似的 TypeScript 类,比如 GoogleScholarService
、NaverNewsService
和 NotionService
,你的 AI 代理可以分析学术论文和新闻文章,然后在 Notion 中撰写文档。当你让代理分析最近的韩国经济趋势,对其进行评论,整理相关论文,并将结果写入 Notion 中时,AI 代理会帮你完成这些任务。
只需定义你的 TypeScript 类即可,就可以创建你想要的任何 AI 代理程序。
参见https://wrtnlabs.io/agentica/tutorial/coding/file-system/
import fs from "fs";
export class 文件系统 {
public __dirname(): string {
return __dirname;
}
public 读取目录(input: {
路径: string;
选项?:
| {
编码: "utf-8";
withFileTypes?: false | undefined;
recursive?: boolean | undefined;
}
| "utf-8"
| null;
}): Promise<string[]> {
return fs.promises.readdir(input.路径, input.选项);
}
public 读取文件(input: { 路径: string }): Promise<string> {
return fs.promises.readFile(input.路径, "utf8");
}
public 写入文件同步(input: {
文件: string;
数据: string;
}): Promise<void> {
return fs.promises.writeFile(input.文件, input.数据);
}
}
全屏查看 / 退出全屏
3. 企业演示有些人可能会疑惑:这些类只有少数几个功能,这只是一个在特定场景下运行良好的玩具项目吗?使用 @agentica
是否能够构建一个企业级聊天机器人?
答案是肯定的,确实可以创建企业级别的聊天机器人。
这里有一个为企业级购物商场打造的聊天机器人,具备289个API功能。它支持大多数标准电商功能,并且如展示,运行顺畅无误。
参考一下,@agentica
也可以从 Swagger 或 OpenAPI 规范文档中获取函数定义。下面的演示来自 @samchon/shopping-backend
。
import { Agentica } from "@agentica/core";
import { HttpLlm, OpenApi } from "@samchon/openapi";
import typia from "typia";
const agent = new Agentica({
model: "chatgpt",
vendor: {
api: new OpenAI({ apiKey: "*****" }),
model: "gpt-4o-mini",
},
controllers: [
{
protocol: "http",
name: "shopping",
// 购物协议应用
application: HttpLlm.application({
model: "chatgpt",
document: await fetch(
"https://shopping-be.wrtn.ai/editor/swagger.json",
).then((r) => r.json()),
}),
connection: {
host: "https://shopping-be.wrtn.ai",
headers: {
Authorization: "Bearer *****",
},
},
},
{
protocol: "class",
name: "counselor",
// 顾问协议应用
application: typia.llm.application<ShoppingCounselor, "chatgpt">(),
execute: new ShoppingCounselor(),
},
{
protocol: "class",
name: "policy",
// 政策协议应用
application: typia.llm.application<ShoppingPolicy, "chatgpt">(),
execute: new ShoppingPolicy(),
},
{
protocol: "class",
name: "rag",
// 搜索协议应用
application: typia.llm.application<ShoppingSearchRag, "chatgpt">(),
execute: new ShoppingSearchRag(),
},
],
});
// 我想买一台MacBook Pro (mù kù bāo pú)
await agent.对话("我想买一台MacBook Pro (mù kù bāo pú)");
切换全屏
4. 原则如果你不太了解人工智能,你可能会好奇@agentica
是如何利用各种函数来完成所有任务的。
或者,如果你是AI代理开发的专家,你可能会有不一样的疑问。传统的代理开发主要围绕代理的工作流程图,那么@agentica
又是如何通过LLM函数调用实现类似功能的?
访问我们的框架主页,或阅读我之前的文章以了解其关键原则。在这些资源中,您将了解到新的 AI 开发范式:“编译驱动开发”和“文档驱动开发”。
- Github 仓库: https://github.com/wrtnlabs/agentica
- 文档指南: https://wrtnlabs.io/agentica
https://dev.to/samchon/every-backend-developer-is-a-great-ai-developer-338m 注:链接中的内容需要单独翻译成中文。
每个后端开发者也是AI开发者。
由于后端开发人员的工作性质,他们实际上比传统的AI/ML工程师更有优势进行AI代理的开发。
让我们将 swagger.json
文件传给 @agentica
,这样在聊天时让AI聊天机器人助手运行API的功能。
共同學習,寫下你的評論
評論加載中...
作者其他優質文章