OpenAI推出Swarm技术:开启真正的智能代理框架新时代?
最近,OpenAI宣布了其新的“Swarm”技术。Swarm是一个教育和实验性的开源框架,旨在简化多个AI代理的协调和管理。该框架允许开发人员更有效地部署、编排和测试基于代理的系统。如果你想了解更多关于OpenAI的相关内容,可以阅读以下这些文章:
OpenAI新的嵌入模型和API更新
OpenAI刚刚证明了人类并没有为即将到来的事情做好准备
OpenAI近期发布了GPTs:创建自己的ChatGPT并从中赚钱(无需编码)
OpenAI是否秘密创造了类脑智能?
Swarm引入了两个核心概念:
- Agents:像OpenAI助手一样,代理执行特定的任务。每个代理都知道自己需要做什么,并拥有帮助完成工作的工具。如果一个代理不能单独完成任务,可以通过切换将其传递给另一个代理。
- Handoffs:允许代理将控制传递给更适合手头任务的其他代理。
在需要同时管理多个独立任务的情况下,Swarm框架具有优势。与处理有状态交互的助手API相比,Swarm的功能是无状态的,允许开发人员完全透明并精确控制每个代理使用的任务和工具。其轻量级和高度可定制的特性使其非常适合需要模块化和可伸缩性的复杂系统。
这些都是积极的一面,但也有一些消极的一面。其中最严重的指控可能是OpenAI实际上盗用了另一个GitHub项目(也称为Swarm)的Swarm的想法和代码。
使用下面的链接查看GitHub:
https://github.com/kyegomez/swarms?source=post_page—–03462692afc1——————————–
“原始”swarm的领导者Kyle Gomez公开指责openAI窃取了他的代码和想法。看看这个推特帖子。
先把它放在一边,在本文的其余部分,我将展示一些示例代码,说明如何使用Swarm – openAI版本,即:-)
设置开发环境
我用Conda来做这个,但你可以用你习惯的方法。
设置好环境后,请确保:
安装swarm, openAI和jupyter
pip install git+https://github.com/openai/swarm.git
pip install jupyter
pip install openai
将OpenAI密钥设置为环境变量
# On Windows
set OPENAI_API_KEY=your-api-key-here
# On Linux
export OPENAI_API_KEY=your-api-key-here
示例代码- Hello world
关于Swarm,我要说的第一件事是,实现它似乎非常简单。openAI为Swarm开发的“hello world”版本很好地证明了这一点。
import openai
import os
# **Step 2: Import Swarm and Agent After Setting the API Key**
from swarm import Swarm, Agent
# **Step 3: Initialize Swarm and Use It**
client = Swarm()
agent = Agent(
name="Agent",
instructions="You are a helpful agent.",
)
messages = [{"role": "user", "content": "Hi!"}]
response = client.run(agent=agent, messages=messages)
print(response.messages[-1]["content"])
在Jupyter Notebook中运行上述代码应该会得到以下响应:
Hello! How can I assist you today?
示例代码 – Virtual classroom assistant
这是一个更复杂但完整的示例。假设您扮演的角色是为教室助理编写代码。你可以有几个代理,他们可以理解并回答学生的各种问题。例如,下面的代码通过将回答这些问题的任务交给最合适的代理来回答五个不同的问题。
from swarm import Swarm, Agent
# Initialize the Swarm client
client = Swarm()
def transfer_to_math_agent():
print("Transferring to the Math Agent...")
return math_agent
def transfer_to_science_agent():
print("Transferring to the Science Agent...")
return science_agent
def transfer_to_admissions_agent():
print("Transferring to the Admissions Agent...")
return admissions_agent
def transfer_to_tech_support_agent():
print("Transferring to the Tech Support Agent...")
return tech_support_agent
# Dispatch Agent
dispatcher = Agent(
name="Dispatcher Agent",
instructions="You help users by directing their query to the agent that's best placed to answer.",
functions=[transfer_to_math_agent, transfer_to_science_agent,transfer_to_admissions_agent,transfer_to_tech_support_agent],
)
# Define agents with specific instructions
admissions_agent = Agent(
name="Admissions Agent",
instructions="Handle inquiries about admissions, enrollment, and registration."
)
math_agent = Agent(
name="Math Agent",
instructions="Answer questions related to mathematics."
)
science_agent = Agent(
name="Science Agent",
instructions="Answer questions related to natural sciences."
)
tech_support_agent = Agent(
name="Tech Support Agent",
instructions="Assist with technological issues, software problems, and digital resources."
)
# Simulate a session with messages
messages = [
{"role": "user", "content": "How do I apply for the biology program?"},
{"role": "user", "content": "What is the integral of x^2 from 0 to 1?"},
{"role": "user", "content": "My laptop won’t connect to the campus Wi-Fi."},
{"role": "user", "content": "When is the registration deadline for new students?"},
{"role": "user", "content": "What is the mass-energy equivalence equation in Physics?"},
]
# Run the system for each message
for message in messages:
dispatch_agent=dispatcher
response = client.run(agent=dispatch_agent, messages=[message])
print("Message:", message['content'])
# Iterate over each message in the response
for msg in response.messages:
print(f"Role: {msg.get('role', 'Unknown')}")
print(f"Sender: {msg.get('sender', 'Unknown Sender')}")
print(f"Content: {msg.get('content', '')}")
print("----------------------------------------------------")
这是你可以期望从上面得到的典型输出:
Transferring to the Admissions Agent...
Message: How do I apply for the biology program?
Role: assistant
Sender: Dispatcher Agent
Content: None
----------------------------------------------------
Role: tool
Sender: Unknown Sender
Content: {"assistant": "Admissions Agent"}
----------------------------------------------------
Role: assistant
Sender: Admissions Agent
Content: I'll connect you to our Admissions Agent who can provide detailed information on applying to the biology program. Please hold on.
----------------------------------------------------
Transferring to the Math Agent...
Message: What is the integral of x^2 from 0 to 1?
Role: assistant
Sender: Dispatcher Agent
Content: None
----------------------------------------------------
Role: tool
Sender: Unknown Sender
Content: {"assistant": "Math Agent"}
----------------------------------------------------
Role: assistant
Sender: Math Agent
Content: To find the integral of \( x^2 \) from 0 to 1, you calculate:
\[
\int_{0}^{1} x^2 \, dx = \left[ \frac{x^3}{3} \right]_{0}^{1} = \frac{1^3}{3} - \frac{0^3}{3} = \frac{1}{3}
\]
So, the integral of \( x^2 \) from 0 to 1 is \( \frac{1}{3} \).
----------------------------------------------------
Transferring to the Tech Support Agent...
Message: My laptop won’t connect to the campus Wi-Fi.
Role: assistant
Sender: Dispatcher Agent
Content: None
----------------------------------------------------
Role: tool
Sender: Unknown Sender
Content: {"assistant": "Tech Support Agent"}
----------------------------------------------------
Role: assistant
Sender: Tech Support Agent
Content: For resolving laptop Wi-Fi issues on campus, please follow these steps:
1. **Check Wi-Fi Settings**: Ensure that Wi-Fi is turned on. Look for a Wi-Fi symbol or use the physical switch if your laptop has one.
2. **Restart Your Device**: Turn off your laptop and turn it back on. This simple step can solve connectivity issues.
3. **Forget and Reconnect**: Go to the Wi-Fi settings on your laptop, find the campus network, and select "Forget." Then try reconnecting by entering the credentials again.
4. **Update Network Drivers**: Go to your laptop’s Device Manager, find your network adapter, and ensure its drivers are up to date.
5. **Check Network Availability**: Confirm if other devices can connect to the campus Wi-Fi. This helps determine if the issue is with the network or your laptop.
6. **Disable VPN or Proxy**: Sometimes, VPNs or proxies can interfere with Wi-Fi connections. Try disabling them to see if the issue is resolved.
7. **Contact IT Support**: If the above steps don't work, there might be an issue with your network profile or the network itself. Contact your campus IT support for assistance.
If you provide more details about your laptop model and OS, I can offer more specific guidance.
----------------------------------------------------
Transferring to the Admissions Agent...
Message: When is the registration deadline for new students?
Role: assistant
Sender: Dispatcher Agent
Content: None
----------------------------------------------------
Role: tool
Sender: Unknown Sender
Content: {"assistant": "Admissions Agent"}
----------------------------------------------------
Role: assistant
Sender: Admissions Agent
Content: I'm sorry, but I would need more specific information such as the name of the institution you're inquiring about, and the semester or program you are interested in. Generally, registration deadlines can vary greatly depending on the university or college, and the specific term or program. It's best to check directly with the institution's admissions office or their official website for the most accurate information.
----------------------------------------------------
Transferring to the Science Agent...
Message: What is the mass-energy equivalence equation in Physics?
Role: assistant
Sender: Dispatcher Agent
Content: None
----------------------------------------------------
Role: tool
Sender: Unknown Sender
Content: {"assistant": "Science Agent"}
----------------------------------------------------
Role: assistant
Sender: Science Agent
Content: The mass-energy equivalence equation in physics is expressed by Einstein's famous equation:
\[ E = mc^2 \]
where:
- \( E \) is the energy equivalent of a mass,
- \( m \) is the mass,
- \( c \) is the speed of light in a vacuum (approximately \( 3 \times 10^8 \) meters per second).
This equation indicates that mass and energy are interchangeable; they are different forms of the same entity. An object's mass can be converted into energy and vice versa.
----------------------------------------------------
总结
Swarm是一个前沿框架,有助于创建复杂的多代理生态系统,以模块化和可扩展的方式解决现实世界中的复杂挑战。
在本文中,我介绍了Swarm的主题,强调了开发者在这个新框架中应关注的两个关键概念:代理和控制传递。
还展示了一些示例代码,从一个简单的“Hello World”应用程序到一个更复杂且实际的多代理应用程序。
尽管Swarm仍处于实验阶段,但它的推出显然表明了OpenAI在发展方向上的新思路。
我很期待看到大家在这个框架下所做的项目,以及你们想到的用例。请在评论中分享你的想法。
感谢阅读!你还可以订阅我们的YouTube频道,观看大量大数据行业相关公开课:https://www.youtube.com/channel/UCa8NLpvi70mHVsW4J_x9OeQ;在LinkedIn上关注我们,扩展你的人际网络!https://www.linkedin.com/company/dataapplab/。
原文作者:Thomas Reid
翻译作者:过儿
美工编辑:过儿
校对审稿:Jason
原文链接:https://medium.com/ai-advances/open-ai-announces-swarm-technology-03462692afc1