parent
1a07e3e4ee
commit
fab1f9e26c
@ -0,0 +1,23 @@
|
|||||||
|
package xyz.wbsite.achat.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
import xyz.wbsite.achat.core.service.ChatService;
|
||||||
|
import xyz.wbsite.achat.core.service.impl.ChatServiceSampleImpl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对话配置
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class ChatConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ChatService chatService() {
|
||||||
|
return new ChatServiceSampleImpl();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
//package xyz.wbsite.achat.core;
|
||||||
|
//
|
||||||
|
///**
|
||||||
|
// * 附件
|
||||||
|
// *
|
||||||
|
// * @author wangbing
|
||||||
|
// * @version 0.0.1
|
||||||
|
// * @since 1.8
|
||||||
|
// */
|
||||||
|
//public class Attachment {
|
||||||
|
// private String filename;
|
||||||
|
//
|
||||||
|
// private String fid;
|
||||||
|
//
|
||||||
|
// public String getFilename() {
|
||||||
|
// return filename;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setFilename(String filename) {
|
||||||
|
// this.filename = filename;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getFid() {
|
||||||
|
// return fid;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setFid(String fid) {
|
||||||
|
// this.fid = fid;
|
||||||
|
// }
|
||||||
|
//}
|
@ -0,0 +1,17 @@
|
|||||||
|
//package xyz.wbsite.achat.core;
|
||||||
|
//
|
||||||
|
//public class Prompt {
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 提示词
|
||||||
|
// */
|
||||||
|
// private String prompt;
|
||||||
|
//
|
||||||
|
// public String getPrompt() {
|
||||||
|
// return prompt;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setPrompt(String prompt) {
|
||||||
|
// this.prompt = prompt;
|
||||||
|
// }
|
||||||
|
//}
|
@ -0,0 +1,126 @@
|
|||||||
|
//package xyz.wbsite.achat.core;
|
||||||
|
//
|
||||||
|
//
|
||||||
|
///**
|
||||||
|
// * 会话
|
||||||
|
// *
|
||||||
|
// * @author wangbing
|
||||||
|
// * @version 0.0.1
|
||||||
|
// * @since 1.8
|
||||||
|
// */
|
||||||
|
//public class Session {
|
||||||
|
// /**
|
||||||
|
// * 主键
|
||||||
|
// */
|
||||||
|
// private String id;
|
||||||
|
// /**
|
||||||
|
// * 用户ID
|
||||||
|
// */
|
||||||
|
// private String uid;
|
||||||
|
// private String title;
|
||||||
|
// private String model;
|
||||||
|
// private String prompt;
|
||||||
|
// private String temperature;
|
||||||
|
// private String topP;
|
||||||
|
// private String frequencyPenalty;
|
||||||
|
// private String presencePenalty;
|
||||||
|
// private String maxTokens;
|
||||||
|
// private String lastTime;
|
||||||
|
// private String lastMessage;
|
||||||
|
//
|
||||||
|
// public String getId() {
|
||||||
|
// return id;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setId(String id) {
|
||||||
|
// this.id = id;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getUid() {
|
||||||
|
// return uid;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setUid(String uid) {
|
||||||
|
// this.uid = uid;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getTitle() {
|
||||||
|
// return title;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setTitle(String title) {
|
||||||
|
// this.title = title;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getModel() {
|
||||||
|
// return model;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setModel(String model) {
|
||||||
|
// this.model = model;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getPrompt() {
|
||||||
|
// return prompt;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setPrompt(String prompt) {
|
||||||
|
// this.prompt = prompt;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getTemperature() {
|
||||||
|
// return temperature;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setTemperature(String temperature) {
|
||||||
|
// this.temperature = temperature;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getTopP() {
|
||||||
|
// return topP;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setTopP(String topP) {
|
||||||
|
// this.topP = topP;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getFrequencyPenalty() {
|
||||||
|
// return frequencyPenalty;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setFrequencyPenalty(String frequencyPenalty) {
|
||||||
|
// this.frequencyPenalty = frequencyPenalty;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getPresencePenalty() {
|
||||||
|
// return presencePenalty;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setPresencePenalty(String presencePenalty) {
|
||||||
|
// this.presencePenalty = presencePenalty;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getMaxTokens() {
|
||||||
|
// return maxTokens;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setMaxTokens(String maxTokens) {
|
||||||
|
// this.maxTokens = maxTokens;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getLastTime() {
|
||||||
|
// return lastTime;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setLastTime(String lastTime) {
|
||||||
|
// this.lastTime = lastTime;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getLastMessage() {
|
||||||
|
// return lastMessage;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setLastMessage(String lastMessage) {
|
||||||
|
// this.lastMessage = lastMessage;
|
||||||
|
// }
|
||||||
|
//}
|
@ -1,30 +0,0 @@
|
|||||||
package xyz.wbsite.achat.core.base;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 附件
|
|
||||||
*
|
|
||||||
* @author wangbing
|
|
||||||
* @version 0.0.1
|
|
||||||
* @since 1.8
|
|
||||||
*/
|
|
||||||
public class Attachment {
|
|
||||||
private String filename;
|
|
||||||
|
|
||||||
private String fid;
|
|
||||||
|
|
||||||
public String getFilename() {
|
|
||||||
return filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFilename(String filename) {
|
|
||||||
this.filename = filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFid() {
|
|
||||||
return fid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFid(String fid) {
|
|
||||||
this.fid = fid;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
package xyz.wbsite.achat.core.base;
|
|
||||||
|
|
||||||
public class Prompt {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 提示词
|
|
||||||
*/
|
|
||||||
private String prompt;
|
|
||||||
|
|
||||||
public String getPrompt() {
|
|
||||||
return prompt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPrompt(String prompt) {
|
|
||||||
this.prompt = prompt;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,126 +0,0 @@
|
|||||||
package xyz.wbsite.achat.core.base;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 会话
|
|
||||||
*
|
|
||||||
* @author wangbing
|
|
||||||
* @version 0.0.1
|
|
||||||
* @since 1.8
|
|
||||||
*/
|
|
||||||
public class Session {
|
|
||||||
/**
|
|
||||||
* 主键
|
|
||||||
*/
|
|
||||||
private String id;
|
|
||||||
/**
|
|
||||||
* 用户ID
|
|
||||||
*/
|
|
||||||
private String uid;
|
|
||||||
private String title;
|
|
||||||
private String model;
|
|
||||||
private String prompt;
|
|
||||||
private String temperature;
|
|
||||||
private String topP;
|
|
||||||
private String frequencyPenalty;
|
|
||||||
private String presencePenalty;
|
|
||||||
private String maxTokens;
|
|
||||||
private String lastTime;
|
|
||||||
private String lastMessage;
|
|
||||||
|
|
||||||
public String getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(String id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUid() {
|
|
||||||
return uid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUid(String uid) {
|
|
||||||
this.uid = uid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitle() {
|
|
||||||
return title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTitle(String title) {
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getModel() {
|
|
||||||
return model;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setModel(String model) {
|
|
||||||
this.model = model;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPrompt() {
|
|
||||||
return prompt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPrompt(String prompt) {
|
|
||||||
this.prompt = prompt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTemperature() {
|
|
||||||
return temperature;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTemperature(String temperature) {
|
|
||||||
this.temperature = temperature;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTopP() {
|
|
||||||
return topP;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTopP(String topP) {
|
|
||||||
this.topP = topP;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFrequencyPenalty() {
|
|
||||||
return frequencyPenalty;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFrequencyPenalty(String frequencyPenalty) {
|
|
||||||
this.frequencyPenalty = frequencyPenalty;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPresencePenalty() {
|
|
||||||
return presencePenalty;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPresencePenalty(String presencePenalty) {
|
|
||||||
this.presencePenalty = presencePenalty;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMaxTokens() {
|
|
||||||
return maxTokens;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMaxTokens(String maxTokens) {
|
|
||||||
this.maxTokens = maxTokens;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLastTime() {
|
|
||||||
return lastTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLastTime(String lastTime) {
|
|
||||||
this.lastTime = lastTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLastMessage() {
|
|
||||||
return lastMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLastMessage(String lastMessage) {
|
|
||||||
this.lastMessage = lastMessage;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,123 @@
|
|||||||
|
package xyz.wbsite.achat.core.chat;
|
||||||
|
|
||||||
|
import xyz.wbsite.achat.core.message.Message;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OpenAI聊天完成请求 - 符合OpenAI官方API规范
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public class ChatCompletionRequest {
|
||||||
|
private String model;
|
||||||
|
private List<Message> messages;
|
||||||
|
private Double temperature;
|
||||||
|
private Double top_p;
|
||||||
|
private Integer n;
|
||||||
|
private Boolean stream;
|
||||||
|
private List<String> stop;
|
||||||
|
private Integer max_tokens;
|
||||||
|
private Double presence_penalty;
|
||||||
|
private Double frequency_penalty;
|
||||||
|
private Object logit_bias;
|
||||||
|
private String user;
|
||||||
|
|
||||||
|
public String getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setModel(String model) {
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Message> getMessages() {
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMessages(List<Message> messages) {
|
||||||
|
this.messages = messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getTemperature() {
|
||||||
|
return temperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTemperature(Double temperature) {
|
||||||
|
this.temperature = temperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getTop_p() {
|
||||||
|
return top_p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTop_p(Double top_p) {
|
||||||
|
this.top_p = top_p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getN() {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setN(Integer n) {
|
||||||
|
this.n = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean getStream() {
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStream(Boolean stream) {
|
||||||
|
this.stream = stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getStop() {
|
||||||
|
return stop;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStop(List<String> stop) {
|
||||||
|
this.stop = stop;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getMax_tokens() {
|
||||||
|
return max_tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMax_tokens(Integer max_tokens) {
|
||||||
|
this.max_tokens = max_tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getPresence_penalty() {
|
||||||
|
return presence_penalty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPresence_penalty(Double presence_penalty) {
|
||||||
|
this.presence_penalty = presence_penalty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getFrequency_penalty() {
|
||||||
|
return frequency_penalty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFrequency_penalty(Double frequency_penalty) {
|
||||||
|
this.frequency_penalty = frequency_penalty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getLogit_bias() {
|
||||||
|
return logit_bias;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLogit_bias(Object logit_bias) {
|
||||||
|
this.logit_bias = logit_bias;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUser() {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUser(String user) {
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
package xyz.wbsite.achat.core.chat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文本补全请求
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public class CompletionRequest {
|
||||||
|
private String model;
|
||||||
|
private String prompt;
|
||||||
|
private boolean stream;
|
||||||
|
|
||||||
|
private Double temperature;
|
||||||
|
private Integer max_tokens;
|
||||||
|
private Double top_p;
|
||||||
|
private Integer n;
|
||||||
|
|
||||||
|
public String getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setModel(String model) {
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPrompt() {
|
||||||
|
return prompt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrompt(String prompt) {
|
||||||
|
this.prompt = prompt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getMax_tokens() {
|
||||||
|
return max_tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMax_tokens(Integer max_tokens) {
|
||||||
|
this.max_tokens = max_tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getTemperature() {
|
||||||
|
return temperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTemperature(Double temperature) {
|
||||||
|
this.temperature = temperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getTop_p() {
|
||||||
|
return top_p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTop_p(Double top_p) {
|
||||||
|
this.top_p = top_p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getN() {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setN(Integer n) {
|
||||||
|
this.n = n;
|
||||||
|
}
|
||||||
|
}
|
@ -1,22 +1,103 @@
|
|||||||
package xyz.wbsite.achat.core.event;
|
//package xyz.wbsite.achat.core.event;
|
||||||
|
//
|
||||||
import xyz.wbsite.achat.core.base.Event;
|
//import xyz.wbsite.achat.core.Event;
|
||||||
|
//
|
||||||
/**
|
///**
|
||||||
* 完成事件
|
// * 完成事件
|
||||||
*
|
// * 用于标识流式响应的结束
|
||||||
* @author wangbing
|
// *
|
||||||
* @version 0.0.1
|
// * @author wangbing
|
||||||
* @since 1.8
|
// * @version 0.0.1
|
||||||
*/
|
// * @since 1.8
|
||||||
public class CompleteEvent extends Event {
|
// */
|
||||||
|
//public class CompleteEvent extends Event {
|
||||||
public CompleteEvent(String sid) {
|
//
|
||||||
super(sid);
|
// /**
|
||||||
setType("complete");
|
// * 完整响应内容
|
||||||
}
|
// */
|
||||||
|
// private String content;
|
||||||
public static CompleteEvent of(String sid) {
|
//
|
||||||
return new CompleteEvent(sid);
|
// /**
|
||||||
}
|
// * 构造函数
|
||||||
}
|
// *
|
||||||
|
// * @param sid 会话ID
|
||||||
|
// */
|
||||||
|
// public CompleteEvent(String sid) {
|
||||||
|
// super();
|
||||||
|
// this.setSid(sid);
|
||||||
|
// this.setObject("chat.completion");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 生成的令牌总数
|
||||||
|
// */
|
||||||
|
// private Integer completionTokens;
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 提示词的令牌数量
|
||||||
|
// */
|
||||||
|
// private Integer promptTokens;
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 总令牌数量
|
||||||
|
// */
|
||||||
|
// private Integer totalTokens;
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 完成状态
|
||||||
|
// */
|
||||||
|
// private String finishReason;
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 生成用时(毫秒)
|
||||||
|
// */
|
||||||
|
// private Long generationTime;
|
||||||
|
//
|
||||||
|
// public String getContent() {
|
||||||
|
// return content;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setContent(String content) {
|
||||||
|
// this.content = content;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public Integer getCompletionTokens() {
|
||||||
|
// return completionTokens;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setCompletionTokens(Integer completionTokens) {
|
||||||
|
// this.completionTokens = completionTokens;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public Integer getPromptTokens() {
|
||||||
|
// return promptTokens;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setPromptTokens(Integer promptTokens) {
|
||||||
|
// this.promptTokens = promptTokens;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public Integer getTotalTokens() {
|
||||||
|
// return totalTokens;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setTotalTokens(Integer totalTokens) {
|
||||||
|
// this.totalTokens = totalTokens;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getFinishReason() {
|
||||||
|
// return finishReason;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setFinishReason(String finishReason) {
|
||||||
|
// this.finishReason = finishReason;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public Long getGenerationTime() {
|
||||||
|
// return generationTime;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setGenerationTime(Long generationTime) {
|
||||||
|
// this.generationTime = generationTime;
|
||||||
|
// }
|
||||||
|
//}
|
@ -0,0 +1,66 @@
|
|||||||
|
//package xyz.wbsite.achat.core.event;
|
||||||
|
//
|
||||||
|
//import xyz.wbsite.achat.core.Event;
|
||||||
|
//
|
||||||
|
///**
|
||||||
|
// * 开始推送事件
|
||||||
|
// * 用于标识流式响应的开始
|
||||||
|
// *
|
||||||
|
// * @author wangbing
|
||||||
|
// * @version 0.0.1
|
||||||
|
// * @since 1.8
|
||||||
|
// */
|
||||||
|
//public class StartEvent extends Event {
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 提示词令牌数量
|
||||||
|
// */
|
||||||
|
// private Integer promptTokens;
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 最大令牌数量限制
|
||||||
|
// */
|
||||||
|
// private Integer maxTokens;
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 温度参数
|
||||||
|
// */
|
||||||
|
// private Double temperature;
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 随机种子
|
||||||
|
// */
|
||||||
|
// private Integer seed;
|
||||||
|
//
|
||||||
|
// public Integer getPromptTokens() {
|
||||||
|
// return promptTokens;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setPromptTokens(Integer promptTokens) {
|
||||||
|
// this.promptTokens = promptTokens;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public Integer getMaxTokens() {
|
||||||
|
// return maxTokens;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setMaxTokens(Integer maxTokens) {
|
||||||
|
// this.maxTokens = maxTokens;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public Double getTemperature() {
|
||||||
|
// return temperature;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setTemperature(Double temperature) {
|
||||||
|
// this.temperature = temperature;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public Integer getSeed() {
|
||||||
|
// return seed;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setSeed(Integer seed) {
|
||||||
|
// this.seed = seed;
|
||||||
|
// }
|
||||||
|
//}
|
@ -0,0 +1,80 @@
|
|||||||
|
package xyz.wbsite.achat.core.model;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OpenAI模型对象 - 符合OpenAI官方API规范
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public class Model {
|
||||||
|
private String id;
|
||||||
|
private String object;
|
||||||
|
private long created;
|
||||||
|
private String owned_by;
|
||||||
|
private List<Permission> permission;
|
||||||
|
private String root;
|
||||||
|
private String parent;
|
||||||
|
|
||||||
|
// Getters and Setters
|
||||||
|
public String getId() { return id; }
|
||||||
|
public void setId(String id) { this.id = id; }
|
||||||
|
public String getObject() { return object; }
|
||||||
|
public void setObject(String object) { this.object = object; }
|
||||||
|
public long getCreated() { return created; }
|
||||||
|
public void setCreated(long created) { this.created = created; }
|
||||||
|
public String getOwned_by() { return owned_by; }
|
||||||
|
public void setOwned_by(String owned_by) { this.owned_by = owned_by; }
|
||||||
|
public List<Permission> getPermission() { return permission; }
|
||||||
|
public void setPermission(List<Permission> permission) { this.permission = permission; }
|
||||||
|
public String getRoot() { return root; }
|
||||||
|
public void setRoot(String root) { this.root = root; }
|
||||||
|
public String getParent() { return parent; }
|
||||||
|
public void setParent(String parent) { this.parent = parent; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模型权限对象
|
||||||
|
*/
|
||||||
|
public static class Permission {
|
||||||
|
private String id;
|
||||||
|
private String object;
|
||||||
|
private long created;
|
||||||
|
private boolean allow_create_engine;
|
||||||
|
private boolean allow_sampling;
|
||||||
|
private boolean allow_logprobs;
|
||||||
|
private boolean allow_search_indices;
|
||||||
|
private boolean allow_view;
|
||||||
|
private boolean allow_fine_tuning;
|
||||||
|
private String organization;
|
||||||
|
private String group;
|
||||||
|
private boolean is_blocking;
|
||||||
|
|
||||||
|
// Getters and Setters
|
||||||
|
public String getId() { return id; }
|
||||||
|
public void setId(String id) { this.id = id; }
|
||||||
|
public String getObject() { return object; }
|
||||||
|
public void setObject(String object) { this.object = object; }
|
||||||
|
public long getCreated() { return created; }
|
||||||
|
public void setCreated(long created) { this.created = created; }
|
||||||
|
public boolean isAllow_create_engine() { return allow_create_engine; }
|
||||||
|
public void setAllow_create_engine(boolean allow_create_engine) { this.allow_create_engine = allow_create_engine; }
|
||||||
|
public boolean isAllow_sampling() { return allow_sampling; }
|
||||||
|
public void setAllow_sampling(boolean allow_sampling) { this.allow_sampling = allow_sampling; }
|
||||||
|
public boolean isAllow_logprobs() { return allow_logprobs; }
|
||||||
|
public void setAllow_logprobs(boolean allow_logprobs) { this.allow_logprobs = allow_logprobs; }
|
||||||
|
public boolean isAllow_search_indices() { return allow_search_indices; }
|
||||||
|
public void setAllow_search_indices(boolean allow_search_indices) { this.allow_search_indices = allow_search_indices; }
|
||||||
|
public boolean isAllow_view() { return allow_view; }
|
||||||
|
public void setAllow_view(boolean allow_view) { this.allow_view = allow_view; }
|
||||||
|
public boolean isAllow_fine_tuning() { return allow_fine_tuning; }
|
||||||
|
public void setAllow_fine_tuning(boolean allow_fine_tuning) { this.allow_fine_tuning = allow_fine_tuning; }
|
||||||
|
public String getOrganization() { return organization; }
|
||||||
|
public void setOrganization(String organization) { this.organization = organization; }
|
||||||
|
public String getGroup() { return group; }
|
||||||
|
public void setGroup(String group) { this.group = group; }
|
||||||
|
public boolean isIs_blocking() { return is_blocking; }
|
||||||
|
public void setIs_blocking(boolean is_blocking) { this.is_blocking = is_blocking; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package xyz.wbsite.achat.core.model;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OpenAI模型列表响应 - 符合OpenAI官方API规范
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public class ModelListResponse {
|
||||||
|
private String object;
|
||||||
|
private List<Model> data;
|
||||||
|
|
||||||
|
// Getters and Setters
|
||||||
|
public String getObject() { return object; }
|
||||||
|
public void setObject(String object) { this.object = object; }
|
||||||
|
public List<Model> getData() { return data; }
|
||||||
|
public void setData(List<Model> data) { this.data = data; }
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package xyz.wbsite.achat.core.service;
|
||||||
|
|
||||||
|
import xyz.wbsite.achat.core.chat.ChatCompletionRequest;
|
||||||
|
import xyz.wbsite.achat.core.message.StreamEmitter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 推理生成器
|
||||||
|
* <p>
|
||||||
|
* 抽象出来用于生成消息的实现层
|
||||||
|
*
|
||||||
|
* @author wangbing
|
||||||
|
* @version 0.0.1
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
public interface ChatCompletionGenerator {
|
||||||
|
|
||||||
|
void on(StreamEmitter emitter, ChatCompletionRequest chatCompletionRequest);
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package xyz.wbsite.achat.core.service;
|
||||||
|
|
||||||
|
import xyz.wbsite.achat.core.chat.ChatCompletionRequest;
|
||||||
|
import xyz.wbsite.achat.core.chat.ChatCompletionResponse;
|
||||||
|
import xyz.wbsite.achat.core.chat.CompletionRequest;
|
||||||
|
import xyz.wbsite.achat.core.chat.CompletionResponse;
|
||||||
|
import xyz.wbsite.achat.core.message.StreamEmitter;
|
||||||
|
|
||||||
|
public interface ChatService {
|
||||||
|
|
||||||
|
CompletionResponse prompt(CompletionRequest request);
|
||||||
|
|
||||||
|
ChatCompletionResponse chat(ChatCompletionRequest request);
|
||||||
|
|
||||||
|
StreamEmitter streamChat(ChatCompletionRequest request);
|
||||||
|
}
|
@ -1,18 +0,0 @@
|
|||||||
package xyz.wbsite.achat.core.service;
|
|
||||||
|
|
||||||
import xyz.wbsite.achat.core.message.MessageSseEmitter;
|
|
||||||
import xyz.wbsite.achat.core.base.Message;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 消息生成器
|
|
||||||
* <p>
|
|
||||||
* 抽象出来用于生成消息的实现层
|
|
||||||
*
|
|
||||||
* @author wangbing
|
|
||||||
* @version 0.0.1
|
|
||||||
* @since 1.8
|
|
||||||
*/
|
|
||||||
public interface MessageGenerator {
|
|
||||||
|
|
||||||
void on(MessageSseEmitter emitter, Message message);
|
|
||||||
}
|
|
@ -0,0 +1,110 @@
|
|||||||
|
package xyz.wbsite.achat.core.service.impl;
|
||||||
|
|
||||||
|
import xyz.wbsite.achat.core.chat.ChatCompletionRequest;
|
||||||
|
import xyz.wbsite.achat.core.chat.ChatCompletionResponse;
|
||||||
|
import xyz.wbsite.achat.core.chat.CompletionRequest;
|
||||||
|
import xyz.wbsite.achat.core.chat.CompletionResponse;
|
||||||
|
import xyz.wbsite.achat.core.message.Message;
|
||||||
|
import xyz.wbsite.achat.core.message.Role;
|
||||||
|
import xyz.wbsite.achat.core.message.StreamEmitter;
|
||||||
|
import xyz.wbsite.achat.core.service.ChatService;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ChatServiceSampleImpl implements ChatService {
|
||||||
|
@Override
|
||||||
|
public CompletionResponse prompt(CompletionRequest request) {
|
||||||
|
CompletionResponse response = new CompletionResponse();
|
||||||
|
response.setObject("chat.completion");
|
||||||
|
response.setCreated(System.currentTimeMillis() / 1000);
|
||||||
|
response.setModel(request.getModel());
|
||||||
|
|
||||||
|
// List<Choice> choices = new ArrayList<>();
|
||||||
|
// choices.add(Choice.builder().index(0).message(Message.builder().role(Role.ASSISTANT).content("您好,我还没有接入AI,请接入后再试!").build()).finish_reason("stop").build());
|
||||||
|
// response.setChoices(choices);
|
||||||
|
// response.setUsage(Usage.builder().prompt_tokens(10).completion_tokens(20).total_tokens(30).build());
|
||||||
|
|
||||||
|
return CompletionResponse.builder()
|
||||||
|
.id("chatcmpl-" + System.currentTimeMillis())
|
||||||
|
.object("chat.completion")
|
||||||
|
.created(System.currentTimeMillis() / 1000)
|
||||||
|
.model(request.getModel())
|
||||||
|
.withChoices(choices -> {
|
||||||
|
// Choice choice1 = new Choice();
|
||||||
|
CompletionResponse.Choice
|
||||||
|
choice1.setIndex(0);
|
||||||
|
choice1.setMessage(new Message());
|
||||||
|
choice1.setFinish_reason("stop");
|
||||||
|
choices.add(choice1);
|
||||||
|
|
||||||
|
Choice choice2 = new Choice();
|
||||||
|
choice2.setIndex(1);
|
||||||
|
choice2.setMessage(new Message());
|
||||||
|
choice2.setFinish_reason("stop");
|
||||||
|
choices.add(choice2);
|
||||||
|
})
|
||||||
|
|
||||||
|
.usage(CompletionResponse.UsageBuilder().prompt_tokens(10).completion_tokens(10).total_tokens(10).build())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChatCompletionResponse chat(ChatCompletionRequest request) {
|
||||||
|
ChatCompletionResponse response = new ChatCompletionResponse();
|
||||||
|
response.setId("chatcmpl-" + System.currentTimeMillis());
|
||||||
|
response.setObject("chat.completion");
|
||||||
|
response.setCreated(System.currentTimeMillis() / 1000);
|
||||||
|
response.setModel(request.getModel());
|
||||||
|
|
||||||
|
List<Choice> choices = new ArrayList<>();
|
||||||
|
choices.add(Choice.builder().index(0).message(Message.builder().role(Role.ASSISTANT).content("您好,我还没有接入AI,请接入后再试!").build()).finish_reason("stop").build());
|
||||||
|
response.setChoices(choices);
|
||||||
|
response.setUsage(Usage.builder().prompt_tokens(10).completion_tokens(20).total_tokens(30).build());
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StreamEmitter streamChat(ChatCompletionRequest request) {
|
||||||
|
// 验证请求参数
|
||||||
|
if (request.getModel() == null) {
|
||||||
|
throw new IllegalArgumentException("模型不能为空");
|
||||||
|
}
|
||||||
|
if (request.getMessages() == null || request.getMessages().isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("消息不能为空");
|
||||||
|
}
|
||||||
|
return StreamEmitter.builder()
|
||||||
|
.chatCompletionRequest(request)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// SseEmitter emitter = new SseEmitter(Long.MAX_VALUE);
|
||||||
|
// 在单独的线程中处理流式响应
|
||||||
|
// new Thread(() -> {
|
||||||
|
// try {
|
||||||
|
// // 模拟流式响应的逻辑
|
||||||
|
// // 实际应用中应从服务层获取流式数据并发送
|
||||||
|
// String id = "chatcmpl-" + System.currentTimeMillis();
|
||||||
|
// long created = System.currentTimeMillis() / 1000;
|
||||||
|
// String model = request.getModel();
|
||||||
|
//
|
||||||
|
// // 发送初始数据块
|
||||||
|
// ChatCompletionChunk chunk = new ChatCompletionChunk();
|
||||||
|
// chunk.setId(id);
|
||||||
|
// chunk.setObject("chat.completion.chunk");
|
||||||
|
// chunk.setCreated(created);
|
||||||
|
// chunk.setModel(model);
|
||||||
|
// // chunk.setChoices(/* 实际的选择项列表 */);
|
||||||
|
// emitter.send(chunk, MediaType.APPLICATION_JSON);
|
||||||
|
//
|
||||||
|
// // 发送更多数据块...
|
||||||
|
//
|
||||||
|
// // 发送完成信号
|
||||||
|
// emitter.complete();
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// emitter.completeWithError(e);
|
||||||
|
// }
|
||||||
|
// }).start();
|
||||||
|
//
|
||||||
|
// return emitter;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue