如何使用Llama 4和AutoGen構建人工智慧代理

使用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 與其他頂級模型在各種標準基準上的比較。

Llama 4基準效能 Llama 4基準效能

Source: Llama 4

推薦閱讀Llama 4 vs. GPT-4o:哪個更適合 RAG?

使用Llama 4構建人工智慧代理

在本節中,我將引導您完成使用 Llama 4 和 AutoGen 構建特定任務代理的過程。我們將建立一個多代理系統,分析客戶對工作的要求,根據他們的經驗和詳細資訊找到適合特定工作的自由職業者,然後生成定製的工作提案供使用者傳送。

第 0 步:設定環境

在構建代理之前,我們將首先介紹必要的先決條件並設定環境。

先決條件

訪問 API

我們將使用 Together API 訪問 Llama 4 模型。在 Together AI 上建立一個賬戶,然後訪問此頁面建立密匙: https://api.together.xyz/

Together API金鑰

第 1 步:設定引導AI代理的庫和工具

首先,我們將匯入所有必要的庫和工具。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import os
import autogen
from IPython.display import display, Markdown
import os import autogen from IPython.display import display, Markdown
import os
import autogen
from IPython.display import display, Markdown

第 2 步:呼叫API

要使用 Llama 4,我們必須載入 Together API。下面的程式碼塊將幫助我們載入 API 並將其配置到環境中。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
with open("together_ai_api.txt") as file:
LLAMA_API_KEY = file.read().strip()
os.environ["LLAMA_API_KEY"] = LLAMA_API_KEY
with open("together_ai_api.txt") as file: LLAMA_API_KEY = file.read().strip() os.environ["LLAMA_API_KEY"] = LLAMA_API_KEY
with open("together_ai_api.txt") as file:
LLAMA_API_KEY = file.read().strip()
os.environ["LLAMA_API_KEY"] = LLAMA_API_KEY

第 3 步:建立代理並定義任務

現在,讓我們建立所需的代理並定義它們的任務,即它們要做什麼。

1. 客戶輸入代理

客戶輸入代理是人類使用者和代理系統之間的主要介面。它從使用者處收集專案細節,如客戶要求、時間表和預算,並將其傳遞給範圍架構師。它還負責轉發後續問題和答覆,並在最終建議被接受時發出終止訊號。

預期輸出:

  • 清晰傳遞使用者的專案描述和自由職業者簡介(技能、經驗、時間估算)。
  • 一旦提交了令人滿意的建議書,或使用者明確表示終止會話,會話即告結束。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 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.
""",
)
# 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. """, )
# 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. 範圍設計代理

範圍設計代理負責從客戶輸入代理那裡獲得最初的專案細節。然後,它會詢問具體問題,以收集自由職業者的技能、工具、以往專案經驗和完成工作的預計時間。它本身並不著手生成建議書,而是確保在將建議書移交給下一個代理之前收集到所有必要的背景資訊。

預期輸出:

  • 結合客戶的專案需求和自由職業者的能力的結構合理的摘要。
  • 一旦收集並彙總了所有所需資料,就會觸發評級推薦代理。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 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.
""",
)
# 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. """, )
# 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. 費率推薦代理

費率推薦代理使用收集到的資訊生成詳細的專案建議書。它等待範圍架構師提供完整的摘要。然後分析專案範圍和自由職業者的詳細資訊,生成專業的建議書文件。其中包括自定義介紹、時間表、多個定價層級和明確的行動號召。

預期輸出:

  • 格式專業的專案建議書文件,包含範圍、定價和下一步步驟。
  • 最終成果可隨時交付客戶審批或進一步討論。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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.""",)
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.""",)
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. 使用者代理

該代理作為啟動互動的入口點或助手。雖然它在此流程中並不扮演核心角色(根據提供的程式碼),但它可以用來啟動或協助面向使用者的任務。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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"""
)
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""" )
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”發言模式確保有序參與
  • 為收集資訊創造受控環境
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# --- Group Chat Setup ---
groupchat = autogen.GroupChat(
agents=[client_agent, scope_architect_agent, rate_recommender_agent],
messages=[],
max_round=4,
speaker_selection_method="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", )
# --- 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 命令時終止
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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."""
)
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.""" )
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 傳送訊息。
  • 將控制權委託給管理器,然後管理器會使用輪循方法和內部系統訊息指令來協調代理,並遵循逐步流程。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# --- 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
)
# --- 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 )
# --- 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) 格式顯示輸出結果。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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.")
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.")
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.")

示例輸出

AI代理輸入示例 AI代理輸入示例

小結

在本文中,我們使用 Llama 4 和 AutoGen 建立了一個專案建議書代理。該代理有效地收集了客戶需求,構建了提案結構,並交付了一份包含清晰定價和時間明細的專業文件。AutoGen 負責處理對話流程,而 Llama 4 則確保在整個過程中做出自然的、上下文感知的回應。這種合作簡化了客戶溝通,為自由職業者和顧問提供了一個精簡的解決方案,只需最少的人工輸入即可自動生成建議書。

Llama 4 透過改進指令跟蹤、更好的上下文保持和高效的少量學習,提高了代理的效能。它在多輪對話中保持連貫性的能力使方案生成過程更加智慧、反應更快。此外,該模型推理速度快、成本低,適合即時應用。Llama 4 和 AutoGen 可共同實現強大的代理工作流程,提高面向客戶任務的生產率和專業性。

評論留言