在探索大语言模型应用的过程中,我们发现单一的对话模型往往难以应对复杂的业务场景。本文将介绍一个基于多 Agent 的智能客服系统,重点展示如何通过多个专业化的 Agent 协同工作来提供更好的客服体验。
核心架构设计
1. Agent 基类设计
from pydantic import BaseModel
from typing import Optional
class Agent(BaseModel):
name: str = "Agent"
model: str = "gpt-4o" # deepseek-chat, mixtral-8x7b-32768, Qwen/Qwen2-72B-Instruct, gpt-4o, llama3-70b-8192
instructions: str = "你是一个非常有用的人工智能助手,你使用中文回答用户的问题。"
tools: list = []
2. 消息处理机制
def run_full_turn(agent, messages):
current_agent = agent
num_init_messages = len(messages)
messages = messages.copy()
while True:
# turn python functions into tools and save a reverse map
tool_schemas = [function_to_schema(tool) for tool in current_agent.tools]
tools = {
tool.__name__: tool for tool in current_agent.tools}