结组作业4
今天继续写后端的代码,包括controller和mapper和service的代码
首先是和团队成员一起写的调用chat接口的controller的代码
1 package org.test.tongyuzhe.Controller; 2 3 4 import com.tencentcloudapi.common.Credential; 5 import com.tencentcloudapi.common.exception.TencentCloudSDKException; 6 import com.tencentcloudapi.common.profile.ClientProfile; 7 import com.tencentcloudapi.common.profile.HttpProfile; 8 import com.tencentcloudapi.hunyuan.v20230901.HunyuanClient; 9 import com.tencentcloudapi.hunyuan.v20230901.models.*; 10 import org.springframework.web.bind.annotation.PostMapping; 11 import org.springframework.web.bind.annotation.RequestBody; 12 import org.springframework.web.bind.annotation.RequestMapping; 13 import org.springframework.web.bind.annotation.RestController; 14 import org.test.tongyuzhe.Pojo.ChatRequest; 15 import org.test.tongyuzhe.Pojo.Result; 16 import org.test.tongyuzhe.utils.JwtUtil; 17 18 import java.util.ArrayList; 19 import java.util.Map; 20 21 @RestController 22 @RequestMapping("/api") 23 public class ChatController { 24 25 26 @PostMapping("/chat") 27 public Result<String> processChatRequest(@RequestBody ChatRequest chatRequest) { 28 System.out.println(chatRequest.toString()); 29 //验证token 30 try { 31 Map<String, Object> claim = JwtUtil.parseToken(chatRequest.getToken()); 32 33 34 String userId = claim.get("id").toString(); 35 36 ChatProRequest req = new ChatProRequest(); 37 ArrayList<Message> messages1 = new ArrayList<Message>(); 38 Message message1 = new Message(); 39 40 message1.setRole("system"); 41 message1.setContent("你是一个用于辅导儿童心理的对话专家,你的名字叫小e" + 42 "我要求你在与儿童对话的过程中," + 43 "辅导儿童心理,同时引导他们树立正确的价值观;在与家长对话的时候," + 44 "能够根据与儿童的对话内容分析儿童的心理健康状况,并给出家长建议"); 45 46 messages1.add(message1); 47 messages1.addAll(chatRequest.getMessages()); 48 Message[] messages = messages1.toArray(new Message[0]); 49 50 req.setMessages(messages); 51 ChatProResponse response = sendPostRequest(req); 52 53 System.out.println(response); 54 55 Choice[] choices = response.getChoices(); 56 Message message = choices[0].getMessage(); 57 58 String choice = message.getContent(); 59 60 for (int i = 0; i < chatRequest.getMessages().size(); i++) { 61 choice+=chatRequest.getMessages(); 62 } 63 64 // System.out.println(choice); 65 return Result.success(choice); 66 } catch (Exception e) { 67 //未登录 68 Result.error("未登录"); 69 throw new RuntimeException(e); 70 } 71 72 73 74 } 75 public static ChatProResponse sendPostRequest(ChatProRequest req) { 76 try{ 77 // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密 78 // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305 79 // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取 80 Credential cred = new Credential("AKIDMF9rRpGgzAYpdP2VnwxmNksmUyGKBop4", "MX6Ws6EFA0idP7LguBblmaDdrulQtGUd"); 81 // 实例化一个http选项,可选的,没有特殊需求可以跳过 82 HttpProfile httpProfile = new HttpProfile(); 83 httpProfile.setEndpoint("hunyuan.tencentcloudapi.com"); 84 // 实例化一个client选项,可选的,没有特殊需求可以跳过 85 ClientProfile clientProfile = new ClientProfile(); 86 clientProfile.setHttpProfile(httpProfile); 87 // 实例化要请求产品的client对象,clientProfile是可选的 88 HunyuanClient client = new HunyuanClient(cred, "", clientProfile); 89 // 实例化一个请求对象,每个接口都会对应一个request对象 90 91 req.setTopP(0F); 92 req.setTemperature(0F); 93 req.setStream(false); 94 // 返回的resp是一个ChatProResponse的实例,与请求对象对应 95 ChatProResponse resp = client.ChatPro(req); 96 return resp; 97 98 } catch (TencentCloudSDKException e) { 99 System.out.println(e.toString()); 100 } 101 return null; 102 } 103 104 105 }
然后mapper层主要是sql语句的书写,比较简单
1 package org.test.tongyuzhe.Mapper; 2 3 import org.apache.ibatis.annotations.Insert; 4 import org.apache.ibatis.annotations.Mapper; 5 import org.apache.ibatis.annotations.Select; 6 import org.test.tongyuzhe.Pojo.Chat; 7 8 import java.util.List; 9 10 @Mapper 11 public interface ChatMapper { 12 @Select("select * from chat where userId=#{userId}") 13 List<Chat> getMessage(Integer userId); 14 @Insert("insert into chat(userId, request, response, time) VALUES (#{userId},#{request},#{response},#{time})") 15 void insertMessage(Chat chat); 16 }