-
Notifications
You must be signed in to change notification settings - Fork 824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only construct Observation.Context for Chat and Vector ops on Model observations #1661
Open
jxblum
wants to merge
1
commit into
spring-projects:main
Choose a base branch
from
jxblum:pr/observation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
|
||
import io.micrometer.observation.Observation; | ||
|
@@ -54,10 +55,12 @@ | |
import org.springframework.ai.chat.observation.ChatModelObservationContext; | ||
import org.springframework.ai.chat.observation.ChatModelObservationConvention; | ||
import org.springframework.ai.chat.observation.ChatModelObservationDocumentation; | ||
import org.springframework.ai.chat.observation.support.ChatModelObservationSupport; | ||
import org.springframework.ai.chat.observation.DefaultChatModelObservationConvention; | ||
import org.springframework.ai.chat.prompt.ChatOptions; | ||
import org.springframework.ai.chat.prompt.ChatOptionsBuilder; | ||
import org.springframework.ai.chat.prompt.Prompt; | ||
import org.springframework.ai.model.Content; | ||
import org.springframework.ai.model.ModelOptionsUtils; | ||
import org.springframework.ai.model.function.FunctionCallback; | ||
import org.springframework.ai.model.function.FunctionCallbackContext; | ||
|
@@ -77,6 +80,7 @@ | |
* @author Mariusz Bernacki | ||
* @author Thomas Vitale | ||
* @author Claudio Silva Junior | ||
* @author John Blum | ||
* @since 1.0.0 | ||
*/ | ||
public class AnthropicChatModel extends AbstractToolCallSupport implements ChatModel { | ||
|
@@ -209,28 +213,31 @@ public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaul | |
|
||
@Override | ||
public ChatResponse call(Prompt prompt) { | ||
|
||
ChatCompletionRequest request = createRequest(prompt, false); | ||
|
||
ChatModelObservationContext observationContext = ChatModelObservationContext.builder() | ||
Supplier<ChatModelObservationContext> observationContext = () -> ChatModelObservationContext.builder() | ||
.prompt(prompt) | ||
.provider(AnthropicApi.PROVIDER_NAME) | ||
.requestOptions(buildRequestOptions(request)) | ||
.build(); | ||
|
||
ChatResponse response = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION | ||
.observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, | ||
this.observationRegistry) | ||
.observe(() -> { | ||
Observation observation = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION.observation( | ||
this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, observationContext, | ||
this.observationRegistry); | ||
|
||
ChatResponse response = observation.observe(() -> { | ||
|
||
ResponseEntity<ChatCompletionResponse> completionEntity = this.retryTemplate | ||
.execute(ctx -> this.anthropicApi.chatCompletionEntity(request)); | ||
ResponseEntity<ChatCompletionResponse> completionEntity = this.retryTemplate | ||
.execute(ctx -> this.anthropicApi.chatCompletionEntity(request)); | ||
|
||
ChatResponse chatResponse = toChatResponse(completionEntity.getBody()); | ||
ChatResponse chatResponse = toChatResponse(completionEntity.getBody()); | ||
|
||
observationContext.setResponse(chatResponse); | ||
ChatModelObservationSupport.getObservationContext(observation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do this instead (might be simpler): if (!observation.isNoop()) {
((ChatModelObservationContext)observation.getContext()).setResponse(chatResponse);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I generally agree, but I'd like to see something like this in Micrometer: interface Observation {
static boolean isNoop(Observation observation) {
return observation == null || observation.isNoop();
}
static boolean isNotNoop(Observation observation) {
return !isNoop(observation);
}
// ...
} |
||
.ifPresent(context -> context.setResponse(chatResponse)); | ||
|
||
return chatResponse; | ||
}); | ||
return chatResponse; | ||
}); | ||
|
||
if (!isProxyToolCalls(prompt, this.defaultOptions) && response != null | ||
&& this.isToolCall(response, Set.of("tool_use"))) { | ||
|
@@ -243,17 +250,19 @@ public ChatResponse call(Prompt prompt) { | |
|
||
@Override | ||
public Flux<ChatResponse> stream(Prompt prompt) { | ||
|
||
return Flux.deferContextual(contextView -> { | ||
|
||
ChatCompletionRequest request = createRequest(prompt, true); | ||
|
||
ChatModelObservationContext observationContext = ChatModelObservationContext.builder() | ||
.prompt(prompt) | ||
.provider(AnthropicApi.PROVIDER_NAME) | ||
Supplier<ChatModelObservationContext> observationContext = () -> ChatModelObservationContext.builder() | ||
.requestOptions(buildRequestOptions(request)) | ||
.provider(AnthropicApi.PROVIDER_NAME) | ||
.prompt(prompt) | ||
.build(); | ||
|
||
Observation observation = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION.observation( | ||
this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, | ||
this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, observationContext, | ||
this.observationRegistry); | ||
|
||
observation.parentObservation(contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null)).start(); | ||
|
@@ -276,7 +285,8 @@ public Flux<ChatResponse> stream(Prompt prompt) { | |
.contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation)); | ||
// @formatter:on | ||
|
||
return new MessageAggregator().aggregate(chatResponseFlux, observationContext::setResponse); | ||
return new MessageAggregator().aggregate(chatResponseFlux, | ||
ChatModelObservationSupport.setChatResponseInObservationContext(observation)); | ||
}); | ||
} | ||
|
||
|
@@ -408,7 +418,7 @@ else if (message.getMessageType() == MessageType.TOOL) { | |
String systemPrompt = prompt.getInstructions() | ||
.stream() | ||
.filter(m -> m.getMessageType() == MessageType.SYSTEM) | ||
.map(m -> m.getContent()) | ||
.map(Content::getContent) | ||
.collect(Collectors.joining(System.lineSeparator())); | ||
|
||
ChatCompletionRequest request = new ChatCompletionRequest(this.defaultOptions.getModel(), userMessages, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about calling it
observationContextSupplier
? (There are a few other occasions.)The point of passing the context via a
Supplier
when you create an Observation is exactly this; when observations are "off" (noop), it will not call the supplier so no unnecessary context objects will be created. 👍🏼In this particular case though, since the
request
can be different for every call, this replaces creating a new context to creating a newSupplier
which I think might be more lightweight but there is a bit of additional complexity through the noop check + insanceof + cast + usingOptional
so which one is more lightweight, I'm not sure, only JMH can tell I guess.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am fine with whatever name we use. In some cases (such as Wrapper types or Wrapper-like types, e.g.
Supplier
, orOptional
), I simply use, or prefer, the name of the thing it wraps.My reasoning is also similar in effect to
List<User> users
vs.List<User> userList
. I like users, particularly if the collection-type might change (e.g.Set
)