上传备份

master
王兵 4 days ago
parent 9bfbd3674d
commit f0337024f1

@ -1,110 +0,0 @@
//package xyz.wbsite.achat;
//
//import org.springframework.web.bind.annotation.DeleteMapping;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
//import xyz.wbsite.achat.core.chat.Message;
//import xyz.wbsite.achat.core.Result;
//import xyz.wbsite.achat.core.Session;
//import xyz.wbsite.achat.core.prompt.MessagePrompt;
//import xyz.wbsite.achat.core.service.SessionService;
//
//import javax.annotation.Resource;
//import java.util.List;
//import java.util.UUID;
//
///**
// * AI会话接口.
// *
// * @author wangbing
// * @version 0.0.1
// * @since 1.8
// */
//@RestController
//public class ChatController {
//
// @Resource
// private SessionService sessionService;
//
// /**
// * 门户
// */
// @GetMapping("/")
// public String chat() {
// return "AChat is running!";
// }
//
// /**
// * 发送消息(流式响应)
// *
// * @return SSE发射器用于流式响应
// */
// @PostMapping(value = "/chat/send", produces = "text/event-stream;charset=UTF-8")
// public SseEmitter send(@RequestBody MessagePrompt message) {
// return sessionService.sendMessage(message);
// }
//
// /**
// * 创建会话
// *
// * @return 创建会话响应
// */
// @PostMapping("/chat/session")
// public Result<Session> createSession() {
// String sid = UUID.randomUUID().toString();
// return sessionService.createSession(sid);
// }
//
// /**
// * 获取会话列表
// *
// * @return 会话列表响应
// */
// @ResponseBody
// @GetMapping("/chat/session/list")
// public Result<List<Session>> listSession() {
// String sid = UUID.randomUUID().toString();
// return sessionService.listSessions(sid);
// }
//
// /**
// * 删除会话
// *
// * @param sid 会话ID
// * @return 删除会话响应
// */
// @ResponseBody
// @DeleteMapping("/chat/session/{sid}")
// public Result<Void> deleteSession(@PathVariable("sid") String sid) {
// return sessionService.deleteSession(sid);
// }
//
// /**
// * 获取会话历史消息
// *
// * @param sid 会话ID
// * @return 历史消息响应
// */
// @ResponseBody
// @GetMapping("/chat/session/{sid}/history")
// public Result<List<Message>> getSessionHistory(@PathVariable("sid") String sid) {
// return sessionService.listMessage(sid);
// }
//
// /**
// * 停止AI回答
// *
// * @param sid 会话ID
// * @return 停止回答响应
// */
// @ResponseBody
// @PostMapping("/chat/session/{sid}/stop")
// public Result<Void> stopSession(@PathVariable("sid") String sid) {
// return sessionService.stopSession(sid);
// }
//}

@ -0,0 +1,23 @@
package xyz.wbsite.achat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
@RestController
public class HelloController {
/**
*
*/
@GetMapping("/")
public String chat() {
return "AChat is running!";
}
}

@ -12,7 +12,7 @@ import xyz.wbsite.achat.core.chat.CompletionResponse;
import xyz.wbsite.achat.core.chat.EmbeddingsRequest;
import xyz.wbsite.achat.core.chat.EmbeddingsResponse;
import xyz.wbsite.achat.core.model.ModelListResponse;
import xyz.wbsite.achat.core.service.ChatService;
import xyz.wbsite.achat.core.chat.ChatService;
import javax.annotation.Resource;

@ -0,0 +1,86 @@
package xyz.wbsite.achat;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.wbsite.achat.core.session.Result;
import xyz.wbsite.achat.core.session.Session;
import xyz.wbsite.achat.core.session.SessionService;
import javax.annotation.Resource;
import java.util.List;
/**
*
*
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
@RestController
public class SessionController {
@Resource
private SessionService sessionService;
/**
*
*
* @param uid
* @return
*/
@PostMapping("/{uid}/session")
public Result<Session> createSession(@PathVariable("uid") String uid) {
return sessionService.createSession(uid);
}
/**
*
*
* @param uid
* @param sid ID
* @return
*/
@DeleteMapping("/{uid}/session/{sid}")
public Result<Void> deleteSession(@PathVariable("uid") String uid, @PathVariable("sid") String sid) {
return sessionService.deleteSession(uid, sid);
}
/**
*
*
* @param uid
* @param sid ID
* @return
*/
@GetMapping("/{uid}/session/{sid}")
public Result<Session> getSession(@PathVariable("uid") String uid, @PathVariable("sid") String sid) {
return sessionService.getSession(uid, sid);
}
/**
*
*
* @param uid
* @return
*/
@GetMapping("/{uid}/session/list")
public Result<List<Session>> listSession(@PathVariable("uid") String uid) {
return sessionService.listSessions(uid);
}
/**
*
*
* @param uid
* @param sid ID
* @return
*/
@DeleteMapping("/{uid}/session/{sid}/messages/{mid}")
public Result<Void> deleteMessage(@PathVariable("uid") String uid, @PathVariable("sid") String sid, @PathVariable("mid") String mid) {
return sessionService.deleteMessage(uid, sid, mid);
}
}

@ -3,8 +3,8 @@ 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;
import xyz.wbsite.achat.core.chat.ChatService;
import xyz.wbsite.achat.core.chat.ChatServiceSampleImpl;
/**
*

@ -0,0 +1,66 @@
package xyz.wbsite.achat.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* 线.
* 线
* 线
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
@Configuration
public class ThreadPoolConfig {
// 获取CPU核心数
private final int cpuCount = Runtime.getRuntime().availableProcessors();
/**
* 线线
* IO线CPU
*/
private final int corePoolSize = Math.max(1, cpuCount);
/**
* 线线
* CPU+11线
*/
private final int maxPoolSize = Math.max(1, cpuCount + 1);
/**
*
* IO使线
*/
private final int queueCapacity = 100;
/**
*
* 线
*/
private final int keepAliveSeconds = 30;
/**
* 线
*/
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
threadPoolTaskExecutor.setThreadNamePrefix("ThreadPool-");
// rejection-policy当pool已经达到max size的时候如何处理新任务
// CALLER_RUNS不在新线程中公式任务而是由调用者所在的线程来执行
// 对拒绝task的处理策略
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
}

@ -1,30 +0,0 @@
//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;
// }
//}

@ -1,75 +0,0 @@
//package xyz.wbsite.achat.core;
//
///**
// * 服务器推送事件
// *
// * @author wangbing
// * @version 0.0.1
// * @since 1.8
// */
//public class Event {
//
// private String id;
//
// private String object;
//
// private String model;
//
// private Long created;
//
// private String sid;
//
// private String uid;
//
// public Event() {
// this.created = System.currentTimeMillis();
// }
//
// 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 String getModel() {
// return model;
// }
//
// public void setModel(String model) {
// this.model = model;
// }
//
// public Long getCreated() {
// return created;
// }
//
// public void setCreated(Long created) {
// this.created = created;
// }
//
// public String getSid() {
// return sid;
// }
//
// public void setSid(String sid) {
// this.sid = sid;
// }
//
// public String getUid() {
// return uid;
// }
//
// public void setUid(String uid) {
// this.uid = uid;
// }
//}

@ -1,17 +0,0 @@
//package xyz.wbsite.achat.core;
//
//public class Prompt {
//
// /**
// * 提示词
// */
// private String prompt;
//
// public String getPrompt() {
// return prompt;
// }
//
// public void setPrompt(String prompt) {
// this.prompt = prompt;
// }
//}

@ -1,209 +0,0 @@
//package xyz.wbsite.achat.core;
//
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * 接口响应结果基类
// * 泛型支持的数据响应封装,提供统一的响应格式和错误处理
// *
// * @author wangbing
// * @version 0.0.1
// * @since 1.8
// */
//public class Result<T> {
//
// /**
// * 响应状态码
// */
// private int code = 200;
//
// /**
// * 响应消息
// */
// private String message = "success";
//
// /**
// * 响应数据
// */
// private T data;
//
// /**
// * 是否成功
// */
// private boolean success = true;
//
// /**
// * 错误详情,用于表单验证等场景
// */
// private Map<String, String> errors;
//
// /**
// * 请求ID用于问题追踪
// */
// private String requestId;
//
// /**
// * 响应时间戳
// */
// private long timestamp = System.currentTimeMillis();
//
// public int getCode() {
// return code;
// }
//
// public Result<T> setCode(int code) {
// this.code = code;
// return this;
// }
//
// public String getMessage() {
// return message;
// }
//
// public Result<T> setMessage(String message) {
// this.message = message;
// return this;
// }
//
// public T getData() {
// return data;
// }
//
// public Result<T> setData(T data) {
// this.data = data;
// return this;
// }
//
// public boolean isSuccess() {
// return success;
// }
//
// public Result<T> setSuccess(boolean success) {
// this.success = success;
// return this;
// }
//
// public Map<String, String> getErrors() {
// return errors;
// }
//
// public Result<T> setErrors(Map<String, String> errors) {
// this.errors = errors;
// return this;
// }
//
// public String getRequestId() {
// return requestId;
// }
//
// public Result<T> setRequestId(String requestId) {
// this.requestId = requestId;
// return this;
// }
//
// public long getTimestamp() {
// return timestamp;
// }
//
// public Result<T> setTimestamp(long timestamp) {
// this.timestamp = timestamp;
// return this;
// }
//
// /**
// * 添加字段级别的错误信息
// *
// * @param field 字段名
// * @param error 错误信息
// * @return 当前结果对象,支持链式调用
// */
// public Result<T> addError(String field, String error) {
// if (errors == null) {
// errors = new HashMap<>();
// }
// errors.put(field, error);
// return this;
// }
//
// /**
// * 返回成功信息
// *
// * @return 结果
// */
// public static <T> Result<T> success() {
// return new Result<>();
// }
//
// /**
// * 返回带数据的成功信息
// *
// * @param data 响应数据
// * @return 结果
// */
// public static <T> Result<T> success(T data) {
// Result<T> result = new Result<>();
// result.setData(data);
// return result;
// }
//
// /**
// * 返回错误信息
// *
// * @param message 错误信息
// * @return 错误信息对象
// */
// public static <T> Result<T> error(String message) {
// Result<T> result = new Result<>();
// result.message = message;
// result.code = 500;
// result.success = false;
// return result;
// }
//
// /**
// * 返回错误信息
// *
// * @param code 错误码
// * @param message 错误信息
// * @return 错误信息对象
// */
// public static <T> Result<T> error(int code, String message) {
// Result<T> result = new Result<>();
// result.code = code;
// result.message = message;
// result.success = false;
// return result;
// }
//
// /**
// * 返回带数据的错误信息
// *
// * @param code 错误码
// * @param message 错误信息
// * @param data 错误相关数据
// * @return 错误信息对象
// */
// public static <T> Result<T> error(int code, String message, T data) {
// Result<T> result = new Result<>();
// result.code = code;
// result.message = message;
// result.success = false;
// result.data = data;
// return result;
// }
//
// /**
// * 从异常创建错误响应
// *
// * @param e 异常
// * @return 错误信息对象
// */
// public static <T> Result<T> error(Exception e) {
// Result<T> result = new Result<>();
// result.code = 500;
// result.message = e.getMessage() != null ? e.getMessage() : "系统异常";
// result.success = false;
// return result;
// }
//}

@ -1,126 +0,0 @@
//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,5 +1,6 @@
package xyz.wbsite.achat.core.service;
package xyz.wbsite.achat.core.chat;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import xyz.wbsite.achat.core.chat.ChatCompletionRequest;
import xyz.wbsite.achat.core.chat.ChatCompletionResponse;
import xyz.wbsite.achat.core.chat.CompletionRequest;
@ -14,7 +15,7 @@ public interface ChatService {
ChatCompletionResponse chat(ChatCompletionRequest request);
StreamEmitter streamChat(ChatCompletionRequest request);
SseEmitter streamChat(ChatCompletionRequest request);
EmbeddingsResponse embeddings(EmbeddingsRequest request);
}

@ -1,5 +1,7 @@
package xyz.wbsite.achat.core.service.impl;
package xyz.wbsite.achat.core.chat;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import xyz.wbsite.achat.core.chat.ChatCompletionRequest;
import xyz.wbsite.achat.core.chat.ChatCompletionResponse;
import xyz.wbsite.achat.core.chat.CompletionRequest;
@ -9,20 +11,31 @@ import xyz.wbsite.achat.core.chat.EmbeddingsResponse;
import xyz.wbsite.achat.core.chat.Role;
import xyz.wbsite.achat.core.chat.StreamEmitter;
import xyz.wbsite.achat.core.chat.Usage;
import xyz.wbsite.achat.core.service.ChatService;
import xyz.wbsite.achat.core.chat.ChatService;
import javax.annotation.Resource;
import java.util.Collections;
/**
* AI
* ChatServicepromptchatstreamChat
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
public class ChatServiceSampleImpl implements ChatService {
@Resource
private ThreadPoolTaskExecutor taskExecutor;
/**
*
*/
private final String DEFAULT_PROMPT = "您好我还没有接入AI请接入后再试";
@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")
@ -32,7 +45,7 @@ public class ChatServiceSampleImpl implements ChatService {
choices.add(CompletionResponse.choiceBuilder()
.index(0)
.role(Role.ASSISTANT)
.content("您好,我还没有接入AI,请接入后再试!")
.content(DEFAULT_PROMPT)
.finish_reason("stop").build());
})
.usage(Usage.builder()
@ -54,7 +67,7 @@ public class ChatServiceSampleImpl implements ChatService {
choices.add(ChatCompletionResponse.choiceBuilder()
.index(0)
.role(Role.ASSISTANT)
.content("您好,我还没有接入AI,请接入后再试!")
.content(DEFAULT_PROMPT)
.finish_reason("stop")
.build());
})
@ -67,7 +80,7 @@ public class ChatServiceSampleImpl implements ChatService {
}
@Override
public StreamEmitter streamChat(ChatCompletionRequest request) {
public SseEmitter streamChat(ChatCompletionRequest request) {
// 验证请求参数
if (request.getModel() == null) {
throw new IllegalArgumentException("模型不能为空");
@ -75,45 +88,54 @@ public class ChatServiceSampleImpl implements ChatService {
if (request.getMessages() == null || request.getMessages().isEmpty()) {
throw new IllegalArgumentException("消息不能为空");
}
StreamEmitter streamEmitter = StreamEmitter.builder()
.chatCompletionRequest(request)
.build();
StreamEmitter streamEmitter = new StreamEmitter(request);
return streamEmitter;
taskExecutor.execute(() -> {
String chatId = "chatcmpl-" + System.currentTimeMillis();
streamEmitter.onStart(chatId);
char[] charArray = DEFAULT_PROMPT.toCharArray();
for (char c : charArray) {
streamEmitter.onPartial(String.valueOf(c));
}
streamEmitter.onComplete();
});
// 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;
return streamEmitter;
}
@Override
public EmbeddingsResponse embeddings(EmbeddingsRequest request) {
return null;
// 验证请求参数
if (request == null) {
throw new IllegalArgumentException("请求参数不能为空");
}
if (request.getModel() == null || request.getModel().isEmpty()) {
throw new IllegalArgumentException("模型不能为空");
}
if (request.getInput() == null || request.getInput().isEmpty()) {
throw new IllegalArgumentException("输入文本不能为空");
}
// 模拟生成嵌入向量响应
return EmbeddingsResponse.builder()
.id("embedding-" + System.currentTimeMillis())
.object("list")
.created(System.currentTimeMillis() / 1000)
.model(request.getModel())
.withData(data -> {
for (int i = 0; i < request.getInput().size(); i++) {
data.add(EmbeddingsResponse.dataBuilder()
.index(i)
.embedding(Collections.emptyList())
.object("embedding")
.build());
}
})
.usage(Usage.builder()
.prompt_tokens(10)
.completion_tokens(10)
.total_tokens(10)
.build())
.build();
}
}

@ -1,12 +1,89 @@
package xyz.wbsite.achat.core.chat;
import java.util.ArrayList;
import java.util.List;
/**
*
* - OpenAIAPI
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
public class EmbeddingsRequest {
private String model;
private List<String> input;
private String user;
// 无参构造函数
public EmbeddingsRequest() {
}
// 私有构造函数用于Builder模式
private EmbeddingsRequest(Builder builder) {
this.model = builder.model;
this.input = builder.input;
this.user = builder.user;
}
// 静态builder方法返回Builder实例
public static Builder builder() {
return new Builder();
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public List<String> getInput() {
return input;
}
public void setInput(List<String> input) {
this.input = input;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
// Builder内部类
public static class Builder {
private String model;
private List<String> input = new ArrayList<>();
private String user;
public Builder model(String model) {
this.model = model;
return this;
}
public Builder input(List<String> input) {
this.input = input;
return this;
}
public Builder addInput(String text) {
this.input.add(text);
return this;
}
public Builder user(String user) {
this.user = user;
return this;
}
// 构建EmbeddingsRequest对象
public EmbeddingsRequest build() {
return new EmbeddingsRequest(this);
}
}
}

@ -4,7 +4,8 @@ import java.util.ArrayList;
import java.util.List;
/**
* OpenAI - OpenAIAPI
* - OpenAIAPI
*
*
* @author wangbing
* @version 0.0.1
@ -15,18 +16,30 @@ public class EmbeddingsResponse {
private String object;
private long created;
private String model;
private List<Choice> choices;
private List<Data> data = new ArrayList<>();
private Usage usage;
/**
*
*/
public EmbeddingsResponse() {
}
/**
* Builder
*/
private EmbeddingsResponse(Builder builder) {
this.id = builder.id;
this.object = builder.object;
this.created = builder.created;
this.model = builder.model;
this.choices = builder.choices;
this.data = builder.data;
this.usage = builder.usage;
}
/**
* builderBuilder
*/
public static Builder builder() {
return new Builder();
}
@ -63,12 +76,15 @@ public class EmbeddingsResponse {
this.model = model;
}
public List<Choice> getChoices() {
return choices;
/**
*
*/
public List<Data> getData() {
return data;
}
public void setChoices(List<Choice> choices) {
this.choices = choices;
public void setData(List<Data> data) {
this.data = data;
}
public Usage getUsage() {
@ -79,12 +95,15 @@ public class EmbeddingsResponse {
this.usage = usage;
}
/**
* BuilderEmbeddingsResponse
*/
public static class Builder {
private String id;
private String object;
private long created;
private String model;
private List<Choice> choices = new ArrayList<>();
private List<Data> data = new ArrayList<>();
private Usage usage;
public Builder id(String id) {
@ -107,13 +126,21 @@ public class EmbeddingsResponse {
return this;
}
public Builder choices(List<Choice> choices) {
this.choices = choices;
public Builder data(List<Data> data) {
this.data = data;
return this;
}
public Builder withChoices(java.util.function.Consumer<List<Choice>> choicesConsumer) {
choicesConsumer.accept(this.choices);
public Builder addData(Data data) {
this.data.add(data);
return this;
}
/**
* data访lambdadata
*/
public Builder withData(java.util.function.Consumer<List<Data>> dataConsumer) {
dataConsumer.accept(this.data);
return this;
}
@ -122,83 +149,115 @@ public class EmbeddingsResponse {
return this;
}
/**
* EmbeddingsResponse
*/
public EmbeddingsResponse build() {
return new EmbeddingsResponse(this);
}
}
public static class Choice {
private int index = 0;
private Message message;
private String finish_reason;
public Integer getIndex() {
return index;
}
/**
* -
*/
public static class Data {
private int index;
private List<Double> embedding;
private String object;
public void setIndex(Integer index) {
this.index = index;
/**
*
*/
public Data() {
}
public Message getMessage() {
return message;
/**
* Builder
*/
private Data(Builder builder) {
this.index = builder.index;
this.embedding = builder.embedding;
this.object = builder.object;
}
public void setMessage(Message message) {
this.message = message;
/**
* builderBuilder
*/
public static Builder builder() {
return new Builder();
}
public String getFinish_reason() {
return finish_reason;
public int getIndex() {
return index;
}
public void setFinish_reason(String finish_reason) {
this.finish_reason = finish_reason;
}
}
public static ChoiceBuilder choiceBuilder() {
return new ChoiceBuilder();
}
public static class ChoiceBuilder {
private Integer index;
private Role role;
private String content;
private String name;
private String finish_reason;
public ChoiceBuilder index(Integer index) {
public void setIndex(int index) {
this.index = index;
return this;
}
public ChoiceBuilder role(Role role) {
this.role = role;
return this;
/**
*
*/
public List<Double> getEmbedding() {
return embedding;
}
public ChoiceBuilder content(String content) {
this.content = content;
return this;
public void setEmbedding(List<Double> embedding) {
this.embedding = embedding;
}
public ChoiceBuilder name(String name) {
this.name = name;
return this;
public String getObject() {
return object;
}
public ChoiceBuilder finish_reason(String finish_reason) {
this.finish_reason = finish_reason;
return this;
public void setObject(String object) {
this.object = object;
}
public Choice build() {
Choice choice = new Choice();
choice.setIndex(index);
choice.setMessage(Message.builder().role(role).content(content).name(name).build());
choice.setFinish_reason(finish_reason);
return choice;
/**
* DataBuilder
*/
public static class Builder {
private int index;
private List<Double> embedding;
private String object = "embedding";
public Builder index(int index) {
this.index = index;
return this;
}
public Builder embedding(List<Double> embedding) {
this.embedding = embedding;
return this;
}
public Builder addEmbeddingValue(double value) {
if (this.embedding == null) {
this.embedding = new ArrayList<>();
}
this.embedding.add(value);
return this;
}
public Builder object(String object) {
this.object = object;
return this;
}
/**
* Data
*/
public Data build() {
return new Data(this);
}
}
}
/**
* DataBuilder
*/
public static Data.Builder dataBuilder() {
return Data.builder();
}
}

@ -1,10 +1,7 @@
package xyz.wbsite.achat.core.chat;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import xyz.wbsite.achat.core.service.ChatCompletionGenerator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
@ -16,123 +13,54 @@ import java.util.concurrent.Executors;
public class StreamEmitter extends SseEmitter {
/**
*
*
*/
private ChatCompletionRequest chatCompletionRequest;
private static final Long DEFAULT_TIMEOUT = 5 * 60 * 1000L;
/**
*
*
*/
private boolean complete;
private ChatCompletionRequest request;
/**
* AI
*
*/
private final StringBuilder answer = new StringBuilder();
private Status status;
/**
*
* ID
*/
private ChatCompletionGenerator chatCompletionGenerator;
private String chatId;
/**
* 线
*
*/
private static final ExecutorService messageExecutor = Executors.newFixedThreadPool(
Math.max(4, Runtime.getRuntime().availableProcessors()),
r -> {
Thread thread = new Thread(r, "message-generator-thread");
thread.setDaemon(true);
return thread;
}
);
private StreamEmitter(Builder builder) {
super(Long.MAX_VALUE);
this.chatCompletionRequest = builder.chatCompletionRequest;
this.chatCompletionGenerator = builder.chatCompletionGenerator;
// 使用线程池执行MessageGenerator
if (this.chatCompletionGenerator != null && this.chatCompletionRequest != null) {
messageExecutor.execute(() -> {
try {
this.chatCompletionGenerator.on(this, this.chatCompletionRequest);
} catch (Exception e) {
onError(e);
} finally {
if (!isComplete()) {
onCompleteResponse(null);
}
}
});
}
}
public static Builder builder() {
return new Builder();
}
// Builder内部类
public static class Builder {
private ChatCompletionRequest chatCompletionRequest;
private ChatCompletionGenerator chatCompletionGenerator;
public Builder chatCompletionRequest(ChatCompletionRequest chatCompletionRequest) {
this.chatCompletionRequest = chatCompletionRequest;
return this;
}
public Builder chatCompletionGenerator(ChatCompletionGenerator chatCompletionGenerator) {
this.chatCompletionGenerator = chatCompletionGenerator;
return this;
}
// 构建StreamEmitter对象
public StreamEmitter build() {
return new StreamEmitter(this);
}
}
public StreamEmitter(ChatCompletionRequest chatCompletionRequest, ChatCompletionGenerator chatCompletionGenerator) {
super(Long.MAX_VALUE);
this.chatCompletionRequest = chatCompletionRequest;
this.chatCompletionGenerator = chatCompletionGenerator;
// 使用线程池执行MessageGenerator
if (chatCompletionGenerator != null && chatCompletionRequest != null) {
messageExecutor.execute(() -> {
try {
chatCompletionGenerator.on(this, chatCompletionRequest);
} catch (Exception e) {
onError(e);
} finally {
if (!isComplete()) {
onCompleteResponse(null);
}
}
});
}
}
private boolean complete;
/**
*
*/
private void onError(Throwable e) {
// this.sendMessage(createPartialMessage("</think></think>" + e.getMessage()));
this.answer.append("</think></think>" + e.getMessage());
this.onCompleteResponse(null);
public StreamEmitter(ChatCompletionRequest request) {
super(DEFAULT_TIMEOUT);
this.request = request;
}
/**
*
*/
public void onPartialResponse(String msg) {
if (complete) {
return;
}
// this.sendMessage(createPartialMessage(msg));
this.answer.append(msg);
}
// /**
// * 错误处理
// */
// private void onError(Throwable e) {
//// this.sendMessage(createPartialMessage("</think></think>" + e.getMessage()));
// this.answer.append("</think></think>" + e.getMessage());
// this.onCompleteResponse(null);
// }
//
// /**
// * 部分响应处理
// */
// public void onPartialResponse(String msg) {
// if (complete) {
// return;
// }
//// this.sendMessage(createPartialMessage(msg));
// this.answer.append(msg);
// }
/**
*
@ -147,20 +75,6 @@ public class StreamEmitter extends SseEmitter {
this.complete();
}
// /**
// * 创建部分消息事件
// */
// private Event createPartialMessage(String partial) {
// return new PartialEvent(messagePrompt.getSid(), partial);
// }
//
// /**
// * 创建完成消息事件
// */
// private Event createCompleteMessage() {
// return new CompleteEvent(messagePrompt.getSid());
// }
/**
* send
*/
@ -173,20 +87,87 @@ public class StreamEmitter extends SseEmitter {
}
}
public void callStart() {
}
/**
*
*
*/
private void sendMessage(Object message) {
private void pushChunk(ChatCompletionChunk chunk) {
try {
this.send(message);
this.send(chunk);
} catch (Exception e) {
complete = true;
}
}
/**
*
*
* @param chatId id
*/
public void onStart(String chatId) {
if (!StringUtils.hasText(chatId)) {
throw new IllegalArgumentException("chatId is empty!");
}
if (this.status != null) {
throw new IllegalArgumentException("chunk has been started!");
}
ChatCompletionChunk chunk = ChatCompletionChunk.builder()
.id(chatId)
.object("chat.completion.chunk")
.created(System.currentTimeMillis() / 1000)
.model(request.getModel())
.withChoices(choices -> {
choices.add(ChatCompletionChunk.choiceBuilder().index(0)
.role(Role.ASSISTANT)
.build());
})
.build();
this.pushChunk(chunk);
this.status = Status.PENDING;
this.chatId = chatId;
}
/**
*
*
* @param text
*/
public void onPartial(String text) {
ChatCompletionChunk chunk = ChatCompletionChunk.builder()
.id(chatId)
.object("chat.completion.chunk")
.created(System.currentTimeMillis() / 1000)
.model(request.getModel())
.withChoices(choices -> {
choices.add(ChatCompletionChunk.choiceBuilder().index(0)
.role(Role.ASSISTANT)
.content(text)
.build());
})
.build();
this.pushChunk(chunk);
}
/**
* OpenAI APIfinish_reason
*/
public void onComplete() {
ChatCompletionChunk chunk = ChatCompletionChunk.builder()
.id(chatId)
.object("chat.completion.chunk")
.created(System.currentTimeMillis() / 1000)
.model(request.getModel())
.withChoices(choices -> {
choices.add(ChatCompletionChunk.choiceBuilder().index(0)
.role(Role.ASSISTANT)
// 符合OpenAI规范最后一个片段需要设置finish_reason
.finish_reason("stop")
.build());
})
.build();
this.pushChunk(chunk);
this.status = Status.SUCCESS;
}
/**
*
*/

@ -1,103 +0,0 @@
//package xyz.wbsite.achat.core.event;
//
//import xyz.wbsite.achat.core.Event;
//
///**
// * 完成事件
// * 用于标识流式响应的结束
// *
// * @author wangbing
// * @version 0.0.1
// * @since 1.8
// */
//public class CompleteEvent extends Event {
//
// /**
// * 完整响应内容
// */
// private String content;
//
// /**
// * 构造函数
// *
// * @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;
// }
//}

@ -1,80 +0,0 @@
//package xyz.wbsite.achat.core.event;
//
//
//import xyz.wbsite.achat.core.Event;
//
///**
// * 分段内容事件
// * 用于推送流式响应的部分内容
// *
// * @author wangbing
// * @version 0.0.1
// * @since 1.8
// */
//public class PartialEvent extends Event {
//
// /**
// * 分段内容
// */
// private String content;
//
// /**
// * 构造函数
// *
// * @param sid 会话ID
// * @param partial 部分内容
// */
// public PartialEvent(String sid, String partial) {
// super();
// this.setSid(sid);
// this.content = partial;
// this.setObject("chat.completion.chunk");
// }
//
// /**
// * 是否是最后一段
// */
// private Boolean isFinal;
//
// /**
// * 完成率范围0-1
// */
// private Double finishRate;
//
// /**
// * 当前累积的生成令牌数量
// */
// private Integer completionTokens;
//
// public String getContent() {
// return content;
// }
//
// public void setContent(String content) {
// this.content = content;
// }
//
// public Boolean getIsFinal() {
// return isFinal;
// }
//
// public void setIsFinal(Boolean isFinal) {
// this.isFinal = isFinal;
// }
//
// public Double getFinishRate() {
// return finishRate;
// }
//
// public void setFinishRate(Double finishRate) {
// this.finishRate = finishRate;
// }
//
// public Integer getCompletionTokens() {
// return completionTokens;
// }
//
// public void setCompletionTokens(Integer completionTokens) {
// this.completionTokens = completionTokens;
// }
//}

@ -1,66 +0,0 @@
//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;
// }
//}

@ -1,105 +0,0 @@
//package xyz.wbsite.achat.core.prompt;
//
//import xyz.wbsite.achat.core.chat.Message;
//import xyz.wbsite.achat.core.Prompt;
//import xyz.wbsite.achat.core.message.UserMessage;
//
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//public class MessagePrompt extends Prompt {
//
// /**
// * 模型
// */
// private String model;
//
// /**
// * 消息列表
// */
// private List<Message> messages;
//
// /**
// * 是否流式返回
// */
// private Boolean stream;
//
// /**
// * 最大token数
// */
// private Integer maxTokens;
//
// /**
// * 温度参
// */
// private Double temperature;
//
// /**
// * 额外参数
// */
// private Map<String, Object> extraParams;
//
// 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 Boolean getStream() {
// return stream;
// }
//
// public void setStream(Boolean stream) {
// this.stream = stream;
// }
//
// 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 Map<String, Object> getExtraParams() {
// return extraParams;
// }
//
// public void setExtraParams(Map<String, Object> extraParams) {
// this.extraParams = extraParams;
// }
//
// public UserMessage getLastUserMessage() {
// List<Message> messageList = messages.stream().filter(message -> message instanceof UserMessage).collect(Collectors.toList());
// UserMessage userMessage = (UserMessage)messageList.get(messageList.size() - 1);
// return userMessage;
// }
//
// public String getUid() {
// return getLastUserMessage().getUid();
// }
//
// public String getSid(){
// return getLastUserMessage().getSid();
// }
//
//}

@ -1,18 +0,0 @@
package xyz.wbsite.achat.core.service;
import xyz.wbsite.achat.core.chat.ChatCompletionRequest;
import xyz.wbsite.achat.core.chat.StreamEmitter;
/**
*
* <p>
*
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
public interface ChatCompletionGenerator {
void on(StreamEmitter emitter, ChatCompletionRequest chatCompletionRequest);
}

@ -1,84 +0,0 @@
//package xyz.wbsite.achat.core.service;
//
//import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
//import xyz.wbsite.achat.core.chat.Message;
//import xyz.wbsite.achat.core.Result;
//import xyz.wbsite.achat.core.Session;
//import xyz.wbsite.achat.core.prompt.MessagePrompt;
//
//import java.util.List;
//
///**
// * 会话管理服务接口
// * 提供会话的创建、删除、查询、消息收发等功能
// *
// * @author wangbing
// * @version 0.0.1
// * @since 1.8
// */
//public interface SessionService {
//
// /**
// * 创建新会话
// *
// * @param uid 用户编号
// * @return 创建的会话对象
// */
// Result<Session> createSession(String uid);
//
// /**
// * 删除会话
// *
// * @param sid 会话ID
// * @return 删除是否成功
// */
// Result<Void> deleteSession(String sid);
//
// /**
// * 查询会话列表
// *
// * @param uid 用户编号
// * @return 会话列表
// */
// Result<List<Session>> listSessions(String uid);
//
// /**
// * 获取会话详情
// *
// * @param sid 会话ID
// * @return 会话对象
// */
// Result<Session> getSession(String sid);
//
// /**
// * 停止会话
// *
// * @param sid 会话ID
// * @return 会话对象
// */
// Result<Void> stopSession(String sid);
//
// /**
// * 发送消息并获取流式响应
// *
// * @param message 消息对象
// * @return SSE发射器用于流式响应
// */
// SseEmitter sendMessage(MessagePrompt message);
//
// /**
// * 获取会话历史消息
// *
// * @param sid 会话ID
// * @return 消息列表
// */
// Result<List<Message>> listMessage(String sid);
//
// /**
// * 获取会话历史消息
// *
// * @param sid 会话ID
// * @return 消息列表
// */
// Result<Void> deleteMessage(String sid);
//}

@ -1,121 +0,0 @@
//package xyz.wbsite.achat.core.service.impl;
//
//import org.springframework.stereotype.Service;
//import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
//import xyz.wbsite.achat.core.message.MessageSseEmitter;
//import xyz.wbsite.achat.core.chat.Message;
//import xyz.wbsite.achat.core.Result;
//import xyz.wbsite.achat.core.Session;
//import xyz.wbsite.achat.core.prompt.MessagePrompt;
//import xyz.wbsite.achat.core.service.SessionService;
//
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.UUID;
//import java.util.stream.Collectors;
//
///**
// * 会话管理服务实现类
// * 实现会话的创建、删除、查询、消息收发等功能
// *
// * @author wangbing
// * @version 0.0.1
// * @since 1.8
// */
//@Service
//public class SessionServiceMemoryImpl implements SessionService {
//
// private final Map<String, Session> sessionStore = new HashMap<>();
//
// private final List<Message> messageStore = new ArrayList<>();
//
// /**
// * 创建新会话
// */
// @Override
// public Result<Session> createSession(String uid) {
// Session session = new Session();
// session.setId(String.valueOf(UUID.randomUUID().toString()));
// session.setUid(uid);
// session.setTitle("新对话");
// sessionStore.put(session.getId(), session);
// return Result.success(session);
// }
//
// /**
// * 删除会话
// */
// @Override
// public Result<Void> deleteSession(String sid) {
// sessionStore.remove(sid);
// return Result.success();
// }
//
// /**
// * 查询会话列表
// */
// @Override
// public Result<List<Session>> listSessions(String uid) {
// List<Session> collect = sessionStore.values()
// .stream()
// .filter(item -> item.getUid().equals(uid))
// .collect(Collectors.toList());
// return Result.success(collect);
// }
//
// /**
// * 获取会话详情
// */
// @Override
// public Result<Session> getSession(String sid) {
// Session session = sessionStore.get(sid);
// if (session == null) {
// return Result.error("会话不存在");
// }
// return Result.success(session);
// }
//
// /**
// * 发送消息并获取流式响应
// */
// @Override
// public SseEmitter sendMessage(MessagePrompt message) {
// // 创建VChatSseEmitter来处理流式响应
// return new MessageSseEmitter(message, (emitter, message1) -> {
// // 这边模拟LLM复述一遍用户问题
// String text = message1.getContent();
// for (char c : text.toCharArray()) {
// if (emitter.isComplete()) {
// return;
// }
// emitter.onPartialResponse(String.valueOf(c));
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// }
// emitter.onCompleteResponse(null);
// });
// }
//
// @Override
// public Result<Void> stopSession(String sid) {
// // todo
// return Result.success();
// }
//
// @Override
// public Result<List<Message>> listMessage(String sid) {
// List<Message> messages = messageStore.stream().filter(item -> item.getSid().equals(sid)).collect(Collectors.toList());
// return Result.success(messages);
// }
//
// @Override
// public Result<Void> deleteMessage(String sid) {
// messageStore.forEach(item -> messageStore.remove(item));
// return null;
// }
//}

@ -0,0 +1,34 @@
package xyz.wbsite.achat.core.session;
/**
*
*/
public class Message extends xyz.wbsite.achat.core.chat.Message {
private String id;
private String uid;
private String sid;
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 getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
}

@ -0,0 +1,163 @@
package xyz.wbsite.achat.core.session;
/**
*
*
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
public class Result<T> {
/**
*
*/
private int code = 200;
/**
*
*/
private String message = "success";
/**
*
*/
private T data;
/**
*
*/
private boolean success = true;
/**
*
*/
private long timestamp = System.currentTimeMillis();
public int getCode() {
return code;
}
public Result<T> setCode(int code) {
this.code = code;
return this;
}
public String getMessage() {
return message;
}
public Result<T> setMessage(String message) {
this.message = message;
return this;
}
public T getData() {
return data;
}
public Result<T> setData(T data) {
this.data = data;
return this;
}
public boolean isSuccess() {
return success;
}
public Result<T> setSuccess(boolean success) {
this.success = success;
return this;
}
public long getTimestamp() {
return timestamp;
}
public Result<T> setTimestamp(long timestamp) {
this.timestamp = timestamp;
return this;
}
/**
*
*
* @return
*/
public static <T> Result<T> success() {
return new Result<>();
}
/**
*
*
* @param data
* @return
*/
public static <T> Result<T> success(T data) {
Result<T> result = new Result<>();
result.setData(data);
return result;
}
/**
*
*
* @param message
* @return
*/
public static <T> Result<T> error(String message) {
Result<T> result = new Result<>();
result.message = message;
result.code = 500;
result.success = false;
return result;
}
/**
*
*
* @param code
* @param message
* @return
*/
public static <T> Result<T> error(int code, String message) {
Result<T> result = new Result<>();
result.code = code;
result.message = message;
result.success = false;
return result;
}
/**
*
*
* @param code
* @param message
* @param data
* @return
*/
public static <T> Result<T> error(int code, String message, T data) {
Result<T> result = new Result<>();
result.code = code;
result.message = message;
result.success = false;
result.data = data;
return result;
}
/**
*
*
* @param e
* @return
*/
public static <T> Result<T> error(Exception e) {
Result<T> result = new Result<>();
result.code = 500;
result.message = e.getMessage() != null ? e.getMessage() : "系统异常";
result.success = false;
return result;
}
}

@ -0,0 +1,138 @@
package xyz.wbsite.achat.core.session;
import java.util.List;
/**
*
*
* @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;
private List<Message> messages;
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;
}
public List<Message> getMessages() {
return messages;
}
public void setMessages(List<Message> messages) {
this.messages = messages;
}
}

@ -0,0 +1,56 @@
package xyz.wbsite.achat.core.session;
import xyz.wbsite.achat.core.chat.Message;
import java.util.List;
/**
*
*
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
public interface SessionService {
/**
*
*
* @param uid
* @return
*/
Result<Session> createSession(String uid);
/**
*
*
* @param sid ID
* @return
*/
Result<Void> deleteSession(String uid, String sid);
/**
*
*
* @param uid
* @return
*/
Result<List<Session>> listSessions(String uid);
/**
*
*
* @param sid ID
* @return
*/
Result<Session> getSession(String uid,String sid);
/**
*
*
* @param sid ID
* @return
*/
Result<Void> deleteMessage(String uid, String sid, String mid);
}

@ -0,0 +1,176 @@
package xyz.wbsite.achat.core.session;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
*
*
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
@Service
public class SessionServiceMemoryImpl implements SessionService {
/**
*
*/
private final List<Session> sessionStore = new ArrayList<>();
/**
*
*/
private final List<Message> messageStore = new ArrayList<>();
/**
*
*
* @param uid ID
* @return
*/
@Override
public Result<Session> createSession(String uid) {
// 检查参数合法性
if (uid == null || uid.isEmpty()) {
return Result.error("用户ID不能为空");
}
Session session = new Session();
session.setId(String.valueOf(UUID.randomUUID().toString()));
session.setUid(uid);
session.setTitle("新对话");
sessionStore.add(session);
return Result.success(session);
}
/**
*
*
* @param uid ID
* @param sid ID
* @return
*/
@Override
public Result<Void> deleteSession(String uid, String sid) {
// 检查参数合法性
if (uid == null || uid.isEmpty() || sid == null || sid.isEmpty()) {
return Result.error("用户ID或会话ID不能为空");
}
// 从sessionStore查找比对uid和sid然后删除
boolean sessionRemoved = sessionStore.removeIf(session ->
session.getId().equals(sid) && session.getUid().equals(uid)
);
// 从messageStore查找比对uid和sid然后删除
messageStore.removeIf(message ->
message.getUid().equals(uid) && message.getSid().equals(sid)
);
if (!sessionRemoved) {
return Result.error("会话不存在或无权限删除");
}
return Result.success();
}
/**
*
*
* @param uid ID
* @return
*/
/**
*
*
* @param uid ID
* @return
*/
@Override
public Result<List<Session>> listSessions(String uid) {
// 检查参数合法性
if (uid == null || uid.isEmpty()) {
return Result.error("用户ID不能为空");
}
// 从sessionStore查找比对uid返回所有会话
List<Session> userSessions = sessionStore.stream()
.filter(session -> session.getUid().equals(uid))
.collect(Collectors.toList());
return Result.success(userSessions);
}
/**
*
*
* @param uid ID
* @param sid ID
* @return
*/
@Override
public Result<Session> getSession(String uid, String sid) {
// 检查参数合法性
if (uid == null || uid.isEmpty() || sid == null || sid.isEmpty()) {
return Result.error("用户ID或会话ID不能为空");
}
// 从sessionStore查找比对uid和sid返回会话
Session session = sessionStore.stream()
.filter(s -> s.getId().equals(sid) && s.getUid().equals(uid))
.findFirst()
.orElse(null);
if (session == null) {
return Result.error("会话不存在或无权限查看");
}
// 创建会话副本
Session sessionCopy = new Session();
sessionCopy.setId(session.getId());
sessionCopy.setUid(session.getUid());
sessionCopy.setTitle(session.getTitle());
// 从messageStore检索出当前会话的聊天记录
List<Message> sessionMessages = messageStore.stream()
.filter(message -> message.getUid().equals(uid) && message.getSid().equals(sid))
.collect(Collectors.toList());
sessionCopy.setMessages(sessionMessages);
return Result.success(sessionCopy);
}
/**
*
*
* @param uid ID
* @param sid ID
* @param mid ID
* @return
*/
@Override
public Result<Void> deleteMessage(String uid, String sid, String mid) {
// 检查参数合法性
if (uid == null || uid.isEmpty() || sid == null || sid.isEmpty() || mid == null || mid.isEmpty()) {
return Result.error("用户ID、会话ID或消息ID不能为空");
}
// 从messageStore查找比对uid、sid和mid然后删除
boolean mr = messageStore.removeIf(message ->
message.getId().equals(mid) && message.getUid().equals(uid) && message.getSid().equals(sid)
);
if (!mr) {
return Result.error("消息不存在或无权限删除");
}
return Result.success();
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.