Skip to content

Commit 5b497ed

Browse files
author
Yuriy Bezsonov
committed
Update demo-flow
1 parent 47783db commit 5b497ed

36 files changed

Lines changed: 346 additions & 1649 deletions
-634 KB
Loading

samples/spring-ai-te-agent/ai-agent/src/main/java/com/example/ai/agent/service/ChatService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class ChatService {
1919
private final ChatMemoryService chatMemoryService;
2020

2121
public static final String SYSTEM_PROMPT = """
22-
You are a helpful AI Agent for travel and expenses.
22+
You are a helpful AI Agent for travel and expenses management.
2323
2424
Guidelines:
2525
1. Use markdown tables for structured data

samples/spring-ai-te-agent/ai-agent/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ spring.ai.bedrock.converse.chat.options.model=global.anthropic.claude-sonnet-4-2
2828
# spring.ai.bedrock.converse.chat.options.model=us.amazon.nova-premier-v1:0
2929

3030
# Document processing model
31-
ai.agent.document.model=global.anthropic.claude-sonnet-4-20250514-v1:0
31+
ai.agent.document.model=global.anthropic.claude-sonnet-4-5-20250929-v1:0
3232

3333
# PostgreSQL Configuration
3434
spring.datasource.url=jdbc:postgresql://localhost:5432/ai_agent_db

samples/spring-ai-te-agent/demo-scripts/01-create-app.sh

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/bin/bash
22

3+
# Clear current directory completely
4+
echo "Clearing current directory: $(pwd)"
5+
rm -rf ./*
6+
rm -rf ./.[!.]* 2>/dev/null || true
7+
38
echo "Checking if spring-boot-cli directory exists and remove it if it does..."
49
if [ -d "spring-boot-cli" ]; then
510
echo "Found existing spring-boot-cli directory, removing it..."
@@ -36,7 +41,7 @@ echo " --type=maven-project \\"
3641
echo " --artifact-id=ai.agent \\"
3742
echo " --name=ai-agent \\"
3843
echo " --group-id=com.example \\"
39-
echo -e " \033[1m--dependencies=spring-ai-bedrock-converse,web,thymeleaf,actuator,devtools,testcontainers,postgresql \033[0m\\"
44+
echo -e " \033[1m--dependencies=spring-ai-bedrock-converse,web,thymeleaf \033[0m\\"
4045
echo " --extract \\"
4146
echo " ai-agent"
4247

@@ -53,7 +58,7 @@ echo "Initializing Spring Boot project..."
5358
--artifact-id=ai.agent \
5459
--name=ai-agent \
5560
--group-id=com.example \
56-
--dependencies=spring-ai-bedrock-converse,web,thymeleaf,actuator,devtools,testcontainers,postgresql \
61+
--dependencies=spring-ai-bedrock-converse,web,thymeleaf \
5762
--extract \
5863
ai-agent
5964

@@ -89,7 +94,7 @@ spring.thymeleaf.suffix=.html
8994
# Amazon Bedrock Configuration
9095
spring.ai.bedrock.aws.region=us-east-1
9196
spring.ai.bedrock.converse.chat.options.max-tokens=10000
92-
spring.ai.bedrock.converse.chat.options.model=openai.gpt-oss-120b-1:0
97+
spring.ai.bedrock.converse.chat.options.model=global.anthropic.claude-sonnet-4-20250514-v1:0
9398
EOL
9499

95100
echo "Creating necessary directories and files..."
@@ -98,7 +103,7 @@ mkdir -p src/main/java/com/example/ai/agent/controller
98103
mkdir -p src/main/java/com/example/ai/agent/service
99104

100105
cp "$SOURCES_FOLDER/ai-agent/src/main/resources/templates/chat.html" src/main/resources/templates/
101-
cp "$SOURCES_FOLDER/ai-agent/src/main/java/com/example/ai/agent/controller/WebViewController.java" src/main/java/com/example/ai/agent/controller/
106+
cp "$SOURCES_FOLDER/demo-scripts/Steps/WebViewController.java.1" src/main/java/com/example/ai/agent/controller/WebViewController.java
102107
cp "$SOURCES_FOLDER/demo-scripts/Steps/ChatService.java.0" src/main/java/com/example/ai/agent/service/ChatService.java
103108
cp "$SOURCES_FOLDER/demo-scripts/Steps/ChatController.java.0" src/main/java/com/example/ai/agent/controller/ChatController.java
104109

@@ -117,10 +122,10 @@ echo "Committing changes to Git repository..."
117122
git add .
118123
git commit -m "Update initial files"
119124

120-
./mvnw spring-boot:test-run
125+
./mvnw spring-boot:run
121126

122127
cp "$SOURCES_FOLDER/demo-scripts/Steps/ChatService.java.1" src/main/java/com/example/ai/agent/service/ChatService.java
123128

124-
./mvnw spring-boot:test-run
129+
./mvnw spring-boot:run
125130

126131
cd ..

samples/spring-ai-te-agent/demo-scripts/02-add-memory.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ cd ai-agent
2525
echo "Copying ChatMemoryService.java..."
2626
cp "$SOURCES_FOLDER/ai-agent/src/main/java/com/example/ai/agent/service/ChatMemoryService.java" src/main/java/com/example/ai/agent/service/
2727

28+
echo "Copying ConversationSummaryService.java..."
29+
cp "$SOURCES_FOLDER/ai-agent/src/main/java/com/example/ai/agent/service/ConversationSummaryService.java" src/main/java/com/example/ai/agent/service/
30+
2831
echo "Copying ChatService.java from version 2..."
2932
cp "$SOURCES_FOLDER/demo-scripts/Steps/ChatService.java.2" src/main/java/com/example/ai/agent/service/ChatService.java
30-
# code src/main/java/com/example/ai/agent/ChatService.java
33+
34+
echo "Copying ChatController.java from version 2..."
35+
cp "$SOURCES_FOLDER/demo-scripts/Steps/ChatController.java.2" src/main/java/com/example/ai/agent/controller/ChatController.java
36+
37+
echo "Copying WebViewController.java from version 2..."
38+
cp "$SOURCES_FOLDER/demo-scripts/Steps/WebViewController.java.2" src/main/java/com/example/ai/agent/controller/WebViewController.java
3139

3240
# Add PostgreSQL and JPA configuration to application.properties
3341
echo "Updating application.properties with database configuration..."
@@ -61,6 +69,21 @@ cat > temp_dependencies.xml << 'EOL'
6169
<groupId>org.springframework.ai</groupId>
6270
<artifactId>spring-ai-starter-model-chat-memory-repository-jdbc</artifactId>
6371
</dependency>
72+
<dependency>
73+
<groupId>org.postgresql</groupId>
74+
<artifactId>postgresql</artifactId>
75+
<scope>runtime</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.springframework.boot</groupId>
79+
<artifactId>spring-boot-testcontainers</artifactId>
80+
<scope>test</scope>
81+
</dependency>
82+
<dependency>
83+
<groupId>org.testcontainers</groupId>
84+
<artifactId>postgresql</artifactId>
85+
<scope>test</scope>
86+
</dependency>
6487
EOL
6588

6689
# Create a temporary file for the modified pom.xml

samples/spring-ai-te-agent/demo-scripts/04-add-tools.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ mkdir -p src/main/java/com/example/ai/agent/tool
2727
echo "Copying DateTimeService.java..."
2828
cp "$SOURCES_FOLDER/ai-agent/src/main/java/com/example/ai/agent/tool/DateTimeService.java" src/main/java/com/example/ai/agent/tool/
2929

30+
echo "Copying WeatherService.java..."
31+
cp "$SOURCES_FOLDER/ai-agent/src/main/java/com/example/ai/agent/tool/WeatherService.java" src/main/java/com/example/ai/agent/tool/
32+
3033
echo "Copying ChatService.java from version 4..."
3134
cp "$SOURCES_FOLDER/demo-scripts/Steps/ChatService.java.4" src/main/java/com/example/ai/agent/service/ChatService.java
3235

samples/spring-ai-te-agent/demo-scripts/05-add-mcp.sh

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,6 @@ if [ ! -d "ai-agent" ]; then
1515
exit 1
1616
fi
1717

18-
# Check if weather folder exists and delete it if it does
19-
echo "Checking for weather folder..."
20-
if [ -d "weather" ]; then
21-
echo "Found existing weather folder, removing it..."
22-
rm -rf weather
23-
fi
24-
25-
# Copy weather folder from source
26-
echo "Copying weather folder from source..."
27-
if [ -d "$SOURCES_FOLDER/weather" ]; then
28-
cp -r "$SOURCES_FOLDER/weather" .
29-
echo "weather folder copied successfully."
30-
else
31-
echo "Error: weather folder not found at $SOURCES_FOLDER/weather."
32-
echo "Please ensure the folder exists at the specified location."
33-
exit 1
34-
fi
35-
3618
# Check if travel folder exists and delete it if it does
3719
echo "Checking for travel folder..."
3820
if [ -d "travel" ]; then
@@ -67,7 +49,6 @@ cat >> src/main/resources/application.properties << 'EOL'
6749
6850
# MCP Client Configuration
6951
spring.ai.mcp.client.toolcallback.enabled=true
70-
spring.ai.mcp.client.sse.connections.server1.url=http://localhost:8081
7152
spring.ai.mcp.client.sse.connections.server2.url=http://localhost:8082
7253
EOL
7354

samples/spring-ai-te-agent/demo-scripts/06-add-multi.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,21 @@ git commit -m "add MCP"
4040

4141
cd ai-agent
4242

43+
echo "Copying ChatController.java with multi-modal support..."
4344
cp "$SOURCES_FOLDER/ai-agent/src/main/java/com/example/ai/agent/controller/ChatController.java" src/main/java/com/example/ai/agent/controller/
45+
46+
echo "Copying WebViewController.java with feature flags..."
47+
cp "$SOURCES_FOLDER/ai-agent/src/main/java/com/example/ai/agent/controller/WebViewController.java" src/main/java/com/example/ai/agent/controller/
48+
49+
echo "Copying DocumentChatService.java..."
4450
cp "$SOURCES_FOLDER/ai-agent/src/main/java/com/example/ai/agent/service/DocumentChatService.java" src/main/java/com/example/ai/agent/service/
4551

46-
echo "Updating application.properties with database configuration..."
52+
echo "Updating application.properties with multi-modal configuration..."
4753
cat >> src/main/resources/application.properties << 'EOL'
4854
spring.ai.mcp.client.sse.connections.server3.url=http://localhost:8083
4955
5056
# Document processing model
51-
ai.agent.document.model=global.anthropic.claude-sonnet-4-20250514-v1:0
57+
ai.agent.document.model=global.anthropic.claude-sonnet-4-5-20250929-v1:0
5258
EOL
5359

5460
# echo "Opening application.properties in VS Code..."
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
package com.example.ai.agent.controller;
22

33
import com.example.ai.agent.service.ChatService;
4-
import org.springframework.web.bind.annotation.PostMapping;
5-
import org.springframework.web.bind.annotation.RequestBody;
6-
import org.springframework.web.bind.annotation.RequestMapping;
7-
import org.springframework.web.bind.annotation.RestController;
4+
import org.springframework.http.MediaType;
5+
import org.springframework.web.bind.annotation.*;
6+
import reactor.core.publisher.Flux;
87

98
@RestController
10-
@RequestMapping("api")
9+
@RequestMapping("api/chat")
1110
public class ChatController {
1211
private final ChatService chatService;
1312

1413
public ChatController(ChatService chatService) {
1514
this.chatService = chatService;
1615
}
1716

18-
@PostMapping("chat")
19-
public String chat(@RequestBody ChatRequest request) {
17+
@PostMapping(value = "message", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
18+
public Flux<String> chat(@RequestBody ChatRequest request) {
2019
return chatService.processChat(request.prompt());
2120
}
2221

23-
public record ChatRequest(String prompt) {}
22+
public record ChatRequest(String prompt, String userId) {}
2423
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.example.ai.agent.controller;
2+
3+
import com.example.ai.agent.service.ChatService;
4+
import com.example.ai.agent.service.ChatMemoryService;
5+
import org.springframework.http.MediaType;
6+
import org.springframework.web.bind.annotation.*;
7+
import reactor.core.publisher.Flux;
8+
import java.security.Principal;
9+
10+
@RestController
11+
@RequestMapping("api/chat")
12+
public class ChatController {
13+
private final ChatService chatService;
14+
private final ChatMemoryService chatMemoryService;
15+
16+
public ChatController(ChatMemoryService chatMemoryService, ChatService chatService) {
17+
this.chatService = chatService;
18+
this.chatMemoryService = chatMemoryService;
19+
}
20+
21+
@PostMapping(value = "message", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
22+
public Flux<String> chat(@RequestBody ChatRequest request, Principal principal) {
23+
String userId = getUserId(request.userId(), principal);
24+
chatMemoryService.setCurrentUserId(userId);
25+
return chatService.processChat(request.prompt());
26+
}
27+
28+
private String getUserId(String requestUserId, Principal principal) {
29+
if (principal != null) {
30+
return principal.getName().toLowerCase();
31+
}
32+
return requestUserId != null ? requestUserId.toLowerCase() : "user1";
33+
}
34+
35+
public record ChatRequest(String prompt, String userId) {}
36+
}

0 commit comments

Comments
 (0)