-
Notifications
You must be signed in to change notification settings - Fork 317
add APIs for llm obs sdk #8135
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
Merged
Merged
add APIs for llm obs sdk #8135
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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
136 changes: 136 additions & 0 deletions
136
dd-trace-api/src/main/java/datadog/trace/api/llmobs/LLMObs.java
This file contains hidden or 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,136 @@ | ||
| package datadog.trace.api.llmobs; | ||
|
|
||
| import datadog.trace.api.llmobs.noop.NoOpLLMObsSpanFactory; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| public class LLMObs { | ||
| protected LLMObs() {} | ||
|
|
||
| protected static LLMObsSpanFactory SPAN_FACTORY = NoOpLLMObsSpanFactory.INSTANCE; | ||
|
|
||
| public static LLMObsSpan startLLMSpan( | ||
| String spanName, | ||
| String modelName, | ||
| String modelProvider, | ||
| @Nullable String mlApp, | ||
| @Nullable String sessionId) { | ||
|
|
||
| return SPAN_FACTORY.startLLMSpan(spanName, modelName, modelProvider, mlApp, sessionId); | ||
| } | ||
|
|
||
| public static LLMObsSpan startAgentSpan( | ||
| String spanName, @Nullable String mlApp, @Nullable String sessionId) { | ||
|
|
||
| return SPAN_FACTORY.startAgentSpan(spanName, mlApp, sessionId); | ||
| } | ||
|
|
||
| public static LLMObsSpan startToolSpan( | ||
| String spanName, @Nullable String mlApp, @Nullable String sessionId) { | ||
|
|
||
| return SPAN_FACTORY.startToolSpan(spanName, mlApp, sessionId); | ||
| } | ||
|
|
||
| public static LLMObsSpan startTaskSpan( | ||
| String spanName, @Nullable String mlApp, @Nullable String sessionId) { | ||
|
|
||
| return SPAN_FACTORY.startTaskSpan(spanName, mlApp, sessionId); | ||
| } | ||
|
|
||
| public static LLMObsSpan startWorkflowSpan( | ||
| String spanName, @Nullable String mlApp, @Nullable String sessionId) { | ||
|
|
||
| return SPAN_FACTORY.startWorkflowSpan(spanName, mlApp, sessionId); | ||
| } | ||
|
|
||
| public interface LLMObsSpanFactory { | ||
| LLMObsSpan startLLMSpan( | ||
| String spanName, | ||
| String modelName, | ||
| String modelProvider, | ||
| @Nullable String mlApp, | ||
| @Nullable String sessionId); | ||
|
|
||
| LLMObsSpan startAgentSpan(String spanName, @Nullable String mlApp, @Nullable String sessionId); | ||
|
|
||
| LLMObsSpan startToolSpan(String spanName, @Nullable String mlApp, @Nullable String sessionId); | ||
|
|
||
| LLMObsSpan startTaskSpan(String spanName, @Nullable String mlApp, @Nullable String sessionId); | ||
|
|
||
| LLMObsSpan startWorkflowSpan( | ||
| String spanName, @Nullable String mlApp, @Nullable String sessionId); | ||
| } | ||
|
|
||
| public static class ToolCall { | ||
| private String name; | ||
| private String type; | ||
| private String toolId; | ||
| private Map<String, Object> arguments; | ||
|
|
||
| public static ToolCall from( | ||
| String name, String type, String toolId, Map<String, Object> arguments) { | ||
| return new ToolCall(name, type, toolId, arguments); | ||
| } | ||
|
|
||
| private ToolCall(String name, String type, String toolId, Map<String, Object> arguments) { | ||
| this.name = name; | ||
| this.type = type; | ||
| this.toolId = toolId; | ||
| this.arguments = arguments; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getType() { | ||
| return type; | ||
| } | ||
|
|
||
| public String getToolId() { | ||
| return toolId; | ||
| } | ||
|
|
||
| public Map<String, Object> getArguments() { | ||
| return arguments; | ||
| } | ||
| } | ||
|
|
||
| public static class LLMMessage { | ||
| private String role; | ||
| private String content; | ||
| private List<ToolCall> toolCalls; | ||
|
|
||
| public static LLMMessage from(String role, String content, List<ToolCall> toolCalls) { | ||
| return new LLMMessage(role, content, toolCalls); | ||
| } | ||
|
|
||
| public static LLMMessage from(String role, String content) { | ||
| return new LLMMessage(role, content); | ||
| } | ||
|
|
||
| private LLMMessage(String role, String content, List<ToolCall> toolCalls) { | ||
| this.role = role; | ||
| this.content = content; | ||
| this.toolCalls = toolCalls; | ||
| } | ||
|
|
||
| private LLMMessage(String role, String content) { | ||
| this.role = role; | ||
| this.content = content; | ||
| } | ||
|
|
||
| public String getRole() { | ||
| return role; | ||
| } | ||
|
|
||
| public String getContent() { | ||
| return content; | ||
| } | ||
|
|
||
| public List<ToolCall> getToolCalls() { | ||
| return toolCalls; | ||
| } | ||
| } | ||
| } | ||
145 changes: 145 additions & 0 deletions
145
dd-trace-api/src/main/java/datadog/trace/api/llmobs/LLMObsSpan.java
This file contains hidden or 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,145 @@ | ||
| package datadog.trace.api.llmobs; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** This interface represent an individual LLM Obs span. */ | ||
| public interface LLMObsSpan { | ||
|
|
||
| /** | ||
| * Annotate the span with inputs and outputs for LLM spans | ||
| * | ||
| * @param inputMessages The input messages of the span in the form of a list | ||
| * @param outputMessages The output messages of the span in the form of a list | ||
| */ | ||
| void annotateIO(List<LLMObs.LLMMessage> inputMessages, List<LLMObs.LLMMessage> outputMessages); | ||
|
|
||
| /** | ||
| * Annotate the span with inputs and outputs | ||
| * | ||
| * @param inputData The input data of the span in the form of a string | ||
| * @param outputData The output data of the span in the form of a string | ||
| */ | ||
| void annotateIO(String inputData, String outputData); | ||
|
|
||
| /** | ||
| * Annotate the span with metadata | ||
| * | ||
| * @param metadata A map of JSON serializable key-value pairs that contains metadata information | ||
| * relevant to the input or output operation described by the span | ||
| */ | ||
| void setMetadata(Map<String, Object> metadata); | ||
|
|
||
| /** | ||
| * Annotate the span with metrics | ||
| * | ||
| * @param metrics A map of JSON serializable keys and numeric values that users can add as metrics | ||
| * relevant to the operation described by the span (input_tokens, output_tokens, total_tokens, | ||
| * etc.). | ||
| */ | ||
| void setMetrics(Map<String, Number> metrics); | ||
|
|
||
| /** | ||
| * Annotate the span with a single metric key value pair for the span’s context (number of tokens | ||
| * document length, etc). | ||
| * | ||
| * @param key the name of the metric | ||
| * @param value the value of the metric | ||
| */ | ||
| void setMetric(CharSequence key, int value); | ||
|
|
||
| /** | ||
| * Annotate the span with a single metric key value pair for the span’s context (number of tokens | ||
| * document length, etc). | ||
| * | ||
| * @param key the name of the metric | ||
| * @param value the value of the metric | ||
| */ | ||
| void setMetric(CharSequence key, long value); | ||
|
|
||
| /** | ||
| * Annotate the span with a single metric key value pair for the span’s context (number of tokens | ||
| * document length, etc). | ||
| * | ||
| * @param key the name of the metric | ||
| * @param value the value of the metric | ||
| */ | ||
| void setMetric(CharSequence key, double value); | ||
|
|
||
| /** | ||
| * Annotate the span with tags | ||
| * | ||
| * @param tags An map of JSON serializable key-value pairs that users can add as tags regarding | ||
| * the span’s context (session, environment, system, versioning, etc.). | ||
| */ | ||
| void setTags(Map<String, Object> tags); | ||
|
|
||
| /** | ||
| * Annotate the span with a single tag key value pair as a tag regarding the span’s context | ||
| * (session, environment, system, versioning, etc.). | ||
| * | ||
| * @param key the key of the tag | ||
| * @param value the value of the tag | ||
| */ | ||
| void setTag(String key, String value); | ||
|
|
||
| /** | ||
| * Annotate the span with a single tag key value pair as a tag regarding the span’s context | ||
| * (session, environment, system, versioning, etc.). | ||
| * | ||
| * @param key the key of the tag | ||
| * @param value the value of the tag | ||
| */ | ||
| void setTag(String key, boolean value); | ||
|
|
||
| /** | ||
| * Annotate the span with a single tag key value pair as a tag regarding the span’s context | ||
| * (session, environment, system, versioning, etc.). | ||
| * | ||
| * @param key the key of the tag | ||
| * @param value the value of the tag | ||
| */ | ||
| void setTag(String key, int value); | ||
|
|
||
| /** | ||
| * Annotate the span with a single tag key value pair as a tag regarding the span’s context | ||
| * (session, environment, system, versioning, etc.). | ||
| * | ||
| * @param key the key of the tag | ||
| * @param value the value of the tag | ||
| */ | ||
| void setTag(String key, long value); | ||
|
|
||
| /** | ||
| * Annotate the span with a single tag key value pair as a tag regarding the span’s context | ||
| * (session, environment, system, versioning, etc.). | ||
| * | ||
| * @param key the key of the tag | ||
| * @param value the value of the tag | ||
| */ | ||
| void setTag(String key, double value); | ||
|
|
||
| /** | ||
| * Annotate the span to indicate that an error occurred | ||
| * | ||
| * @param error whether an error occurred | ||
| */ | ||
| void setError(boolean error); | ||
|
|
||
| /** | ||
| * Annotate the span with an error message | ||
| * | ||
| * @param errorMessage the message of the error | ||
| */ | ||
| void setErrorMessage(String errorMessage); | ||
|
|
||
| /** | ||
| * Annotate the span with a throwable | ||
| * | ||
| * @param throwable the errored throwable | ||
| */ | ||
| void addThrowable(Throwable throwable); | ||
|
|
||
| /** Finishes (closes) a span */ | ||
| void finish(); | ||
| } |
61 changes: 61 additions & 0 deletions
61
dd-trace-api/src/main/java/datadog/trace/api/llmobs/noop/NoOpLLMObsSpan.java
This file contains hidden or 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,61 @@ | ||
| package datadog.trace.api.llmobs.noop; | ||
|
|
||
| import datadog.trace.api.llmobs.LLMObs; | ||
| import datadog.trace.api.llmobs.LLMObsSpan; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class NoOpLLMObsSpan implements LLMObsSpan { | ||
| public static final LLMObsSpan INSTANCE = new NoOpLLMObsSpan(); | ||
|
|
||
| @Override | ||
| public void annotateIO(List<LLMObs.LLMMessage> inputData, List<LLMObs.LLMMessage> outputData) {} | ||
|
|
||
| @Override | ||
| public void annotateIO(String inputData, String outputData) {} | ||
|
|
||
| @Override | ||
| public void setMetadata(Map<String, Object> metadata) {} | ||
|
|
||
| @Override | ||
| public void setMetrics(Map<String, Number> metrics) {} | ||
|
|
||
| @Override | ||
| public void setMetric(CharSequence key, int value) {} | ||
|
|
||
| @Override | ||
| public void setMetric(CharSequence key, long value) {} | ||
|
|
||
| @Override | ||
| public void setMetric(CharSequence key, double value) {} | ||
|
|
||
| @Override | ||
| public void setTags(Map<String, Object> tags) {} | ||
|
|
||
| @Override | ||
| public void setTag(String key, String value) {} | ||
|
|
||
| @Override | ||
| public void setTag(String key, boolean value) {} | ||
|
|
||
| @Override | ||
| public void setTag(String key, int value) {} | ||
|
|
||
| @Override | ||
| public void setTag(String key, long value) {} | ||
|
|
||
| @Override | ||
| public void setTag(String key, double value) {} | ||
|
|
||
| @Override | ||
| public void setError(boolean error) {} | ||
|
|
||
| @Override | ||
| public void setErrorMessage(String errorMessage) {} | ||
|
|
||
| @Override | ||
| public void addThrowable(Throwable throwable) {} | ||
|
|
||
| @Override | ||
| public void finish() {} | ||
| } |
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.
Should it a public final class with private constructor if it’s only an helper class?
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.
https://github.com/DataDog/dd-trace-java/pull/8390/files#diff-ee71e1544f39cc6c551488ad74b877a819b780b9d07c2983be13b80161958aa9R1-R10
i extend this class here to allow setting an implemented, non-noop span factory (and in another PR, another factory)
i would say that this is not exactly only a helper class, this is the entry point of the SDK manual API and the calls are registered with the llmobs agent (as seen in #8390)