调用工具:文件读写测试
This commit is contained in:
parent
7d65f1ced7
commit
18107370b0
1
.gitignore
vendored
1
.gitignore
vendored
@ -32,3 +32,4 @@ build/
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
/src/main/resources/application-dev.yml
|
||||
/tmp/
|
||||
|
@ -8,4 +8,8 @@ package com.huangge1199.aiagent.Service;
|
||||
*/
|
||||
public interface ToolsService {
|
||||
String getWeather(String question);
|
||||
|
||||
String writeFileTest(String context, String name);
|
||||
|
||||
String readFileTest(String name);
|
||||
}
|
||||
|
@ -2,7 +2,8 @@ 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 com.huangge1199.aiagent.tools.FileTool;
|
||||
import com.huangge1199.aiagent.tools.WeatherTool;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.ollama.OllamaChatModel;
|
||||
@ -25,7 +26,19 @@ public class ToolsServiceImpl implements ToolsService {
|
||||
return ChatClient.create(ollamaChatModel)
|
||||
.prompt(question)
|
||||
.advisors(new MyLoggerAdvisor())
|
||||
.tools(new WeatherTools())
|
||||
.tools(new WeatherTool())
|
||||
.call().content();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String writeFileTest(String context, String name) {
|
||||
FileTool fileTool = new FileTool();
|
||||
return fileTool.writeFile(name,context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readFileTest(String name) {
|
||||
FileTool fileTool = new FileTool();
|
||||
return fileTool.readFile(name);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,10 @@
|
||||
package com.huangge1199.aiagent.controller;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.alibaba.cloud.nacos.utils.StringUtils;
|
||||
import com.huangge1199.aiagent.Service.ToolsService;
|
||||
import com.huangge1199.aiagent.common.R;
|
||||
import com.huangge1199.aiagent.util.CheckUtils;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
@ -33,4 +36,24 @@ public class ToolController {
|
||||
String result = toolsService.getWeather(question);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
@PostMapping("/writeFile")
|
||||
@Operation(summary = "写文件测试")
|
||||
public R<?> writeFile(@RequestBody JSONObject params) {
|
||||
String context = params.getStr("context");
|
||||
String name = params.getStr("name");
|
||||
CheckUtils.checkEmpty(context, "内容");
|
||||
CheckUtils.checkEmpty(name, "文件名");
|
||||
String result = toolsService.writeFileTest(context, name);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
@PostMapping("/readFile")
|
||||
@Operation(summary = "读文件测试")
|
||||
public R<?> readFile(@RequestBody JSONObject params) {
|
||||
String name = params.getStr("name");
|
||||
CheckUtils.checkEmpty(name, "文件名");
|
||||
String result = toolsService.readFileTest(name);
|
||||
return R.ok(result);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
package com.huangge1199.aiagent.tools;
|
||||
|
||||
/**
|
||||
* FileConstant
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/5/27 16:50:37
|
||||
*/
|
||||
public interface FileConstant {
|
||||
|
||||
String FILE_SAVE_DIR = System.getProperty("user.dir") + "/tmp";
|
||||
}
|
42
src/main/java/com/huangge1199/aiagent/tools/FileTool.java
Normal file
42
src/main/java/com/huangge1199/aiagent/tools/FileTool.java
Normal file
@ -0,0 +1,42 @@
|
||||
package com.huangge1199.aiagent.tools;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import org.springframework.ai.tool.annotation.Tool;
|
||||
import org.springframework.ai.tool.annotation.ToolParam;
|
||||
|
||||
/**
|
||||
* FileTools
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/5/27 16:51:46
|
||||
*/
|
||||
public class FileTool {
|
||||
|
||||
private final String FILE_DIR = FileConstant.FILE_SAVE_DIR + "/file";
|
||||
|
||||
@Tool(description = "Read content from a file")
|
||||
public String readFile(@ToolParam(description = "Name of the file to read") String fileName) {
|
||||
String filePath = FILE_DIR + "/" + fileName;
|
||||
try {
|
||||
return FileUtil.readUtf8String(filePath);
|
||||
} catch (Exception e) {
|
||||
return "Error reading file: " + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
@Tool(description = "Write content to a file")
|
||||
public String writeFile(
|
||||
@ToolParam(description = "Name of the file to write") String fileName,
|
||||
@ToolParam(description = "Content to write to the file") String content) {
|
||||
String filePath = FILE_DIR + "/" + fileName;
|
||||
try {
|
||||
// 创建目录
|
||||
FileUtil.mkdir(FILE_DIR);
|
||||
FileUtil.writeUtf8String(content, filePath);
|
||||
return "File written successfully to: " + filePath;
|
||||
} catch (Exception e) {
|
||||
return "Error writing to file: " + e.getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import org.springframework.ai.tool.annotation.ToolParam;
|
||||
* @author huangge1199
|
||||
* @since 2025/5/27 15:01:04
|
||||
*/
|
||||
public class WeatherTools {
|
||||
public class WeatherTool {
|
||||
|
||||
@Tool(description = "Get current weather for a location")
|
||||
public String getWeather(@ToolParam(description = "The city name") String city) {
|
20
src/main/java/com/huangge1199/aiagent/util/CheckUtils.java
Normal file
20
src/main/java/com/huangge1199/aiagent/util/CheckUtils.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.huangge1199.aiagent.util;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* CheckUtils
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/5/28 10:12:57
|
||||
*/
|
||||
@Component
|
||||
public class CheckUtils {
|
||||
|
||||
public static void checkEmpty(String str, String name) {
|
||||
if (StringUtils.isEmpty(str)) {
|
||||
throw new RuntimeException(name + "不能为空!");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user