调用工具:获取天气

This commit is contained in:
huangge1199 2025-05-27 16:21:15 +08:00
parent 7b2dfa43e9
commit 7d65f1ced7
5 changed files with 97 additions and 1 deletions

View File

@ -0,0 +1,11 @@
package com.huangge1199.aiagent.Service;
/**
* ToolsService
*
* @author huangge1199
* @since 2025/5/27 15:06:53
*/
public interface ToolsService {
String getWeather(String question);
}

View File

@ -0,0 +1,31 @@
package com.huangge1199.aiagent.Service.impl;
import com.huangge1199.aiagent.Service.ToolsService;
import com.huangge1199.aiagent.config.MyLoggerAdvisor;
import com.huangge1199.aiagent.tools.WeatherTools;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.stereotype.Service;
/**
* ToolsServiceImpl
*
* @author huangge1199
* @since 2025/5/27 15:07:06
*/
@Service
public class ToolsServiceImpl implements ToolsService {
@Resource
private OllamaChatModel ollamaChatModel;
@Override
public String getWeather(String question) {
return ChatClient.create(ollamaChatModel)
.prompt(question)
.advisors(new MyLoggerAdvisor())
.tools(new WeatherTools())
.call().content();
}
}

View File

@ -0,0 +1,36 @@
package com.huangge1199.aiagent.controller;
import com.huangge1199.aiagent.Service.ToolsService;
import com.huangge1199.aiagent.common.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* ToolController
*
* @author huangge1199
* @since 2025/5/27 14:59:32
*/
@RestController
@RequestMapping("/tool")
@Tag(name = "工具调用")
public class ToolController {
@Resource
private ToolsService toolsService;
@PostMapping("/getWeather")
@Operation(summary = "获取天气")
public R<String> getWeather(@RequestBody String question) {
String result = toolsService.getWeather(question);
return R.ok(result);
}
}

View File

@ -0,0 +1,18 @@
package com.huangge1199.aiagent.tools;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
/**
* WeatherTools
*
* @author huangge1199
* @since 2025/5/27 15:01:04
*/
public class WeatherTools {
@Tool(description = "Get current weather for a location")
public String getWeather(@ToolParam(description = "The city name") String city) {
return "Current weather in " + city +": Sunny, 25°";
}
}

View File

@ -11,7 +11,7 @@ spring:
ollama: ollama:
base-url: http://192.168.188.2:11435 base-url: http://192.168.188.2:11435
chat: chat:
model: gemma3:1b model: llama3.2:3b
vectorstore: vectorstore:
pgvector: pgvector:
index-type: HNSW index-type: HNSW