| 
1 | 1 | [[Advisors]]  | 
2 | 2 | 
 
  | 
3 |  | -= Spring AI Advisors API  | 
 | 3 | += Advisors API  | 
4 | 4 | 
 
  | 
5 | 5 | The Spring AI Advisors API introduces a powerful component called `AroundAdvisor`.   | 
6 | 6 | This component dynamically intercepts chat-completion requests and responses, allowing for transformation or utilization of information within these interactions.  | 
@@ -42,14 +42,14 @@ image::advisors-api-classes.jpg[Advisors API Classes, width=600, align="center"]  | 
42 | 42 | The `AdvisedRequest` represents the unsealed Prompt request, while the `AdvisedResponse` represents the Chat Completion response.  | 
43 | 43 | Both contain an `AdviserContext` object for sharing state across the advisor chain.  | 
44 | 44 | 
 
  | 
45 |  | -The `nextAroundCall()` and the `nextAroundStream()` are key advisor methods, typically performing these actions:  | 
 | 45 | +The `nextAroundCall()` and the `nextAroundStream()` are the key advisor methods, typically performing actions like:  | 
46 | 46 | 
 
  | 
47 |  | -* Examining the unsealed Prompt data  | 
48 |  | -* Customizing and augmenting the Prompt data  | 
49 |  | -* Invoking the next entity in the advisor chain  | 
50 |  | -* Optionally blocking the request  | 
51 |  | -* Examining the chat completion response  | 
52 |  | -* Throwing exceptions to indicate processing errors  | 
 | 47 | +- Examining the unsealed Prompt data  | 
 | 48 | +- Customizing and augmenting the Prompt data  | 
 | 49 | +- Invoking the next entity in the advisor chain  | 
 | 50 | +- Optionally blocking the request  | 
 | 51 | +- Examining the chat completion response  | 
 | 52 | +- Throwing exceptions to indicate processing errors  | 
53 | 53 | 
 
  | 
54 | 54 | In addition the `getOrder()` method determines the advisor's priority in the chain and the `getName()` method provides a unique identifier for the advisor.  | 
55 | 55 | 
 
  | 
@@ -143,6 +143,14 @@ public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamA  | 
143 | 143 | }  | 
144 | 144 | ----  | 
145 | 145 | 
 
  | 
 | 146 | +=== Advisor Order  | 
 | 147 | + | 
 | 148 | +The order of advisors in the chain is determined by the `getOrder()` method. Advisors with lower order values are executed first.  | 
 | 149 | +Because the advisor chain is a stack, the first advisor in the chain is the last to process the request and the first to process the response.  | 
 | 150 | +If you want to ensure that an advisor is executed last, set its order to `Integer.MAX_VALUE - 1` and vice versa.  | 
 | 151 | +If you have multiple advisors with the same order value, the order of execution is not guaranteed.  | 
 | 152 | + | 
 | 153 | +For use case that needs to be first in the chain on the input side and first on the output side, you have to use separate advisors for each side and use teh advisor context to share state between them.  | 
146 | 154 | 
 
  | 
147 | 155 | === Best Practices  | 
148 | 156 | 
 
  | 
 | 
0 commit comments