Skip to content

Commit

Permalink
Refactor Advisor Interfaces and add AroundAdvisor support
Browse files Browse the repository at this point in the history
 Refactoring and enhancements to the advisor functionality.

 New Advisor interfaces in the org.springframework.ai.chat.client.advisor.api package:
 - Advisor: Base interface for all advisor types.
 - RequestAdvisor: For advising on request data before execution.
 - ResponseAdvisor: For advising on response data after execution, with enhanced streaming modes.
 - CallAroundAdvisor and StreamAroundAdvisor: For around advice on synchronous and streaming requests respectively.
 - AroundAdvisorChain and DefaultAroundAdvisorChain: To manage chaining of around advisors.

 Advisor Chain and Prompt Generation:
 - Added the DefaultAroundAdvisorChain class to manage the sequence of advisors applied around chat model methods.
 - Adjusted the prompt generation (toPrompt) to integrate with the refactored AdvisedRequest object.

 Refactoring and Updates:
 - Replaced the deprecated RequestResponseAdvisor interface with RequestAdvisor and ResponseAdvisor across the spring-ai-core and test modules.
 - Updated the DefaultChatClient and related classes to use the new Advisor interface, improving modularity and consistency.
 - Refactored DefaultAdvisorSpec and DefaultChatClientRequestSpec to handle the new Advisor type, and revised advisor lists and methods accordingly.
 - Enhanced the handling of streaming responses, introducing StreamResponseMode for better control during streaming scenarios.
  • Loading branch information
tzolov authored and markpollack committed Sep 5, 2024
1 parent 53721c6 commit 28276d1
Show file tree
Hide file tree
Showing 22 changed files with 955 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.model.Media;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
Expand All @@ -45,6 +44,7 @@
import org.springframework.ai.converter.BeanOutputConverter;
import org.springframework.ai.converter.ListOutputConverter;
import org.springframework.ai.converter.MapOutputConverter;
import org.springframework.ai.model.Media;
import org.springframework.ai.model.function.FunctionCallbackWrapper;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.ai.openai.chat;

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

import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand All @@ -26,11 +28,10 @@
import org.junit.jupiter.params.provider.ValueSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;

import org.springframework.ai.chat.client.AdvisedRequest;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.RequestResponseAdvisor;
import org.springframework.ai.chat.client.advisor.api.RequestAdvisor;
import org.springframework.ai.chat.client.advisor.api.ResponseAdvisor;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.converter.BeanOutputConverter;
import org.springframework.ai.model.function.FunctionCallbackContext;
Expand All @@ -47,7 +48,7 @@
import org.springframework.context.annotation.Description;
import org.springframework.core.ParameterizedTypeReference;

import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;

/**
* @author Christian Tzolov
Expand All @@ -64,10 +65,14 @@ public class OpenAiPaymentTransactionIT {
record TransactionStatusResponse(String id, String status) {
}

private static class LoggingAdvisor implements RequestResponseAdvisor {
private static class LoggingAdvisor implements RequestAdvisor, ResponseAdvisor {

private final Logger logger = LoggerFactory.getLogger(LoggingAdvisor.class);

public String getName() {
return this.getClass().getSimpleName();
}

@Override
public AdvisedRequest adviseRequest(AdvisedRequest request, Map<String, Object> context) {
logger.info("System text: \n" + request.systemText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.AdvisedRequest;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.RequestResponseAdvisor;
import org.springframework.ai.chat.client.advisor.api.RequestAdvisor;
import org.springframework.ai.chat.client.advisor.api.ResponseAdvisor;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.model.function.FunctionCallbackContext;
import org.springframework.ai.model.function.FunctionCallbackWrapper.Builder.SchemaType;
Expand Down Expand Up @@ -64,10 +65,15 @@ public class VertexAiGeminiPaymentTransactionIT {
record TransactionStatusResponse(String id, String status) {
}

private static class LoggingAdvisor implements RequestResponseAdvisor {
private static class LoggingAdvisor implements RequestAdvisor, ResponseAdvisor {

private final Logger logger = LoggerFactory.getLogger(LoggingAdvisor.class);

@Override
public String getName() {
return this.getClass().getSimpleName();
}

@Override
public AdvisedRequest adviseRequest(AdvisedRequest request, Map<String, Object> context) {
logger.info("System text: \n" + request.systemText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Map;

import org.springframework.ai.model.Media;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
Expand All @@ -46,7 +47,7 @@
*/
public record AdvisedRequest(ChatModel chatModel, String userText, String systemText, ChatOptions chatOptions,
List<Media> media, List<String> functionNames, List<FunctionCallback> functionCallbacks, List<Message> messages,
Map<String, Object> userParams, Map<String, Object> systemParams, List<RequestResponseAdvisor> advisors,
Map<String, Object> userParams, Map<String, Object> systemParams, List<Advisor> advisors,
Map<String, Object> advisorParams) {

public static Builder from(AdvisedRequest from) {
Expand Down Expand Up @@ -92,7 +93,7 @@ public static class Builder {

private Map<String, Object> systemParams = Map.of();

private List<RequestResponseAdvisor> advisors = List.of();
private List<Advisor> advisors = List.of();

private Map<String, Object> advisorParams = Map.of();

Expand Down Expand Up @@ -146,7 +147,7 @@ public Builder withSystemParams(Map<String, Object> systemParams) {
return this;
}

public Builder withAdvisors(List<RequestResponseAdvisor> advisors) {
public Builder withAdvisors(List<Advisor> advisors) {
this.advisors = advisors;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.function.Consumer;

import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.client.observation.ChatClientObservationConvention;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.model.ChatModel;
Expand Down Expand Up @@ -122,9 +123,9 @@ interface AdvisorSpec {

AdvisorSpec params(Map<String, Object> p);

AdvisorSpec advisors(RequestResponseAdvisor... advisors);
AdvisorSpec advisors(Advisor... advisors);

AdvisorSpec advisors(List<RequestResponseAdvisor> advisors);
AdvisorSpec advisors(List<Advisor> advisors);

}

Expand Down Expand Up @@ -192,9 +193,9 @@ interface ChatClientRequestSpec {

ChatClientRequestSpec advisors(Consumer<AdvisorSpec> consumer);

ChatClientRequestSpec advisors(RequestResponseAdvisor... advisors);
ChatClientRequestSpec advisors(Advisor... advisors);

ChatClientRequestSpec advisors(List<RequestResponseAdvisor> advisors);
ChatClientRequestSpec advisors(List<Advisor> advisors);

ChatClientRequestSpec messages(Message... messages);

Expand Down Expand Up @@ -237,11 +238,11 @@ <I, O> ChatClientRequestSpec function(String name, String description, Class<I>
*/
interface Builder {

Builder defaultAdvisors(RequestResponseAdvisor... advisor);
Builder defaultAdvisors(Advisor... advisor);

Builder defaultAdvisors(Consumer<AdvisorSpec> advisorSpecConsumer);

Builder defaultAdvisors(List<RequestResponseAdvisor> advisors);
Builder defaultAdvisors(List<Advisor> advisors);

Builder defaultOptions(ChatOptions chatOptions);

Expand Down
Loading

0 comments on commit 28276d1

Please sign in to comment.