Skip to content

Commit 238dd8a

Browse files
committed
Merge branch 'gary/add-llm-obs-api' into gary/llmobs-java-sdk-integration
2 parents 71cbb06 + c75df4b commit 238dd8a

File tree

5 files changed

+371
-0
lines changed

5 files changed

+371
-0
lines changed

dd-trace-api/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ excludedClassesCoverage += [
3131
'datadog.trace.api.profiling.ProfilingScope',
3232
'datadog.trace.api.profiling.ProfilingContext',
3333
'datadog.trace.api.profiling.ProfilingContextAttribute.NoOp',
34+
'datadog.trace.api.llmobs.LLMObs',
35+
'datadog.trace.api.llmobs.LLMObs.LLMMessage',
36+
'datadog.trace.api.llmobs.LLMObs.ToolCall',
37+
'datadog.trace.api.llmobs.LLMObsSpan',
38+
'datadog.trace.api.llmobs.noop.NoOpLLMObsSpan',
39+
'datadog.trace.api.llmobs.noop.NoOpLLMObsSpanFactory',
3440
'datadog.trace.api.experimental.DataStreamsCheckpointer',
3541
'datadog.trace.api.experimental.DataStreamsCheckpointer.NoOp',
3642
'datadog.trace.api.experimental.DataStreamsContextCarrier',
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package datadog.trace.api.llmobs;
2+
3+
import datadog.trace.api.llmobs.noop.NoOpLLMObsSpanFactory;
4+
import java.util.List;
5+
import java.util.Map;
6+
import javax.annotation.Nullable;
7+
8+
public class LLMObs {
9+
private static LLMObsSpanFactory SPAN_FACTORY = NoOpLLMObsSpanFactory.INSTANCE;
10+
11+
public static LLMObsSpan startLLMSpan(
12+
String spanName,
13+
String modelName,
14+
String modelProvider,
15+
@Nullable String mlApp,
16+
@Nullable String sessionID) {
17+
18+
return SPAN_FACTORY.startLLMSpan(spanName, modelName, modelProvider, mlApp, sessionID);
19+
}
20+
21+
public static LLMObsSpan startAgentSpan(
22+
String spanName, @Nullable String mlApp, @Nullable String sessionID) {
23+
24+
return SPAN_FACTORY.startAgentSpan(spanName, mlApp, sessionID);
25+
}
26+
27+
public static LLMObsSpan startToolSpan(
28+
String spanName, @Nullable String mlApp, @Nullable String sessionID) {
29+
30+
return SPAN_FACTORY.startToolSpan(spanName, mlApp, sessionID);
31+
}
32+
33+
public static LLMObsSpan startTaskSpan(
34+
String spanName, @Nullable String mlApp, @Nullable String sessionID) {
35+
36+
return SPAN_FACTORY.startTaskSpan(spanName, mlApp, sessionID);
37+
}
38+
39+
public static LLMObsSpan startWorkflowSpan(
40+
String spanName, @Nullable String mlApp, @Nullable String sessionID) {
41+
42+
return SPAN_FACTORY.startWorkflowSpan(spanName, mlApp, sessionID);
43+
}
44+
45+
public interface LLMObsSpanFactory {
46+
LLMObsSpan startLLMSpan(
47+
String spanName,
48+
String modelName,
49+
String modelProvider,
50+
@Nullable String mlApp,
51+
@Nullable String sessionID);
52+
53+
LLMObsSpan startAgentSpan(String spanName, @Nullable String mlApp, @Nullable String sessionID);
54+
55+
LLMObsSpan startToolSpan(String spanName, @Nullable String mlApp, @Nullable String sessionID);
56+
57+
LLMObsSpan startTaskSpan(String spanName, @Nullable String mlApp, @Nullable String sessionID);
58+
59+
LLMObsSpan startWorkflowSpan(
60+
String spanName, @Nullable String mlApp, @Nullable String sessionID);
61+
}
62+
63+
public static class ToolCall {
64+
private String name;
65+
private String type;
66+
private String toolID;
67+
private Map<String, Object> arguments;
68+
69+
public ToolCall(String name, String type, String toolID, Map<String, Object> arguments) {
70+
this.name = name;
71+
this.type = type;
72+
this.toolID = toolID;
73+
this.arguments = arguments;
74+
}
75+
76+
public String getName() {
77+
return name;
78+
}
79+
80+
public String getType() {
81+
return type;
82+
}
83+
84+
public String getToolID() {
85+
return toolID;
86+
}
87+
88+
public Map<String, Object> getArguments() {
89+
return arguments;
90+
}
91+
}
92+
93+
public static class LLMMessage {
94+
private String role;
95+
private String content;
96+
private List<ToolCall> toolCalls;
97+
98+
public LLMMessage(String role, String content, List<ToolCall> toolCalls) {
99+
this.role = role;
100+
this.content = content;
101+
this.toolCalls = toolCalls;
102+
}
103+
104+
public LLMMessage(String role, String content) {
105+
this.role = role;
106+
this.content = content;
107+
}
108+
109+
public String getRole() {
110+
return role;
111+
}
112+
113+
public String getContent() {
114+
return content;
115+
}
116+
117+
public List<ToolCall> getToolCalls() {
118+
return toolCalls;
119+
}
120+
}
121+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package datadog.trace.api.llmobs;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
/** This interface represent an individual LLM Obs span. */
7+
public interface LLMObsSpan {
8+
9+
/**
10+
* Annotate the span with inputs and outputs for LLM spans
11+
*
12+
* @param inputMessages The input messages of the span in the form of a list
13+
* @param outputMessages The output messages of the span in the form of a list
14+
*/
15+
void annotateIO(List<LLMObs.LLMMessage> inputMessages, List<LLMObs.LLMMessage> outputMessages);
16+
17+
/**
18+
* Annotate the span with inputs and outputs
19+
*
20+
* @param inputData The input data of the span in the form of a string
21+
* @param outputData The output data of the span in the form of a string
22+
*/
23+
void annotateIO(String inputData, String outputData);
24+
25+
/**
26+
* Annotate the span with metadata
27+
*
28+
* @param metadata A map of JSON serializable key-value pairs that contains metadata information
29+
* relevant to the input or output operation described by the span
30+
*/
31+
void setMetadata(Map<String, Object> metadata);
32+
33+
/**
34+
* Annotate the span with metrics
35+
*
36+
* @param metrics A map of JSON serializable keys and numeric values that users can add as metrics
37+
* relevant to the operation described by the span (input_tokens, output_tokens, total_tokens,
38+
* etc.).
39+
*/
40+
void setMetrics(Map<String, Number> metrics);
41+
42+
/**
43+
* Annotate the span with a single metric key value pair for the span’s context (number of tokens
44+
* document length, etc).
45+
*
46+
* @param key the name of the metric
47+
* @param value the value of the metric
48+
*/
49+
void setMetric(CharSequence key, int value);
50+
51+
/**
52+
* Annotate the span with a single metric key value pair for the span’s context (number of tokens
53+
* document length, etc).
54+
*
55+
* @param key the name of the metric
56+
* @param value the value of the metric
57+
*/
58+
void setMetric(CharSequence key, long value);
59+
60+
/**
61+
* Annotate the span with a single metric key value pair for the span’s context (number of tokens
62+
* document length, etc).
63+
*
64+
* @param key the name of the metric
65+
* @param value the value of the metric
66+
*/
67+
void setMetric(CharSequence key, double value);
68+
69+
/**
70+
* Annotate the span with tags
71+
*
72+
* @param tags An map of JSON serializable key-value pairs that users can add as tags regarding
73+
* the span’s context (session, environment, system, versioning, etc.).
74+
*/
75+
void setTags(Map<String, Object> tags);
76+
77+
/**
78+
* Annotate the span with a single tag key value pair as a tag regarding the span’s context
79+
* (session, environment, system, versioning, etc.).
80+
*
81+
* @param key the key of the tag
82+
* @param value the value of the tag
83+
*/
84+
void setTag(String key, String value);
85+
86+
/**
87+
* Annotate the span with a single tag key value pair as a tag regarding the span’s context
88+
* (session, environment, system, versioning, etc.).
89+
*
90+
* @param key the key of the tag
91+
* @param value the value of the tag
92+
*/
93+
void setTag(String key, boolean value);
94+
95+
/**
96+
* Annotate the span with a single tag key value pair as a tag regarding the span’s context
97+
* (session, environment, system, versioning, etc.).
98+
*
99+
* @param key the key of the tag
100+
* @param value the value of the tag
101+
*/
102+
void setTag(String key, int value);
103+
104+
/**
105+
* Annotate the span with a single tag key value pair as a tag regarding the span’s context
106+
* (session, environment, system, versioning, etc.).
107+
*
108+
* @param key the key of the tag
109+
* @param value the value of the tag
110+
*/
111+
void setTag(String key, long value);
112+
113+
/**
114+
* Annotate the span with a single tag key value pair as a tag regarding the span’s context
115+
* (session, environment, system, versioning, etc.).
116+
*
117+
* @param key the key of the tag
118+
* @param value the value of the tag
119+
*/
120+
void setTag(String key, double value);
121+
122+
/**
123+
* Annotate the span to indicate that an error occurred
124+
*
125+
* @param error whether an error occurred
126+
*/
127+
void setError(boolean error);
128+
129+
/**
130+
* Annotate the span with an error message
131+
*
132+
* @param errorMessage the message of the error
133+
*/
134+
void setErrorMessage(String errorMessage);
135+
136+
/**
137+
* Annotate the span with a throwable
138+
*
139+
* @param throwable the errored throwable
140+
*/
141+
void addThrowable(Throwable throwable);
142+
143+
/** Finishes (closes) a span */
144+
void finish();
145+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package datadog.trace.api.llmobs.noop;
2+
3+
import datadog.trace.api.llmobs.LLMObs;
4+
import datadog.trace.api.llmobs.LLMObsSpan;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class NoOpLLMObsSpan implements LLMObsSpan {
9+
public static final LLMObsSpan INSTANCE = new NoOpLLMObsSpan();
10+
11+
@Override
12+
public void annotateIO(List<LLMObs.LLMMessage> inputData, List<LLMObs.LLMMessage> outputData) {}
13+
14+
@Override
15+
public void annotateIO(String inputData, String outputData) {}
16+
17+
@Override
18+
public void setMetadata(Map<String, Object> metadata) {}
19+
20+
@Override
21+
public void setMetrics(Map<String, Number> metrics) {}
22+
23+
@Override
24+
public void setMetric(CharSequence key, int value) {}
25+
26+
@Override
27+
public void setMetric(CharSequence key, long value) {}
28+
29+
@Override
30+
public void setMetric(CharSequence key, double value) {}
31+
32+
@Override
33+
public void setTags(Map<String, Object> tags) {}
34+
35+
@Override
36+
public void setTag(String key, String value) {}
37+
38+
@Override
39+
public void setTag(String key, boolean value) {}
40+
41+
@Override
42+
public void setTag(String key, int value) {}
43+
44+
@Override
45+
public void setTag(String key, long value) {}
46+
47+
@Override
48+
public void setTag(String key, double value) {}
49+
50+
@Override
51+
public void setError(boolean error) {}
52+
53+
@Override
54+
public void setErrorMessage(String errorMessage) {}
55+
56+
@Override
57+
public void addThrowable(Throwable throwable) {}
58+
59+
@Override
60+
public void finish() {}
61+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package datadog.trace.api.llmobs.noop;
2+
3+
import datadog.trace.api.llmobs.LLMObs;
4+
import datadog.trace.api.llmobs.LLMObsSpan;
5+
import javax.annotation.Nullable;
6+
7+
public class NoOpLLMObsSpanFactory implements LLMObs.LLMObsSpanFactory {
8+
public static final NoOpLLMObsSpanFactory INSTANCE = new NoOpLLMObsSpanFactory();
9+
10+
public LLMObsSpan startLLMSpan(
11+
String spanName,
12+
String modelName,
13+
String modelProvider,
14+
@Nullable String mlApp,
15+
@Nullable String sessionID) {
16+
return NoOpLLMObsSpan.INSTANCE;
17+
}
18+
19+
public LLMObsSpan startAgentSpan(
20+
String spanName, @Nullable String mlApp, @Nullable String sessionID) {
21+
return NoOpLLMObsSpan.INSTANCE;
22+
}
23+
24+
public LLMObsSpan startToolSpan(
25+
String spanName, @Nullable String mlApp, @Nullable String sessionID) {
26+
return NoOpLLMObsSpan.INSTANCE;
27+
}
28+
29+
public LLMObsSpan startTaskSpan(
30+
String spanName, @Nullable String mlApp, @Nullable String sessionID) {
31+
return NoOpLLMObsSpan.INSTANCE;
32+
}
33+
34+
public LLMObsSpan startWorkflowSpan(
35+
String spanName, @Nullable String mlApp, @Nullable String sessionID) {
36+
return NoOpLLMObsSpan.INSTANCE;
37+
}
38+
}

0 commit comments

Comments
 (0)