parent
289b34c477
commit
096710b3a6
@ -0,0 +1,178 @@
|
||||
package cn.teammodel.ai.deepseek;
|
||||
|
||||
import cn.teammodel.common.ErrorCode;
|
||||
import cn.teammodel.config.exception.ServiceException;
|
||||
import cn.teammodel.model.dto.ai.deepseek.ChatRequestDto;
|
||||
import cn.teammodel.model.dto.ai.deepseek.ChatResponseDto;
|
||||
import cn.teammodel.model.dto.ai.deepseek.MessageDto;
|
||||
import cn.teammodel.utils.JsonUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||
import com.google.gson.Gson;
|
||||
import okhttp3.*;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.*;
|
||||
|
||||
public class DeepSeekClient {
|
||||
private static final String API_Key;
|
||||
private static final String API_Url;
|
||||
public static String API_Model;
|
||||
/**
|
||||
* 读取配置文件 读取key 和url
|
||||
*/
|
||||
static {
|
||||
Properties props = new Properties();
|
||||
try {
|
||||
InputStream is = DeepSeekClient.class.getResourceAsStream("/DeepSeekConfig.properties");
|
||||
props.load(is);
|
||||
API_Key = props.getProperty("key");
|
||||
API_Url = props.getProperty("url");
|
||||
API_Model = props.getProperty("model");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 提问题 测试使用
|
||||
* @param mssage
|
||||
*/
|
||||
private static Map<String, Object> ask(MessageDto mssage)
|
||||
{
|
||||
Map<String, Object> mapper = new HashMap<>();
|
||||
//创建消息列表
|
||||
List<MessageDto> msg = new ArrayList<>();
|
||||
msg.add(mssage);
|
||||
|
||||
//构建请求头
|
||||
ChatRequestDto requestBody = new ChatRequestDto();
|
||||
requestBody.setModel(API_Model);
|
||||
requestBody.setMessages(msg);
|
||||
requestBody.setTemperature(0);
|
||||
requestBody.setMax_tokens(1024);
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
//发起请求
|
||||
ChatResponseDto response = SendRequests(requestBody);
|
||||
//Map<String, Object> response = SendRequest(requestBody);
|
||||
Long endTime = System.currentTimeMillis();
|
||||
//思考耗时 秒
|
||||
long time = (endTime-startTime)/1000;
|
||||
response.setWasteTime(time);
|
||||
return mapper;
|
||||
|
||||
}
|
||||
|
||||
/***
|
||||
* OkHttpClient 方式请求
|
||||
* 返回请求结果 转 实体有问题
|
||||
* @param requestBody
|
||||
* @return
|
||||
*/
|
||||
public static ChatResponseDto SendRequests(ChatRequestDto requestBody)
|
||||
{
|
||||
ChatResponseDto chatResponse = new ChatResponseDto();
|
||||
OkHttpClient client = new OkHttpClient().newBuilder().build();
|
||||
MediaType mediaType = MediaType.parse("application/json");
|
||||
//String content = "{\n \"messages\": [\n {\n \"content\": \"You are a helpful assistant\",\n \"role\": \"system\"\n },\n {\n \"content\": \"Hi\",\n \"role\": \"user\"\n }\n ],\n \"model\": \"deepseek-chat\",\n \"frequency_penalty\": 0,\n \"max_tokens\": 2048,\n \"presence_penalty\": 0,\n \"response_format\": {\n \"type\": \"text\"\n },\n \"stop\": null,\n \"stream\": false,\n \"stream_options\": null,\n \"temperature\": 1,\n \"top_p\": 1,\n \"tools\": null,\n \"tool_choice\": \"none\",\n \"logprobs\": false,\n \"top_logprobs\": null\n}";
|
||||
String content = JsonUtil.convertToJson(requestBody);
|
||||
|
||||
RequestBody body = RequestBody.create(mediaType, content);
|
||||
Request request = new Request.Builder()
|
||||
.url(API_Url)
|
||||
.method("POST", body)
|
||||
.addHeader("Content-Type", "application/json")
|
||||
.addHeader("Accept", "application/json")
|
||||
.addHeader("Authorization", "Bearer "+API_Key)
|
||||
.build();
|
||||
|
||||
try(Response response = client.newCall(request).execute()) {
|
||||
if (response.isSuccessful() && response.body() != null) {
|
||||
String responseBody = response.body().string();
|
||||
// 使用 Gson 将 JSON 字符串转换为 MyEntity 对象
|
||||
Gson gson = new Gson();
|
||||
chatResponse = gson.fromJson(responseBody, ChatResponseDto.class);
|
||||
// 确保关闭响应体以释放资源
|
||||
response.body().close();
|
||||
chatResponse.setCode(200);
|
||||
chatResponse.setMsg("成功");
|
||||
} else {
|
||||
chatResponse.setCode( response.code());
|
||||
chatResponse.setMsg(response.body().string());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
if (e.getMessage().equals("Connection timed out") || e.getMessage().equals("timeout"))
|
||||
{
|
||||
chatResponse.setCode(408);
|
||||
chatResponse.setMsg("请求DeepSeek服务器超时");
|
||||
}else {
|
||||
chatResponse.setCode( 500);
|
||||
chatResponse.setMsg(e.getMessage());
|
||||
}
|
||||
}
|
||||
return chatResponse;
|
||||
}
|
||||
|
||||
/***
|
||||
* HttpClient 方式请求
|
||||
* @param requestBody
|
||||
* @return
|
||||
*/
|
||||
public static Map<String, Object> SendRequest(ChatRequestDto requestBody) {
|
||||
Map<String, Object> mapper = new HashMap<>();
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
// 创建HttpPost对象
|
||||
HttpPost httpPost = new HttpPost(API_Url);
|
||||
//添加请求头
|
||||
httpPost.setHeader("Content-Type", "application/json");
|
||||
httpPost.setHeader("Accept", "application/json");
|
||||
httpPost.setHeader("Authorization", "Bearer " +API_Key);
|
||||
|
||||
// 设置请求体
|
||||
String jsonContent = JsonUtil.convertToJson(requestBody);
|
||||
httpPost.setEntity(new StringEntity(jsonContent, ContentType.create("application/json", "UTF-8")));
|
||||
|
||||
// 发送请求
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
||||
// 获取响应实体
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
// 解析响应内容
|
||||
String jsonString = EntityUtils.toString(entity);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
mapper = objectMapper.readValue(jsonString, TypeFactory.defaultInstance().constructMapType(Map.class, String.class, Object.class));
|
||||
}
|
||||
|
||||
// 检查响应状态码
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != 200) throw new RuntimeException("Failed : HTTP error code : " + statusCode);
|
||||
} catch (IOException e) {
|
||||
throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据解析异常");
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "请求头异常"+e.getMessage());
|
||||
}
|
||||
//TODO 请求接口
|
||||
return mapper;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
package cn.teammodel.controller.frontend;
|
||||
|
||||
import cn.teammodel.common.IdRequest;
|
||||
import cn.teammodel.model.dto.ai.UpdateSessionDto;
|
||||
import cn.teammodel.model.dto.ai.deepseek.ChatResponseDto;
|
||||
import cn.teammodel.model.dto.ai.deepseek.MessageDto;
|
||||
import cn.teammodel.model.entity.TmdUserDetail;
|
||||
import cn.teammodel.model.entity.ai.ChatSession;
|
||||
import cn.teammodel.model.entity.ai.DeepSeekSession;
|
||||
import cn.teammodel.model.entity.ai.DeepSeekSession.DeepSeekMessage;
|
||||
import cn.teammodel.repository.ChatSessionRepository;
|
||||
import cn.teammodel.repository.DeepSeekRepository;
|
||||
import cn.teammodel.security.utils.SecurityUtil;
|
||||
import cn.teammodel.service.DeepSeekService;
|
||||
import cn.teammodel.common.R;
|
||||
import cn.teammodel.service.DeepSeekSessionService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Valid;
|
||||
import java.util.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/aiDeepSeek")
|
||||
@Api(tags = "AI DeepSeek 能力")
|
||||
public class AiDeepSeekController {
|
||||
|
||||
/**
|
||||
* 访问DeepSeek方法
|
||||
*/
|
||||
@Resource
|
||||
private DeepSeekService deepSeekChatService;
|
||||
|
||||
/**
|
||||
* 会话管理
|
||||
*/
|
||||
@Resource
|
||||
private DeepSeekSessionService deepSeekSessionService;
|
||||
|
||||
/**
|
||||
* 创建默认会话
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("session/create/default")
|
||||
@ApiOperation("创建默认会话")
|
||||
public R<DeepSeekSession> CreateDefaultSession(){
|
||||
String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
|
||||
String name = (String) ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().get("name");
|
||||
DeepSeekSession session = deepSeekSessionService.CreateDefaultSession(userId,name);
|
||||
return R.success(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建会话 按照结构传输
|
||||
* @param createSession
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("session/create")
|
||||
@ApiOperation("创建会话(实体)")
|
||||
public R<DeepSeekSession> CreateDefaultSession(@RequestBody @Valid DeepSeekSession createSession){
|
||||
DeepSeekSession session = deepSeekSessionService.CreateSession(createSession);
|
||||
return R.success(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除会话
|
||||
* @param idRequest
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("session/del")
|
||||
@ApiOperation("删除会话")
|
||||
public R<String> removeSession(@RequestBody @Valid IdRequest idRequest) {
|
||||
String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
|
||||
deepSeekSessionService.deleteSession(idRequest.getId(), userId);
|
||||
return R.success("删除会话成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新会话
|
||||
* @param upSession
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("session/update")
|
||||
@ApiOperation("更新聊天会话")
|
||||
public R<DeepSeekSession> updateSession(@RequestBody @Valid DeepSeekSession upSession) {
|
||||
String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
|
||||
DeepSeekSession session = deepSeekSessionService.updateSession(upSession, userId);
|
||||
return R.success(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询我的会话
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("session/userid")
|
||||
@ApiOperation("查询自己的会话列表")
|
||||
public R<List<DeepSeekSession>> UserId(){
|
||||
String userId = SecurityUtil.getLoginUser().getId();
|
||||
List<DeepSeekSession> sessions = deepSeekSessionService.UserSessionList(userId);
|
||||
return R.success(sessions);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询某个会话的聊天记录
|
||||
* @param sessionId
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("session/history/{sessionId}")
|
||||
@ApiOperation("查询我的聊天记录")
|
||||
public R<List<DeepSeekMessage>> getHistory(@PathVariable String sessionId) {
|
||||
String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
|
||||
List<DeepSeekMessage> history = deepSeekSessionService.listHistory(sessionId, userId);
|
||||
return R.success(history);
|
||||
}
|
||||
|
||||
/**
|
||||
*与deepseek的对话
|
||||
* @param messageDto
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("chat")
|
||||
@ApiOperation("与deepseek的对话")
|
||||
public R<ChatResponseDto> ChatCompletion(@RequestBody @Valid MessageDto messageDto) {
|
||||
ChatResponseDto chatResponse = deepSeekChatService.ChatAsk(messageDto);
|
||||
return R.success(chatResponse);
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package cn.teammodel.model.dto.ai.deepseek;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 接着响应的json 格式来设计响应体 用来响应体字符串
|
||||
*/
|
||||
@Data
|
||||
public class ChatResponseDto {
|
||||
private int code;
|
||||
private String msg;
|
||||
private long wasteTime;
|
||||
private String id;
|
||||
private String object;
|
||||
private long created;
|
||||
private String model;
|
||||
|
||||
private Usage usage;
|
||||
|
||||
/**
|
||||
* 返回内容
|
||||
*/
|
||||
private List<Choice> choices;
|
||||
private String system_fingerprint;
|
||||
@Data
|
||||
public static class Choice {
|
||||
private int index;
|
||||
private MessageDto message;
|
||||
private String logprobs;
|
||||
private String finish_reason;
|
||||
}
|
||||
@Data
|
||||
public static class Usage{
|
||||
private int prompt_tokens;
|
||||
private int completion_tokens;
|
||||
private int total_tokens;
|
||||
private Prompt_Tokens_Details prompt_tokens_details;
|
||||
private int prompt_cache_hit_tokens;
|
||||
private int prompt_cache_miss_tokens;
|
||||
}
|
||||
@Data
|
||||
public static class Prompt_Tokens_Details {
|
||||
private int cached_tokens;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package cn.teammodel.model.dto.ai.deepseek;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 内部类定义请求/响应结构
|
||||
*/
|
||||
@Data
|
||||
public class MessageDto {
|
||||
private String role;
|
||||
private String sessionId;
|
||||
private String content;
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package cn.teammodel.repository;
|
||||
|
||||
import cn.teammodel.model.entity.ai.ChatSession;
|
||||
import cn.teammodel.model.entity.ai.DeepSeekSession;
|
||||
import com.azure.spring.data.cosmos.repository.CosmosRepository;
|
||||
import com.azure.spring.data.cosmos.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
/**
|
||||
* 数据库操作接口
|
||||
* Created by jiangjingming on 17/6/19.
|
||||
*/
|
||||
@Repository
|
||||
public interface DeepSeekRepository extends CosmosRepository<DeepSeekSession, String> {
|
||||
/**
|
||||
* 根据id和code查询会话信息
|
||||
* @param id
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
DeepSeekSession findSessionByIdAndCode(String id, String code);
|
||||
|
||||
/**
|
||||
* 根据会话id查询会话信息
|
||||
* @param sessionId
|
||||
* @return
|
||||
*/
|
||||
@Query("select * from c where c.code='DeepSeekSession' and c.sessionId = @sessionId")
|
||||
List<DeepSeekSession> findBySessionId(String sessionId);
|
||||
|
||||
/**
|
||||
* 根据用户id查询会话信息
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@Query("select c.id,c.userId,c.title,c.model,c.createTime,c.updateTime from c where c.code='DeepSeekSession' and c.userId = @userId")
|
||||
List<DeepSeekSession> findByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 根据会话id查询会话信息
|
||||
* @param sessionId
|
||||
* @return
|
||||
*/
|
||||
@Query("SELECT value ARRAY_SLICE(c.history, -3) FROM c where c.id = @sessionId and c.code = 'DeepSeekSession'")
|
||||
List<ChatSession.Message> findLatestMessage(String sessionId);
|
||||
|
||||
/**
|
||||
* 根据用户id查询会话信息
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@Query("select * from c where c.code = 'DeepSeekSession' and c.id = @userId")
|
||||
List<ChatSession> findCommentsById(String userId);
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package cn.teammodel.service;
|
||||
|
||||
import cn.teammodel.model.dto.ai.deepseek.ChatResponseDto;
|
||||
import cn.teammodel.model.dto.ai.deepseek.MessageDto;
|
||||
import cn.teammodel.model.entity.ai.DeepSeekSession;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 访问DeepSeek方法
|
||||
*/
|
||||
public interface DeepSeekService {
|
||||
|
||||
/**
|
||||
* 获取AI的回答
|
||||
* @param message
|
||||
* @return
|
||||
*/
|
||||
ChatResponseDto ChatAsk(MessageDto message);
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package cn.teammodel.service;
|
||||
|
||||
import cn.teammodel.model.dto.ai.deepseek.ChatResponseDto;
|
||||
import cn.teammodel.model.entity.ai.DeepSeekSession;
|
||||
import cn.teammodel.model.entity.ai.DeepSeekSession.DeepSeekMessage;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 会话服务接口
|
||||
* Created by Jiayiwu on 16/5/26.
|
||||
*/
|
||||
public interface DeepSeekSessionService {
|
||||
|
||||
/**
|
||||
* 创建默认会话
|
||||
* @param userId
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
DeepSeekSession CreateDefaultSession(String userId, String name);
|
||||
|
||||
/**
|
||||
* 创建会话
|
||||
* @param session
|
||||
* @return
|
||||
*/
|
||||
DeepSeekSession CreateSession(DeepSeekSession session);
|
||||
|
||||
/**
|
||||
* 更新会话
|
||||
* @param upSession
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
DeepSeekSession updateSession(DeepSeekSession upSession, String userId);
|
||||
|
||||
/**
|
||||
* 删除会话
|
||||
* @param id
|
||||
* @param userId
|
||||
*/
|
||||
void deleteSession (String id, String userId);
|
||||
|
||||
/**
|
||||
* 获取用户会话列表
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
List<DeepSeekSession> UserSessionList(String userId);
|
||||
|
||||
/**
|
||||
* 获取历史消息
|
||||
* @param sessionId
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
List<DeepSeekMessage> listHistory(String sessionId, String userId);
|
||||
|
||||
}
|
@ -0,0 +1,133 @@
|
||||
package cn.teammodel.service.impl;
|
||||
|
||||
import cn.teammodel.common.ErrorCode;
|
||||
import cn.teammodel.common.PK;
|
||||
import cn.teammodel.config.exception.ServiceException;
|
||||
import cn.teammodel.model.entity.ai.ChatSession;
|
||||
import cn.teammodel.model.entity.ai.DeepSeekSession;
|
||||
import cn.teammodel.model.entity.ai.DeepSeekSession.DeepSeekMessage;
|
||||
import cn.teammodel.repository.DeepSeekRepository;
|
||||
import cn.teammodel.service.DeepSeekSessionService;
|
||||
import cn.teammodel.utils.RepositoryUtil;
|
||||
import com.azure.cosmos.models.CosmosPatchOperations;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.Instant;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class DeepSeekSessionServiceImpl implements DeepSeekSessionService {
|
||||
@Resource // 注入
|
||||
private DeepSeekRepository deepSeekRepository;
|
||||
|
||||
/**
|
||||
* 创建初始会话
|
||||
* @param userId
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public DeepSeekSession CreateDefaultSession(String userId, String name) {
|
||||
//初始化第一条欢迎语
|
||||
DeepSeekMessage message = DeepSeekMessage.of("你好"+name, "我是你的私人 DeepSeek AI 助手小豆,你可以问我任何包括但不仅限于教育的问题,我会尽力为您解答!");
|
||||
List<DeepSeekMessage> history = Collections.singletonList(message);
|
||||
DeepSeekSession session = new DeepSeekSession();
|
||||
session.setId(UUID.randomUUID().toString());
|
||||
session.setCode(PK.DEEPSEEK_SESSION);
|
||||
session.setTitle("新对话");
|
||||
session.setUserId(userId);
|
||||
session.setCreateTime(Instant.now().toEpochMilli());
|
||||
session.setUpdateTime(Instant.now().toEpochMilli());
|
||||
session.setHistory(history);
|
||||
return deepSeekRepository.save(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新会话
|
||||
* @param session
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public DeepSeekSession CreateSession(DeepSeekSession session) {
|
||||
return deepSeekRepository.save(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新会话
|
||||
* @param upSession
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public DeepSeekSession updateSession (DeepSeekSession upSession, String userId)
|
||||
{
|
||||
String id = upSession.getId();
|
||||
String title = upSession.getTitle();
|
||||
DeepSeekSession session = RepositoryUtil.findOne(deepSeekRepository.findBySessionId(id), "");
|
||||
if (!session.getUserId().equals(userId)) {
|
||||
throw new ServiceException(ErrorCode.NO_AUTH_ERROR);
|
||||
}
|
||||
CosmosPatchOperations options = CosmosPatchOperations.create()
|
||||
.replace("/title", title);
|
||||
|
||||
deepSeekRepository.save(id, PK.of(PK.CHAT_SESSION), DeepSeekSession.class, options);
|
||||
return session;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除会话
|
||||
* @param id
|
||||
* @param userId
|
||||
*/
|
||||
@Override
|
||||
public void deleteSession (String id, String userId)
|
||||
{
|
||||
DeepSeekSession session = RepositoryUtil.findOne(deepSeekRepository.findBySessionId(id), "");
|
||||
if (!session.getUserId().equals(userId)) {
|
||||
throw new ServiceException(ErrorCode.NO_AUTH_ERROR);
|
||||
}
|
||||
deepSeekRepository.deleteById(id, PK.of(PK.CHAT_SESSION));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取用户会话列表
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<DeepSeekSession> UserSessionList(String userId) {
|
||||
List<DeepSeekSession> sessions = deepSeekRepository.findByUserId(userId);
|
||||
// 按更新时间排序
|
||||
if (ObjectUtils.isNotEmpty(sessions))
|
||||
{
|
||||
sessions = sessions.stream().sorted(Comparator.comparing(DeepSeekSession::getUpdateTime)).collect(Collectors.toList());
|
||||
}
|
||||
return sessions;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取历史消息
|
||||
* @param sessionId
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<DeepSeekMessage> listHistory(String sessionId, String userId) {
|
||||
DeepSeekSession session = deepSeekRepository.findSessionByIdAndCode(sessionId, PK.DEEPSEEK_SESSION);
|
||||
if (!userId.equals(session.getUserId())) {
|
||||
throw new ServiceException(ErrorCode.NO_AUTH_ERROR);
|
||||
}
|
||||
return session.getHistory();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
key=sk-83b5b6dd85c745cbae2572ea01c54e44
|
||||
url=https://api.deepseek.com/chat/completions
|
||||
model=deepseek-chat
|
Loading…
Reference in new issue