Browse Source

金聪提交

jc 6 days ago
parent
commit
8676687c1d

+ 5 - 0
pom.xml

@@ -148,6 +148,11 @@
 <!--            <groupId>org.springframework.boot</groupId>-->
 <!--            <artifactId>spring-boot-starter-thymeleaf</artifactId>-->
 <!--        </dependency>-->
+        <dependency>
+            <groupId>io.github.lnyo-cly</groupId>
+            <artifactId>ai4j-spring-boot-stater</artifactId>
+            <version>0.6.2</version>
+        </dependency>
 
     </dependencies>
     <dependencyManagement>

+ 24 - 0
src/main/java/com/neko/ai/OllamaController.java

@@ -0,0 +1,24 @@
+package com.neko.ai;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @Date 2025/5/26 22:40
+ * @Author neko
+ **/
+@RestController
+@RequestMapping("/ollama")
+public class OllamaController {
+    @Autowired
+    private OllamaService ollamaService;
+
+    @GetMapping("/chat")
+    public String getChatMessage(@RequestParam String question){
+        String chatMessage = ollamaService.getChatMessage(question);
+        return chatMessage;
+    }
+}

+ 9 - 0
src/main/java/com/neko/ai/OllamaService.java

@@ -0,0 +1,9 @@
+package com.neko.ai;
+
+/**
+ * @Date 2025/5/26 22:41
+ * @Author neko
+ **/
+public interface OllamaService {
+    String getChatMessage(String question);
+}

+ 49 - 0
src/main/java/com/neko/ai/OllamaServiceImpl.java

@@ -0,0 +1,49 @@
+package com.neko.ai;
+
+import io.github.lnyocly.ai4j.platform.openai.chat.entity.ChatCompletion;
+import io.github.lnyocly.ai4j.platform.openai.chat.entity.ChatCompletionResponse;
+import io.github.lnyocly.ai4j.platform.openai.chat.entity.ChatMessage;
+import io.github.lnyocly.ai4j.service.IChatService;
+import io.github.lnyocly.ai4j.service.PlatformType;
+import io.github.lnyocly.ai4j.service.factor.AiService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * @Date 2025/5/26 22:41
+ * @Author neko
+ **/
+@Service
+public class OllamaServiceImpl implements OllamaService{
+    // 注入Ai服务
+    @Autowired
+    private AiService aiService;
+    @Override
+    public String getChatMessage(String question) {
+        // 获取OLLAMA的聊天服务
+        IChatService chatService = aiService.getChatService(PlatformType.OLLAMA);
+
+        // 创建请求参数
+        ChatCompletion chatCompletion = ChatCompletion.builder()
+                .model("deepseek-r1:7b")
+                .message(ChatMessage.withUser(question))
+                .build();
+
+        System.out.println(chatCompletion);
+
+        // 发送chat请求
+        ChatCompletionResponse chatCompletionResponse = null;
+        try {
+            chatCompletionResponse = chatService.chatCompletion(chatCompletion);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+        // 获取聊天内容和token消耗
+        String content = chatCompletionResponse.getChoices().get(0).getMessage().getContent();
+        long totalTokens = chatCompletionResponse.getUsage().getTotalTokens();
+        System.out.println("总token消耗: " + totalTokens);
+
+        return content;
+    }
+}

+ 6 - 0
src/main/java/com/neko/service/impl/UserServiceImpl.java

@@ -564,6 +564,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
         List<FriendRelation> fList = list.stream()
                 .sorted(Comparator.comparing(FriendRelation::getLetter))
                 .collect(Collectors.toList());
+        for(FriendRelation ft:fList){
+            QueryWrapper<UserProfile> wrapper = new QueryWrapper<>();
+            wrapper.lambda().eq(UserProfile::getUserId,ft.getFriendId());
+            UserProfile userProfile = userProfileMapper.selectOne(wrapper);
+            ft.setUserProfiles(userProfile);
+        }
 
         return ResultVo.success(ApiServiceExceptionEnum.RESULT_SUCCES,fList);
     }

+ 3 - 0
src/main/resources/application.yml

@@ -3,6 +3,9 @@ server:
 spring:
   websocket:
     enabled=true
+  ai:
+    ollama:
+      api-host: "http://localhost:11434/"
   servlet:
     multipart:
       enabled: true