模型聊天Service
This commit is contained in:
parent
99b5ddff66
commit
ba2cb9ab3c
@ -58,4 +58,10 @@ public class LLMBackendProperties {
|
||||
|
||||
@NotNull(message = "登录 POST")
|
||||
private String login;
|
||||
|
||||
@NotNull(message = "大模型列表 GET")
|
||||
private String modelList;
|
||||
|
||||
@NotNull(message = "大模型聊天 POST")
|
||||
private String modelCompletions;
|
||||
}
|
||||
|
@ -0,0 +1,54 @@
|
||||
package cn.iocoder.yudao.module.llm.service.http;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
|
||||
import cn.iocoder.yudao.module.llm.framework.backend.config.LLMBackendProperties;
|
||||
import cn.iocoder.yudao.module.llm.service.http.vo.ChatCompletion;
|
||||
import cn.iocoder.yudao.module.llm.service.http.vo.ModelCompletionsReqVO;
|
||||
import cn.iocoder.yudao.module.llm.service.http.vo.ModelCompletionsRespVO;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Service
|
||||
public class ModelService {
|
||||
|
||||
public final static String DEFAULT_MODEL_ID = "qwen2.5-instruct";
|
||||
|
||||
@Resource
|
||||
private LLMBackendProperties llmBackendProperties;
|
||||
|
||||
/**
|
||||
* 获取模型列表
|
||||
*/
|
||||
public String modelList(){
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public ModelCompletionsRespVO modelCompletions(ModelCompletionsReqVO req) {
|
||||
if (StringUtils.isBlank(req.getModel())) {
|
||||
req.setModel(DEFAULT_MODEL_ID);
|
||||
}
|
||||
String result = HttpUtils.post(llmBackendProperties.getModelCompletions(), null, JSON.toJSONString(req));
|
||||
if (StringUtils.isBlank(result)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
ChatCompletion chatCompletion = JSON.parseObject(result, ChatCompletion.class);
|
||||
if (StringUtils.isBlank(chatCompletion.getDetail())) {
|
||||
// 没有detail,就是没有错误
|
||||
ModelCompletionsRespVO respVO = new ModelCompletionsRespVO();
|
||||
respVO.setSystem("助手");
|
||||
respVO.setQuestion(req.getMessages().get(0).getContent());
|
||||
respVO.setAnswer(chatCompletion.getChoices().get(0).getMessage().getContent());
|
||||
return respVO;
|
||||
}
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package cn.iocoder.yudao.module.llm.service.http.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ChatCompletion {
|
||||
|
||||
private String id;
|
||||
private String object;
|
||||
private long created;
|
||||
private String model;
|
||||
private List<Choice> choices;
|
||||
private Usage usage;
|
||||
private String detail;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class Choice {
|
||||
private int index;
|
||||
private Message message;
|
||||
private String finish_reason;
|
||||
}
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class Message {
|
||||
private String role;
|
||||
private String content;
|
||||
}
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class Usage {
|
||||
private int prompt_tokens;
|
||||
private int completion_tokens;
|
||||
private int total_tokens;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package cn.iocoder.yudao.module.llm.service.http.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ModelCompletionsReqVO {
|
||||
|
||||
private String model;
|
||||
private List<ModelCompletionsMessage> messages;
|
||||
private Integer max_tokens = 512;
|
||||
private Double temperature = 0.7;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class ModelCompletionsMessage {
|
||||
|
||||
private String role;
|
||||
private String content;
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package cn.iocoder.yudao.module.llm.service.http.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ModelCompletionsRespVO {
|
||||
|
||||
private String system;
|
||||
private String question;
|
||||
private String answer;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user