Skip to content

Commit

Permalink
Refactored chat
Browse files Browse the repository at this point in the history
  • Loading branch information
Slawek84PL committed Nov 28, 2024
1 parent 9efc854 commit 3aea426
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.context.annotation.Configuration;
import pl.ateam.disasteralerts.ai.chat.dto.ConversationDTO;
import pl.ateam.disasteralerts.disasteralert.DisasterType;
import pl.ateam.disasteralerts.disasteralert.dto.DisasterAddDTO;

import java.util.ArrayList;
Expand Down Expand Up @@ -59,7 +60,7 @@ private Message getSystemMessage() {
Jesteś wirtualnym asystentem do przyjmowania zgłoszeń o awariach i zagrożeniach naturalnych od użytkowników. Twoim zadaniem jest przeprowadzenie użytkownika przez proces zgłoszenia, zadawanie precyzyjnych pytań w celu uzyskania wszystkich kluczowych informacji o zgłoszeniu, a następnie wygenerowanie odpowiedzi w formacie JSON na podstawie uzyskanych danych.
Podczas rozmowy:
- Zidentyfikuj typ zgłoszenia: Na początku rozmowy ustal, czy użytkownik zgłasza awarię (np. wodociągową, elektryczną, drogową) czy zagrożenie naturalne (np. powódź, pożar, osunięcie ziemi).
- Zidentyfikuj typ zgłoszenia: Na początku rozmowy ustal, czy użytkownik zgłasza zagrożenie naturalne na podstawie {disasterType}.
- Pytania o szczegóły: Zadawaj pytania, aby uzyskać szczegółowe lub ogólne informacje o:
- Skali problemu
- Widocznych skutkach
Expand All @@ -78,8 +79,13 @@ private Message getSystemMessage() {

return new SystemPromptTemplate(stringPrompt)
.createMessage(Map.of(
"disasterType", DisasterType.values(),
"conversation", ConversationDTO.class,
"messages", messages
));
}

public void cleanMessage() {
messages.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ private void addToMessages(String answerFromUser, ConversationDTO answer) {
openAiConfig.addMessage(new AssistantMessage(answer.toString()));
}
}

public void cleanMessage() {
openAiConfig.cleanMessage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand All @@ -10,6 +11,7 @@
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import pl.ateam.disasteralerts.ai.chat.OpenAiService;
import pl.ateam.disasteralerts.ai.chat.dto.ConversationDTO;
Expand Down Expand Up @@ -40,17 +42,19 @@ public class DisasterViewController {

@GetMapping("add")
public String showAddDisasterForm(Model model, @AuthenticationPrincipal AppUser appUser) {
baseModel(model, appUser);

if (!model.containsAttribute("conversation")) {
openAiService.cleanMessage();
ConversationDTO conversation = openAiService.getResponse("Jestem " + appUser.getUsername() + " i chcę zgłosić zagrożenie.");
model.addAttribute("conversation", conversation);
}

baseModel(model, appUser);
if (!model.containsAttribute("disasterAddDTO")) {
model.addAttribute("disasterTypSelected", null);
model.addAttribute("disasterAddDTO", new DisasterAddDTO(null, null, null, null));
}

model.addAttribute("selectedLocation", appUser.getUserDTO().location());
model.addAttribute("googleApiKey", googleApiKey);
model.addAttribute("disasterTypes", DisasterType.values());
Expand Down
25 changes: 7 additions & 18 deletions src/main/resources/templates/addDisaster.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,39 @@ <h1 class="display-5 fw-bold text-body-emphasis">Dodaj <span class="text-primary
<div>
<div class="container col-xl-10 col-xxl-10 px-4 ">
<div class="row g-lg-5 py-5">
<!-- Sekcja Czatowa -->
<div class="col-lg-7 text-center text-lg-start d-flex flex-column h-100">
<div th:if="${disasterTypSelected == null}" class="col-md-10 mx-auto col-lg-8">
<div class="chat-container d-flex flex-column h-100">
<div class="textarea-container bg-body-tertiary p-3 rounded mt-3">
<form th:action="@{/disasters/chat}" method="post">
<!-- Renderowanie pytania i opcji wyboru jako radio buttony -->
<div th:if="${conversation != null}">
<label class="form-label fw-bold" th:text="${conversation.question}"></label>
<div class="form-check" th:each="answer, iter : ${conversation.answers}">
<input class="form-check-input" type="radio" name="selectedAnswer"
th:value="${answer}" th:id="${'answer-' + iter.index}" required>
<label class="form-check-label" th:for="${'answer-' + iter.index}" th:text="${answer}"></label>
</div>

<!-- Pole do wpisania własnej odpowiedzi -->
<div class="form-check mt-2">
<input class="form-check-input" type="radio" name="selectedAnswer" value="custom" id="answer-custom">
<input type="text" class="form-control mt-2" name="selectedAnswer" placeholder="Wpisz własną odpowiedź">
</div>

<button type="submit" class="btn btn-success mt-3">Wyślij</button>
<button type="submit" class="w-100 btn btn-lg btn-primary">Wyślij</button>
<div th:replace="~{fragments :: toast}"></div>
</div>
</form>
</div>
</div>
</div>

<div class="col-md-10 mx-auto col-lg-8">
<div th:if="${disasterTypSelected != null}" class="col-md-10 mx-auto col-lg-8">
<form th:action="@{/disasters/add}" method="post" th:object="${disasterAddDTO}" class="p-4 p-md-5 rounded-3 bg-body-tertiary">

<div class="form-floating mb-3">
<select th:value="${disasterTypSelected}" name="type" class="form-select" id="type" required>
<option th:if="${disasterTypSelected == null}" value="" disabled selected>Wybierz typ zdarzenia</option>
<option th:each="type : ${disasterType}"
th:value="${type}"
th:selected="${type == disasterTypSelected}"
th:text="${type}"></option>
</select>
<label for="type">Typ Zdarzenia</label>
<input th:value="${disasterTypSelected}" name="type" type="hidden" class="form-control" id="tye" required>
</div>

<div class="form-floating mb-3">
<input th:value="*{description()}" name="description" type="text" class="form-control" id="description" placeholder="Opis" required>
<label for="description">Opis</label>
<input th:value="*{description()}" name="description" type="hidden" class="form-control" id="description" required>
</div>
<div th:replace="~{fragments :: citiesList}"></div>

Expand All @@ -66,7 +56,6 @@ <h1 class="display-5 fw-bold text-body-emphasis">Dodaj <span class="text-primary
</div>

<button class="w-100 btn btn-lg btn-primary" type="submit">Dodaj Zdarzenie</button>
<div th:replace="~{fragments :: toast}"></div>
</form>
</div>
</div>
Expand Down

0 comments on commit 3aea426

Please sign in to comment.