工具调用总体实现:

设计工具类:

复制代码

@Component
public class WeatherInquiryTools {

    @Autowired
    private WeatherService weatherService;

    @Tool(description = "根据城市名称查询城市LocationID")
    public String getLocationId(@ToolParam(description = "城市名称") String cityName){
        return weatherService.getLocationId(cityName).getLocation().get(0).getId();
    }

    @Tool(description = "根据城市LocationID查询实时温度")
    public String getWeather(@ToolParam(description = "城市LocationID") String locationId){
        return weatherService.getWeather(locationId).getNow().getTemp();
    }
}

复制代码

将工具交给ai客户端:

屏幕截图 2026-01-23 203014

 对话测试:

屏幕截图 2026-01-23 202500

 具体实现:

实体类:

复制代码

@NoArgsConstructor
@AllArgsConstructor
@Data
public class WeatherResponse {
    @JsonProperty("code")
    private String code;

    @JsonProperty("updateTime")
    private String updateTime;

    @JsonProperty("fxLink")
    private String fxLink;

    @JsonProperty("now")
    private NowData now;

    @JsonProperty("refer")
    private Refer refer;
}

复制代码

复制代码

@Data
@AllArgsConstructor
@NoArgsConstructor
public class NowData {

    // 观测时间
    @JsonProperty("obsTime")
    private String obsTime;

    // 温度,单位:摄氏度
    @JsonProperty("temp")
    private String temp;

    // 体感温度,单位:摄氏度
    @JsonProperty("feelsLike")
    private String feelsLike;

    // 天气图标代码
    @JsonProperty("icon")
    private String icon;

    // 天气描述
    @JsonProperty("text")
    private String text;

    // 风向360角度
    @JsonProperty("wind360")
    private String wind360;

    // 风向
    @JsonProperty("windDir")
    private String windDir;

    // 风力等级
    @JsonProperty("windScale")
    private String windScale;

    // 风速,单位:公里/小时
    @JsonProperty("windSpeed")
    private String windSpeed;

    // 相对湿度,单位:%
    @JsonProperty("humidity")
    private String humidity;

    // 降水量,单位:毫米
    @JsonProperty("precip")
    private String precip;

    // 大气压强,单位:百帕
    @JsonProperty("pressure")
    private String pressure;

    // 能见度,单位:公里
    @JsonProperty("vis")
    private String vis;

    // 云量,单位:%
    @JsonProperty("cloud")
    private String cloud;

    //露点温度,单位:摄氏度
    @JsonProperty("dew")
    private String dew;
}

复制代码

复制代码

@NoArgsConstructor
@AllArgsConstructor
@Data
public class LocationResponse {

    @JsonProperty("code")
    private String code;

    @JsonProperty("location")
    private List<Location> location;

    @JsonProperty("refer")
    private Refer refer;
}

复制代码

复制代码

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Location {

    @JsonProperty("name")
    private String name;

    @JsonProperty("id")
    private String id;

    @JsonProperty("lat")
    private String lat;

    @JsonProperty("lon")
    private String lon;

    @JsonProperty("adm2")
    private String adm2;

    @JsonProperty("adm1")
    private String adm1;

    @JsonProperty("country")
    private String country;

    @JsonProperty("tz")
    private String timezone;

    @JsonProperty("utcOffset")
    private String utcOffset;

    @JsonProperty("isDst")
    private String isDst;

    @JsonProperty("type")
    private String type;

    @JsonProperty("rank")
    private String rank;

    @JsonProperty("fxLink")
    private String fxLink;
}

复制代码

复制代码

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Refer {

    @JsonProperty("sources")
    private List<String> sources;

    @JsonProperty("license")
    private List<String> license;
}

复制代码

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐