Skip to content

Funcation Calling

案例

调用星图云开放平台的天气接口,可查询近2周指定地区的天气

准备资料

星图云开放平台接口

官方文档

官方示例

案例测试

http://localhost:8080/fc/weather?query

FjFanm_NppA0J3ZN4oKmv59aWdCA

1、北京石景山的天气怎么样,你可以调用currentWeather函数,北京石景山的location为WTX_CH101011000

FusW3hylNI-4QQUYCX28gmgBH1cD

2、北京石景山的天气怎么样,北京石景山的location为WTX_CH101011000

FtSe1Xlj4k-ODopB4pTgEUDcPj_h

FjjiHRYA5_WG2UOOBgbce-_lRYfF

代码片段

ImageDescriptionFunction

java
public class ImageDescriptionFunction
        implements Function<ImageDescriptionFunction.Request, ImageDescriptionFunction.Response> {

    // 回调
    @Override
    public Response apply(Request request) {
        if(request.name == null){
            return new Response("请提供图片名称");
        }
        return new Response("图片名称为:" + request.name);
    }

    @Data
    public class Request{
        public Request(String name){
            this.name = name;
        }
        private String name;
    }

    @Data
    public class Response{
        public Response(String description){
            this.description = description;
        }
        String description;
    }
}

WeatherService

java
@Service
public class WeatherService implements Function<Weather.Request, Weather.Response> {

    @Autowired
    WeatherServiceBuilder weatherServiceBuilder;

    @Override
    public Weather.Response apply(Weather.Request request) {
        return weatherServiceBuilder.getWeather(request.location());
    }
}

WeatherServiceBuilder

java
@Service
public class WeatherServiceBuilder {

    @Value("${spring.weather.api.base-url}")
    String weatherBaseUrl;

    @Value("${spring.weather.api.key}")
    String weatherApiKey;


    @Autowired
    RestClient restClient;
    public Weather.Response getWeather(String location){
        return restClient.get()
                .uri(UriComponentsBuilder.fromUriString(weatherBaseUrl)
                        .path("/v2/cn/city/basic")
                        .queryParam("token", weatherApiKey)
                        .queryParam("location",location)
                        .toUriString())
                .retrieve()
                .body(Weather.Response.class);
    }
}

Controller

java
@Autowired
OpenAiChatModel chatModel;

@Autowired
WeatherServiceBuilder weatherServiceBuilder;

// query=北京石景山的天气怎么样,你可以调用currentWeather函数,北京石景山的location为WTX_CH101011000
@GetMapping("/weather")
public String weather(@RequestParam(value = "query") String query) {
    try {
        UserMessage userMessage = new UserMessage(query);
        OpenAiChatOptions options = OpenAiChatOptions.builder()
                .withFunction("currentWeather")
                .withModel(OpenAiApi.ChatModel.GPT_4)
                .build();

        log.info("query: {}", query);
        ChatResponse response = chatModel.call(new Prompt(userMessage, options));
        log.info("Received response from OpenAI: {}", response.getResult().getOutput().getContent());

        return response.getResult().getOutput().getContent();
    } catch (Exception e) {
        log.error("Error processing weather request", e);
        return e.getMessage();
    }
}

Weather实体

java
@Data
public class Weather {
    public record Request(String location){};

    // 解析接口数据
    public record Response(Location location,Result result){};
    public record Result(ArrayList<ResultItem> datas){};
    public record ResultItem(String fc_time,String tem_min,String tem_max){}

    public record Location(String path,String areaCode){}
}