feat(core): 集成Spring AI与Apple登录并新增聊天接口
This commit is contained in:
@@ -5,11 +5,20 @@ import com.yolo.keyborad.common.ResultUtils;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
|
||||
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
|
||||
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
|
||||
import org.springframework.ai.chat.model.ChatModel;
|
||||
import org.springframework.ai.openai.OpenAiChatOptions;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
@@ -22,9 +31,40 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@Tag(name = "测试控制器", description = "测试控制器")
|
||||
public class DemoController {
|
||||
|
||||
private final ChatClient openAiChatClient;
|
||||
|
||||
private final ChatModel chatModel;
|
||||
|
||||
public DemoController(@Qualifier("openAiChatModel") ChatModel chatModel) {
|
||||
|
||||
this.chatModel = chatModel;
|
||||
|
||||
// 构造时,可以设置 ChatClient 的参数
|
||||
// {@link org.springframework.ai.chat.client.ChatClient};
|
||||
this.openAiChatClient = ChatClient.builder(chatModel)
|
||||
.defaultOptions(OpenAiChatOptions.builder()
|
||||
.build())
|
||||
// 实现 Chat Memory 的 Advisor
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@GetMapping("/test")
|
||||
@Operation(summary = "测试接口", description = "测试接口")
|
||||
public BaseResponse<String> testDemo(){
|
||||
return ResultUtils.success("hello world");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@GetMapping("/talk")
|
||||
@Operation(summary = "测试聊天接口", description = "测试接口")
|
||||
public Flux<String> testTalk(String userInput){
|
||||
return openAiChatClient
|
||||
.prompt("You're a 25-year-old guy—witty and laid-back, always replying in English.\n" +
|
||||
"Read the user's last message, then write three short and funny replies that sound like something a guy would say. Go easy on the emojis.\n" +
|
||||
"Keep each under 20 words. Use '/t' to separate them—no numbers or extra labels.").user("you are so cute!")
|
||||
.stream().content();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.yolo.keyborad.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.yolo.keyborad.service.impl.IAppleService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 用户前端控制器
|
||||
*
|
||||
* @author Salmon
|
||||
* @since 2024-07-20
|
||||
*/
|
||||
@RestController
|
||||
@Slf4j
|
||||
@RequestMapping("/user")
|
||||
@Tag(name = "用户")
|
||||
@AllArgsConstructor
|
||||
public class UserController {
|
||||
|
||||
@Resource
|
||||
private IAppleService appleService;
|
||||
|
||||
/**
|
||||
* 苹果登录
|
||||
*
|
||||
* @param code JWT
|
||||
*/
|
||||
@PostMapping("/appleLogin")
|
||||
public String appleLogin(String code) throws Exception {
|
||||
String subjectId = appleService.login(code);
|
||||
return JSON.toJSONString(subjectId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user