AliBaiLianAIService.java 3.8 KB

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