-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathLLMObsSystem.java
More file actions
223 lines (191 loc) · 7.29 KB
/
Copy pathLLMObsSystem.java
File metadata and controls
223 lines (191 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package datadog.trace.llmobs;
import datadog.communication.ddagent.SharedCommunicationObjects;
import datadog.trace.api.Config;
import datadog.trace.api.llmobs.LLMObs;
import datadog.trace.api.llmobs.LLMObsSpan;
import datadog.trace.api.llmobs.LLMObsTags;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import datadog.trace.llmobs.domain.DDLLMObsSpan;
import datadog.trace.llmobs.domain.LLMObsEval;
import datadog.trace.llmobs.domain.LLMObsInternal;
import java.lang.instrument.Instrumentation;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LLMObsSystem {
private static final Logger LOGGER = LoggerFactory.getLogger(LLMObsSystem.class);
private static final String CUSTOM_MODEL_VAL = "custom";
public static void start(Instrumentation inst, SharedCommunicationObjects sco) {
Config config = Config.get();
if (!config.isLlmObsEnabled()) {
LOGGER.debug("LLM Observability is disabled");
return;
}
sco.createRemaining(config);
LLMObsInternal.setLLMObsSpanFactory(
new LLMObsManualSpanFactory(config.getLlmObsMlApp(), config.getServiceName()));
String mlApp = config.getLlmObsMlApp();
LLMObsInternal.setLLMObsSpanFactory(
new LLMObsManualSpanFactory(mlApp, config.getServiceName()));
LLMObsInternal.setLLMObsEvalProcessor(new LLMObsCustomEvalProcessor(mlApp, sco, config));
}
private static class LLMObsCustomEvalProcessor implements LLMObs.LLMObsEvalProcessor {
private final String defaultMLApp;
private final EvalProcessingWorker evalProcessingWorker;
public LLMObsCustomEvalProcessor(
String defaultMLApp, SharedCommunicationObjects sco, Config config) {
this.defaultMLApp = defaultMLApp;
this.evalProcessingWorker =
new EvalProcessingWorker(1024, 100, TimeUnit.MILLISECONDS, sco, config);
this.evalProcessingWorker.start();
}
@Override
public void SubmitEvaluation(
LLMObsSpan llmObsSpan, String label, double scoreValue, Map<String, Object> tags) {
SubmitEvaluation(llmObsSpan, label, scoreValue, defaultMLApp, tags);
}
@Override
public void SubmitEvaluation(
LLMObsSpan llmObsSpan,
String label,
double scoreValue,
String mlApp,
Map<String, Object> tags) {
if (llmObsSpan == null) {
LOGGER.error("null llm obs span provided, eval not recorded");
return;
}
if (mlApp == null || mlApp.isEmpty()) {
mlApp = defaultMLApp;
}
String traceID = llmObsSpan.getTraceId().toHexString();
long spanID = llmObsSpan.getSpanId();
LLMObsEval.Score score =
new LLMObsEval.Score(
traceID, spanID, System.currentTimeMillis(), mlApp, label, tags, scoreValue);
if (!this.evalProcessingWorker.addToQueue(score)) {
LOGGER.warn(
"queue full, failed to add score eval, ml_app={}, trace_id={}, span_id={}, label={}",
mlApp,
traceID,
spanID,
label);
}
}
@Override
public void SubmitEvaluation(
LLMObsSpan llmObsSpan, String label, String categoricalValue, Map<String, Object> tags) {
SubmitEvaluation(llmObsSpan, label, categoricalValue, defaultMLApp, tags);
}
@Override
public void SubmitEvaluation(
LLMObsSpan llmObsSpan,
String label,
String categoricalValue,
String mlApp,
Map<String, Object> tags) {
if (llmObsSpan == null) {
LOGGER.error("null llm obs span provided, eval not recorded");
return;
}
if (mlApp == null || mlApp.isEmpty()) {
mlApp = defaultMLApp;
}
String traceID = llmObsSpan.getTraceId().toHexString();
long spanID = llmObsSpan.getSpanId();
LLMObsEval.Categorical category =
new LLMObsEval.Categorical(
traceID, spanID, System.currentTimeMillis(), mlApp, label, tags, categoricalValue);
if (!this.evalProcessingWorker.addToQueue(category)) {
LOGGER.warn(
"queue full, failed to add categorical eval, ml_app={}, trace_id={}, span_id={}, label={}",
mlApp,
traceID,
spanID,
label);
}
}
}
private static class LLMObsManualSpanFactory implements LLMObs.LLMObsSpanFactory {
private final String serviceName;
private final String defaultMLApp;
public LLMObsManualSpanFactory(String defaultMLApp, String serviceName) {
this.defaultMLApp = defaultMLApp;
this.serviceName = serviceName;
}
@Override
public LLMObsSpan startLLMSpan(
String spanName,
String modelName,
String modelProvider,
@Nullable String mlApp,
@Nullable String sessionId) {
DDLLMObsSpan span =
new DDLLMObsSpan(
Tags.LLMOBS_LLM_SPAN_KIND, spanName, getMLApp(mlApp), sessionId, serviceName);
if (modelName == null || modelName.isEmpty()) {
modelName = CUSTOM_MODEL_VAL;
}
span.setTag(LLMObsTags.MODEL_NAME, modelName);
if (modelProvider == null || modelProvider.isEmpty()) {
modelProvider = CUSTOM_MODEL_VAL;
}
span.setTag(LLMObsTags.MODEL_PROVIDER, modelProvider);
return span;
}
@Override
public LLMObsSpan startAgentSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionId) {
return new DDLLMObsSpan(
Tags.LLMOBS_AGENT_SPAN_KIND, spanName, getMLApp(mlApp), sessionId, serviceName);
}
@Override
public LLMObsSpan startToolSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionId) {
return new DDLLMObsSpan(
Tags.LLMOBS_TOOL_SPAN_KIND, spanName, getMLApp(mlApp), sessionId, serviceName);
}
@Override
public LLMObsSpan startTaskSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionId) {
return new DDLLMObsSpan(
Tags.LLMOBS_TASK_SPAN_KIND, spanName, getMLApp(mlApp), sessionId, serviceName);
}
@Override
public LLMObsSpan startWorkflowSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionId) {
return new DDLLMObsSpan(
Tags.LLMOBS_WORKFLOW_SPAN_KIND, spanName, getMLApp(mlApp), sessionId, serviceName);
}
@Override
public LLMObsSpan startEmbeddingSpan(
String spanName,
@Nullable String mlApp,
@Nullable String modelProvider,
@Nullable String modelName,
@Nullable String sessionId) {
if (modelProvider == null) {
modelProvider = "custom";
}
DDLLMObsSpan embeddingSpan =
new DDLLMObsSpan(
Tags.LLMOBS_EMBEDDING_SPAN_KIND, spanName, getMLApp(mlApp), sessionId, serviceName);
embeddingSpan.setTag(LLMObsTags.MODEL_PROVIDER, modelProvider);
embeddingSpan.setTag(LLMObsTags.MODEL_NAME, modelName);
return embeddingSpan;
}
public LLMObsSpan startRetrievalSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionId) {
return new DDLLMObsSpan(
Tags.LLMOBS_RETRIEVAL_SPAN_KIND, spanName, getMLApp(mlApp), sessionId, serviceName);
}
private String getMLApp(String mlApp) {
if (mlApp == null || mlApp.isEmpty()) {
return defaultMLApp;
}
return mlApp;
}
}
}