Skip to content

Add ChatClient Advisor Observability support #1259

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

import org.springframework.ai.chat.client.advisor.observation.AdvisorObservationConvention;
import org.springframework.ai.chat.client.advisor.observation.ObservableRequestResponseAdvisor;
import org.springframework.ai.chat.client.observation.ChatClientObservationContext;
import org.springframework.ai.chat.client.observation.ChatClientObservationConvention;
import org.springframework.ai.chat.client.observation.ChatClientObservationDocumentation;
Expand Down Expand Up @@ -654,22 +656,35 @@ public ChatClientRequestSpec advisors(Consumer<ChatClient.AdvisorSpec> consumer)
var as = new DefaultAdvisorSpec();
consumer.accept(as);
this.advisorParams.putAll(as.getParams());
this.advisors.addAll(as.getAdvisors());
this.advisors.addAll(toObservableAdvisors(as.getAdvisors(), this.observationRegistry, null));
return this;
}

public ChatClientRequestSpec advisors(RequestResponseAdvisor... advisors) {
Assert.notNull(advisors, "the advisors must be non-null");
this.advisors.addAll(List.of(advisors));
this.advisors.addAll(toObservableAdvisors(List.of(advisors), this.observationRegistry, null));
return this;
}

public ChatClientRequestSpec advisors(List<RequestResponseAdvisor> advisors) {
Assert.notNull(advisors, "the advisors must be non-null");
this.advisors.addAll(advisors);
this.advisors.addAll(toObservableAdvisors(advisors, this.observationRegistry, null));
return this;
}

private List<RequestResponseAdvisor> toObservableAdvisors(List<RequestResponseAdvisor> advisors,
ObservationRegistry observationRegistry, AdvisorObservationConvention customObservationConvention) {
if (CollectionUtils.isEmpty(advisors)) {
return advisors;
}
List<RequestResponseAdvisor> observableAdvisors = new ArrayList<>();
for (RequestResponseAdvisor advisor : advisors) {
observableAdvisors.add(new ObservableRequestResponseAdvisor(advisor, observationRegistry,
customObservationConvention));
}
return observableAdvisors;
}

public ChatClientRequestSpec messages(Message... messages) {
Assert.notNull(messages, "the messages must be non-null");
this.messages.addAll(List.of(messages));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import org.springframework.ai.chat.client.AdvisedRequest;
import org.springframework.ai.chat.client.RequestResponseAdvisor;
import org.springframework.ai.chat.metadata.ChatResponseMetadata;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.document.Document;
import org.springframework.ai.model.Content;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright 2024 - 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ai.chat.client.advisor.observation;

import java.util.Map;

import org.springframework.ai.chat.client.AdvisedRequest;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.util.Assert;

import io.micrometer.observation.Observation;

/**
* @author Christian Tzolov
* @since 1.0.0
*/

public class AdvisorObservationContext extends Observation.Context {

public enum Type {

BEFORE, AFTER, AROUND;

};

private String advisorName;

private Type advisorType;

/**
* The {@link AdvisedRequest} data to be advised. Represents the row
* {@link ChatClient.ChatClientRequestSpec} data before sealed into a {@link Prompt}.
*/
private AdvisedRequest avisorRequest;

/**
* The shared data between the advisors in the chain. It is shared between all request
* and response advising points of all advisors in the chain.
*/
private Map<String, Object> advisorRequestContext;

/**
* the shared data between the advisors in the chain. It is shared between all request
* and response advising points of all advisors in the chain.
*/
private Map<String, Object> advisorResponseContext;

public void setAdvisorName(String advisorName) {
this.advisorName = advisorName;
}

public String getAdvisorName() {
return this.advisorName;
}

public Type getAdvisorType() {
return this.advisorType;
}

public void setAdvisorType(Type type) {
this.advisorType = type;
}

public AdvisedRequest getAdvisedRequest() {
return this.avisorRequest;
}

public void setAdvisedRequest(AdvisedRequest advisedRequest) {
this.avisorRequest = advisedRequest;
}

public Map<String, Object> getAdvisorRequestContext() {
return this.advisorRequestContext;
}

public void setAdvisorRequestContext(Map<String, Object> advisorRequestContext) {
this.advisorRequestContext = advisorRequestContext;
}

public Map<String, Object> getAdvisorResponseContext() {
return this.advisorResponseContext;
}

public void setAdvisorResponseContext(Map<String, Object> advisorResponseContext) {
this.advisorResponseContext = advisorResponseContext;
}

public static Builder builder() {
return new Builder();
}

public static class Builder {

private final AdvisorObservationContext context = new AdvisorObservationContext();

public Builder withAdvisorName(String advisorName) {
this.context.setAdvisorName(advisorName);
return this;
}

public Builder withAdvisorType(Type advisorType) {
this.context.setAdvisorType(advisorType);
return this;
}

public Builder withAdvisedRequest(AdvisedRequest advisedRequest) {
this.context.setAdvisedRequest(advisedRequest);
return this;
}

public Builder withAdvisorRequestContext(Map<String, Object> advisorRequestContext) {
this.context.setAdvisorRequestContext(advisorRequestContext);
return this;
}

public Builder withAdvisorResponseContext(Map<String, Object> advisorResponseContext) {
this.context.setAdvisorResponseContext(advisorResponseContext);
return this;
}

public AdvisorObservationContext build() {
Assert.hasText(this.context.advisorName, "The advisorName must not be empty!");
Assert.notNull(this.context.advisorType, "The advisorType must not be null!");
return this.context;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2024 - 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ai.chat.client.advisor.observation;

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationConvention;

/**
* @author Christian Tzolov
* @since 1.0.0
*/

public interface AdvisorObservationConvention extends ObservationConvention<AdvisorObservationContext> {

@Override
default boolean supportsContext(Observation.Context context) {
return context instanceof AdvisorObservationContext;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2024 - 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ai.chat.client.advisor.observation;

import io.micrometer.common.docs.KeyName;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationConvention;
import io.micrometer.observation.docs.ObservationDocumentation;

/**
* @author Christian Tzolov
* @since 1.0.0
*/
public enum AdvisorObservationDocumentation implements ObservationDocumentation {

/**
* AI Chat Client observations
*/
AI_ADVISOR {
@Override
public Class<? extends ObservationConvention<? extends Observation.Context>> getDefaultConvention() {
return DefaultAdvisorObservationConvention.class;
}

@Override
public KeyName[] getLowCardinalityKeyNames() {
return LowCardinalityKeyNames.values();
}

@Override
public KeyName[] getHighCardinalityKeyNames() {
return HighCardinalityKeyNames.values();
}

};

public enum LowCardinalityKeyNames implements KeyName {

/**
* Spring AI kind.
*/
SPRING_AI_KIND {
@Override
public String asString() {
return "spring.ai.kind";
}
},

/**
* Advisor type: Before, After or Around.
*/
ADVISOR_TYPE {
@Override
public String asString() {
return "spring.ai.chat.client.advisor.type";
}
};

}

public enum HighCardinalityKeyNames implements KeyName {

/**
* Chat Model name.
*/
ADVISOR_NAME {
@Override
public String asString() {
return "spring.ai.chat.client.advisor.name";
}
};

}

}
Loading