AI对话、多模态、系统提示词
This commit is contained in:
parent
c116d43ed0
commit
8f8ab67b5d
@ -0,0 +1,59 @@
|
||||
package com.huangge1199.ai.controller;
|
||||
|
||||
import com.huangge1199.ai.common.R;
|
||||
import com.huangge1199.ai.service.AiService;
|
||||
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;
|
||||
|
||||
/**
|
||||
* AI测试类
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/7/12 9:30:51
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/ai")
|
||||
public class AiController {
|
||||
|
||||
@Resource
|
||||
private AiService aiService;
|
||||
|
||||
/**
|
||||
* AI对话
|
||||
*
|
||||
* @param message 输入信息
|
||||
* @return ai返回结果
|
||||
*/
|
||||
@PostMapping("/chat")
|
||||
public R<String> chat(@RequestBody String message) {
|
||||
String result = aiService.chat(message);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 多模态-描述图片
|
||||
*
|
||||
* @param message 输入信息
|
||||
* @return ai返回结果
|
||||
*/
|
||||
@PostMapping("/chatWithPicUrl")
|
||||
public R<String> chatWithPicUrl(@RequestBody String message) {
|
||||
String result = aiService.chatWithPicUrl(message);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* AI对话-带上系统提示词
|
||||
*
|
||||
* @param message 输入信息
|
||||
* @return ai返回结果
|
||||
*/
|
||||
@PostMapping("/chatWithSysMessage")
|
||||
public R<String> chatWithSysMessage(@RequestBody String message) {
|
||||
String result = aiService.chatWithSysMessage(message);
|
||||
return R.ok(result);
|
||||
}
|
||||
}
|
59
src/main/java/com/huangge1199/ai/service/AiService.java
Normal file
59
src/main/java/com/huangge1199/ai/service/AiService.java
Normal file
@ -0,0 +1,59 @@
|
||||
package com.huangge1199.ai.service;
|
||||
|
||||
import dev.langchain4j.data.message.*;
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.chat.response.ChatResponse;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* AiCodeHelper
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/7/12 9:26:14
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class AiService {
|
||||
|
||||
@Resource
|
||||
private ChatModel qwenChatModel;
|
||||
|
||||
public String chat(String message) {
|
||||
UserMessage userMessage = UserMessage.from(message);
|
||||
ChatResponse chatResponse = qwenChatModel.chat(userMessage);
|
||||
AiMessage aiMessage = chatResponse.aiMessage();
|
||||
log.info("AI 输出:{}", aiMessage.toString());
|
||||
return aiMessage.text();
|
||||
}
|
||||
|
||||
public String chatWithPicUrl(String picUrl) {
|
||||
UserMessage userMessage = UserMessage.from(
|
||||
TextContent.from("描述图片"),
|
||||
ImageContent.from(picUrl)
|
||||
);
|
||||
ChatResponse chatResponse = qwenChatModel.chat(userMessage);
|
||||
AiMessage aiMessage = chatResponse.aiMessage();
|
||||
log.info("AI 输出:{}", aiMessage.toString());
|
||||
return aiMessage.text();
|
||||
}
|
||||
|
||||
private static final String SYSTEM_MESSAGE = """
|
||||
你是旅游方面的小助手,帮助用户解答旅游路线相关的问题,并给出建议。重点关注 4 个方向:
|
||||
1. 规划旅游路线
|
||||
2. 提供当地可游玩的景点
|
||||
3. 给出整个行程的计划
|
||||
4. 分享高质量的行程规划
|
||||
请用简洁易懂的语言回答,提高用户的兴趣减少用户的规划时间。
|
||||
""";
|
||||
|
||||
public String chatWithSysMessage(String message) {
|
||||
SystemMessage systemMessage = SystemMessage.from(SYSTEM_MESSAGE);
|
||||
UserMessage userMessage = UserMessage.from(message);
|
||||
ChatResponse chatResponse = qwenChatModel.chat(systemMessage, userMessage);
|
||||
AiMessage aiMessage = chatResponse.aiMessage();
|
||||
log.info("AI 输出:{}", aiMessage.toString());
|
||||
return aiMessage.text();
|
||||
}
|
||||
}
|
6
src/main/resources/system-prompt.txt
Normal file
6
src/main/resources/system-prompt.txt
Normal file
@ -0,0 +1,6 @@
|
||||
你是旅游方面的小助手,帮助用户解答旅游路线相关的问题,并给出建议。重点关注 4 个方向:
|
||||
1. 规划旅游路线
|
||||
2. 提供当地可游玩的景点
|
||||
3. 给出整个行程的计划
|
||||
4. 分享高质量的行程规划
|
||||
请用简洁易懂的语言回答,提高用户的兴趣减少用户的规划时间。
|
Loading…
Reference in New Issue
Block a user