自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

OpenAI開源多智能體編排框架Swarm!

原創(chuàng) 精選
人工智能
如果開發(fā)者想要尋求完全托管的線程以及內(nèi)置的內(nèi)存管理和檢索,那么 Assistants API 就已經(jīng)是很好的選擇了。但如果開發(fā)者想要完全的透明度,并且能夠細粒度地控制上下文、步驟和工具調(diào)用,那么 Swarm 才是最佳選擇。

編輯 | 星璇

出品 | 51CTO技術棧(微信號:blog51cto)

OpenAI 終于又Open了一回!這次開源的對象,竟然輪到了多智能體框架!

圖片圖片

圖片

Swarm 開源后,X上立馬引來開發(fā)者的的討論,有網(wǎng)友表示這能幫助簡化許多潛在的多智能體用例的工作流程。

圖片圖片

下面我們就來簡單介紹一下這個開源項目。

一、Swarm是什么?

據(jù)github文當介紹,Swarm 是一個實驗性質的多智能體框架,并不是為生產(chǎn)目的開發(fā)的,因此團隊表示不會提供任何官方支持。

其中,Swarm 關注的重點是讓智能體協(xié)作和執(zhí)行變得輕量、高度可控且易于測試。

為此,它使用了兩種原語抽象:智能體(agent)和交接(handoff)。其中,智能體包含指令和工具,并且在任何時間都可以選擇將對話交接給另一個智能體。

該團隊表示,這些原語很強大,「足以表達工具和智能體網(wǎng)絡之間的豐富動態(tài),讓你可以針對真實世界問題構建可擴展的解決方案,同時避免陡峭的學習曲線?!?/p>

另外,該團隊指出,請注意 Swarm 智能體與 Assistants API 中的 Assistants 無關。之所以名字相似,只是為了方便。Swarm 完全由 Chat Completions API 提供支持,因此在調(diào)用之間是無狀態(tài)的。

二、為什么要使用 Swarm?

在設計上,Swarm 是輕量級、可擴展且高度可定制的。它最適合處理存在大量獨立功能和指令的情況——這些功能和指令很難編碼成單個提示詞。

如果開發(fā)者想要尋求完全托管的線程以及內(nèi)置的內(nèi)存管理和檢索,那么 Assistants API 就已經(jīng)是很好的選擇了。但如果開發(fā)者想要完全的透明度,并且能夠細粒度地控制上下文、步驟和工具調(diào)用,那么 Swarm 才是最佳選擇。Swarm (幾乎)完全運行在客戶端,與 Chat Completions API 非常相似,不會在調(diào)用之間存儲狀態(tài)。

該團隊還展示了一個應用示例,包括天氣查詢智能體、用于在航空公司環(huán)境中處理不同客戶服務請求的多智能體設置、客服機器人、可以幫助銷售和退款的個人智能體等。具體示例請訪問 Swarm 代碼庫。

圖片圖片

簡單的天氣查詢智能體示例,問題先經(jīng)過篩選智能體處理,再轉交給天氣智能體解答

我們先來看一個例子。首先安裝 Swarm,很簡單:

pip install git+ssh://git@github.com/openai/swarm.git

裝好這個框架之后,用起來也很方便。以下代碼定義了 2 個智能體,而用戶的指令是與智能體 B 交談:

from swarm import Swarm, Agent

client = Swarm()

def transfer_to_agent_b():
return agent_b

agent_a = Agent(
name="Agent A",
instructinotallow="You are a helpful agent.",
functions=[transfer_to_agent_b],
)

agent_b = Agent(
name="Agent B",
instructinotallow="Only speak in Haikus.",
)

response = client.run(
agent=agent_a,
messages=[{"role": "user", "content": "I want to talk to agent B."}],
)

print(response.messages[-1]["content"])

輸出消息:

Hope glimmers brightly,
New paths converge gracefully,
What can I assist?

三、Swarm 的核心組件

Swarm 的核心組件包括 client(客戶端)、Agent(智能體)、Function(函數(shù))。

運行 Swarm 就是從實例化一個 client 開始的(其就是在內(nèi)部實例化一個 OpenAI 客戶端)。

from swarm import Swarm
client = Swarm()

1. client.run()

Swarm 的 run() 函數(shù)類似于 Chat Completions API 中的 chat.completions.create() 函數(shù)——接收消息并返回消息,并且在調(diào)用之間不保存任何狀態(tài)。但重點在于,它還處理 Agent 函數(shù)執(zhí)行、交接、上下文變量引用,并且可以在返回給用戶之前進行多輪執(zhí)行。

究其核心,Swarm 的 client.run() 是實現(xiàn)以下循環(huán):

  • 先讓當前智能體完成一個結果
  • 執(zhí)行工具調(diào)用并附加結果
  • 如有必要,切換智能體
  • 如有必要,更新上下文變量
  • 如果沒有新的函數(shù)調(diào)用,則返回

參數(shù)

client.run() 的參數(shù)包括:

圖片圖片

client.run() 完成后(可能進行過多次智能體和工具調(diào)用),會返回一個響應,其中包含所有相關的已更新狀態(tài)。具體來說,即包含新消息、最后調(diào)用的智能體、最新的上下文變量。你可以將這些值(加上新的用戶消息)傳遞給 client.run() 的下一次執(zhí)行,以繼續(xù)上次的交互——就像是 chat.completions.create()

響應字段

圖片圖片

2. Agent

Agent 就是將一組指令與一組函數(shù)封裝在一起(再加上一些額外的設置),并且其有能力將執(zhí)行過程交接給另一個 Agent。Agent 字段如下:

圖片圖片

指令(instructions)

Agent instructions 會直接轉換成對話的系統(tǒng)提示詞(作為第一條消息)。只有當前活動的 Agent 的指令會被使用(當發(fā)生智能體交接時,系統(tǒng)提示詞會變化,但聊天歷史不會)。

agent = Agent(
instructinotallow="You are a helpful agent."
)

instructions 可以是常規(guī)字符串,也可以是返回字符串的函數(shù)。該函數(shù)可以選擇性地接收 context_variables 參數(shù),該參數(shù)將由傳入 client.run() 的 context_variables 填充。

def instructions(context_variables):
user_name = context_variables["user_name"]
return f"Help the user, {user_name}, do whatever they want."

agent = Agent(
instructinotallow=instructions
)
response = client.run(
agent=agent,
messages=[{"role":"user", "content": "Hi!"}],
context_variables={"user_name":"John"}
)
print(response.messages[-1]["content"])

輸出消息:

Hi John, how can I assist you today?

3. Function

  • Swarm Agent 可以直接調(diào)用 Python 函數(shù)。
  • 函數(shù)通常應返回一個字符串(數(shù)值會被轉換為字符串)。
  • 如果一個函數(shù)返回了一個 Agent,則執(zhí)行過程將轉交給該 Agent。
  • 如果函數(shù)定義了 context_variables 參數(shù),則它將由傳遞到 client.run() 的 context_variables 填充。
def greet(context_variables, language):
user_name = context_variables["user_name"]
greeting = "Hola" if language.lower() == "spanish" else "Hello"
print(f"{greeting}, {user_name}!")
return "Done"

agent = Agent(
functions=[print_hello]
)

client.run(
agent=agent,
messages=[{"role": "user", "content": "Usa greet() por favor."}],
context_variables={"user_name": "John"}
)

輸出:

Hola, John!

如果某個 Agent 函數(shù)調(diào)用出錯(缺少函數(shù)、參數(shù)錯誤等),則會在聊天之中附加一條報錯響應,以便 Agent 恢復正常。

如果 Ageny 調(diào)用多個函數(shù),則按順序執(zhí)行它們。

交接和更新上下文變量

通過在返回的函數(shù)中包含一個 Agent,可將執(zhí)行過程交接給這個 Agent。

sales_agent = Agent(name="Sales Agent")

def transfer_to_sales():
return sales_agent

agent = Agent(functions=[transfer_to_sales])

response = client.run(agent, [{"role":"user", "content":"Transfer me to sales."}])
print(response.agent.name)

輸出:

Sales Agent

它還可以通過返回更完整的 Result 對象來更新 context_variables。如果你希望用單個函數(shù)返回一個值、更新智能體并更新上下文變量(或三者中的任何組合),它還可以包含一個 value 和一個 agent。

sales_agent = Agent(name="Sales Agent")

def talk_to_sales():
print("Hello, World!")
return Result(
value="Done",
agent=sales_agent,c
ontext_variables={"department": "sales"}
)

agent = Agent(functions=[talk_to_sales])

response = client.run(a
gent=agent,
messages=[{"role": "user", "content": "Transfer me to sales"}],
context_variables={"user_name": "John"}
)
print(response.agent.name)
print(response.context_variables)

輸出:

Sales Agent
{'department': 'sales', 'user_name': 'John'}

注意:如果一個 Agent 調(diào)用了多個交接 Agent 的函數(shù),則僅使用最后一個交接函數(shù)。

四、函數(shù)模式

Swarm 會自動將函數(shù)轉換為 JSON 模式,然后將其傳遞給聊天補全工具。

  • 文檔字符串會轉換為函數(shù) description。
  • 沒有默認值的參數(shù)會設置為 required。
  • 類型提示會映射到參數(shù)的 type(默認為 string)。
  • 不明確支持對每個參數(shù)進行描述,但如果只是在文檔字符串中添加,應該能以相似的方式工作。
def greet(name, age: int, location: str = "New York"):
"""Greets the user. Make sure to get their name and age before calling.
Args:
name: Name of the user.
age: Age of the user.
location: Best place on earth.
"""
print(f"Hello {name}, glad you are {age} in {location}!")
{"type": "function",
"function": {
"name": "greet",
"description": "Greets the user. Make sure to get their name and age before calling.\n\nArgs:\n name: Name of the user.\n age: Age of the user.\n location: Best place on earth.",
"parameters": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"location": {"type": "string"}},
"required": ["name", "age"]
}
}
}

五、流式處理

Swarm 也支持流式處理。

stream = client.run(agent, messages, stream=True)
for chunk in stream:
print(chunk)

使用了與 Chat Completions API streaming 一樣的事件,但添加了兩個事件類型:

  • {"delim":"start"} 和 {"delim":"start"},用于在 Agent 每次處理單個消息(響應或函數(shù)調(diào)用)時發(fā)出信號。這有助于識別 Agent 之間的切換。
  • 為方便起見,{"response": Response} 將在流的末尾返回帶有已聚合的(完整)響應的 Response 對象。

六、核心貢獻者

Swarm 的核心貢獻者均就職于 OpenAI,他們分別是(右側為對應的 GitHub 用戶名):

  • Ilan Bigio - ibigio
  • James Hills - jhills20
  • Shyamal Anadkat - shyamal-anadkat
  • Charu Jaiswal - charuj
  • Colin Jarvis - colin-openai

想了解更多AIGC的內(nèi)容,請訪問:

51CTO AI.x社區(qū)

http://www.scjtxx.cn/aigc/

責任編輯:武曉燕 來源: 51CTO技術棧
相關推薦

2024-10-12 12:30:18

2024-10-18 15:20:00

2024-10-14 10:00:00

OpenAI代碼

2025-03-12 11:34:35

2024-10-22 20:00:00

2024-03-25 00:30:00

AI框架

2021-12-02 16:20:17

開源微服務框架

2023-05-04 15:53:34

強化學習開發(fā)

2023-09-22 07:23:50

Alice模型任務

2024-11-29 08:26:37

2021-07-22 15:25:14

開源技術 框架

2025-02-05 08:30:00

開源模型實踐

2025-04-14 10:58:53

Agno多模態(tài)智能體LangGraph

2025-01-22 15:17:43

2023-05-05 17:49:23

2024-01-02 08:00:00

云計算容器Docker

2025-04-07 02:00:00

2017-01-19 11:10:20

5G網(wǎng)絡Open Baton虛擬網(wǎng)絡

2024-10-15 13:45:36

2025-03-12 13:04:01

點贊
收藏

51CTO技術棧公眾號