✨Feat: 完成 LLM 接入
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.wonderland.stardewvalley.ai;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ChatUtil {
|
||||
|
||||
private final ChatClient chatClient;
|
||||
|
||||
public ChatUtil(ChatClient.Builder chatClientBuilder) {
|
||||
this.chatClient = chatClientBuilder.build();
|
||||
}
|
||||
|
||||
public String doChat(String background, String input) {
|
||||
String content = chatClient
|
||||
.prompt()
|
||||
.system("你是一个AI助手,请根据背景和输入生成回复")
|
||||
.user("[BackGround]: " + background + "\n[Input]: " + input)
|
||||
.call()
|
||||
.content();
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.wonderland.stardewvalley.constant;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class BackGroundConstant {
|
||||
|
||||
private static final String BACKGROUND_SPRING = "春天";
|
||||
private static final String BACKGROUND_SUMMER = "夏天";
|
||||
private static final String BACKGROUND_AUTUMN = "秋天";
|
||||
private static final String BACKGROUND_WINTER = "冬天";
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.wonderland.stardewvalley.controller;
|
||||
|
||||
import com.wonderland.stardewvalley.ai.ChatUtil;
|
||||
import com.wonderland.stardewvalley.entity.AIChatRequest;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("chat")
|
||||
public class ChatController {
|
||||
|
||||
@Resource
|
||||
private ChatUtil chatUtil;
|
||||
|
||||
@PostMapping
|
||||
public String chat(@RequestBody AIChatRequest aiChatRequest) {
|
||||
return chatUtil.doChat(aiChatRequest.getBackGround(), aiChatRequest.getContent());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.wonderland.stardewvalley.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AIChatRequest {
|
||||
private String backGround;
|
||||
private String content;
|
||||
}
|
||||
Reference in New Issue
Block a user