-
Notifications
You must be signed in to change notification settings - Fork 893
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prompt content and completion as span events
Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
- Loading branch information
1 parent
80fe5e4
commit 3fa102e
Showing
12 changed files
with
612 additions
and
53 deletions.
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
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
53 changes: 53 additions & 0 deletions
53
...n/java/org/springframework/ai/chat/observation/ChatModelCompletionObservationHandler.java
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 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.observation; | ||
|
||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.ObservationHandler; | ||
import io.micrometer.tracing.handler.TracingObservationHandler; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.trace.Span; | ||
import org.springframework.ai.observation.conventions.AiObservationAttributes; | ||
import org.springframework.ai.observation.conventions.AiObservationEventNames; | ||
|
||
/** | ||
* Handler for including the chat completion content in the observation as a span event. | ||
* | ||
* @author Thomas Vitale | ||
* @since 1.0.0 | ||
*/ | ||
public class ChatModelCompletionObservationHandler implements ObservationHandler<ChatModelObservationContext> { | ||
|
||
@Override | ||
public void onStop(ChatModelObservationContext context) { | ||
TracingObservationHandler.TracingContext tracingContext = context | ||
.get(TracingObservationHandler.TracingContext.class); | ||
Span otelSpan = ChatModelObservationContentProcessor.extractOtelSpan(tracingContext); | ||
|
||
if (otelSpan != null) { | ||
otelSpan.addEvent(AiObservationEventNames.CONTENT_COMPLETION.value(), | ||
Attributes.of(AttributeKey.stringArrayKey(AiObservationAttributes.COMPLETION.value()), | ||
ChatModelObservationContentProcessor.completion(context))); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean supportsContext(Observation.Context context) { | ||
return context instanceof ChatModelObservationContext; | ||
} | ||
|
||
} |
99 changes: 99 additions & 0 deletions
99
...in/java/org/springframework/ai/chat/observation/ChatModelObservationContentProcessor.java
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 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.observation; | ||
|
||
import io.micrometer.tracing.handler.TracingObservationHandler; | ||
import io.opentelemetry.api.trace.Span; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.ai.model.Content; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.CollectionUtils; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
import java.util.StringJoiner; | ||
|
||
/** | ||
* Utilities to process the prompt and completion content in observations for chat models. | ||
* | ||
* @author Thomas Vitale | ||
*/ | ||
public final class ChatModelObservationContentProcessor { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ChatModelObservationContentProcessor.class); | ||
|
||
public static List<String> prompt(ChatModelObservationContext context) { | ||
if (CollectionUtils.isEmpty(context.getRequest().getInstructions())) { | ||
return List.of(); | ||
} | ||
|
||
return context.getRequest().getInstructions().stream().map(Content::getContent).toList(); | ||
} | ||
|
||
public static List<String> completion(ChatModelObservationContext context) { | ||
if (context == null || context.getResponse() == null || context.getResponse().getResults() == null | ||
|| CollectionUtils.isEmpty(context.getResponse().getResults())) { | ||
return List.of(); | ||
} | ||
|
||
if (!StringUtils.hasText(context.getResponse().getResult().getOutput().getContent())) { | ||
return List.of(); | ||
} | ||
|
||
return context.getResponse() | ||
.getResults() | ||
.stream() | ||
.filter(generation -> generation.getOutput() != null | ||
&& StringUtils.hasText(generation.getOutput().getContent())) | ||
.map(generation -> generation.getOutput().getContent()) | ||
.toList(); | ||
} | ||
|
||
public static String concatenateStrings(List<String> strings) { | ||
var promptMessagesJoiner = new StringJoiner(", ", "[", "]"); | ||
strings.forEach(string -> promptMessagesJoiner.add("\"" + string + "\"")); | ||
return promptMessagesJoiner.toString(); | ||
} | ||
|
||
@Nullable | ||
public static Span extractOtelSpan(@Nullable TracingObservationHandler.TracingContext tracingContext) { | ||
if (tracingContext == null) { | ||
return null; | ||
} | ||
|
||
io.micrometer.tracing.Span micrometerSpan = tracingContext.getSpan(); | ||
try { | ||
Method toOtelMethod = tracingContext.getSpan() | ||
.getClass() | ||
.getDeclaredMethod("toOtel", io.micrometer.tracing.Span.class); | ||
toOtelMethod.setAccessible(true); | ||
Object otelSpanObject = toOtelMethod.invoke(null, micrometerSpan); | ||
if (otelSpanObject instanceof Span otelSpan) { | ||
return otelSpan; | ||
} | ||
} | ||
catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) { | ||
logger.warn("It wasn't possible to extract the OpenTelemetry Span object from Micrometer", ex); | ||
return null; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
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
54 changes: 54 additions & 0 deletions
54
...ava/org/springframework/ai/chat/observation/ChatModelPromptContentObservationHandler.java
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 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.observation; | ||
|
||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.ObservationHandler; | ||
import io.micrometer.tracing.handler.TracingObservationHandler; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.trace.Span; | ||
import org.springframework.ai.observation.conventions.AiObservationAttributes; | ||
import org.springframework.ai.observation.conventions.AiObservationEventNames; | ||
|
||
/** | ||
* Handler for including the chat prompt content in the observation as a span event. | ||
* | ||
* @author Thomas Vitale | ||
* @since 1.0.0 | ||
*/ | ||
public class ChatModelPromptContentObservationHandler implements ObservationHandler<ChatModelObservationContext> { | ||
|
||
@Override | ||
public void onStop(ChatModelObservationContext context) { | ||
TracingObservationHandler.TracingContext tracingContext = context | ||
.get(TracingObservationHandler.TracingContext.class); | ||
Span otelSpan = ChatModelObservationContentProcessor.extractOtelSpan(tracingContext); | ||
|
||
if (otelSpan != null) { | ||
otelSpan.addEvent(AiObservationEventNames.CONTENT_PROMPT.value(), | ||
Attributes.of(AttributeKey.stringArrayKey(AiObservationAttributes.PROMPT.value()), | ||
ChatModelObservationContentProcessor.prompt(context))); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public boolean supportsContext(Observation.Context context) { | ||
return context instanceof ChatModelObservationContext; | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
...a/org/springframework/ai/chat/observation/ChatModelCompletionObservationHandlerTests.java
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 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.observation; | ||
|
||
import io.micrometer.tracing.handler.TracingObservationHandler; | ||
import io.micrometer.tracing.otel.bridge.OtelCurrentTraceContext; | ||
import io.micrometer.tracing.otel.bridge.OtelTracer; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.sdk.trace.ReadableSpan; | ||
import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.ai.chat.messages.AssistantMessage; | ||
import org.springframework.ai.chat.model.ChatResponse; | ||
import org.springframework.ai.chat.model.Generation; | ||
import org.springframework.ai.chat.prompt.ChatOptionsBuilder; | ||
import org.springframework.ai.chat.prompt.Prompt; | ||
import org.springframework.ai.observation.conventions.AiObservationAttributes; | ||
import org.springframework.ai.observation.conventions.AiObservationEventNames; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Unit tests for {@link ChatModelCompletionObservationHandler}. | ||
* | ||
* @author Thomas Vitale | ||
*/ | ||
class ChatModelCompletionObservationHandlerTests { | ||
|
||
@Test | ||
void whenCompletionWithTextThenSpanEvent() { | ||
var observationContext = ChatModelObservationContext.builder() | ||
.prompt(new Prompt("supercalifragilisticexpialidocious")) | ||
.provider("mary-poppins") | ||
.requestOptions(ChatOptionsBuilder.builder().withModel("spoonful-of-sugar").build()) | ||
.build(); | ||
observationContext.setResponse(new ChatResponse(List.of(new Generation(new AssistantMessage("say please")), | ||
new Generation(new AssistantMessage("seriously, say please"))))); | ||
var sdkTracer = SdkTracerProvider.builder().build().get("test"); | ||
var otelTracer = new OtelTracer(sdkTracer, new OtelCurrentTraceContext(), null); | ||
var span = otelTracer.nextSpan(); | ||
var tracingContext = new TracingObservationHandler.TracingContext(); | ||
tracingContext.setSpan(span); | ||
observationContext.put(TracingObservationHandler.TracingContext.class, tracingContext); | ||
|
||
new ChatModelCompletionObservationHandler().onStop(observationContext); | ||
|
||
var otelSpan = ChatModelObservationContentProcessor.extractOtelSpan(tracingContext); | ||
assertThat(otelSpan).isNotNull(); | ||
var spanData = ((ReadableSpan) otelSpan).toSpanData(); | ||
assertThat(spanData.getEvents().size()).isEqualTo(1); | ||
assertThat(spanData.getEvents().get(0).getName()).isEqualTo(AiObservationEventNames.CONTENT_COMPLETION.value()); | ||
assertThat(spanData.getEvents() | ||
.get(0) | ||
.getAttributes() | ||
.get(AttributeKey.stringArrayKey(AiObservationAttributes.COMPLETION.value()))) | ||
.containsOnly("say please", "seriously, say please"); | ||
} | ||
|
||
} |
Oops, something went wrong.