Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.stereotype.Service;

@Service
Expand Down Expand Up @@ -33,8 +35,14 @@ public class ChatService {

public ChatService(DateTimeService dateTimeService,
ChatClient.Builder chatClientBuilder) {

var chatMemory = MessageWindowChatMemory.builder()
.maxMessages(20)
.build();

this.chatClient = chatClientBuilder
.defaultSystem(SYSTEM_PROMPT)
.defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())
.defaultTools(dateTimeService)
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.ai.agent.service;


import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
public class ChatClientMemoryTest {

@Autowired
private ChatService chatService;

@Test
public void shouldMemorizeMemoryContext() {
// Test if the model knows basic facts
String memoryAdvice = "My name is Andrei";
chatService.processChat(memoryAdvice);

String question = "What is my name?";
String response = chatService.processChat(question);
System.out.println("=== DIRECT MODEL TEST ===");
System.out.println("Question: " + question);
System.out.println("Response: " + response);

assertThat(response).isNotNull().isNotEmpty();
assertThat(response.toLowerCase()).contains("andrei");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ class SecurityConfiguration {
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests(auth -> auth
.anyRequest().authenticated()
)
.oauth2Login(Customizer.withDefaults())
.authorizeHttpRequests(auth -> auth.anyRequest().permitAll())
.oauth2Client(Customizer.withDefaults())
.csrf(CsrfConfigurer::disable)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.ai.agent.controller;

import com.example.ai.agent.service.ChatService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -14,40 +15,10 @@ public ChatController(ChatService chatService) {
}

@PostMapping("chat")
public String chat(@RequestBody ChatRequest request) {
public String chat(@RequestBody ChatRequest request, HttpServletRequest httpRequest) {
var headers = httpRequest.getHeaderNames();
return chatService.processChat(request.prompt());
}

@GetMapping("gui")
public String chat() {
String prompt = "Please give me all available hotels in Paris, Checkin 10.10.2025, checkout 15.10.2025";
String chatResponse = chatService.processChat(prompt);

String currentHotel = """
<h2>Available hotels %s</h2>
<p>%s</p>
<form action="" method="GET">
<button type="submit">Clear</button>
</form>
""".formatted(prompt, chatResponse);

return """
<h1>Demo controller</h1>
%s

<hr>

<h2>Ask for the weather</h2>
<p>In which city would you like to see the weather?</p>
<form action="" method="GET">
<input type="text" name="query" value="" placeholder="Paris" />
<button type="submit">Ask the LLM</button>
</form>

<hr>
""".formatted(currentHotel);
}


public record ChatRequest(String prompt) {}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.example.ai.agent.controller;

import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class WebViewController {
@GetMapping("/")
public String index() {
public String index(@RegisteredOAuth2AuthorizedClient("authserver") OAuth2AuthorizedClient authorizedClient) {
authorizedClient.getAccessToken();
return "chat";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ <h1 class="text-3xl font-bold text-dev-accent">🤖 AI Agent</h1>
} catch (error) {
console.error('Error:', error);
addMessage('Sorry, I encountered an error. Please try again.', 'ai');
throw error
}
});

Expand Down