-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathLLMObs.java
More file actions
321 lines (253 loc) · 8.8 KB
/
Copy pathLLMObs.java
File metadata and controls
321 lines (253 loc) · 8.8 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package datadog.trace.api.llmobs;
import datadog.trace.api.llmobs.noop.NoOpLLMObsEvalProcessor;
import datadog.trace.api.llmobs.noop.NoOpLLMObsSpanFactory;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
public class LLMObs {
protected LLMObs() {}
protected static LLMObsSpanFactory SPAN_FACTORY = NoOpLLMObsSpanFactory.INSTANCE;
protected static LLMObsEvalProcessor EVAL_PROCESSOR = NoOpLLMObsEvalProcessor.INSTANCE;
public static LLMObsSpan startLLMSpan(
String spanName,
String modelName,
String modelProvider,
@Nullable String mlApp,
@Nullable String sessionId) {
return SPAN_FACTORY.startLLMSpan(spanName, modelName, modelProvider, mlApp, sessionId);
}
public static LLMObsSpan startAgentSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionId) {
return SPAN_FACTORY.startAgentSpan(spanName, mlApp, sessionId);
}
public static LLMObsSpan startToolSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionId) {
return SPAN_FACTORY.startToolSpan(spanName, mlApp, sessionId);
}
public static LLMObsSpan startTaskSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionId) {
return SPAN_FACTORY.startTaskSpan(spanName, mlApp, sessionId);
}
public static LLMObsSpan startWorkflowSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionId) {
return SPAN_FACTORY.startWorkflowSpan(spanName, mlApp, sessionId);
}
public static LLMObsSpan startEmbeddingSpan(
String spanName,
@Nullable String mlApp,
@Nullable String modelProvider,
@Nullable String modelName,
@Nullable String sessionId) {
return SPAN_FACTORY.startEmbeddingSpan(spanName, mlApp, modelProvider, modelName, sessionId);
}
public static LLMObsSpan startRetrievalSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionId) {
return SPAN_FACTORY.startRetrievalSpan(spanName, mlApp, sessionId);
}
public static void SubmitEvaluation(
LLMObsSpan llmObsSpan, String label, String categoricalValue, Map<String, Object> tags) {
EVAL_PROCESSOR.SubmitEvaluation(llmObsSpan, label, categoricalValue, tags);
}
public static void SubmitEvaluation(
LLMObsSpan llmObsSpan,
String label,
String categoricalValue,
String mlApp,
Map<String, Object> tags) {
EVAL_PROCESSOR.SubmitEvaluation(llmObsSpan, label, categoricalValue, mlApp, tags);
}
public static void SubmitEvaluation(
LLMObsSpan llmObsSpan, String label, double scoreValue, Map<String, Object> tags) {
EVAL_PROCESSOR.SubmitEvaluation(llmObsSpan, label, scoreValue, tags);
}
public static void SubmitEvaluation(
LLMObsSpan llmObsSpan,
String label,
double scoreValue,
String mlApp,
Map<String, Object> tags) {
EVAL_PROCESSOR.SubmitEvaluation(llmObsSpan, label, scoreValue, mlApp, tags);
}
public interface LLMObsSpanFactory {
LLMObsSpan startLLMSpan(
String spanName,
String modelName,
String modelProvider,
@Nullable String mlApp,
@Nullable String sessionId);
LLMObsSpan startAgentSpan(String spanName, @Nullable String mlApp, @Nullable String sessionId);
LLMObsSpan startToolSpan(String spanName, @Nullable String mlApp, @Nullable String sessionId);
LLMObsSpan startTaskSpan(String spanName, @Nullable String mlApp, @Nullable String sessionId);
LLMObsSpan startWorkflowSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionId);
LLMObsSpan startEmbeddingSpan(
String spanName,
@Nullable String mlApp,
@Nullable String modelProvider,
@Nullable String modelName,
@Nullable String sessionId);
LLMObsSpan startRetrievalSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionId);
}
public interface LLMObsEvalProcessor {
void SubmitEvaluation(
LLMObsSpan llmObsSpan, String label, double scoreValue, Map<String, Object> tags);
void SubmitEvaluation(
LLMObsSpan llmObsSpan,
String label,
double scoreValue,
String mlApp,
Map<String, Object> tags);
void SubmitEvaluation(
LLMObsSpan llmObsSpan, String label, String categoricalValue, Map<String, Object> tags);
void SubmitEvaluation(
LLMObsSpan llmObsSpan,
String label,
String categoricalValue,
String mlApp,
Map<String, Object> tags);
}
public static class ToolCall {
private String name;
private String type;
private String toolId;
private Map<String, Object> arguments;
public static ToolCall from(
String name, String type, String toolId, Map<String, Object> arguments) {
return new ToolCall(name, type, toolId, arguments);
}
private ToolCall(String name, String type, String toolId, Map<String, Object> arguments) {
this.name = name;
this.type = type;
this.toolId = toolId;
this.arguments = arguments;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getToolId() {
return toolId;
}
public Map<String, Object> getArguments() {
return arguments;
}
}
public static class ToolDefinition {
private String name;
private String description;
private Map<String, Object> schema;
private String version;
public static ToolDefinition from(String name) {
return new ToolDefinition(name, null, null, null);
}
public static ToolDefinition from(String name, String description) {
return new ToolDefinition(name, description, null, null);
}
public static ToolDefinition from(String name, String description, Map<String, Object> schema) {
return new ToolDefinition(name, description, schema, null);
}
public static ToolDefinition from(
String name, String description, Map<String, Object> schema, String version) {
return new ToolDefinition(name, description, schema, version);
}
private ToolDefinition(
String name, String description, Map<String, Object> schema, String version) {
this.name = name;
this.description = description;
this.schema = schema;
this.version = version;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public Map<String, Object> getSchema() {
return schema;
}
public String getVersion() {
return version;
}
}
public static class ToolResult {
private String name;
private String type;
private String toolId;
private String result;
public static ToolResult from(String name, String type, String toolId, String result) {
return new ToolResult(name, type, toolId, result);
}
private ToolResult(String name, String type, String toolId, String result) {
this.name = name;
this.type = type;
this.toolId = toolId;
this.result = result;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getToolId() {
return toolId;
}
public String getResult() {
return result;
}
}
public static class LLMMessage {
private String role;
private String content;
private List<ToolCall> toolCalls;
private List<ToolResult> toolResults;
public static LLMMessage from(String role, String content, List<ToolCall> toolCalls) {
return new LLMMessage(role, content, toolCalls, null);
}
public static LLMMessage from(
String role, String content, List<ToolCall> toolCalls, List<ToolResult> toolResults) {
return new LLMMessage(role, content, toolCalls, toolResults);
}
public static LLMMessage from(String role, String content) {
return new LLMMessage(role, content, null, null);
}
public static LLMMessage fromToolResults(String role, List<ToolResult> toolResults) {
return new LLMMessage(role, null, null, toolResults);
}
private LLMMessage(
String role, String content, List<ToolCall> toolCalls, List<ToolResult> toolResults) {
this.role = role;
this.content = content;
this.toolCalls = toolCalls;
this.toolResults = toolResults;
}
public String getRole() {
return role;
}
public String getContent() {
return content;
}
public List<ToolCall> getToolCalls() {
return toolCalls;
}
public List<ToolResult> getToolResults() {
return toolResults;
}
}
public static class Document {
private String text;
public static Document from(String text) {
return new Document(text);
}
private Document(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
}