iT邦幫忙

2024 iThome 鐵人賽

1
生成式 AI

使用 Spring AI 打造企業 RAG 知識庫系列 第 31

使用 Spring AI 打造企業 RAG 知識庫【31】- 取得真正的氣象資料

  • 分享至 

  • xImage
  •  

有了AI寫程式都可以偷懶

https://ithelp.ithome.com.tw/upload/images/20240909/201612907BPUH9n27l.jpg

還記得 Day11 提到要取得即時資訊只能夠過 Function Calling 吧,網路上一堆教學都使用模擬資料,今天凱文大叔來教大家怎麼抓中央氣象局的資料吧

▋取得中央氣象局開放平台資料

中央氣象局有個資料開放平台:https://opendata.cwa.gov.tw/index

註冊後就能在 會員資訊-取得授權碼 拿到自己的授權碼
https://ithelp.ithome.com.tw/upload/images/20240909/20161290up6bZ6KQlE.png

可以在資料主題找需要的資料,由於我只要抓到目前的氣象資訊,所以選擇 觀測-現在天氣觀測報告

最好找有 API 的,不然需要下載檔案再用讀檔的方式處理

https://ithelp.ithome.com.tw/upload/images/20240909/20161290EJkKMWlIYg.png

進入後就是 API 常用的 Swagger 網站了
https://ithelp.ithome.com.tw/upload/images/20240909/20161290hFRef7D2l7.png

點選 Try it out 輸入授權碼,按下 Execute 就能取得 JSon 資料了
https://ithelp.ithome.com.tw/upload/images/20240909/20161290DoyYhUY2D2.png

▋程式碼實作

上面那串URL就是我們要在程式中呼叫的 API,接下來看程式如何撰寫吧

  1. 首先我們還是要先寫一支實作 Function<Request, Response> 的 Service,這隻程式就是要去氣象開放資料平台取得 Json 資料的類別
@Service
public class WeatherService implements Function<WeatherService.Request, WeatherService.Response>{

    private final RestTemplate restTemplate;  //呼叫API的物件
    private final String apiUrl = "https://opendata.cwa.gov.tw/api/v1/rest/datastore/O-A0003-001";
    @Value("${CWA_API_KEY}")
    private String apiKey;
    
    public WeatherService(RestTemplateBuilder restTemplateBuilder) {
        this.restTemplate = restTemplateBuilder.build();
    }
    
	public JsonNode getFullWeatherData() {
		String fullUrl = apiUrl + "?Authorization=" + apiKey;
        try {
		        //直接將API結果轉成Json物件
            JsonNode response = restTemplate.getForObject(fullUrl, JsonNode.class);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
	
	@Override
	public Response apply(Request request) {
		return new Response(getFullWeatherData());
	}
	public record Request(String State){  //這裡的Request參數其實沒任何作用
	}
	public record Response(JsonNode currWehther) {  //將所有的Json內容傳回,讓AI自行解析
	}
}

由於氣象局的Json內容太複雜,我們也不容易取得各個站點名稱,所以就不帶入任何參數,直接將所有的內容返回

  1. 接著要產生 AI Bean,這要在 Config 類別中加入
@Configuration
@RequiredArgsConstructor
public class AiConfig {	
	@Autowired
	private WeatherService weatherService;

	@Bean
    public FunctionCallback currectWeather() {
        return FunctionCallbackWrapper.builder(weatherService)
                .withName("CurrectWeather")
                .withDescription("Get Currect Weather")
                .build();
    }
}
  1. 將 Function 註冊進 Model 中
@RestController
@RequiredArgsConstructor
public class FunctionCallingController {
    private final ChatModel chatModel;

    @GetMapping("/func")
    public String func(String prompt) {        
        return chatModel.call(new Prompt(prompt, 
				                         OpenAiChatOptions.builder()
				                         .withFunction("CurrectWeather")
				                         .build())
        		).getResult().getOutput().getContent();
    }
}

▋驗收成果

prompt=桃園目前天氣

https://ithelp.ithome.com.tw/upload/images/20240909/20161290mhuC5OMja5.png

prompt=全台最高溫的前三名

https://ithelp.ithome.com.tw/upload/images/20240909/20161290o5GJhYUwbe.png

可以看到 AI 不只是呼叫 Function 取得資料,還能幫我們進行統計分析,不過還是要注意一下,氣象局的原始資料非常大,很容易超過每次傳送的 Token 上限,若能做一些篩選在送出除了能避免錯誤外也能讓資料更為精準

▋回顧

今天學到的內容:

  1. 如何取得真實氣象資訊
  2. Spring Boot 呼叫 API 的方式
  3. AI 能幫我們分析 Json 資料

可以將瑣碎的內容處理交給 AI 處理

▋Source Code

今日的程式碼: https://github.com/kevintsai1202/SpringBoot-AI-Day31.git


▋認識凱文大叔

凱文大叔使用 Java 開發程式超過 20 年,對於 Java 生態非常熟悉,曾使用反射機制開發 ETL 框架,對 Spring 背後的原理非常清楚,目前以 Spring Boot 作為後端開發框架,前端使用 React 搭配 Ant Design
下班之餘在 Amazing Talker 擔任程式語言講師,並獲得學員的一致好評

最近剛成立一個粉絲專頁-凱文大叔教你寫程式 歡迎大家多追蹤,我會不定期分享實用的知識以及程式開發技巧

想討論 Spring 的 Java 開發人員可以加入 FB 討論區 Spring Boot Developer Taiwan

我是凱文大叔,歡迎一起加入學習程式的行列


上一篇
使用 Spring AI 打造企業 RAG 知識庫【30】- 是結束也是開始
下一篇
使用 Spring AI 打造企業 RAG 知識庫【32】 - 提高RAG準確率從Embedding換起
系列文
使用 Spring AI 打造企業 RAG 知識庫33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言