Skip to content

Commit 21e59de

Browse files
authored
Add spring-ai recursive advisor demo using ToolCallAdvisor (#78)
Signed-off-by: Christian Tzolov <[email protected]>
1 parent 316d0d7 commit 21e59de

File tree

23 files changed

+1093
-24
lines changed

23 files changed

+1093
-24
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/mvnw text eol=lf
2+
*.cmd text eol=crlf
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
wrapperVersion=3.3.4
2+
distributionType=only-script
3+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.11/apache-maven-3.9.11-bin.zip
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Spring AI Recursive Advisors Demo
2+
3+
A Spring Boot demonstration project showcasing the new **Recursive Advisors** feature in Spring AI 1.1.0-M4+, which enables looping through advisor chains multiple times for iterative AI workflows.
4+
5+
## Overview
6+
7+
This project demonstrates the **Recursive Advisors** pattern using Spring AI's `ToolCallAdvisor` - a built-in recursive advisor that handles tool calling loops within the advisor chain. Key features shown:
8+
9+
## Prerequisites
10+
11+
- Java 17 or higher
12+
- Maven 3.6+
13+
- Anthropic API key
14+
15+
## Key Components
16+
17+
### 1. Main Application (`RecursiveAdvisorDemoApplication.java`)
18+
19+
The main class demonstrates the recursive advisor pattern:
20+
21+
```java
22+
ChatClient chatClient = chatClientBuilder
23+
.defaultTools(new MyTools())
24+
.defaultAdvisors(
25+
ToolCallAdvisor.builder().build(), // Built-in recursive advisor
26+
new MyLogAdvisor()) // Custom logging advisor
27+
.build();
28+
```
29+
30+
Key aspects:
31+
- **ToolCallAdvisor**: Built-in recursive advisor that loops until all tool calls are completed
32+
- **User-Controlled Tool Execution**: Tools execute within the advisor chain, not inside the model
33+
- **Advisor Ordering**: Multiple advisors working together in the chain
34+
35+
### 2. Custom Tools (`MyTools` class)
36+
37+
```java
38+
@Tool(description = "Get the current weather for a given location")
39+
public String weather(String location) {
40+
return "The current weather in " + location + " is sunny with a temperature of 25°C.";
41+
}
42+
```
43+
44+
Demonstrates how to create custom tools that the AI can call during conversations.
45+
46+
### 3. Custom Advisor (`MyLogAdvisor` class)
47+
48+
Implements a non-recursive advisor to demonstrate observability in advisor chain flows:
49+
50+
```java
51+
static class MyLogAdvisor implements BaseAdvisor {
52+
@Override
53+
public ChatClientRequest before(ChatClientRequest request, AdvisorChain chain) {
54+
print("REQUEST", request.prompt().getInstructions());
55+
return request;
56+
}
57+
58+
@Override
59+
public ChatClientResponse after(ChatClientResponse response, AdvisorChain chain) {
60+
print("RESPONSE", response.chatResponse().getResults());
61+
return response;
62+
}
63+
}
64+
```
65+
66+
This advisor logs each iteration as the `ToolCallAdvisor` loops through tool executions, providing visibility into the recursive process.
67+
68+
## Expected output
69+
70+
```
71+
REQUEST:[{"messageType":"USER","metadata":{"messageType":"USER"},"media":[],"text":"What is current weather in Paris?"}]
72+
73+
RESPONSE:[{"metadata":{"finishReason":"tool_use","contentFilters":[],"empty":true},"output":{"messageType":"ASSISTANT","metadata":{"messageType":"ASSISTANT"},"toolCalls":[],"media":[],"text":"I'll check the current weather in Paris for you."}},{"metadata":{"finishReason":"tool_use","contentFilters":[],"empty":true},"output":{"messageType":"ASSISTANT","metadata":{"messageType":"ASSISTANT"},"toolCalls":[{"id":"toolu_01HNde1Z7mwtwXh4qiK3tavZ","type":"function","name":"weather","arguments":"{\"location\":\"Paris\"}"}],"media":[],"text":""}}]
74+
75+
REQUEST:[{"messageType":"USER","metadata":{"messageType":"USER"},"media":[],"text":"What is current weather in Paris?"},{"messageType":"ASSISTANT","metadata":{"messageType":"ASSISTANT"},"toolCalls":[{"id":"toolu_01HNde1Z7mwtwXh4qiK3tavZ","type":"function","name":"weather","arguments":"{\"location\":\"Paris\"}"}],"media":[],"text":""},{"messageType":"TOOL","metadata":{"messageType":"TOOL"},"responses":[{"id":"toolu_01HNde1Z7mwtwXh4qiK3tavZ","name":"weather","responseData":"\"The current weather in Paris is sunny with a temperature of 25°C.\""}],"text":""}]
76+
77+
RESPONSE:[{"metadata":{"finishReason":"end_turn","contentFilters":[],"empty":true},"output":{"messageType":"ASSISTANT","metadata":{"messageType":"ASSISTANT"},"toolCalls":[],"media":[],"text":"The current weather in Paris is sunny with a temperature of 25°C."}}]
78+
79+
The current weather in Paris is sunny with a temperature of 25°C.
80+
```

0 commit comments

Comments
 (0)