Skip to content

Commit 40dcdfe

Browse files
committed
support llm messages with tool calls
1 parent 5163db5 commit 40dcdfe

File tree

1 file changed

+12
-37
lines changed
  • dd-java-agent/agent-llmobs/src/main/java/datadog/trace/llmobs/domain

1 file changed

+12
-37
lines changed

dd-java-agent/agent-llmobs/src/main/java/datadog/trace/llmobs/domain/DDLLMObsSpan.java

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
11
package datadog.trace.llmobs.domain;
22

33
import datadog.trace.api.DDSpanTypes;
4+
import datadog.trace.api.llmobs.LLMObs;
45
import datadog.trace.api.llmobs.LLMObsSpan;
56
import datadog.trace.api.llmobs.LLMObsTags;
67
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
78
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
89
import datadog.trace.bootstrap.instrumentation.api.Tags;
9-
import java.util.Arrays;
1010
import java.util.Collections;
1111
import java.util.HashMap;
12-
import java.util.HashSet;
1312
import java.util.List;
1413
import java.util.Map;
15-
import java.util.Set;
1614
import javax.annotation.Nonnull;
1715
import org.slf4j.Logger;
1816
import org.slf4j.LoggerFactory;
1917

2018
public class DDLLMObsSpan implements LLMObsSpan {
21-
22-
private enum State {
23-
VALID,
24-
INVALID_IO_MESSAGE_KEY
25-
}
26-
27-
private static final String MESSAGE_KEY_ROLE = "role";
28-
private static final String MESSAGE_KEY_CONTENT = "content";
29-
30-
private static final Set<String> VALID_MESSAGE_KEYS =
31-
new HashSet<>(Arrays.asList(MESSAGE_KEY_ROLE, MESSAGE_KEY_CONTENT));
19+
private static final String LLM_MESSAGE_UNKNOWN_ROLE = "unknown";
3220

3321
// Well known tags for LLM obs will be prefixed with _ml_obs_(tags|metrics).
3422
// Prefix for tags
@@ -92,35 +80,15 @@ public String toString() {
9280
+ this.span.getTag(SPAN_KIND);
9381
}
9482

95-
private static State validateIOMessages(List<Map<String, Object>> messages) {
96-
for (Map<String, Object> message : messages) {
97-
for (String key : message.keySet()) {
98-
if (!VALID_MESSAGE_KEYS.contains(key)) {
99-
return State.INVALID_IO_MESSAGE_KEY;
100-
}
101-
}
102-
}
103-
return State.VALID;
104-
}
105-
10683
@Override
107-
public void annotateIO(
108-
List<Map<String, Object>> inputData, List<Map<String, Object>> outputData) {
84+
public void annotateIO(List<LLMObs.LLMMessage> inputData, List<LLMObs.LLMMessage> outputData) {
10985
if (finished) {
11086
return;
11187
}
11288
if (inputData != null && !inputData.isEmpty()) {
113-
State inputState = validateIOMessages(inputData);
114-
if (validateIOMessages(inputData) != State.VALID) {
115-
LOGGER.debug("malformed/unexpected input message, state={}", inputState);
116-
}
11789
this.span.setTag(INPUT, inputData);
11890
}
11991
if (outputData != null && !outputData.isEmpty()) {
120-
State outputState = validateIOMessages(outputData);
121-
if (validateIOMessages(outputData) != State.VALID) {
122-
LOGGER.debug("malformed/unexpected output message, state={}", outputState);
123-
}
12492
this.span.setTag(OUTPUT, outputData);
12593
}
12694
}
@@ -130,24 +98,31 @@ public void annotateIO(String inputData, String outputData) {
13098
if (finished) {
13199
return;
132100
}
101+
boolean wrongSpanKind = false;
133102
if (inputData != null && !inputData.isEmpty()) {
134103
if (Tags.LLMOBS_LLM_SPAN_KIND.equals(this.spanKind)) {
104+
wrongSpanKind = true;
135105
annotateIO(
136-
Collections.singletonList(Collections.singletonMap(MESSAGE_KEY_CONTENT, inputData)),
106+
Collections.singletonList(new LLMObs.LLMMessage(LLM_MESSAGE_UNKNOWN_ROLE, inputData)),
137107
null);
138108
} else {
139109
this.span.setTag(INPUT, inputData);
140110
}
141111
}
142112
if (outputData != null && !outputData.isEmpty()) {
143113
if (Tags.LLMOBS_LLM_SPAN_KIND.equals(this.spanKind)) {
114+
wrongSpanKind = true;
144115
annotateIO(
145116
null,
146-
Collections.singletonList(Collections.singletonMap(MESSAGE_KEY_CONTENT, outputData)));
117+
Collections.singletonList(new LLMObs.LLMMessage(LLM_MESSAGE_UNKNOWN_ROLE, outputData)));
147118
} else {
148119
this.span.setTag(OUTPUT, outputData);
149120
}
150121
}
122+
if (wrongSpanKind) {
123+
LOGGER.warn(
124+
"the span being annotated is an LLM span, it is recommended to use the overload with List<LLMObs.LLMMessage> as arguments");
125+
}
151126
}
152127

153128
@Override

0 commit comments

Comments
 (0)