|
| 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