欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > OpenAI swarm+ Ollama快速构建本地多智能体服务 - 1. 服务构建教程

OpenAI swarm+ Ollama快速构建本地多智能体服务 - 1. 服务构建教程

2024/10/23 23:13:40 来源:https://blog.csdn.net/zhuchengzhang/article/details/143032475  浏览:    关键词:OpenAI swarm+ Ollama快速构建本地多智能体服务 - 1. 服务构建教程

OpenAI开源了多智能体编排的工程swarm,今天介绍一下swarm与OLLAMA如何结合使用的教程,在本地构建自己的多智能体服务,并给大家实践演示几个案例。

安装步骤

  • 安装ollama,在官网下载对应操作系统的版本即可,下载后用ollama部署大模型,网上教程很多,本文不再描述。
  • 安装ollama的python接口:
pip install ollama
  • 安装swarm框架:
pip install git+https://github.com/openai/swarm.git

注意,这一步的前提是已经安装了git,如果本地没有安装请先行下载安装

服务构建示例

swarm官网给出的使用示例如下:

from swarm import Swarm, Agentclient = Swarm()def transfer_to_agent_b():return agent_bagent_a = Agent(name="Agent A",instructions="You are a helpful agent.",functions=[transfer_to_agent_b],
)agent_b = Agent(name="Agent B",instructions="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"])

在这个示例的基础上,与ollama以及在ollama中安装的大模型结合的方法如下:


from swarm import Swarm, Agent# 利用OpenAI的接口(安装swarm时会自动下载),建立与ollama服务连接的客户端
from openai import OpenAI
ollama_client = OpenAI(base_url = 'http://localhost:11434/v1',api_key='ollama', # required, but unused
)# 在swarm构建时,指定与ollama连接的客户端
client = Swarm(client=ollama_client)def transfer_to_agent_b():return agent_bagent_a = Agent(name="Agent A",model="qwen2.5:7b",   # 在构建智能体时指定ollama中的模型,传入在ollama中构建好的大模型名称即可,例如qwen2.5:7binstructions="You are a helpful agent.",functions=[transfer_to_agent_b],
)agent_b = Agent(name="Agent B",model="qwen2.5:7b",   # 在构建智能体时指定ollama中的模型,传入在ollama中构建好的大模型名称即可,例如qwen2.5:7binstructions="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"])

总结一下,若想将swarmollama结合,比官方示例多了三步,非常简单:

  1. 利用OpenAI的接口(安装swarm时会自动下载),建立与ollama服务连接的客户端
  2. 在swarm构建时,指定与ollama连接的客户端
  3. 在构建智能体时指定ollama中的模型

大家可以利用上述三个步骤自行尝试改造swarm开源代码中提供的示例进行尝试,后面有时间也会讲讲swarm框架的具体用法供大家参考。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com