Skip to content

Commit 77ddec8

Browse files
committed
add APIs for llm obs
1 parent 121f38f commit 77ddec8

File tree

5 files changed

+310
-0
lines changed

5 files changed

+310
-0
lines changed

dd-trace-api/build.gradle

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