基于Swarm的大模型应用:一个天气助手

本次实践主要是基于open Ai 的swarm与deepSeek模型,定制一个大模型智能体,帮助我们认识swam(多智能体写作文)以及如何使用;

1 deekSeeK

注册:DeepSeek

申请自己的apiKey:******;

注意:model="deepseek-chat", 这一个参数我们需要在OpenAi 中配置;

复制代码
from openai import OpenAI

client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": "Hello"},
    ],
    stream=False
)

print(response.choices[0].message.content)

2 python环境

注意:安装swarm需要依赖于3.10.0版本;

pip install git+https://github.com/openai/swarm.git

3 代码

swarm:是一个集群

Agent:是一个具体的工作流;

python 复制代码
import requests
import os

from swarm import Swarm, Agent
import openai



def get_weather(latitude: float, longitude: float) -> str:
    """
    使用Open-Meteo API获取给定坐标的当前天气。

    参数:
    latitude (float): 纬度
    longitude (float): 经度

    返回:
    str: 包含当前温度和风速的天气信息JSON字符串
    """
    url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&current=temperature_2m,wind_speed_10m"
    
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        current = data['current']
        return str(current)  # 返回JSON字符串
    else:
        return '{"error": "无法获取天气数据"}'

def get_coordinates(location: str) -> str:
    """
    获取给定位置名称的坐标。

    参数:
    location (str): 位置名称

    返回:
    str: 包含纬度和经度的JSON字符串
    """
    #print('11111111')
    coordinates = {
        "纽约": (40.7128, -74.0060),
        "伦敦": (51.5074, -0.1278),
        "东京": (35.6762, 139.6503),
        "巴黎": (48.8566, 2.3522),
        "柏林": (52.5200, 13.4050)
    }
    lat, lon = coordinates.get(location.lower(), (0, 0))
    print(f"获取当前的时间:{location}, {lat}")
    return f'{{"latitude": {lat}, "longitude": {lon}}}'

from openai import OpenAI
# 自己api_key

open_ai = OpenAI(api_key="*****"
                 ,base_url='https://api.deepseek.com/')

client = Swarm(client=open_ai)

weather_agent = Agent(
    name="天气助手",
    instructions="""
    你是一个有帮助的天气助手。当被问到特定位置的天气时:
    1. 使用get_coordinates函数获取该位置的坐标。
    2. 使用get_weather函数获取天气数据。
    3. 解析返回的JSON数据,提供一个友好的回复,包含天气信息。
    如果无法识别该位置,礼貌地通知用户并建议他们尝试一个主要城市。
    """,
    model='deepseek-chat', # 这个参数很重要,不然总是报错
    functions=[get_coordinates, get_weather]
)

def run_weather_query(query: str) -> str:
    """
    运行天气查询并返回结果。

    参数:
    query (str): 用户的天气查询

    返回:
    str: Agent的响应
    """
    messages = [{"role": "user", "content": query}]
    #print("11111222222")
    response = client.run(agent=weather_agent, messages=messages)
    return response.messages[-1]["content"]

# 使用示例
print(run_weather_query("纽约的天气怎么样?"))
相关推荐
双向332 分钟前
私有化部署全攻略:开源模型本地化改造的性能与安全评测
人工智能
189228048613 分钟前
NY243NY253美光固态闪存NY257NY260
大数据·网络·人工智能·缓存
AI波克布林4 分钟前
发文暴论!线性注意力is all you need!
人工智能·深度学习·神经网络·机器学习·注意力机制·线性注意力
张子夜 iiii4 分钟前
机器学习算法系列专栏:主成分分析(PCA)降维算法(初学者)
人工智能·python·算法·机器学习
GIS宇宙7 分钟前
五分钟免费开启你的Vibe Coding之旅!
人工智能
用户5191495848458 分钟前
Three.js实例化技术:高效渲染数千3D对象
人工智能·aigc
weixin_456904279 分钟前
一文讲清楚Pytorch 张量、链式求导、正向传播、反向求导、计算图等基础知识
人工智能·pytorch·学习
ZLRRLZ13 分钟前
【C++】C++11
开发语言·c++
ciku21 分钟前
Spring AI Starter和文档解读
java·人工智能·spring
全栈软件开发23 分钟前
PHP域名授权系统网站源码_授权管理工单系统_精美UI_附教程
开发语言·ui·php·php域名授权·授权系统网站源码