Skip to content

Commit c1f63dc

Browse files
committed
add llm message class to support llm spans
1 parent cb95fbb commit c1f63dc

File tree

4 files changed

+84
-8
lines changed

4 files changed

+84
-8
lines changed

dd-trace-api/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ excludedClassesCoverage += [
3232
'datadog.trace.api.profiling.ProfilingContext',
3333
'datadog.trace.api.profiling.ProfilingContextAttribute.NoOp',
3434
'datadog.trace.api.llmobs.LLMObs',
35+
'datadog.trace.api.llmobs.LLMObs.LLMMessage',
36+
'datadog.trace.api.llmobs.LLMObs.ToolCall',
3537
'datadog.trace.api.llmobs.LLMObsSpan',
3638
'datadog.trace.api.llmobs.noop.NoOpLLMObsSpan',
3739
'datadog.trace.api.llmobs.noop.NoOpLLMObsSpanFactory',

dd-trace-api/src/main/java/datadog/trace/api/llmobs/LLMObs.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package datadog.trace.api.llmobs;
22

33
import datadog.trace.api.llmobs.noop.NoOpLLMObsSpanFactory;
4+
import java.util.List;
5+
import java.util.Map;
46
import javax.annotation.Nullable;
57

68
public class LLMObs {
9+
private LLMObs() {}
10+
711
private static LLMObsSpanFactory SPAN_FACTORY = NoOpLLMObsSpanFactory.INSTANCE;
812

913
public static LLMObsSpan startLLMSpan(
@@ -57,4 +61,76 @@ LLMObsSpan startLLMSpan(
5761
LLMObsSpan startWorkflowSpan(
5862
String spanName, @Nullable String mlApp, @Nullable String sessionID);
5963
}
64+
65+
public static class ToolCall {
66+
private String name;
67+
private String type;
68+
private String toolID;
69+
private Map<String, Object> arguments;
70+
71+
public static ToolCall from(
72+
String name, String type, String toolID, Map<String, Object> arguments) {
73+
return new ToolCall(name, type, toolID, arguments);
74+
}
75+
76+
private ToolCall(String name, String type, String toolID, Map<String, Object> arguments) {
77+
this.name = name;
78+
this.type = type;
79+
this.toolID = toolID;
80+
this.arguments = arguments;
81+
}
82+
83+
public String getName() {
84+
return name;
85+
}
86+
87+
public String getType() {
88+
return type;
89+
}
90+
91+
public String getToolID() {
92+
return toolID;
93+
}
94+
95+
public Map<String, Object> getArguments() {
96+
return arguments;
97+
}
98+
}
99+
100+
public static class LLMMessage {
101+
private String role;
102+
private String content;
103+
private List<ToolCall> toolCalls;
104+
105+
public static LLMMessage from(String role, String content, List<ToolCall> toolCalls) {
106+
return new LLMMessage(role, content, toolCalls);
107+
}
108+
109+
public static LLMMessage from(String role, String content) {
110+
return new LLMMessage(role, content);
111+
}
112+
113+
private LLMMessage(String role, String content, List<ToolCall> toolCalls) {
114+
this.role = role;
115+
this.content = content;
116+
this.toolCalls = toolCalls;
117+
}
118+
119+
private LLMMessage(String role, String content) {
120+
this.role = role;
121+
this.content = content;
122+
}
123+
124+
public String getRole() {
125+
return role;
126+
}
127+
128+
public String getContent() {
129+
return content;
130+
}
131+
132+
public List<ToolCall> getToolCalls() {
133+
return toolCalls;
134+
}
135+
}
60136
}

dd-trace-api/src/main/java/datadog/trace/api/llmobs/LLMObsSpan.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
public interface LLMObsSpan {
88

99
/**
10-
* Annotate the span with inputs and outputs
10+
* Annotate the span with inputs and outputs for LLM spans
1111
*
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
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
1614
*/
17-
void annotateIO(List<Map<String, Object>> inputData, List<Map<String, Object>> outputData);
15+
void annotateIO(List<LLMObs.LLMMessage> inputMessages, List<LLMObs.LLMMessage> outputMessages);
1816

1917
/**
2018
* Annotate the span with inputs and outputs

dd-trace-api/src/main/java/datadog/trace/api/llmobs/noop/NoOpLLMObsSpan.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.trace.api.llmobs.noop;
22

3+
import datadog.trace.api.llmobs.LLMObs;
34
import datadog.trace.api.llmobs.LLMObsSpan;
45
import java.util.List;
56
import java.util.Map;
@@ -8,8 +9,7 @@ public class NoOpLLMObsSpan implements LLMObsSpan {
89
public static final LLMObsSpan INSTANCE = new NoOpLLMObsSpan();
910

1011
@Override
11-
public void annotateIO(
12-
List<Map<String, Object>> inputData, List<Map<String, Object>> outputData) {}
12+
public void annotateIO(List<LLMObs.LLMMessage> inputData, List<LLMObs.LLMMessage> outputData) {}
1313

1414
@Override
1515
public void annotateIO(String inputData, String outputData) {}

0 commit comments

Comments
 (0)