
一、后端controller
package com.mentalhealth.ai;
import com.baidubce.appbuilder.console.appbuilderclient.AppBuilderClient;
import com.baidubce.appbuilder.model.appbuilderclient.AppBuilderClientIterator;
import com.baidubce.appbuilder.model.appbuilderclient.AppBuilderClientResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class AiController {
@ResponseBody
@RequestMapping("/aiduihua")
public String ai(@RequestParam("question") String question){
String aiAnswer = null;
try {
aiAnswer = getAiAnswer(question);
} catch (Exception e) {
e.printStackTrace();
return "ai系统报错,请稍后再试!";
}
return aiAnswer;
}
public String getAiAnswer(String question) throws Exception {
System.setProperty("APPBUILDER_TOKEN", "自己去申请一个秘钥");
String appId = "8c9c0cd0-df9d-41ce-9da7-5d80b6df140d";
AppBuilderClient builder = new AppBuilderClient(appId);
String conversationId = builder.createConversation();
AppBuilderClientIterator itor = builder.run(question, conversationId, new String[] {}, false);
StringBuilder answer = new StringBuilder();
while (itor.hasNext()) {
AppBuilderClientResult response = itor.next();
answer.append(response.getAnswer());
}
System.out.println(answer);
return answer.toString();
}
}
二、前端页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI 对话</title>
<style>
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: #f2f2f2;
}
.chat-container {
width: 800px;
height: 600px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
overflow: hidden;
display: flex;
flex-direction: column;
margin-top: 50px;
}
.chat-history {
flex: 1;
padding: 20px;
overflow-y: auto;
}
.chat-message {
display: flex;
margin-bottom: 15px;
max-width: 99%;
border-radius: 10px;
padding: 10px;
}
.chat-message.user {
justify-content: flex-end;
background-color: #dcf8c6;
align-items: flex-end;
}
.chat-message.ai {
justify-content: flex-start;
background-color: #e0e0e0;
align-items: flex-start;
}
.chat-message .text {
word-wrap: break-word;
}
.chat-input {
display: flex;
padding: 10px;
background-color: #f9f9f9;
border-top: 1px solid #ddd;
}
.chat-input input {
flex: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
outline: none;
}
.chat-input button {
margin-left: 10px;
padding: 10px 20px;
border: none;
background-color: #1aad19;
color: #fff;
border-radius: 5px;
cursor: pointer;
}
.chat-input button:hover {
background-color: #168d12;
}
</style>
</head>
<body>
<div style="position: absolute;top: 0;width: 100%;">
<div th:replace="foreground/client/header::header(心理社区,null)"></div>
</div>
<div class="chat-container">
<div class="chat-history" id="chatHistory">
</div>
<div class="chat-input">
<input type="text" id="questionInput" placeholder="输入您的问题...">
<button onclick="sendQuestion()">发送</button>
</div>
</div>
<script>
function sendQuestion() {
const question = document.getElementById('questionInput').value;
const xhr = new XMLHttpRequest();
xhr.open('GET', `/aiduihua?question=${encodeURIComponent(question)}`, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
displayAnswer(xhr.responseText);
document.getElementById('questionInput').value = '';
}
};
xhr.send();
}
function displayAnswer(answer) {
const chatHistory = document.getElementById('chatHistory');
const aiMessage = document.createElement('div');
aiMessage.className = 'chat-message ai';
aiMessage.innerHTML = `<span class="text">${answer}</span>`;
const userMessage = document.createElement('div');
userMessage.className = 'chat-message user';
const userInputText = document.getElementById('questionInput').value;
userMessage.innerHTML = `<span class="text">${userInputText}</span>`;
chatHistory.appendChild(userMessage);
chatHistory.appendChild(aiMessage);
chatHistory.scrollTop = chatHistory.scrollHeight;
}
window.onload = function () {
const simulatedQuestion = "你好,AI!";
document.getElementById('questionInput').value = simulatedQuestion;
setTimeout(() => {
const simulatedAnswer = "您好!我是AI助手,很高兴与您对话。";
displayAnswer(simulatedAnswer);
document.getElementById('questionInput').value = '';
}, 1000);
};
</script>
</body>
</html>