langwatch 0.1.0 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/copy-types.sh +17 -0
- package/dist/chunk-2I4YLOQY.mjs +736 -0
- package/dist/chunk-2I4YLOQY.mjs.map +1 -0
- package/dist/index.d.mts +352 -4
- package/dist/index.d.ts +352 -4
- package/dist/index.js +6505 -413
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +571 -359
- package/dist/index.mjs.map +1 -1
- package/dist/{utils-DDcm0z9v.d.mts → utils-CFtM8VVg.d.mts} +108 -31
- package/dist/{utils-DDcm0z9v.d.ts → utils-CFtM8VVg.d.ts} +108 -31
- package/dist/utils.d.mts +1 -2
- package/dist/utils.d.ts +1 -2
- package/dist/utils.js +437 -0
- package/dist/utils.js.map +1 -1
- package/dist/utils.mjs +3 -1
- package/example/README.md +3 -1
- package/example/app/(chat)/chat/[id]/page.tsx +1 -1
- package/example/app/(chat)/page.tsx +10 -5
- package/example/app/guardrails/page.tsx +26 -0
- package/example/app/langchain/page.tsx +27 -0
- package/example/app/langchain-rag/page.tsx +28 -0
- package/example/app/share/[id]/page.tsx +1 -1
- package/example/components/chat-list.tsx +1 -1
- package/example/components/chat-panel.tsx +1 -1
- package/example/components/header.tsx +39 -13
- package/example/components/prompt-form.tsx +1 -1
- package/example/components/stocks/stock-purchase.tsx +1 -1
- package/example/components/stocks/stocks.tsx +1 -1
- package/example/lib/chat/guardrails.tsx +181 -0
- package/example/lib/chat/langchain-rag.tsx +191 -0
- package/example/lib/chat/langchain.tsx +112 -0
- package/example/lib/chat/{actions.tsx → vercel-ai.tsx} +1 -1
- package/example/package-lock.json +289 -6
- package/example/package.json +1 -0
- package/package.json +13 -5
- package/src/evaluations.ts +219 -0
- package/src/index.test.ts +5 -0
- package/src/index.ts +190 -7
- package/src/langchain.ts +557 -0
- package/src/{helpers.ts → typeUtils.ts} +20 -2
- package/src/types.ts +7 -3
- package/src/utils.ts +25 -2
- package/ts-to-zod.config.js +2 -0
- package/dist/chunk-AP23NJ57.mjs +0 -296
- package/dist/chunk-AP23NJ57.mjs.map +0 -1
package/src/langchain.ts
ADDED
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
import type { AgentAction, AgentFinish } from "@langchain/core/agents";
|
|
2
|
+
import { BaseCallbackHandler } from "@langchain/core/callbacks/base";
|
|
3
|
+
import { type DocumentInterface } from "@langchain/core/documents";
|
|
4
|
+
import type { Serialized } from "@langchain/core/load/serializable";
|
|
5
|
+
import {
|
|
6
|
+
AIMessage,
|
|
7
|
+
AIMessageChunk,
|
|
8
|
+
FunctionMessage,
|
|
9
|
+
FunctionMessageChunk,
|
|
10
|
+
HumanMessage,
|
|
11
|
+
HumanMessageChunk,
|
|
12
|
+
SystemMessage,
|
|
13
|
+
SystemMessageChunk,
|
|
14
|
+
ToolMessage,
|
|
15
|
+
ToolMessageChunk,
|
|
16
|
+
mapChatMessagesToStoredMessages,
|
|
17
|
+
type BaseMessage,
|
|
18
|
+
type StoredMessage,
|
|
19
|
+
} from "@langchain/core/messages";
|
|
20
|
+
import type { ChatGeneration, LLMResult } from "@langchain/core/outputs";
|
|
21
|
+
import type { ChainValues } from "@langchain/core/utils/types";
|
|
22
|
+
import { stringify } from "javascript-stringify";
|
|
23
|
+
import {
|
|
24
|
+
type LangWatchRAGSpan,
|
|
25
|
+
type LangWatchSpan,
|
|
26
|
+
type LangWatchTrace,
|
|
27
|
+
} from ".";
|
|
28
|
+
import {
|
|
29
|
+
type RAGSpan,
|
|
30
|
+
type BaseSpan,
|
|
31
|
+
type ChatMessage,
|
|
32
|
+
type ChatRichContent,
|
|
33
|
+
type SpanInputOutput,
|
|
34
|
+
} from "./types";
|
|
35
|
+
|
|
36
|
+
export class LangWatchCallbackHandler extends BaseCallbackHandler {
|
|
37
|
+
name = "LangWatchCallbackHandler";
|
|
38
|
+
trace: LangWatchTrace;
|
|
39
|
+
spans: Record<string, LangWatchSpan> = {};
|
|
40
|
+
|
|
41
|
+
constructor({ trace }: { trace: LangWatchTrace }) {
|
|
42
|
+
super();
|
|
43
|
+
this.trace = trace;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async handleLLMStart(
|
|
47
|
+
llm: Serialized,
|
|
48
|
+
prompts: string[],
|
|
49
|
+
runId: string,
|
|
50
|
+
parentRunId?: string | undefined,
|
|
51
|
+
extraParams?: Record<string, unknown> | undefined,
|
|
52
|
+
_tags?: string[] | undefined,
|
|
53
|
+
metadata?: Record<string, unknown> | undefined,
|
|
54
|
+
name?: string
|
|
55
|
+
): Promise<void> {
|
|
56
|
+
this.spans[runId] = this.buildLLMSpan({
|
|
57
|
+
llm,
|
|
58
|
+
runId,
|
|
59
|
+
parentRunId,
|
|
60
|
+
input: {
|
|
61
|
+
type: "json",
|
|
62
|
+
value: prompts,
|
|
63
|
+
},
|
|
64
|
+
extraParams,
|
|
65
|
+
metadata,
|
|
66
|
+
name,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private buildLLMSpan({
|
|
71
|
+
llm,
|
|
72
|
+
runId,
|
|
73
|
+
parentRunId,
|
|
74
|
+
input,
|
|
75
|
+
extraParams,
|
|
76
|
+
metadata,
|
|
77
|
+
name,
|
|
78
|
+
}: {
|
|
79
|
+
llm: Serialized;
|
|
80
|
+
runId: string;
|
|
81
|
+
parentRunId?: string | undefined;
|
|
82
|
+
input: SpanInputOutput;
|
|
83
|
+
extraParams?: Record<string, unknown> | undefined;
|
|
84
|
+
metadata?: Record<string, unknown> | undefined;
|
|
85
|
+
name?: string | undefined;
|
|
86
|
+
}) {
|
|
87
|
+
try {
|
|
88
|
+
const parent = this.getParent(parentRunId);
|
|
89
|
+
|
|
90
|
+
const vendor = metadata?.ls_provider ?? llm.id.at(-2)?.toString();
|
|
91
|
+
const model =
|
|
92
|
+
metadata?.ls_model_name ?? (llm as any).kwargs?.model ?? "unknown";
|
|
93
|
+
|
|
94
|
+
const span = parent.startLLMSpan({
|
|
95
|
+
spanId: runId,
|
|
96
|
+
name: name ?? llm.id.at(-1)?.toString(),
|
|
97
|
+
input,
|
|
98
|
+
model: [vendor, model].filter((x) => x).join("/"),
|
|
99
|
+
params: {
|
|
100
|
+
temperature: (extraParams?.invocation_params as any)?.temperature,
|
|
101
|
+
...((extraParams?.invocation_params as any)?.functions
|
|
102
|
+
? { functions: (extraParams?.invocation_params as any)?.functions }
|
|
103
|
+
: {}),
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return span;
|
|
108
|
+
} catch (e) {
|
|
109
|
+
this.trace.client.emit("error", e);
|
|
110
|
+
throw e;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async handleChatModelStart(
|
|
115
|
+
llm: Serialized,
|
|
116
|
+
messages: BaseMessage[][],
|
|
117
|
+
runId: string,
|
|
118
|
+
parentRunId?: string | undefined,
|
|
119
|
+
extraParams?: Record<string, unknown> | undefined,
|
|
120
|
+
tags?: string[] | undefined,
|
|
121
|
+
metadata?: Record<string, unknown> | undefined,
|
|
122
|
+
name?: string
|
|
123
|
+
): Promise<void> {
|
|
124
|
+
this.spans[runId] = this.buildLLMSpan({
|
|
125
|
+
name,
|
|
126
|
+
llm,
|
|
127
|
+
runId,
|
|
128
|
+
parentRunId,
|
|
129
|
+
input: {
|
|
130
|
+
type: "chat_messages",
|
|
131
|
+
value: messages.flatMap(convertFromLangChainMessages),
|
|
132
|
+
},
|
|
133
|
+
extraParams,
|
|
134
|
+
metadata,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async handleNewToken(_token: string, runId: string): Promise<void> {
|
|
139
|
+
const span = this.spans[runId];
|
|
140
|
+
if (runId && span && !span.timestamps.firstTokenAt) {
|
|
141
|
+
span.update({
|
|
142
|
+
timestamps: { ...span.timestamps, firstTokenAt: Date.now() },
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async handleLLMEnd(
|
|
148
|
+
response: LLMResult,
|
|
149
|
+
runId: string,
|
|
150
|
+
_parentRunId?: string | undefined
|
|
151
|
+
): Promise<void> {
|
|
152
|
+
try {
|
|
153
|
+
const span = this.spans[runId];
|
|
154
|
+
if (!span) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const outputs: SpanInputOutput[] = [];
|
|
159
|
+
for (const generation of response.generations) {
|
|
160
|
+
// TODO: again, why the twice loop? Can OpenAI generate multiple chat outputs?
|
|
161
|
+
for (const generation_ of generation) {
|
|
162
|
+
if ("message" in generation_) {
|
|
163
|
+
outputs.push({
|
|
164
|
+
type: "chat_messages",
|
|
165
|
+
value: convertFromLangChainMessages([
|
|
166
|
+
(generation_ as ChatGeneration).message,
|
|
167
|
+
]),
|
|
168
|
+
});
|
|
169
|
+
} else if ("text" in generation_) {
|
|
170
|
+
outputs.push({
|
|
171
|
+
type: "text",
|
|
172
|
+
value: generation_.text,
|
|
173
|
+
});
|
|
174
|
+
} else {
|
|
175
|
+
outputs.push({
|
|
176
|
+
type: "text",
|
|
177
|
+
value: JSON.stringify(generation_),
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const output: SpanInputOutput | undefined =
|
|
184
|
+
outputs.length === 1
|
|
185
|
+
? outputs[0]
|
|
186
|
+
: { type: "list", value: outputs as any };
|
|
187
|
+
|
|
188
|
+
// Commenting it out because LangChain.js prompt and completion tokens is broken, this one doesn't work as it should with python,
|
|
189
|
+
// and response_metadata.prompt and response_metadata.completion is there but it's always 0. Better let our server count.
|
|
190
|
+
// const metrics = response.llmOutput?.token_usage
|
|
191
|
+
// ? {
|
|
192
|
+
// promptTokens: response.llmOutput.token_usage.prompt_tokens,
|
|
193
|
+
// completionTokens: response.llmOutput.token_usage.completion_tokens,
|
|
194
|
+
// }
|
|
195
|
+
// : undefined;
|
|
196
|
+
|
|
197
|
+
span.end({
|
|
198
|
+
output,
|
|
199
|
+
// ...(metrics ? { metrics } : {}),
|
|
200
|
+
});
|
|
201
|
+
} catch (e) {
|
|
202
|
+
this.trace.client.emit("error", e);
|
|
203
|
+
throw e;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async handleLLMError(
|
|
208
|
+
err: Error,
|
|
209
|
+
runId: string,
|
|
210
|
+
_parentRunId?: string | undefined
|
|
211
|
+
): Promise<void> {
|
|
212
|
+
this.errorSpan({ runId, error: err });
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async handleChainStart(
|
|
216
|
+
chain: Serialized,
|
|
217
|
+
inputs: ChainValues,
|
|
218
|
+
runId: string,
|
|
219
|
+
parentRunId?: string | undefined,
|
|
220
|
+
_tags?: string[] | undefined,
|
|
221
|
+
_metadata?: Record<string, unknown> | undefined,
|
|
222
|
+
_runType?: string,
|
|
223
|
+
name?: string
|
|
224
|
+
): Promise<void> {
|
|
225
|
+
this.spans[runId] = this.buildSpan({
|
|
226
|
+
type: "chain",
|
|
227
|
+
serialized: chain,
|
|
228
|
+
runId,
|
|
229
|
+
parentRunId,
|
|
230
|
+
input: inputs,
|
|
231
|
+
name,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async handleChainEnd(
|
|
236
|
+
output: ChainValues,
|
|
237
|
+
runId: string,
|
|
238
|
+
_parentRunId?: string | undefined
|
|
239
|
+
): Promise<void> {
|
|
240
|
+
this.endSpan({
|
|
241
|
+
runId,
|
|
242
|
+
output,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async handleChainError(
|
|
247
|
+
err: Error,
|
|
248
|
+
runId: string,
|
|
249
|
+
_parentRunId?: string | undefined,
|
|
250
|
+
_tags?: string[] | undefined,
|
|
251
|
+
_kwargs?: { inputs?: Record<string, unknown> | undefined } | undefined
|
|
252
|
+
): Promise<void> {
|
|
253
|
+
this.errorSpan({ runId, error: err });
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
async handleToolStart(
|
|
257
|
+
tool: Serialized,
|
|
258
|
+
input: string,
|
|
259
|
+
runId: string,
|
|
260
|
+
parentRunId?: string | undefined,
|
|
261
|
+
_tags?: string[] | undefined,
|
|
262
|
+
_metadata?: Record<string, unknown> | undefined,
|
|
263
|
+
name?: string
|
|
264
|
+
): Promise<void> {
|
|
265
|
+
this.spans[runId] = this.buildSpan({
|
|
266
|
+
type: "tool",
|
|
267
|
+
serialized: tool,
|
|
268
|
+
runId,
|
|
269
|
+
parentRunId,
|
|
270
|
+
input,
|
|
271
|
+
name,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async handleToolEnd(
|
|
276
|
+
output: string,
|
|
277
|
+
runId: string,
|
|
278
|
+
_parentRunId?: string | undefined
|
|
279
|
+
): Promise<void> {
|
|
280
|
+
this.endSpan({ runId, output });
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async handleToolError(
|
|
284
|
+
err: Error,
|
|
285
|
+
runId: string,
|
|
286
|
+
_parentRunId?: string | undefined,
|
|
287
|
+
_tags?: string[] | undefined
|
|
288
|
+
): Promise<void> {
|
|
289
|
+
this.errorSpan({ runId, error: err });
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async handleRetrieverStart(
|
|
293
|
+
retriever: Serialized,
|
|
294
|
+
query: string,
|
|
295
|
+
runId: string,
|
|
296
|
+
parentRunId?: string | undefined,
|
|
297
|
+
_tags?: string[] | undefined,
|
|
298
|
+
_metadata?: Record<string, unknown> | undefined,
|
|
299
|
+
name?: string | undefined
|
|
300
|
+
) {
|
|
301
|
+
try {
|
|
302
|
+
const parent = this.getParent(parentRunId);
|
|
303
|
+
|
|
304
|
+
this.spans[runId] = parent.startRAGSpan({
|
|
305
|
+
spanId: runId,
|
|
306
|
+
name: name ?? retriever.name ?? retriever.id.at(-1)?.toString(),
|
|
307
|
+
input: this.autoconvertTypedValues(query),
|
|
308
|
+
});
|
|
309
|
+
} catch (e) {
|
|
310
|
+
this.trace.client.emit("error", e);
|
|
311
|
+
throw e;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async handleRetrieverEnd(
|
|
316
|
+
documents: DocumentInterface<Record<string, any>>[],
|
|
317
|
+
runId: string,
|
|
318
|
+
_parentRunId?: string | undefined,
|
|
319
|
+
_tags?: string[] | undefined
|
|
320
|
+
) {
|
|
321
|
+
try {
|
|
322
|
+
const contexts: RAGSpan["contexts"] = documents.map((doc) => ({
|
|
323
|
+
content: doc.pageContent,
|
|
324
|
+
...(doc.metadata.source ? { documentId: doc.metadata.source } : {}),
|
|
325
|
+
}));
|
|
326
|
+
|
|
327
|
+
const span = this.spans[runId] as LangWatchRAGSpan;
|
|
328
|
+
if (!span) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
span.end({
|
|
333
|
+
contexts,
|
|
334
|
+
output: this.autoconvertTypedValues(documents),
|
|
335
|
+
});
|
|
336
|
+
} catch (e) {
|
|
337
|
+
this.trace.client.emit("error", e);
|
|
338
|
+
throw e;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
async handleRetrieverError(
|
|
343
|
+
err: Error,
|
|
344
|
+
runId: string,
|
|
345
|
+
_parentRunId?: string | undefined,
|
|
346
|
+
_tags?: string[] | undefined
|
|
347
|
+
) {
|
|
348
|
+
this.errorSpan({ runId, error: err });
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async handleAgentAction(
|
|
352
|
+
_action: AgentAction,
|
|
353
|
+
runId: string,
|
|
354
|
+
_parentRunId?: string | undefined,
|
|
355
|
+
_tags?: string[] | undefined
|
|
356
|
+
): Promise<void> {
|
|
357
|
+
const span = this.spans[runId];
|
|
358
|
+
if (!span) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
span.update({
|
|
363
|
+
type: "agent",
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
async handleAgentEnd(
|
|
368
|
+
action: AgentFinish,
|
|
369
|
+
runId: string,
|
|
370
|
+
_parentRunId?: string | undefined,
|
|
371
|
+
_tags?: string[] | undefined
|
|
372
|
+
): Promise<void> {
|
|
373
|
+
this.endSpan({
|
|
374
|
+
runId,
|
|
375
|
+
output: action.returnValues,
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
private buildSpan({
|
|
380
|
+
type,
|
|
381
|
+
serialized,
|
|
382
|
+
runId,
|
|
383
|
+
parentRunId,
|
|
384
|
+
input,
|
|
385
|
+
name,
|
|
386
|
+
}: {
|
|
387
|
+
type: BaseSpan["type"];
|
|
388
|
+
serialized: Serialized;
|
|
389
|
+
runId: string;
|
|
390
|
+
parentRunId?: string | undefined;
|
|
391
|
+
input: unknown;
|
|
392
|
+
name?: string | undefined;
|
|
393
|
+
}) {
|
|
394
|
+
try {
|
|
395
|
+
const parent = this.getParent(parentRunId);
|
|
396
|
+
|
|
397
|
+
const span = parent.startSpan({
|
|
398
|
+
spanId: runId,
|
|
399
|
+
type,
|
|
400
|
+
name: name ?? serialized.name ?? serialized.id.at(-1)?.toString(),
|
|
401
|
+
input: this.autoconvertTypedValues(input),
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
return span;
|
|
405
|
+
} catch (e) {
|
|
406
|
+
this.trace.client.emit("error", e);
|
|
407
|
+
throw e;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
private endSpan({ runId, output }: { runId: string; output: unknown }): void {
|
|
412
|
+
try {
|
|
413
|
+
const span = this.spans[runId];
|
|
414
|
+
if (!span) {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
span.end({
|
|
419
|
+
output: this.autoconvertTypedValues(output),
|
|
420
|
+
});
|
|
421
|
+
} catch (e) {
|
|
422
|
+
this.trace.client.emit("error", e);
|
|
423
|
+
throw e;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
private errorSpan({ runId, error }: { runId: string; error: Error }): void {
|
|
428
|
+
const span = this.spans[runId];
|
|
429
|
+
if (!span) {
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
span.end({
|
|
434
|
+
error,
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
private autoconvertTypedValues(value: any): SpanInputOutput | undefined {
|
|
439
|
+
if (
|
|
440
|
+
!value ||
|
|
441
|
+
(typeof value === "object" && Object.keys(value).length === 0)
|
|
442
|
+
) {
|
|
443
|
+
return undefined;
|
|
444
|
+
}
|
|
445
|
+
if (typeof value === "string") {
|
|
446
|
+
return { type: "text", value };
|
|
447
|
+
}
|
|
448
|
+
try {
|
|
449
|
+
JSON.stringify(value);
|
|
450
|
+
return { type: "json", value };
|
|
451
|
+
} catch (e) {
|
|
452
|
+
return { type: "text", value: stringify(value) ?? value.toString() };
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
private getParent(
|
|
457
|
+
parentRunId?: string | undefined
|
|
458
|
+
): LangWatchSpan | LangWatchTrace {
|
|
459
|
+
return (
|
|
460
|
+
(parentRunId
|
|
461
|
+
? this.spans[parentRunId]
|
|
462
|
+
: this.spans[Object.keys(this.spans).at(-1) ?? ""]) ?? this.trace
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export const convertFromLangChainMessages = (
|
|
468
|
+
messages: BaseMessage[]
|
|
469
|
+
): ChatMessage[] => {
|
|
470
|
+
const chatMessages: ChatMessage[] = [];
|
|
471
|
+
for (const message of messages) {
|
|
472
|
+
chatMessages.push(convertFromLangChainMessage(message));
|
|
473
|
+
}
|
|
474
|
+
return chatMessages;
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
const convertFromLangChainMessage = (
|
|
478
|
+
message: BaseMessage & { id?: string[] }
|
|
479
|
+
): ChatMessage => {
|
|
480
|
+
let role: ChatMessage["role"] = "user";
|
|
481
|
+
|
|
482
|
+
const message_: (BaseMessage | StoredMessage) & {
|
|
483
|
+
id?: string[];
|
|
484
|
+
type?: string;
|
|
485
|
+
} = message.lc_serializable
|
|
486
|
+
? mapChatMessagesToStoredMessages([message])[0]!
|
|
487
|
+
: message;
|
|
488
|
+
|
|
489
|
+
// Dang this is so hard, langchain.js has 3 ways of representing the same thing...
|
|
490
|
+
if (
|
|
491
|
+
message_ instanceof HumanMessage ||
|
|
492
|
+
message_ instanceof HumanMessageChunk ||
|
|
493
|
+
message_.id?.at(-1) === "HumanMessage" ||
|
|
494
|
+
message_.id?.at(-1) === "HumanMessageChunk" ||
|
|
495
|
+
message_.type === "human"
|
|
496
|
+
) {
|
|
497
|
+
role = "user";
|
|
498
|
+
} else if (
|
|
499
|
+
message instanceof AIMessage ||
|
|
500
|
+
message instanceof AIMessageChunk ||
|
|
501
|
+
message.id?.at(-1) === "AIMessage" ||
|
|
502
|
+
message.id?.at(-1) === "AIMessageChunk" ||
|
|
503
|
+
message_.type === "ai"
|
|
504
|
+
) {
|
|
505
|
+
role = "assistant";
|
|
506
|
+
} else if (
|
|
507
|
+
message instanceof SystemMessage ||
|
|
508
|
+
message instanceof SystemMessageChunk ||
|
|
509
|
+
message.id?.at(-1) === "SystemMessage" ||
|
|
510
|
+
message.id?.at(-1) === "SystemMessageChunk" ||
|
|
511
|
+
message_.type === "system"
|
|
512
|
+
) {
|
|
513
|
+
role = "system";
|
|
514
|
+
} else if (
|
|
515
|
+
message instanceof FunctionMessage ||
|
|
516
|
+
message instanceof FunctionMessageChunk ||
|
|
517
|
+
message.id?.at(-1) === "FunctionMessage" ||
|
|
518
|
+
message.id?.at(-1) === "FunctionMessageChunk" ||
|
|
519
|
+
message_.type === "function"
|
|
520
|
+
) {
|
|
521
|
+
role = "function";
|
|
522
|
+
} else if (
|
|
523
|
+
message instanceof ToolMessage ||
|
|
524
|
+
message instanceof ToolMessageChunk ||
|
|
525
|
+
message.id?.at(-1) === "ToolMessage" ||
|
|
526
|
+
message.id?.at(-1) === "ToolMessageChunk" ||
|
|
527
|
+
message_.type === "tool"
|
|
528
|
+
) {
|
|
529
|
+
role = "tool";
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
const content =
|
|
533
|
+
typeof message.content === "string"
|
|
534
|
+
? message.content
|
|
535
|
+
: message.content.map(
|
|
536
|
+
(content): ChatRichContent =>
|
|
537
|
+
content.type === "text"
|
|
538
|
+
? { type: "text", text: content.text }
|
|
539
|
+
: content.type == "image_url"
|
|
540
|
+
? { type: "image_url", image_url: content.image_url }
|
|
541
|
+
: { type: "text", text: JSON.stringify(content) }
|
|
542
|
+
);
|
|
543
|
+
|
|
544
|
+
const functionCall = message.additional_kwargs as
|
|
545
|
+
| ChatMessage["function_call"]
|
|
546
|
+
| undefined;
|
|
547
|
+
|
|
548
|
+
return {
|
|
549
|
+
role,
|
|
550
|
+
content,
|
|
551
|
+
...(functionCall &&
|
|
552
|
+
typeof functionCall === "object" &&
|
|
553
|
+
Object.keys(functionCall).length > 0
|
|
554
|
+
? { function_call: functionCall }
|
|
555
|
+
: {}),
|
|
556
|
+
};
|
|
557
|
+
};
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
reservedSpanParamsSchema,
|
|
3
|
+
reservedTraceMetadataSchema
|
|
4
|
+
} from "./server/types/tracer.generated";
|
|
5
|
+
|
|
1
6
|
export type Strict<T> = T & { [K in Exclude<keyof any, keyof T>]: never };
|
|
2
7
|
|
|
3
8
|
type SnakeToCamelCase<S extends string> = S extends `${infer T}_${infer U}`
|
|
@@ -44,7 +49,10 @@ function camelToSnakeCase(str: string): string {
|
|
|
44
49
|
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
|
45
50
|
}
|
|
46
51
|
|
|
47
|
-
export function camelToSnakeCaseNested<T>(
|
|
52
|
+
export function camelToSnakeCaseNested<T>(
|
|
53
|
+
obj: T,
|
|
54
|
+
parentKey?: string
|
|
55
|
+
): CamelToSnakeCaseNested<T> {
|
|
48
56
|
if (Array.isArray(obj)) {
|
|
49
57
|
return obj.map((item) =>
|
|
50
58
|
camelToSnakeCaseNested(item)
|
|
@@ -54,7 +62,17 @@ export function camelToSnakeCaseNested<T>(obj: T): CamelToSnakeCaseNested<T> {
|
|
|
54
62
|
for (const key in obj) {
|
|
55
63
|
if (obj.hasOwnProperty(key)) {
|
|
56
64
|
const newKey = camelToSnakeCase(key);
|
|
57
|
-
|
|
65
|
+
// Keep arbitrary keys the same
|
|
66
|
+
if (
|
|
67
|
+
(parentKey === "metadata" &&
|
|
68
|
+
!Object.keys(reservedTraceMetadataSchema.shape).includes(newKey)) ||
|
|
69
|
+
(parentKey === "params" &&
|
|
70
|
+
!Object.keys(reservedSpanParamsSchema.shape).includes(newKey))
|
|
71
|
+
) {
|
|
72
|
+
newObj[key] = (obj as any)[key];
|
|
73
|
+
} else {
|
|
74
|
+
newObj[newKey] = camelToSnakeCaseNested((obj as any)[key], newKey);
|
|
75
|
+
}
|
|
58
76
|
}
|
|
59
77
|
}
|
|
60
78
|
return newObj as CamelToSnakeCaseNested<T>;
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type modelPrices from "llm-cost/model_prices_and_context_window.json";
|
|
2
1
|
import type { OpenAI } from "openai";
|
|
3
|
-
import { type SnakeToCamelCaseNested } from "./
|
|
2
|
+
import { type SnakeToCamelCaseNested } from "./typeUtils";
|
|
4
3
|
import {
|
|
5
4
|
type BaseSpan as ServerBaseSpan,
|
|
6
5
|
type ChatMessage as ServerChatMessage,
|
|
@@ -10,6 +9,7 @@ import {
|
|
|
10
9
|
type SpanInputOutput as ServerSpanInputOutput,
|
|
11
10
|
type TypedValueChatMessages,
|
|
12
11
|
type Trace,
|
|
12
|
+
type RESTEvaluation as ServerRESTEvaluation,
|
|
13
13
|
} from "./server/types/tracer";
|
|
14
14
|
|
|
15
15
|
export type Metadata = SnakeToCamelCaseNested<Trace["metadata"]>;
|
|
@@ -63,8 +63,12 @@ export type PendingBaseSpan = PendingSpan<BaseSpan>;
|
|
|
63
63
|
// vendor is deprecated, and we try to force the available models here
|
|
64
64
|
export type LLMSpan = ConvertServerSpan<
|
|
65
65
|
Omit<ServerLLMSpan, "vendor" | "model">
|
|
66
|
-
> & { model:
|
|
66
|
+
> & { model: string };
|
|
67
67
|
export type PendingLLMSpan = PendingSpan<LLMSpan>;
|
|
68
68
|
|
|
69
69
|
export type RAGSpan = ConvertServerSpan<ServerRAGSpan>;
|
|
70
70
|
export type PendingRAGSpan = PendingSpan<RAGSpan>;
|
|
71
|
+
|
|
72
|
+
export type RESTEvaluation = SnakeToCamelCaseNested<
|
|
73
|
+
Omit<ServerRESTEvaluation, "error">
|
|
74
|
+
> & { error?: ServerRESTEvaluation["error"] };
|
package/src/utils.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
|
|
2
|
-
import { type
|
|
3
|
-
import {
|
|
2
|
+
import { type CoreMessage, type ImagePart } from "ai";
|
|
3
|
+
import { z } from "zod";
|
|
4
4
|
import { type ErrorCapture } from "./server/types/tracer";
|
|
5
|
+
import { chatMessageSchema } from "./server/types/tracer.generated";
|
|
6
|
+
import { type ChatMessage, type SpanInputOutput } from "./types";
|
|
5
7
|
|
|
6
8
|
const convertImageToUrl = (
|
|
7
9
|
image: ImagePart["image"],
|
|
@@ -177,3 +179,24 @@ export const captureError = (error: unknown): ErrorCapture => {
|
|
|
177
179
|
};
|
|
178
180
|
}
|
|
179
181
|
};
|
|
182
|
+
|
|
183
|
+
export const autoconvertTypedValues = (value: unknown): SpanInputOutput => {
|
|
184
|
+
if (typeof value === "string") {
|
|
185
|
+
return { type: "text", value };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const chatMessages = z.array(chatMessageSchema).safeParse(value);
|
|
189
|
+
if (Array.isArray(value) && chatMessages.success) {
|
|
190
|
+
return {
|
|
191
|
+
type: "chat_messages",
|
|
192
|
+
value: chatMessages.data,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
try {
|
|
197
|
+
JSON.stringify(value);
|
|
198
|
+
return { type: "json", value: value as object };
|
|
199
|
+
} catch (e) {
|
|
200
|
+
return { type: "raw", value: value as any };
|
|
201
|
+
}
|
|
202
|
+
};
|