如何使用Llama 4和AutoGen构建人工智能代理

Meta 的 Llama 4 系列模型目前正统治着不断进步的人工智能世界。这些模型凭借其本地多模态功能,正在彻底改变我们构建智能系统的方式。当 Llama 4 与 AutoGen 相结合时,就能释放出构建动态、灵敏、强大的人工智能代理的全部潜能。通过利用 Llama 4 和 AutoGen 之间的集成,开发人员可以创建能够高效推理、协作和适应的创新型人工智能代理。在本文中,我们将了解如何使用 Llama 4 和 AutoGen 为特定应用构建人工智能代理。

为什么要考虑使用Llama 4?

Llama 4 模型系列(包括 Scout 和 Maverick 变体)代表了开源人工智能技术的重大飞跃。这些模型具有以下几个关键优势:

  • 多模态智能:Llama 4 具有原生多模态功能,可将不同类型的输入整合到统一的架构中。这样就能在不同的媒体类型中进行更复杂的推理。
  • 大语境长度:在 Llama 3 的 128K 限制基础上,它支持多达 1000 万个标记。它可以处理超长的上下文。这使得诸如多文档综合分析、基于用户历史的广泛个性化以及大型代码库导航等高级应用成为可能。
  • 高效性能:Llama 4 采用了混合专家架构,在处理每个标记时,只激活模型的特定部分。这种方法使模型非常高效。例如,Llama 4 Maverick 在运行过程中只使用了 4000 亿个参数中的 170 亿个。这使得它可以在一台 H100 DGX 主机上运行。
  • 卓越的性能和功能:基准测试表明,Llama 4 Maverick 在编码、推理、多语言能力和图像理解方面的表现优于 GPT-4o 和 Gemini 2.0 等同类机型。
  • 开源和可访问性:Meta 提供模型供下载。这鼓励了开放式创新,使开发人员能够在不同的应用和平台上定制和部署技术。

Llama 4基准性能

为了了解该模型的性能有多好,下面是 Llama 4 与其他顶级模型在各种标准基准上的比较。

Source: Llama 4

推荐阅读Llama 4 vs. GPT-4o:哪个更适合 RAG?

使用Llama 4构建人工智能代理

在本节中,我将引导您完成使用 Llama 4 和 AutoGen 构建特定任务代理的过程。我们将创建一个多代理系统,分析客户对工作的要求,根据他们的经验和详细信息找到适合特定工作的自由职业者,然后生成定制的工作提案供用户发送。

第 0 步:设置环境

在构建代理之前,我们将首先介绍必要的先决条件并设置环境。

先决条件

  • 熟悉终端或命令提示符。
  • 在系统中设置环境变量的能力。
  • 能够使用终端或命令提示符运行程序。
  • 必须安装 Python:https://www.python.org/downloads/
  • 对 AutoGen 有基本了解:https://docs.ag2.ai/latest/

访问 API

我们将使用 Together API 访问 Llama 4 模型。在 Together AI 上创建一个账户,然后访问此页面创建密匙: https://api.together.xyz/

第 1 步:设置引导AI代理的库和工具

首先,我们将导入所有必要的库和工具。

import os
import autogen
from IPython.display import display, Markdown

第 2 步:调用API

要使用 Llama 4,我们必须加载 Together API。下面的代码块将帮助我们加载 API 并将其配置到环境中。

with open("together_ai_api.txt") as file:
LLAMA_API_KEY = file.read().strip()
os.environ["LLAMA_API_KEY"] = LLAMA_API_KEY

第 3 步:创建代理并定义任务

现在,让我们创建所需的代理并定义它们的任务,即它们要做什么。

1. 客户输入代理

客户输入代理是人类用户和代理系统之间的主要接口。它从用户处收集项目细节,如客户要求、时间表和预算,并将其传递给范围架构师。它还负责转发后续问题和答复,并在最终建议被接受时发出终止信号。

预期输出:

  • 清晰传递用户的项目描述和自由职业者简介(技能、经验、时间估算)。
  • 一旦提交了令人满意的建议书,或用户明确表示终止会话,会话即告结束。
# Agent 1: Handles Human Input for Client Requirements
client_agent = autogen.UserProxyAgent(
name="Client_Input_Agent",
human_input_mode="ALWAYS",  # asks the human for input
max_consecutive_auto_reply=1, # Only reply once
is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
system_message="""You are the primary point of contact for the user.
Your first task is to provide the initial project details received from the human user (client requirements, product details, timeline, budget) to the group chat.
After the Scope Architect asks questions, relay the human user's answers about their skills, experience, tools, and time estimate back to the chat.
Reply TERMINATE when the final proposal is generated and satisfactory, or if the user wishes to stop. Otherwise, relay the user's input.
""",
)

2. 范围设计代理

范围设计代理负责从客户输入代理那里获得最初的项目细节。然后,它会询问具体问题,以收集自由职业者的技能、工具、以往项目经验和完成工作的预计时间。它本身并不着手生成建议书,而是确保在将建议书移交给下一个代理之前收集到所有必要的背景信息。

预期输出:

  • 结合客户的项目需求和自由职业者的能力的结构合理的摘要。
  • 一旦收集并汇总了所有所需数据,就会触发评级推荐代理。
# Agent 2: Gathers User's Profile and Estimates
scope_architect_agent = autogen.AssistantAgent(
name="Scope_Architect",
llm_config=llm_config,
human_input_mode="ALWAYS",
max_consecutive_auto_reply=1, # Only reply once 
is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
system_message="""You are a Scope Architect. Your role is to understand the project requirements provided initially and then gather necessary details *from the Client_Input_Agent (representing the user/freelancer)*.
1. Wait for the initial project details from Client_Input_Agent.
2. Once you have the project details, formulate clear questions for the Client_Input_Agent to ask the human user about their:
- Relevant past work/projects and collaborations.
- Key skills and tools applicable to this project.
- Their estimated time to complete the defined work.
3. Do NOT proceed to proposal generation. Wait for the Client_Input_Agent to provide the user's answers.
4. Once you have both the client requirements AND the user's details (skills, experience, time estimate), summarize this information clearly for the Rate Recommender. Signal that you have all necessary info.
""",
)

3. 费率推荐代理

费率推荐代理使用收集到的信息生成详细的项目建议书。它等待范围架构师提供完整的摘要。然后分析项目范围和自由职业者的详细信息,生成专业的建议书文档。其中包括自定义介绍、时间表、多个定价层级和明确的行动号召。

预期输出:

  • 格式专业的项目建议书文档,包含范围、定价和下一步步骤。
  • 最终成果可随时交付客户审批或进一步讨论。
rate_recommender_agent = autogen.AssistantAgent(
name="Rate_Recommender",
llm_config=llm_config,
max_consecutive_auto_reply=1, # Only reply once
system_message=f"""
You are a Proposal Generator and Rate Recommender. Your task is to create a structured project proposal.
Wait until the Scope_Architect shares a summary containing BOTH the client's project requirements AND the user's profile (skills, experience, time estimate, past work if available).
Analyze all received data: client needs, user expertise, estimated time, and any prior rate insights.
Generate a well-structured proposal addressed to the client, including the following sections:
Custom Introduction: Professionally introduce the user's services and reference the client's company and project.
Project Scope & Timeline: Clearly outline the deliverables with estimated timelines based on user input.
Suggested Pricing Tiers: Provide 1–3 pricing options (hourly, fixed fee, retainer) with justifications based on scope, user experience, or complexity.
Next Steps (CTA): Recommend scheduling a brief kickoff call to finalize and clarify details.
Present ONLY the final formatted proposal. Do not include additional commentary unless clarification is requested.""",)

4. 用户代理

该代理作为启动交互的入口点或助手。虽然它在此流程中并不扮演核心角色(根据提供的代码),但它可以用来启动或协助面向用户的任务。

user_proxy = autogen.UserProxyAgent(
name="user_proxy",
max_consecutive_auto_reply=1,
# is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
llm_config=llm_config,
system_message="""you are an helpful assistant and initate the conversation"""
)

第 4 步:创建小组管理器

此步骤设置中央协调员,负责管理所有专业代理之间的沟通和团队合作。

1. 设置群组聊天

群组聊天为三个专业代理建立了一个结构化的对话环境。它们是客户代理、范围架构代理和速率推荐代理。它通过回合限制和有序选择发言人来管理对话流。

要点

  • 容纳三个专业代理,以创建提案
  • 最多四轮,以保持专注
  • “Round_robin”发言模式确保有序参与
  • 为收集信息创造受控环境
# --- Group Chat Setup ---
groupchat = autogen.GroupChat(
agents=[client_agent, scope_architect_agent, rate_recommender_agent],
messages=[],
max_round=4,
speaker_selection_method="round_robin",
)

2. 创建群组聊天管理器

群组聊天管理器负责协调整个对话,通过从项目细节到提案生成的逻辑过程来引导互动。其系统信息为代理互动提供了分步说明,并定义了明确的终止条件。

要点

  • 引导所有代理之间的对话流程
  • 链接到群组聊天对象
  • 保持一致的 LLM 配置
  • 包含详细的流程指示
  • 在提议完成时或使用 TERMINATE 命令时终止
manager = autogen.GroupChatManager(
groupchat=groupchat,
llm_config=llm_config,
# System message for the manager guiding the overall flow
system_message="""Manage the conversation flow between the agents.
1. Start with the Client_Input_Agent providing project details.
2. Ensure the Scope_Architect asks the necessary questions about the user's background.
3. Ensure the Client_Input_Agent relays the user's answers.
4. Ensure the Rate_Recommender waits for all info before generating the final proposal in the specified format.
The conversation finishes when the final proposal is generated or the Client_Input_Agent says TERMINATE."""
)

第 5 步:启动聊天

既然代理已经就位,我们就来启动代理之间的协作工作流程。为此,我们将从 user_proxy 代理向 GroupChatManager 发送明确的指令提示。

要点

  • 使用 user_proxy.init_chat()触发对话,启动群聊并向 GroupChatManager 发送消息。
  • 将控制权委托给管理器,然后管理器会使用轮循方法和内部系统消息指令来协调代理,并遵循逐步流程。
# --- Initiate Chat ---
print("Starting the proposal generation process...")
print("Please provide the initial client and project details when prompted.")
initial_prompt_message = """
Start the process. First, I need the client/project details from the user (via Client_Input_Agent).
Then, Scope_Architect should ask the user (via Client_Input_Agent) about their background.
Finally, Rate_Recommender should generate the proposal.
"""
user_proxy.initiate_chat(
manager,
message=initial_prompt_message
)

第 6 步:格式化输出

这段代码将帮助我们以 markdown(.md) 格式显示输出结果。

chat_history = manager.chat_messages[client_agent] # Or potentially just manager.chat_messages if structure differs slightly
# Find the last message from the Rate_Recommender agent
final_proposal_message = None
for msg in reversed(chat_history):
if msg.get("role") == "assistant" and msg.get("name") == rate_recommender_agent.name:
if "Custom Introduction:" in msg.get("content", ""):
final_proposal_message = msg
break
if final_proposal_message:
final_proposal_string = final_proposal_message.get("content", "Proposal content not found.")
try:
display(Markdown(final_proposal_string))
except NameError:
print("\n(Displaying raw Markdown text as rich output is unavailable)\n")
print(final_proposal_string)
else:
print("\nCould not automatically extract the final proposal from the chat history.")
print("You may need to review the full chat history above.")

示例输出

小结

在本文中,我们使用 Llama 4 和 AutoGen 创建了一个项目建议书代理。该代理有效地收集了客户需求,构建了提案结构,并交付了一份包含清晰定价和时间明细的专业文档。AutoGen 负责处理对话流程,而 Llama 4 则确保在整个过程中做出自然的、上下文感知的回应。这种合作简化了客户沟通,为自由职业者和顾问提供了一个精简的解决方案,只需最少的人工输入即可自动生成建议书。

Llama 4 通过改进指令跟踪、更好的上下文保持和高效的少量学习,提高了代理的性能。它在多轮对话中保持连贯性的能力使方案生成过程更加智能、反应更快。此外,该模型推理速度快、成本低,适合实时应用。Llama 4 和 AutoGen 可共同实现强大的代理工作流程,提高面向客户任务的生产率和专业性。

© 版权声明
THE END
喜欢就支持一下吧
点赞20 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容