利用人工智能代理自动化客服退款查询:使用 Agentica
由 Mux 赞助的 DEV 全球展示挑战赛:展示你的项目!
概述
最近,我们内部团队开发了一款人工智能代理,用于自动化处理客户服务(CS)的退款咨询。该代理基于我们自主开发的开源库Agentica构建,目前已能代表客服人员处理退款咨询,极大地提高了工作效率。
在本文中,我将分享开发过程、关键代码示例、实施前后的生产力对比,以及我们反复试验的经验和最终采取的解决方案。
背景和目标
客服团队每天花费大量时间手动处理数十个退款咨询。由于退款咨询大多是重复性问题和简单操作,因此自动化需求不断涌现。我们认为人工智能代理可以有效解决这个问题。
项目目标如下:
将退款处理时间缩短 80% 以上,
使客服团队能够专注于解决更复杂的问题。
开发过程和代码示例
Agentica 库简介我们的团队开发并开源了一个名为Agentica 的
开源库。使用此库,任何人都可以通过实例化一个简单的类轻松创建具有聊天功能的代理。如果您有 TypeScript 类或 Swagger 文档,LLM 可以自动调用必要的函数作为工具调用。
例如,我们来创建一个支持 Gmail 功能的代理:
import { Agentica } from "@agentica/core";
import { GmailService } from "@wrtnlabs/connector-gmail";
import dotenv from "dotenv";
import OpenAI from "openai";
import typia from "typia";
dotenv.config();
const openai = new OpenAI({ apiKey: process.env.OPENAI_KEY });
class GmailService {
async sendMail(auth: OAuth2Client, to: string, from: string, subject: string, message: string) {
const gmail = google.gmail({ version: 'v1', auth });
const raw = createEmail(to, from, subject, message);
try {
const response = await gmail.users.messages.send({
userId: 'me',
requestBody: {
raw,
},
});
console.log('Email sent successfully:', response.data);
return response.data;
} catch (error) {
console.error('Error sending email:', error);
throw error;
}
}
}
export const GmailAgent = new Agentica({
model: "chatgpt",
vendor: {
api: openai,
model: "gpt-4o-mini",
},
controllers: [
{
name: "Gmail Connector",
protocol: "class",
application: typia.llm.application<GmailService, "chatgpt">(),
execute: new GmailService({
clientId: process.env.GMAIL_CLIENT_ID!,
clientSecret: process.env.GMAIL_CLIENT_SECRET!,
secret: process.env.GMAIL_REFRESH_TOKEN!,
}),
},
],
});
如您所见,示例代码展示了使用 Agentica 创建代理是多么容易。
以这种方式实现时,应用程序中定义的函数可供 LLM 调用作为工具,而实际执行则通过“execute”参数进行。
如果您想使用 Gmail 以外的功能,您可以将它们实现为类,或者为它们提供 Swagger 文档。
例如:
import { Agentica } from "@agentica/core";
import { GmailService } from "@wrtnlabs/connector-gmail";
import dotenv from "dotenv";
import OpenAI from "openai";
import typia from "typia";
dotenv.config();
const openai = new OpenAI({ apiKey: process.env.OPENAI_KEY });
class GmailService {
async sendMail(auth: OAuth2Client, to: string, from: string, subject: string, message: string) {
const gmail = google.gmail({ version: 'v1', auth });
const raw = createEmail(to, from, subject, message);
try {
const response = await gmail.users.messages.send({
userId: 'me',
requestBody: {
raw,
},
});
console.log('Email sent successfully:', response.data);
return response.data;
} catch (error) {
console.error('Error sending email:', error);
throw error;
}
}
}
class GoogleCalenderService {
async createEvent(auth: OAuth2Client){
const calendar = google.calendar({ version: 'v3', auth });
const event = {
summary: 'Test Event',
location: 'Online',
description: 'Test event created via the Google Calendar API.',
start: {
dateTime: '2025-03-20T10:00:00+09:00',
timeZone: 'Asia/Seoul',
},
end: {
dateTime: '2025-03-20T11:00:00+09:00',
timeZone: 'Asia/Seoul',
},
attendees: [
{ email: 'attendee@example.com' },
],
reminders: {
useDefault: false,
overrides: [
{ method: 'email', minutes: 24 * 60 },
{ method: 'popup', minutes: 10 },
],
},
};
try {
const response = await calendar.events.insert({
calendarId: 'primary',
requestBody: event,
sendUpdates: 'all',
});
console.log('Event created successfully:', response.data);
console.log('Event link:', response.data.htmlLink);
return response.data;
} catch (error) {
console.error('Error creating event:', error);
throw error;
}
}
}
export const ExampleAgent = new Agentica({
model: "chatgpt",
vendor: {
api: openai,
model: "gpt-4o-mini",
},
controllers: [
{
protocol: "http",
name: "shopping",
application: HttpLlm.application({
model: "chatgpt",
document: OpenApi.convert(
await fetch(
"https://shopping-be.wrtn.ai/editor/swagger.json",
).then(r => r.json()),
),
}),
connection: {
host: "https://shopping-be.wrtn.ai",
headers: {
Authorization: "Bearer *****",
},
},
},
{
name: "Gmail Connector",
protocol: "class",
application: typia.llm.application<GmailService, "chatgpt">(),
execute: new GmailService({
clientId: process.env.GMAIL_CLIENT_ID!,
clientSecret: process.env.GMAIL_CLIENT_SECRET!,
secret: process.env.GMAIL_REFRESH_TOKEN!,
}),
},
{
name: "Google Calendar Connector",
protocol: "class",
application: typia.llm.application<GoogleCalenderService, "chatgpt">(),
execute: new GmailService({
clientId: process.env.GOOGLE_CALENDAR_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CALENDAR_CLIENT_SECRET!,
secret: process.env.GOOGLE_CALENDAR_REFRESH_TOKEN!,
}),
},
],
});
为了方便起见,我们还创建了各种功能的连接器库。您可以在这里查看并试用。
有关 Agentica 的更多详细信息,请参阅Agentica 文档。
现在,让我们来看看我是如何实际实现退款客服AI代理的。
退款流程非常简单。系统会检查过去7天的付款记录,如果付款记录符合退款条件,就会进行退款。
我们构建了必要的退款功能,并将其作为 API 提供,以工具的形式提供(使用 Swagger 文档中的函数调用),Agentica 库可以通过该工具创建退款 CS AI 代理。
export function RefundCsAgent(histories: Primitive<IAgenticaPrompt[]>) {
const openai = new OpenAI({
apiKey: isNotNull(
process.env.CHATGPT_API_KEY,
"Environment variable CHATGPT_API_KEY is not set or empty. Please define it in your environment configuration",
),
});
return new Agentica({
provider: {
type: "chatgpt",
api: openai,
model: "gpt-4o-2024-11-20",
},
histories: histories,
controllers: [
{
protocol: "http",
name: "Refund Connector",
connection,
application,
},
],
});
}
const application = HttpLlm.application({
model: "chatgpt",
document: OpenApi.convert(
await fetch(
isNotNull(
process.env.SWAGGER_URL,
"Environment variable SWAGGER_URL is not set or empty. Please define it in your environment configuration",
),
{
cache: "no-store",
},
).then((r) => r.json()),
),
});
const connection: IHttpConnection = {
host: isNotNull(
process.env.CONNECTION_HOST,
"Environment variable CONNECTION_HOST is not set or empty. Please define it in your environment configuration",
),
};
试错法和解决方法
在开发过程中,LLM工具调用错误频繁出现。虽然函数结构看起来没有问题,但仅仅改进提示信息收效甚微。最终,我们通过简化函数结构和明确函数描述解决了这个问题。
流程改进:我们将流程从三个阶段简化为两个阶段,从而最大限度地减少了LLM在工具选择过程中出错的概率。API
接口和函数名称变更:我们修改了接口和函数名称,使其更便于LLM使用。
简化函数描述:我们摒弃了冗长复杂的描述,转而以简洁的方式提供核心信息。
应用前后生产力对比
在实施新系统之前,客服团队平均每处理一个退款申请要花费超过30分钟。他们需要手动从用户那里获取账户信息,核实是否符合退款条件,然后手动处理退款,这非常耗时。
引入退款AI代理后,退款查询可以全天候24小时即时处理——即使在正常工作时间之外也是如此。换句话说,我们构建了一个随时随地都能访问的退款系统。
这使得生产力提高了约 5 倍,使客户服务团队能够处理更复杂的咨询,并专注于提高客户服务的整体质量。
结论与经验教训
该项目成功达成预期目标,与之前的人工流程相比,退款查询处理时间缩短了 80% 以上。这大幅减轻了客服团队的工作量,使他们能够专注于解决更复杂、更有价值的问题,同时通过全天候实时处理提升了客户满意度。
Agentica 库带来的快速高效的开发缩短了项目时间,生产力的提高(比手动方法高出约 5 倍)是一项重大成就。
从这个项目中我们得到的一个重要教训是,LLM工具调用的成功与否很大程度上取决于功能设计和描述的清晰度和简洁性。复杂冗长的功能结构往往会导致LLM频繁出现故障。简化结构并专注于核心功能可以提高性能——这一经验对于未来设计与人工智能交互的界面至关重要。
我希望我的经验能帮助您避免类似的陷阱,并启发您进行更高效、功能更丰富的开发。感谢您对 Agentica 库发展历程的关注——请继续关注它的最新进展。欢迎随时提问,感谢您阅读这篇长文!
文章来源:https://dev.to/leo_ko_7e30548a8d765dde77/automating-cs-refund-inquiries-with-an-ai-agent-using-agentica-6bn