|
|
|
@ -11,13 +11,14 @@ import javax.servlet.annotation.WebServlet;
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
|
|
|
|
|
public class ChatServlet extends HttpServlet {
|
|
|
|
|
private static final String API_KEY = "sk-0ad1eaf5194249439f82698893607bef"; // 请替换为您的实际 API 密钥
|
|
|
|
|
private static final String APP_ID = "1d2b00751a914efd84ea4b354fffd962"; // 请替换为您的实际应用 ID
|
|
|
|
|
private static final String API_KEY = "sk-0ad1eaf5194249439f82698893607bef"; // 请替换为您的实际 API 密钥
|
|
|
|
|
private static final String APP_ID = "1d2b00751a914efd84ea4b354fffd962"; // 请替换为您的实际应用 ID
|
|
|
|
|
private static final String API_URL = "https://dashscope.aliyuncs.com/api/v1/apps/" + APP_ID + "/completion";
|
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(ChatServlet.class.getName());
|
|
|
|
|
|
|
|
|
@ -27,12 +28,17 @@ public class ChatServlet extends HttpServlet {
|
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
|
|
|
|
|
|
String prompt = request.getParameter("prompt");
|
|
|
|
|
HttpSession session = request.getSession();
|
|
|
|
|
String sessionId = (String) session.getAttribute("chatSessionId");
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
HttpClient client = HttpClient.newHttpClient();
|
|
|
|
|
JSONObject requestBody = new JSONObject();
|
|
|
|
|
JSONObject input = new JSONObject();
|
|
|
|
|
input.put("prompt", prompt);
|
|
|
|
|
if (sessionId != null) {
|
|
|
|
|
input.put("session_id", sessionId);
|
|
|
|
|
}
|
|
|
|
|
requestBody.put("input", input);
|
|
|
|
|
requestBody.put("parameters", new JSONObject());
|
|
|
|
|
|
|
|
|
@ -44,38 +50,44 @@ public class ChatServlet extends HttpServlet {
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
HttpResponse<String> apiResponse = client.send(apiRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
/*
|
|
|
|
|
if (apiResponse.statusCode() == 200) {
|
|
|
|
|
PrintWriter out = response.getWriter();
|
|
|
|
|
out.print(apiResponse.body());
|
|
|
|
|
out.flush();
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (apiResponse.statusCode() == 200) {
|
|
|
|
|
String responseBody = apiResponse.body();
|
|
|
|
|
String formattedResponse = formatResponse(responseBody); // 格式化响应
|
|
|
|
|
JSONObject jsonResponse = new JSONObject(responseBody);
|
|
|
|
|
String newSessionId = jsonResponse.getJSONObject("output").getString("session_id");
|
|
|
|
|
session.setAttribute("chatSessionId", newSessionId);
|
|
|
|
|
|
|
|
|
|
String formattedResponse = formatResponse(responseBody);
|
|
|
|
|
PrintWriter out = response.getWriter();
|
|
|
|
|
out.print(formattedResponse);
|
|
|
|
|
out.flush();
|
|
|
|
|
} else {
|
|
|
|
|
LOGGER.log(Level.SEVERE, "API request failed with status code: " + apiResponse.statusCode());
|
|
|
|
|
LOGGER.log(Level.SEVERE, "API请求失败,状态码: " + apiResponse.statusCode());
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
response.getWriter().write("{\"error\":\"API请求失败\"}");
|
|
|
|
|
response.getWriter().write("{\"error\":\"API请求失败\"}");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOGGER.log(Level.SEVERE, "Error in ChatServlet", e);
|
|
|
|
|
LOGGER.log(Level.SEVERE, "ChatServlet出错", e);
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
response.getWriter().write("{\"error\":\"服务器错误: " + e.getMessage() + "\"}");
|
|
|
|
|
response.getWriter().write("{\"error\":\"服务器错误: " + e.getMessage() + "\"}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 格式化函数
|
|
|
|
|
|
|
|
|
|
// 格式化响应
|
|
|
|
|
private String formatResponse(String responseBody) {
|
|
|
|
|
// 假设responseBody是字符串形式的JSON,您可能需要先解析它
|
|
|
|
|
// 这里简单示例对字符串进行替换和格式化
|
|
|
|
|
return responseBody
|
|
|
|
|
.replaceAll("\\*\\*(.*?)\\*\\*", "<strong>$1</strong>") // 加粗文本
|
|
|
|
|
.replaceAll("\\n", "<br>") // 替换换行符
|
|
|
|
|
.replaceAll("(\\d+\\. )", "<li>$1") // 列表项处理
|
|
|
|
|
.replaceAll("\\s+", " "); // 去除多余空白
|
|
|
|
|
JSONObject jsonResponse = new JSONObject(responseBody);
|
|
|
|
|
String text = jsonResponse.getJSONObject("output").getString("text");
|
|
|
|
|
|
|
|
|
|
// 格式化文本
|
|
|
|
|
text = text.replaceAll("\\*\\*(.*?)\\*\\*", "<strong>$1</strong>") // 加粗文本
|
|
|
|
|
.replaceAll("\\n", "<br>") // 替换换行符
|
|
|
|
|
.replaceAll("(\\d+\\. )", "<li>$1") // 列表项目
|
|
|
|
|
.replaceAll("\\s+", " "); // 去除多余空白
|
|
|
|
|
|
|
|
|
|
JSONObject formattedResponse = new JSONObject();
|
|
|
|
|
formattedResponse.put("text", text);
|
|
|
|
|
formattedResponse.put("session_id", jsonResponse.getJSONObject("output").getString("session_id"));
|
|
|
|
|
|
|
|
|
|
return formattedResponse.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|