内测QQ群:832615113
自定义函数开发:
- 打开Groovy控制台
- 定义以下俩注解
import java.lang.annotation.* @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented @interface Parameter { String value() default ""; String name() default ""; boolean required() default false; } @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @interface GPTFunction { String value() default ""; String name() default ""; }
- 定义一个方法参数、参数必须使用一个实体类定义,并且使用@Parameter注解标注字段
class Command { @Parameter(name = "command", value = "一个完整的cmd命令脚本", required = true) String command; }
- 定义一个Groovy函数 函数必须使用@GPTFunction注解标注
@GPTFunction(name="executeCommand",value = "执行cmd脚本,参数必须可以在客户端本地主机直接执行") def String executeCommand(Command command) { StringBuilder output = new StringBuilder(); output.append("执行成功:") try { ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.directory(new File("C://")) processBuilder.command("cmd.exe", "/c", command.command) processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { output.append(line).append("\n"); } process.waitFor(); } catch (IOException | InterruptedException e) { e.printStackTrace(); println("IOException: ${e.getMessage()}") } return output.toString(); }
- 继续 写个方法调用、Ctrl+Shilt+F10 运行测试一下
getCurrentWeather(latitudeAndLongitude) /*******************************TEST**************************************/ command = new Command() command.command = "ipconfig" println(executeCommand(command))
-
执行成功:
- 选中脚本并加载:
-
现在X-ChatGPT可以在你的主机上执行命令了!
- 附一个 完整函数脚本 包含了 3个函数 :根据经纬度查询天气、 本地命令执行、github仓库搜索。安装本文上方附件插件后将本脚本保存至项目任意目录 文件名xxx.groovy、右键>加载GPT回调函数即可
import groovy.json.JsonSlurper import java.lang.annotation.* @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented @interface Parameter { String value() default ""; String name() default ""; boolean required() default false; } @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @interface GPTFunction { String value() default ""; String name() default ""; } class LatitudeAndLongitude { @Parameter(name = "latitude", value = "经度", required = true) public double latitude; @Parameter(name = "longitude", value = "维度") public double longitude } @GPTFunction(name = "getCurrentWeather", value = "根据经纬度获取天气信息") getCurrentWeather(LatitudeAndLongitude latitudeAndLongitude) { //获取天气信息 def apiKey = '825dfb2693524a968515c7ad58e72122' def apiUrl = "https://api.weatherbit.io/v2.0/current?&lat=${latitudeAndLongitude.latitude}&lon=${latitudeAndLongitude.longitude}&lang=zh&key=${apiKey}" def connection = new URL(apiUrl).openConnection() as HttpURLConnection connection.setRequestMethod("GET") try { def responseCode = connection.getResponseCode() if (responseCode == HttpURLConnection.HTTP_OK) { def reader = new BufferedReader(new InputStreamReader(connection.getInputStream())) StringBuilder response = new StringBuilder() String line while ((line = reader.readLine()) != null) { response.append(line) } reader.close() println(response.toString()) return response.toString(); } else { println("Request failed. Response Code: $responseCode") } } catch (Exception e) { println("An error occurred: ${e.getMessage()}") } finally { connection.disconnect() } return "" } class Command { @Parameter(name = "command", value = "一个完整的cmd命令脚本", required = true) String command; } /** * 执行cmd脚本,参数必须可以在客户端本地主机直接执行 * * @param command 命令对象,包含要执行的cmd命令脚本 * @return 返回cmd命令执行结果字符串 */ @GPTFunction(name = "executeCommand", value = "执行cmd脚本,参数必须可以在客户端本地主机直接执行") /** * 执行命令并返回执行结果 * @param command 命令对象 * @return 执行结果字符串 */ String executeCommand(Command command) { StringBuilder output = new StringBuilder(); output.append("执行成功:"); try { ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.directory(new File("C://")); processBuilder.command("cmd.exe", "/c", command.command); processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK")); String line; // 逐行读取命令输出并添加到结果字符串中 while ((line = reader.readLine()) != null) { output.append(line).append("\n"); } process.waitFor(); } catch (IOException | InterruptedException e) { e.printStackTrace(); println("IOException: ${e.getMessage()}"); } return output.toString(); } class Keyword { @Parameter(name = "keyword", value = "搜索github仓库时用的q关键词", required = true) String keyword @Parameter(name = "page", value = "搜索github仓库时指定第几页", required = true) Integer page = 1 @Parameter(name = "per_page", value = "搜索github仓库时指定每页返回条数", required = true) Integer per_page @Parameter(name = "order", value = "排序顺序 asc/desc", required = false) String order @Parameter(name = "sort", value = "排序方式 stars/forks/updated/help-wanted-issues/good-first-issues/best-match", required = false) String sort @Parameter(name = "language", value = "指定编程语言", required = false) String language @Parameter(name = "user", value = "指定搜索仓库的所有者", required = false) String user } @GPTFunction(name = "githubSearch", value = "通过此方法可以搜索git仓库") static String githubSearch(Keyword search) { StringBuilder output = new StringBuilder(); def searchUrl = "https://api.github.com/search/repositories?" + "language=${search.language}" + "&user=${search.user}" + "&sort=${search.sort}" + "&order=${search.order}" + "&page=${search.page}" + "&per_page=${search.per_page}" + "&q=${search.keyword}" def connection = new URL(searchUrl).openConnection() connection.setRequestProperty("Accept", "application/vnd.github.v3+json") connection.connect() BufferedReader stdinReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = stdinReader.readLine()) != null) { output.append(line).append("\n"); } def slurper = new JsonSlurper() def data = slurper.parseText(output.toString()) def result = data["items"].collect { item -> def projectName = item.name def projectUrl = item.html_url return [name : projectName, url : projectUrl, language : item.language, stargazers_count: item.stargazers_count, forks : item.forks, description : item.description] } return result } /*******************************TEST**************************************/ LatitudeAndLongitude latitudeAndLongitude = new LatitudeAndLongitude(); latitudeAndLongitude.latitude = 31.2304 latitudeAndLongitude.longitude = 121.4737 getCurrentWeather(latitudeAndLongitude) /*******************************TEST**************************************/ command = new Command() command.command = "ipconfig" println(executeCommand(command)) /*******************************TEST**************************************/ key = new Keyword() key.keyword = "gpt" key.page = 1 key.per_page = 10 println(githubSearch(key))