构建多代理系统

大语言模型的强大应用是 LLM 赋能的代理。代理是一种系统,可通过规划如何将任务拆分为更小的任务来执行复杂任务,并(借助工具调用)执行与数据库甚至实体设备等外部资源交互的任务。

以下是使用单个提示和几个工具构建的非常简单的客户服务客服人员的摘录:

const menuLookupTool = ai.defineTool(
  {
    name: 'menuLookupTool',
    description: 'use this tool to look up the menu for a given date',
    inputSchema: z.object({
      date: z.string().describe('the date to look up the menu for'),
    }),
    outputSchema: z.string().describe('the menu for a given date'),
  },
  async (input) => {
    // Retrieve the menu from a database, website, etc.
    // ...
  }
);

const reservationTool = ai.defineTool(
  {
    name: 'reservationTool',
    description: 'use this tool to try to book a reservation',
    inputSchema: z.object({
      partySize: z.coerce.number().describe('the number of guests'),
      date: z.string().describe('the date to book for'),
    }),
    outputSchema: z
      .string()
      .describe(
        "true if the reservation was successfully booked and false if there's" +
          ' no table available for the requested time'
      ),
  },
  async (input) => {
    // Access your database to try to make the reservation.
    // ...
  }
);
const chat = ai.chat({
  model: gemini15Pro,
  system:
    "You are an AI customer service agent for Pavel's Cafe. Use the tools " +
    'available to you to help the customer. If you cannot help the ' +
    'customer with the available tools, politely explain so.',
  tools: [menuLookupTool, reservationTool],
});

如果您的代理只有少数功能,上述简单的架构就足以满足需求。不过,即使是上述有限的示例,您也可以看到,客户可能会期望某些功能:例如列出客户的当前预订、取消预订等。随着您构建越来越多的工具来实现这些额外的功能,您会开始遇到一些问题:

  • 您添加的工具越多,模型就越能根据任务持续正确地使用合适的工具。
  • 某些任务最好通过用户和客服人员之间更专注的来回交流来完成,而不是通过单次工具调用。
  • 某些任务可能需要使用专用提示。例如,如果客服人员要回复不满意的客户,您可能希望其采用更商务化的措辞,而最初与客户打招呼的客服人员则可以采用更友好、更轻松的措辞。

您可以使用以下方法来解决构建复杂代理时出现的这些问题:创建多个专用代理,并使用通用代理将任务委托给它们。Genkit 支持这种架构,因为它允许您将提示指定为工具。每个提示都代表一个专用代理,具有自己的一组工具,这些代理又可作为工具供单个协调代理使用,后者是与用户的主要界面。

下面是将前面的示例扩展为多代理系统后的示意图:

// Define a prompt that represents a specialist agent
const reservationAgent = ai.definePrompt(
  {
    name: 'reservationAgent',
    description: 'Reservation Agent can help manage guest reservations',
    tools: [reservationTool, reservationCancelationTool, reservationListTool],
  },
  '{{role "system"}} Help guests make and manage reservations'
);

// Or load agents from .prompt files
const menuInfoAgent = ai.prompt('menuInfoAgent');
const complaintAgent = ai.prompt('complaintAgent');

// The triage agent is the agent that users interact with initially
const triageAgent = ai.definePrompt(
  {
    name: 'triageAgent',
    description: 'Triage Agent',
    tools: [reservationAgent, menuInfoAgent, complaintAgent],
  },
  `{{role "system"}} You are an AI customer service agent for Pavel's Cafe.
  Greet the user and ask them how you can help. If appropriate, transfer to an
  agent that can better handle the request. If you cannot help the customer with
  the available tools, politely explain so.`
);
// Start a chat session, initially with the triage agent
const chat = ai.chat(triageAgent);