From 4d0b72c0a0b22574d59c9c0174fe0b1c9e4aca69 Mon Sep 17 00:00:00 2001 From: xbx <1827135378@qq.com> Date: Fri, 5 Jul 2024 17:44:06 +0800 Subject: [PATCH] 2.0 --- .../example/aitest/controller/ApiController.java | 2 +- .../controller/websocket/WebSocketServer.java | 3 +-- .../com/example/aitest/jwt/InterceptorConfig.java | 1 + .../example/aitest/service/impl/ApiServiceImpl.java | 13 ++++++++++++- src/main/resources/application.yml | 4 +--- .../com/example/aitest/AiTestApplicationTests.java | 9 +++++---- src/test/java/com/example/aitest/MD5Test.java | 2 +- src/test/java/com/example/aitest/WebsocketTest.java | 2 +- 8 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/example/aitest/controller/ApiController.java b/src/main/java/com/example/aitest/controller/ApiController.java index 5022992..381efda 100644 --- a/src/main/java/com/example/aitest/controller/ApiController.java +++ b/src/main/java/com/example/aitest/controller/ApiController.java @@ -37,7 +37,7 @@ public class ApiController { return openAiService.processImage(path.toFile()); } - @RequestMapping("/timeChange") + @GetMapping("/timeChange") public ResponseEntity timeChange(@RequestParam("time") String time) { return openAiService.timeChange(time); } diff --git a/src/main/java/com/example/aitest/controller/websocket/WebSocketServer.java b/src/main/java/com/example/aitest/controller/websocket/WebSocketServer.java index 9e6d222..dbd1fdf 100644 --- a/src/main/java/com/example/aitest/controller/websocket/WebSocketServer.java +++ b/src/main/java/com/example/aitest/controller/websocket/WebSocketServer.java @@ -22,7 +22,6 @@ public class WebSocketServer { public void onOpen(Session session) throws IOException { sessionList.add(session); log.info("有新用户加入聊天,当前在线人数为{}", sessionList.size()); - sendAllSession("二级"); } @OnMessage @@ -46,7 +45,7 @@ public class WebSocketServer { /* * 发消息给客户端 * */ - public void sendAllSession(String message){ + public static void sendAllSession(String message){ try { for (Session session : sessionList) { diff --git a/src/main/java/com/example/aitest/jwt/InterceptorConfig.java b/src/main/java/com/example/aitest/jwt/InterceptorConfig.java index d2d6073..502eb7a 100644 --- a/src/main/java/com/example/aitest/jwt/InterceptorConfig.java +++ b/src/main/java/com/example/aitest/jwt/InterceptorConfig.java @@ -23,6 +23,7 @@ public class InterceptorConfig implements WebMvcConfigurer { excludePath.add("/apiSend"); excludePath.add("/timeChange"); excludePath.add("/timeGet"); + excludePath.add("/images/**");//图片 registry.addInterceptor(tokenInterceptor)//注册拦截器 .addPathPatterns("/**")//拦截所有请求 .excludePathPatterns(excludePath);//添加拦截白名单 diff --git a/src/main/java/com/example/aitest/service/impl/ApiServiceImpl.java b/src/main/java/com/example/aitest/service/impl/ApiServiceImpl.java index ffcbcff..36d047d 100644 --- a/src/main/java/com/example/aitest/service/impl/ApiServiceImpl.java +++ b/src/main/java/com/example/aitest/service/impl/ApiServiceImpl.java @@ -2,6 +2,7 @@ package com.example.aitest.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.example.aitest.controller.websocket.WebSocketServer; import com.example.aitest.service.ApiService; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; @@ -31,7 +32,7 @@ public class ApiServiceImpl implements ApiService { // 构建请求体 JSONObject content = new JSONObject(); content.put("type", "text"); - content.put("text", "请你对该图片的人物行为进行分析"); + content.put("text", "你现在是一个带有警报系统的人类安全分析监控,只分析我上传的图片中的人物的动作、神态、在做什么?是否有生命危险,判断后给出警报等级:一级警报、二级警报、三级警报。在你的回答的最后,需要有一个括号,括号中包含判断出的警报等级"); JSONObject imageContent = new JSONObject(); imageContent.put("type", "image_url"); @@ -63,6 +64,16 @@ public class ApiServiceImpl implements ApiService { JSONObject jsonResponse = JSON.parseObject(responseBody); jsonResponse.put("imgUrl", imageFile.getName()); System.out.println(jsonResponse.toJSONString()); + if (jsonResponse.toJSONString().contains("一级警报")) { + WebSocketServer.sendAllSession("一级"); + jsonResponse.put("grade", "一级"); + } else if (jsonResponse.toJSONString().contains("二级警报")) { + WebSocketServer.sendAllSession("二级"); + jsonResponse.put("grade", "二级"); + } else { + WebSocketServer.sendAllSession("三级"); + jsonResponse.put("grade", "三级"); + } return jsonResponse; } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 400a665..ab26689 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -2,11 +2,9 @@ spring: application: name: AITest redis: - port: 6379 -# password: ob666666 - host: 49.233.248.140 password: aB3cDeF9 + host: 49.233.248.140 datasource: username: root password: 3534957771fb4925 diff --git a/src/test/java/com/example/aitest/AiTestApplicationTests.java b/src/test/java/com/example/aitest/AiTestApplicationTests.java index 5650cde..90dd631 100644 --- a/src/test/java/com/example/aitest/AiTestApplicationTests.java +++ b/src/test/java/com/example/aitest/AiTestApplicationTests.java @@ -18,19 +18,19 @@ class AiTestApplicationTests { private static final String API_SECRET_KEY = "sk-zk23dcb27bb18e67d9ea0d749ed62f8088a667936c4c0682"; private static final String BASE_URL = "https://api.zhizengzeng.com/v1/chat/completions"; - @Test +// @Test void contextLoads() { CloseableHttpClient httpClient = HttpClients.createDefault(); System.out.println("进入processImage"); // 构建请求体 JSONObject content = new JSONObject(); content.put("type", "text"); - content.put("text", "请你对该图片的人物行为进行分析"); + content.put("text", "你现在是一个带有警报系统的人类安全分析监控,只分析我上传的图片中的人物的动作、神态、在做什么?是否有生命危险,判断后给出警报等级:一级警报、二级警报、三级警报。在你的回答的最后,需要有一个括号,括号中包含判断出的警报等级"); JSONObject imageContent = new JSONObject(); imageContent.put("type", "image_url"); JSONObject imageUrl = new JSONObject(); - imageUrl.put("url", "http://124.71.135.249:8081/images/avatar/1718618415826.jpg"); + imageUrl.put("url", "http://49.233.248.140:8081/images/1720169434341.jpg"); imageContent.put("image_url", imageUrl); JSONObject message = new JSONObject(); @@ -40,7 +40,7 @@ class AiTestApplicationTests { JSONObject requestBodyJson = new JSONObject(); requestBodyJson.put("model", "gpt-4o"); requestBodyJson.put("messages", new JSONObject[]{message}); - requestBodyJson.put("max_tokens", 300); + requestBodyJson.put("max_tokens", 100); StringEntity requestEntity = new StringEntity(requestBodyJson.toJSONString(), ContentType.APPLICATION_JSON); @@ -55,6 +55,7 @@ class AiTestApplicationTests { // Parse the JSON response String responseBody = new String(response.getEntity().getContent().readAllBytes()); JSONObject jsonResponse = JSON.parseObject(responseBody); + System.out.println(jsonResponse.toJSONString()); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/test/java/com/example/aitest/MD5Test.java b/src/test/java/com/example/aitest/MD5Test.java index 421ca5d..ba4f9e7 100644 --- a/src/test/java/com/example/aitest/MD5Test.java +++ b/src/test/java/com/example/aitest/MD5Test.java @@ -9,7 +9,7 @@ import java.security.NoSuchAlgorithmException; @SpringBootTest public class MD5Test { - @Test +// @Test public void test() throws NoSuchAlgorithmException { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update("ob666666".getBytes()); diff --git a/src/test/java/com/example/aitest/WebsocketTest.java b/src/test/java/com/example/aitest/WebsocketTest.java index ff46851..1ee0dd3 100644 --- a/src/test/java/com/example/aitest/WebsocketTest.java +++ b/src/test/java/com/example/aitest/WebsocketTest.java @@ -6,7 +6,7 @@ import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class WebsocketTest { - @Test +// @Test public void test() { System.out.println("Test"); }