This commit is contained in:
Mohammad Zwaib
2026-07-16 00:02:45 +02:00
parent 794d87e408
commit 7cef62cc48
3 changed files with 52 additions and 7 deletions
@@ -4,11 +4,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.homme.demo.dto.AskResponse;
import com.homme.demo.dto.ChatRequest;
import com.homme.demo.service.RagService;
@@ -20,20 +22,23 @@ public class ChatController {
@Autowired
private RagService ragService;
@PostMapping("/ask")
public AskResponse ask(@RequestParam String question) {
@PostMapping("/ask")
public AskResponse ask(@RequestBody ChatRequest request) {
String question = request.getMessage();
if (question == null || question.isBlank()) {
String warn = "Bitte stellen Sie eine Frage.";
return AskResponse.builder()
.answer("Bitte stellen Sie eine Frage.")
.answer(warn).reply(warn).message(warn)
.build();
}
String answer = ragService.answerQuestion(question);
return AskResponse.builder()
.answer(ragService.answerQuestion(question))
.answer(answer).reply(answer).message(answer)
.build();
}
@@ -4,7 +4,9 @@ import lombok.Builder;
import lombok.Getter;
/**
* Response body for the chatbot endpoint: { "answer": "..." }.
* Response body for the chatbot endpoint.
* Enthaelt mehrere gaengige Feldnamen (answer/reply/message),
* damit das Widget den Text sicher findet.
*/
@Getter
@Builder
@@ -12,4 +14,8 @@ public class AskResponse {
private String answer;
private String reply;
private String message;
}
@@ -0,0 +1,34 @@
package com.homme.demo.dto;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
/**
* Request body sent by the WordPress chat widget:
* { "session_id": "...", "history": [{role, content}], "message": "..." }
*/
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class ChatRequest {
private String message;
@JsonProperty("session_id")
private String sessionId;
private List<HistoryItem> history;
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public static class HistoryItem {
private String role;
private String content;
}
}