|
@@ -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;
|
|
|
+ }
|
|
|
+}
|