KimiAIService.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.webchat.aigc.llm;
  2. import com.webchat.aigc.config.properties.KimiLLMPropertiesConfig;
  3. import com.webchat.common.util.llm.MoonShotAIClient;
  4. import com.webchat.domain.vo.llm.ChatCompletionMessage;
  5. import com.webchat.domain.vo.llm.ChatCompletionRequest;
  6. import com.webchat.domain.vo.llm.ChatCompletionResponse;
  7. import com.webchat.domain.vo.llm.ChatCompletionStreamChoice;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
  11. import java.util.List;
  12. /**
  13. * @Author 程序员七七
  14. * @webSite https://www.coderutil.com
  15. * @Date 2024/10/29 00:22
  16. * @description
  17. */
  18. @Service
  19. public class KimiAIService extends AbstractLLMChatService {
  20. @Autowired
  21. private KimiLLMPropertiesConfig kimiLLMPropertiesConfig;
  22. /**
  23. * 同步对话
  24. *
  25. * @param messageList
  26. * @return
  27. * @throws Exception
  28. */
  29. public ChatCompletionResponse chat(List<ChatCompletionMessage> messageList) throws Exception {
  30. MoonShotAIClient client = new MoonShotAIClient(kimiLLMPropertiesConfig.getApiKey());
  31. final List<ChatCompletionMessage> messages = messageList;
  32. return client.ChatCompletion(new ChatCompletionRequest(
  33. kimiLLMPropertiesConfig.getModel(),
  34. messages,
  35. 4096,
  36. 0.3f,
  37. 1
  38. ));
  39. }
  40. /**
  41. * 流式对话
  42. *
  43. * @param emitter
  44. * @param messageList
  45. * @throws Exception
  46. */
  47. public String chat(SseEmitter emitter, List<ChatCompletionMessage> messageList) throws Exception {
  48. StringBuilder aiMessage = new StringBuilder();
  49. MoonShotAIClient client = new MoonShotAIClient(kimiLLMPropertiesConfig.getApiKey());
  50. final List<ChatCompletionMessage> messages = messageList;
  51. try {
  52. client.ChatCompletionStream(new ChatCompletionRequest(
  53. kimiLLMPropertiesConfig.getModel(),
  54. messages,
  55. 2000,
  56. 0.3f,
  57. 1
  58. )).subscribe(
  59. streamResponse -> {
  60. if (streamResponse.getChoices().isEmpty()) {
  61. return;
  62. }
  63. for (ChatCompletionStreamChoice choice : streamResponse.getChoices()) {
  64. String finishReason = choice.getFinishReason();
  65. if (finishReason != null) {
  66. emitter.send("finished");
  67. continue;
  68. }
  69. String responseContent = choice.getDelta().getContent();
  70. if (responseContent != null) {
  71. responseContent = responseContent.replaceAll("\n", "<br>");
  72. System.out.println(responseContent);
  73. emitter.send(responseContent);
  74. aiMessage.append(responseContent);
  75. }
  76. }
  77. },
  78. error -> {
  79. error.printStackTrace();
  80. },
  81. () -> {
  82. // emitter.complete(); // 完成事件流发送
  83. }
  84. );
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. return aiMessage.toString();
  89. }
  90. }