Refactor Advisor builder methods
- Deprecate the builder methods with the prefix `with` - Update references and docs
This commit is contained in:
committed by
Mark Pollack
parent
da45244d99
commit
429bd2af35
@@ -67,8 +67,8 @@ public class ReReadingAdvisor implements CallAroundAdvisor, StreamAroundAdvisor
|
||||
advisedUserParams.put("re2_input_query", advisedRequest.userText());
|
||||
|
||||
return AdvisedRequest.from(advisedRequest)
|
||||
.withUserText(this.re2AdviseTemplate)
|
||||
.withUserParams(advisedUserParams)
|
||||
.userText(this.re2AdviseTemplate)
|
||||
.userParams(advisedUserParams)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -67,9 +67,7 @@ public interface RequestResponseAdvisor extends CallAroundAdvisor, StreamAroundA
|
||||
default AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
|
||||
var context = new HashMap<>(advisedRequest.adviseContext());
|
||||
var requestPrim = adviseRequest(advisedRequest, context);
|
||||
advisedRequest = AdvisedRequest.from(requestPrim)
|
||||
.withAdviseContext(Collections.unmodifiableMap(context))
|
||||
.build();
|
||||
advisedRequest = AdvisedRequest.from(requestPrim).adviseContext(Collections.unmodifiableMap(context)).build();
|
||||
|
||||
var advisedResponse = chain.nextAroundCall(advisedRequest);
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @param <T> the type of the chat memory.
|
||||
* @author Christian Tzolov
|
||||
* @author Ilayaperumal Gopinathan
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class AbstractChatMemoryAdvisor<T> implements CallAroundAdvisor, StreamAroundAdvisor {
|
||||
@@ -238,7 +239,7 @@ public abstract class AbstractChatMemoryAdvisor<T> implements CallAroundAdvisor,
|
||||
* @param conversationId the conversation id
|
||||
* @return the builder
|
||||
*/
|
||||
public AbstractBuilder withConversationId(String conversationId) {
|
||||
public AbstractBuilder conversationId(String conversationId) {
|
||||
this.conversationId = conversationId;
|
||||
return this;
|
||||
}
|
||||
@@ -248,7 +249,7 @@ public abstract class AbstractChatMemoryAdvisor<T> implements CallAroundAdvisor,
|
||||
* @param chatMemoryRetrieveSize the chat memory retrieve size
|
||||
* @return the builder
|
||||
*/
|
||||
public AbstractBuilder withChatMemoryRetrieveSize(int chatMemoryRetrieveSize) {
|
||||
public AbstractBuilder chatMemoryRetrieveSize(int chatMemoryRetrieveSize) {
|
||||
this.chatMemoryRetrieveSize = chatMemoryRetrieveSize;
|
||||
return this;
|
||||
}
|
||||
@@ -258,7 +259,7 @@ public abstract class AbstractChatMemoryAdvisor<T> implements CallAroundAdvisor,
|
||||
* @param protectFromBlocking whether to protect from blocking
|
||||
* @return the builder
|
||||
*/
|
||||
public AbstractBuilder withProtectFromBlocking(boolean protectFromBlocking) {
|
||||
public AbstractBuilder protectFromBlocking(boolean protectFromBlocking) {
|
||||
this.protectFromBlocking = protectFromBlocking;
|
||||
return this;
|
||||
}
|
||||
@@ -268,6 +269,42 @@ public abstract class AbstractChatMemoryAdvisor<T> implements CallAroundAdvisor,
|
||||
* @param order the order
|
||||
* @return the builder
|
||||
*/
|
||||
public AbstractBuilder order(int order) {
|
||||
this.order = order;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #conversationId( String)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public AbstractBuilder withConversationId(String conversationId) {
|
||||
this.conversationId = conversationId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #chatMemoryRetrieveSize(int)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public AbstractBuilder withChatMemoryRetrieveSize(int chatMemoryRetrieveSize) {
|
||||
this.chatMemoryRetrieveSize = chatMemoryRetrieveSize;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #protectFromBlocking(boolean)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public AbstractBuilder withProtectFromBlocking(boolean protectFromBlocking) {
|
||||
this.protectFromBlocking = protectFromBlocking;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #order(int)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public AbstractBuilder withOrder(int order) {
|
||||
this.order = order;
|
||||
return this;
|
||||
|
||||
@@ -86,11 +86,11 @@ public class DefaultAroundAdvisorChain implements CallAroundAdvisorChain, Stream
|
||||
var advisor = this.callAroundAdvisors.pop();
|
||||
|
||||
var observationContext = AdvisorObservationContext.builder()
|
||||
.withAdvisorName(advisor.getName())
|
||||
.withAdvisorType(AdvisorObservationContext.Type.AROUND)
|
||||
.withAdvisedRequest(advisedRequest)
|
||||
.withAdvisorRequestContext(advisedRequest.adviseContext())
|
||||
.withOrder(advisor.getOrder())
|
||||
.advisorName(advisor.getName())
|
||||
.advisorType(AdvisorObservationContext.Type.AROUND)
|
||||
.advisedRequest(advisedRequest)
|
||||
.advisorRequestContext(advisedRequest.adviseContext())
|
||||
.order(advisor.getOrder())
|
||||
.build();
|
||||
|
||||
return AdvisorObservationDocumentation.AI_ADVISOR
|
||||
@@ -108,11 +108,11 @@ public class DefaultAroundAdvisorChain implements CallAroundAdvisorChain, Stream
|
||||
var advisor = this.streamAroundAdvisors.pop();
|
||||
|
||||
AdvisorObservationContext observationContext = AdvisorObservationContext.builder()
|
||||
.withAdvisorName(advisor.getName())
|
||||
.withAdvisorType(AdvisorObservationContext.Type.AROUND)
|
||||
.withAdvisedRequest(advisedRequest)
|
||||
.withAdvisorRequestContext(advisedRequest.adviseContext())
|
||||
.withOrder(advisor.getOrder())
|
||||
.advisorName(advisor.getName())
|
||||
.advisorType(AdvisorObservationContext.Type.AROUND)
|
||||
.advisedRequest(advisedRequest)
|
||||
.advisorRequestContext(advisedRequest.adviseContext())
|
||||
.order(advisor.getOrder())
|
||||
.build();
|
||||
|
||||
var observation = AdvisorObservationDocumentation.AI_ADVISOR.observation(null,
|
||||
|
||||
@@ -91,7 +91,7 @@ public class MessageChatMemoryAdvisor extends AbstractChatMemoryAdvisor<ChatMemo
|
||||
advisedMessages.addAll(memoryMessages);
|
||||
|
||||
// 3. Create a new request with the advised messages.
|
||||
AdvisedRequest advisedRequest = AdvisedRequest.from(request).withMessages(advisedMessages).build();
|
||||
AdvisedRequest advisedRequest = AdvisedRequest.from(request).messages(advisedMessages).build();
|
||||
|
||||
// 4. Add the new user input to the conversation memory.
|
||||
UserMessage userMessage = new UserMessage(request.userText(), request.media());
|
||||
|
||||
@@ -122,8 +122,8 @@ public class PromptChatMemoryAdvisor extends AbstractChatMemoryAdvisor<ChatMemor
|
||||
|
||||
// 3. Create a new request with the advised system text and parameters.
|
||||
AdvisedRequest advisedRequest = AdvisedRequest.from(request)
|
||||
.withSystemText(advisedSystemText)
|
||||
.withSystemParams(advisedSystemParams)
|
||||
.systemText(advisedSystemText)
|
||||
.systemParams(advisedSystemParams)
|
||||
.build();
|
||||
|
||||
// 4. Add the new user input to the conversation memory.
|
||||
@@ -152,6 +152,15 @@ public class PromptChatMemoryAdvisor extends AbstractChatMemoryAdvisor<ChatMemor
|
||||
super(chatMemory);
|
||||
}
|
||||
|
||||
public Builder systemTextAdvise(String systemTextAdvise) {
|
||||
this.systemTextAdvise = systemTextAdvise;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #systemTextAdvise(String)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withSystemTextAdvise(String systemTextAdvise) {
|
||||
this.systemTextAdvise = systemTextAdvise;
|
||||
return this;
|
||||
|
||||
@@ -48,6 +48,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Christian Tzolov
|
||||
* @author Timo Salm
|
||||
* @author Ilayaperumal Gopinathan
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class QuestionAnswerAdvisor implements CallAroundAdvisor, StreamAroundAdvisor {
|
||||
@@ -234,9 +235,9 @@ public class QuestionAnswerAdvisor implements CallAroundAdvisor, StreamAroundAdv
|
||||
advisedUserParams.put("question_answer_context", documentContext);
|
||||
|
||||
AdvisedRequest advisedRequest = AdvisedRequest.from(request)
|
||||
.withUserText(advisedUserText)
|
||||
.withUserParams(advisedUserParams)
|
||||
.withAdviseContext(context)
|
||||
.userText(advisedUserText)
|
||||
.userParams(advisedUserParams)
|
||||
.adviseContext(context)
|
||||
.build();
|
||||
|
||||
return advisedRequest;
|
||||
@@ -285,23 +286,61 @@ public class QuestionAnswerAdvisor implements CallAroundAdvisor, StreamAroundAdv
|
||||
this.vectorStore = vectorStore;
|
||||
}
|
||||
|
||||
public Builder searchRequest(SearchRequest searchRequest) {
|
||||
Assert.notNull(searchRequest, "The searchRequest must not be null!");
|
||||
this.searchRequest = searchRequest;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder userTextAdvise(String userTextAdvise) {
|
||||
Assert.hasText(userTextAdvise, "The userTextAdvise must not be empty!");
|
||||
this.userTextAdvise = userTextAdvise;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder protectFromBlocking(boolean protectFromBlocking) {
|
||||
this.protectFromBlocking = protectFromBlocking;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder order(int order) {
|
||||
this.order = order;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #searchRequest(SearchRequest)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withSearchRequest(SearchRequest searchRequest) {
|
||||
Assert.notNull(searchRequest, "The searchRequest must not be null!");
|
||||
this.searchRequest = searchRequest;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #userTextAdvise(String)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withUserTextAdvise(String userTextAdvise) {
|
||||
Assert.hasText(userTextAdvise, "The userTextAdvise must not be empty!");
|
||||
this.userTextAdvise = userTextAdvise;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #protectFromBlocking(boolean)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withProtectFromBlocking(boolean protectFromBlocking) {
|
||||
this.protectFromBlocking = protectFromBlocking;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #order(int)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withOrder(int order) {
|
||||
this.order = order;
|
||||
return this;
|
||||
|
||||
@@ -133,7 +133,7 @@ public final class RetrievalAugmentationAdvisor implements BaseAdvisor {
|
||||
Query augmentedQuery = this.queryAugmenter.augment(originalQuery, documents);
|
||||
|
||||
// 6. Update advised request with augmented prompt.
|
||||
return AdvisedRequest.from(request).withUserText(augmentedQuery.text()).withAdviseContext(context).build();
|
||||
return AdvisedRequest.from(request).userText(augmentedQuery.text()).adviseContext(context).build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.springframework.util.CollectionUtils;
|
||||
* response if the user input contains any of the sensitive words.
|
||||
*
|
||||
* @author Christian Tzolov
|
||||
* @author Ilayaperumal Gopinathan
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class SafeGuardAdvisor implements CallAroundAdvisor, StreamAroundAdvisor {
|
||||
@@ -116,16 +117,43 @@ public class SafeGuardAdvisor implements CallAroundAdvisor, StreamAroundAdvisor
|
||||
private Builder() {
|
||||
}
|
||||
|
||||
public Builder sensitiveWords(List<String> sensitiveWords) {
|
||||
this.sensitiveWords = sensitiveWords;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder failureResponse(String failureResponse) {
|
||||
this.failureResponse = failureResponse;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder order(int order) {
|
||||
this.order = order;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #sensitiveWords(List)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withSensitiveWords(List<String> sensitiveWords) {
|
||||
this.sensitiveWords = sensitiveWords;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #failureResponse(String)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withFailureResponse(String failureResponse) {
|
||||
this.failureResponse = failureResponse;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #order(int)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withOrder(int order) {
|
||||
this.order = order;
|
||||
return this;
|
||||
|
||||
@@ -153,8 +153,8 @@ public class VectorStoreChatMemoryAdvisor extends AbstractChatMemoryAdvisor<Vect
|
||||
advisedSystemParams.put("long_term_memory", longTermMemory);
|
||||
|
||||
AdvisedRequest advisedRequest = AdvisedRequest.from(request)
|
||||
.withSystemText(advisedSystemText)
|
||||
.withSystemParams(advisedSystemParams)
|
||||
.systemText(advisedSystemText)
|
||||
.systemParams(advisedSystemParams)
|
||||
.build();
|
||||
|
||||
UserMessage userMessage = new UserMessage(request.userText(), request.media());
|
||||
@@ -212,6 +212,15 @@ public class VectorStoreChatMemoryAdvisor extends AbstractChatMemoryAdvisor<Vect
|
||||
super(chatMemory);
|
||||
}
|
||||
|
||||
public Builder systemTextAdvise(String systemTextAdvise) {
|
||||
this.systemTextAdvise = systemTextAdvise;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #systemTextAdvise(String)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withSystemTextAdvise(String systemTextAdvise) {
|
||||
this.systemTextAdvise = systemTextAdvise;
|
||||
return this;
|
||||
|
||||
@@ -59,6 +59,7 @@ import org.springframework.util.StringUtils;
|
||||
* @param toolContext the tool context
|
||||
* @author Christian Tzolov
|
||||
* @author Thomas Vitale
|
||||
* @author Ilayaperumal Gopinathan
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public record AdvisedRequest(
|
||||
@@ -141,7 +142,7 @@ public record AdvisedRequest(
|
||||
public AdvisedRequest updateContext(Function<Map<String, Object>, Map<String, Object>> contextTransform) {
|
||||
Assert.notNull(contextTransform, "contextTransform cannot be null");
|
||||
return from(this)
|
||||
.withAdviseContext(Collections.unmodifiableMap(contextTransform.apply(new HashMap<>(this.adviseContext))))
|
||||
.adviseContext(Collections.unmodifiableMap(contextTransform.apply(new HashMap<>(this.adviseContext))))
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -228,7 +229,7 @@ public record AdvisedRequest(
|
||||
* @param chatModel the chat model
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder withChatModel(ChatModel chatModel) {
|
||||
public Builder chatModel(ChatModel chatModel) {
|
||||
this.chatModel = chatModel;
|
||||
return this;
|
||||
}
|
||||
@@ -238,7 +239,7 @@ public record AdvisedRequest(
|
||||
* @param userText the user text
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder withUserText(String userText) {
|
||||
public Builder userText(String userText) {
|
||||
this.userText = userText;
|
||||
return this;
|
||||
}
|
||||
@@ -248,7 +249,7 @@ public record AdvisedRequest(
|
||||
* @param systemText the system text
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder withSystemText(String systemText) {
|
||||
public Builder systemText(String systemText) {
|
||||
this.systemText = systemText;
|
||||
return this;
|
||||
}
|
||||
@@ -258,7 +259,7 @@ public record AdvisedRequest(
|
||||
* @param chatOptions the chat options
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder withChatOptions(ChatOptions chatOptions) {
|
||||
public Builder chatOptions(ChatOptions chatOptions) {
|
||||
this.chatOptions = chatOptions;
|
||||
return this;
|
||||
}
|
||||
@@ -268,7 +269,7 @@ public record AdvisedRequest(
|
||||
* @param media the media
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder withMedia(List<Media> media) {
|
||||
public Builder media(List<Media> media) {
|
||||
this.media = media;
|
||||
return this;
|
||||
}
|
||||
@@ -278,7 +279,7 @@ public record AdvisedRequest(
|
||||
* @param functionNames the function names
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder withFunctionNames(List<String> functionNames) {
|
||||
public Builder functionNames(List<String> functionNames) {
|
||||
this.functionNames = functionNames;
|
||||
return this;
|
||||
}
|
||||
@@ -288,7 +289,7 @@ public record AdvisedRequest(
|
||||
* @param functionCallbacks the function callbacks
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder withFunctionCallbacks(List<FunctionCallback> functionCallbacks) {
|
||||
public Builder functionCallbacks(List<FunctionCallback> functionCallbacks) {
|
||||
this.functionCallbacks = functionCallbacks;
|
||||
return this;
|
||||
}
|
||||
@@ -298,7 +299,7 @@ public record AdvisedRequest(
|
||||
* @param messages the messages
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder withMessages(List<Message> messages) {
|
||||
public Builder messages(List<Message> messages) {
|
||||
this.messages = messages;
|
||||
return this;
|
||||
}
|
||||
@@ -308,7 +309,7 @@ public record AdvisedRequest(
|
||||
* @param userParams the user params
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder withUserParams(Map<String, Object> userParams) {
|
||||
public Builder userParams(Map<String, Object> userParams) {
|
||||
this.userParams = userParams;
|
||||
return this;
|
||||
}
|
||||
@@ -318,7 +319,7 @@ public record AdvisedRequest(
|
||||
* @param systemParams the system params
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder withSystemParams(Map<String, Object> systemParams) {
|
||||
public Builder systemParams(Map<String, Object> systemParams) {
|
||||
this.systemParams = systemParams;
|
||||
return this;
|
||||
}
|
||||
@@ -328,7 +329,7 @@ public record AdvisedRequest(
|
||||
* @param advisors the advisors
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder withAdvisors(List<Advisor> advisors) {
|
||||
public Builder advisors(List<Advisor> advisors) {
|
||||
this.advisors = advisors;
|
||||
return this;
|
||||
}
|
||||
@@ -338,7 +339,7 @@ public record AdvisedRequest(
|
||||
* @param advisorParams the advisor params
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder withAdvisorParams(Map<String, Object> advisorParams) {
|
||||
public Builder advisorParams(Map<String, Object> advisorParams) {
|
||||
this.advisorParams = advisorParams;
|
||||
return this;
|
||||
}
|
||||
@@ -348,7 +349,7 @@ public record AdvisedRequest(
|
||||
* @param adviseContext the advise context
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder withAdviseContext(Map<String, Object> adviseContext) {
|
||||
public Builder adviseContext(Map<String, Object> adviseContext) {
|
||||
this.adviseContext = adviseContext;
|
||||
return this;
|
||||
}
|
||||
@@ -358,6 +359,132 @@ public record AdvisedRequest(
|
||||
* @param toolContext the tool context
|
||||
* @return this {@link Builder} instance
|
||||
*/
|
||||
public Builder toolContext(Map<String, Object> toolContext) {
|
||||
this.toolContext = toolContext;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #chatModel(ChatModel)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withChatModel(ChatModel chatModel) {
|
||||
this.chatModel = chatModel;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #userText(String)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withUserText(String userText) {
|
||||
this.userText = userText;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #systemText(String)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withSystemText(String systemText) {
|
||||
this.systemText = systemText;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #chatOptions(ChatOptions)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withChatOptions(ChatOptions chatOptions) {
|
||||
this.chatOptions = chatOptions;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #media(List)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withMedia(List<Media> media) {
|
||||
this.media = media;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #functionNames(List)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withFunctionNames(List<String> functionNames) {
|
||||
this.functionNames = functionNames;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #functionCallbacks(List)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withFunctionCallbacks(List<FunctionCallback> functionCallbacks) {
|
||||
this.functionCallbacks = functionCallbacks;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #messages(List)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withMessages(List<Message> messages) {
|
||||
this.messages = messages;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #userParams(Map)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withUserParams(Map<String, Object> userParams) {
|
||||
this.userParams = userParams;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #systemParams(Map)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withSystemParams(Map<String, Object> systemParams) {
|
||||
this.systemParams = systemParams;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #advisors(List)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withAdvisors(List<Advisor> advisors) {
|
||||
this.advisors = advisors;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #advisorParams(Map)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withAdvisorParams(Map<String, Object> advisorParams) {
|
||||
this.advisorParams = advisorParams;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #adviseContext(Map)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withAdviseContext(Map<String, Object> adviseContext) {
|
||||
this.adviseContext = adviseContext;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #toolContext(Map)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withToolContext(Map<String, Object> toolContext) {
|
||||
this.toolContext = toolContext;
|
||||
return this;
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.ai.chat.model.ChatModel;
|
||||
import org.springframework.ai.chat.model.ChatResponse;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -32,6 +33,7 @@ import org.springframework.util.Assert;
|
||||
* @param adviseContext the context to advise the response
|
||||
* @author Christian Tzolov
|
||||
* @author Thomas Vitale
|
||||
* @author Ilayaperumal Gopinathan
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public record AdvisedResponse(@Nullable ChatResponse response, Map<String, Object> adviseContext) {
|
||||
@@ -62,7 +64,7 @@ public record AdvisedResponse(@Nullable ChatResponse response, Map<String, Objec
|
||||
*/
|
||||
public static Builder from(AdvisedResponse advisedResponse) {
|
||||
Assert.notNull(advisedResponse, "advisedResponse cannot be null");
|
||||
return new Builder().withResponse(advisedResponse.response).withAdviseContext(advisedResponse.adviseContext);
|
||||
return new Builder().response(advisedResponse.response).adviseContext(advisedResponse.adviseContext);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,7 +96,7 @@ public record AdvisedResponse(@Nullable ChatResponse response, Map<String, Objec
|
||||
* @param response the chat response
|
||||
* @return the builder
|
||||
*/
|
||||
public Builder withResponse(@Nullable ChatResponse response) {
|
||||
public Builder response(@Nullable ChatResponse response) {
|
||||
this.response = response;
|
||||
return this;
|
||||
}
|
||||
@@ -104,6 +106,24 @@ public record AdvisedResponse(@Nullable ChatResponse response, Map<String, Objec
|
||||
* @param adviseContext the context to advise the response
|
||||
* @return the builder
|
||||
*/
|
||||
public Builder adviseContext(Map<String, Object> adviseContext) {
|
||||
this.adviseContext = adviseContext;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #response(ChatResponse)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withResponse(@Nullable ChatResponse response) {
|
||||
this.response = response;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #adviseContext(Map)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withAdviseContext(Map<String, Object> adviseContext) {
|
||||
this.adviseContext = adviseContext;
|
||||
return this;
|
||||
|
||||
@@ -221,7 +221,7 @@ public class AdvisorObservationContext extends Observation.Context {
|
||||
* @param advisorName the advisor name
|
||||
* @return the builder
|
||||
*/
|
||||
public Builder withAdvisorName(String advisorName) {
|
||||
public Builder advisorName(String advisorName) {
|
||||
this.advisorName = advisorName;
|
||||
return this;
|
||||
}
|
||||
@@ -231,7 +231,7 @@ public class AdvisorObservationContext extends Observation.Context {
|
||||
* @param advisorType the advisor type
|
||||
* @return the builder
|
||||
*/
|
||||
public Builder withAdvisorType(Type advisorType) {
|
||||
public Builder advisorType(Type advisorType) {
|
||||
this.advisorType = advisorType;
|
||||
return this;
|
||||
}
|
||||
@@ -241,7 +241,7 @@ public class AdvisorObservationContext extends Observation.Context {
|
||||
* @param advisedRequest the advised request
|
||||
* @return the builder
|
||||
*/
|
||||
public Builder withAdvisedRequest(AdvisedRequest advisedRequest) {
|
||||
public Builder advisedRequest(AdvisedRequest advisedRequest) {
|
||||
this.advisorRequest = advisedRequest;
|
||||
return this;
|
||||
}
|
||||
@@ -251,7 +251,7 @@ public class AdvisorObservationContext extends Observation.Context {
|
||||
* @param advisorRequestContext the advisor request context
|
||||
* @return the builder
|
||||
*/
|
||||
public Builder withAdvisorRequestContext(Map<String, Object> advisorRequestContext) {
|
||||
public Builder advisorRequestContext(Map<String, Object> advisorRequestContext) {
|
||||
this.advisorRequestContext = advisorRequestContext;
|
||||
return this;
|
||||
}
|
||||
@@ -261,7 +261,7 @@ public class AdvisorObservationContext extends Observation.Context {
|
||||
* @param advisorResponseContext the advisor response context
|
||||
* @return the builder
|
||||
*/
|
||||
public Builder withAdvisorResponseContext(Map<String, Object> advisorResponseContext) {
|
||||
public Builder advisorResponseContext(Map<String, Object> advisorResponseContext) {
|
||||
this.advisorResponseContext = advisorResponseContext;
|
||||
return this;
|
||||
}
|
||||
@@ -271,6 +271,60 @@ public class AdvisorObservationContext extends Observation.Context {
|
||||
* @param order the order of the advisor in the advisor chain
|
||||
* @return the builder
|
||||
*/
|
||||
public Builder order(int order) {
|
||||
this.order = order;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #advisorName(String)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withAdvisorName(String advisorName) {
|
||||
this.advisorName = advisorName;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #advisorType(Type)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withAdvisorType(Type advisorType) {
|
||||
this.advisorType = advisorType;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #advisedRequest(AdvisedRequest)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withAdvisedRequest(AdvisedRequest advisedRequest) {
|
||||
this.advisorRequest = advisedRequest;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #advisorRequestContext(Map)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withAdvisorRequestContext(Map<String, Object> advisorRequestContext) {
|
||||
this.advisorRequestContext = advisorRequestContext;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #advisorResponseContext(Map)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withAdvisorResponseContext(Map<String, Object> advisorResponseContext) {
|
||||
this.advisorResponseContext = advisorResponseContext;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #order(int)} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.0.0-M5")
|
||||
public Builder withOrder(int order) {
|
||||
this.order = order;
|
||||
return this;
|
||||
|
||||
@@ -59,8 +59,8 @@ public class MessageAggregator {
|
||||
}), aggregatedChatResponse -> {
|
||||
|
||||
AdvisedResponse aggregatedAdvisedResponse = AdvisedResponse.builder()
|
||||
.withResponse(aggregatedChatResponse)
|
||||
.withAdviseContext(adviseContext.get())
|
||||
.response(aggregatedChatResponse)
|
||||
.adviseContext(adviseContext.get())
|
||||
.build();
|
||||
|
||||
aggregationHandler.accept(aggregatedAdvisedResponse);
|
||||
|
||||
@@ -32,8 +32,8 @@ class AdvisorObservationContextTests {
|
||||
@Test
|
||||
void whenMandatoryOptionsThenReturn() {
|
||||
AdvisorObservationContext observationContext = AdvisorObservationContext.builder()
|
||||
.withAdvisorName("MyName")
|
||||
.withAdvisorType(AdvisorObservationContext.Type.BEFORE)
|
||||
.advisorName("MyName")
|
||||
.advisorType(AdvisorObservationContext.Type.BEFORE)
|
||||
.build();
|
||||
|
||||
assertThat(observationContext).isNotNull();
|
||||
@@ -41,15 +41,15 @@ class AdvisorObservationContextTests {
|
||||
|
||||
@Test
|
||||
void missingAdvisorName() {
|
||||
assertThatThrownBy(() -> AdvisorObservationContext.builder()
|
||||
.withAdvisorType(AdvisorObservationContext.Type.BEFORE)
|
||||
.build()).isInstanceOf(IllegalArgumentException.class)
|
||||
assertThatThrownBy(
|
||||
() -> AdvisorObservationContext.builder().advisorType(AdvisorObservationContext.Type.BEFORE).build())
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("advisorName must not be null or empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingAdvisorType() {
|
||||
assertThatThrownBy(() -> AdvisorObservationContext.builder().withAdvisorName("MyName").build())
|
||||
assertThatThrownBy(() -> AdvisorObservationContext.builder().advisorName("MyName").build())
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("advisorType must not be null");
|
||||
}
|
||||
|
||||
@@ -46,8 +46,8 @@ class DefaultAdvisorObservationConventionTests {
|
||||
@Test
|
||||
void contextualName() {
|
||||
AdvisorObservationContext observationContext = AdvisorObservationContext.builder()
|
||||
.withAdvisorName("MyName")
|
||||
.withAdvisorType(AdvisorObservationContext.Type.AROUND)
|
||||
.advisorName("MyName")
|
||||
.advisorType(AdvisorObservationContext.Type.AROUND)
|
||||
.build();
|
||||
assertThat(this.observationConvention.getContextualName(observationContext)).isEqualTo("my_name");
|
||||
}
|
||||
@@ -55,8 +55,8 @@ class DefaultAdvisorObservationConventionTests {
|
||||
@Test
|
||||
void supportsAdvisorObservationContext() {
|
||||
AdvisorObservationContext observationContext = AdvisorObservationContext.builder()
|
||||
.withAdvisorName("MyName")
|
||||
.withAdvisorType(AdvisorObservationContext.Type.AROUND)
|
||||
.advisorName("MyName")
|
||||
.advisorType(AdvisorObservationContext.Type.AROUND)
|
||||
.build();
|
||||
assertThat(this.observationConvention.supportsContext(observationContext)).isTrue();
|
||||
assertThat(this.observationConvention.supportsContext(new Observation.Context())).isFalse();
|
||||
@@ -65,8 +65,8 @@ class DefaultAdvisorObservationConventionTests {
|
||||
@Test
|
||||
void shouldHaveLowCardinalityKeyValuesWhenDefined() {
|
||||
AdvisorObservationContext observationContext = AdvisorObservationContext.builder()
|
||||
.withAdvisorName("MyName")
|
||||
.withAdvisorType(AdvisorObservationContext.Type.AROUND)
|
||||
.advisorName("MyName")
|
||||
.advisorType(AdvisorObservationContext.Type.AROUND)
|
||||
.build();
|
||||
assertThat(this.observationConvention.getLowCardinalityKeyValues(observationContext)).contains(
|
||||
KeyValue.of(LowCardinalityKeyNames.ADVISOR_TYPE.asString(),
|
||||
@@ -80,9 +80,9 @@ class DefaultAdvisorObservationConventionTests {
|
||||
@Test
|
||||
void shouldHaveKeyValuesWhenDefinedAndResponse() {
|
||||
AdvisorObservationContext observationContext = AdvisorObservationContext.builder()
|
||||
.withAdvisorName("MyName")
|
||||
.withAdvisorType(AdvisorObservationContext.Type.AROUND)
|
||||
.withOrder(678)
|
||||
.advisorName("MyName")
|
||||
.advisorType(AdvisorObservationContext.Type.AROUND)
|
||||
.order(678)
|
||||
.build();
|
||||
|
||||
assertThat(this.observationConvention.getHighCardinalityKeyValues(observationContext))
|
||||
|
||||
@@ -272,11 +272,11 @@ public class ReReadingAdvisor implements CallAroundAdvisor, StreamAroundAdvisor
|
||||
advisedUserParams.put("re2_input_query", advisedRequest.userText());
|
||||
|
||||
return AdvisedRequest.from(advisedRequest)
|
||||
.withUserText("""
|
||||
.userText("""
|
||||
{re2_input_query}
|
||||
Read the question again: {re2_input_query}
|
||||
""")
|
||||
.withUserParams(advisedUserParams)
|
||||
.userParams(advisedUserParams)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user