-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathutils.ts
More file actions
204 lines (177 loc) · 6.68 KB
/
utils.ts
File metadata and controls
204 lines (177 loc) · 6.68 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
import type { Span } from '../../types-hoist/span';
import {
GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE,
GEN_AI_RESPONSE_MODEL_ATTRIBUTE,
GEN_AI_RESPONSE_TEXT_ATTRIBUTE,
GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE,
GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE,
GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE,
GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE,
} from '../ai/gen-ai-attributes';
import type { BaseChatModel, LangChainMessage } from '../langchain/types';
import { normalizeLangChainMessages } from '../langchain/utils';
import type { CompiledGraph, LangGraphTool } from './types';
/**
* Extract LLM model object from createReactAgent params
*/
export function extractLLMFromParams(args: unknown[]): null | BaseChatModel {
const arg = args[0];
return typeof arg === 'object' &&
!!arg &&
'llm' in arg &&
!!arg.llm &&
typeof arg.llm === 'object' &&
typeof (arg.llm as BaseChatModel).modelName === 'string'
? (arg.llm as BaseChatModel)
: null;
}
/**
* Extract tool calls from messages
*/
export function extractToolCalls(messages: Array<Record<string, unknown>> | null): unknown[] | null {
if (!messages || messages.length === 0) {
return null;
}
const toolCalls: unknown[] = [];
for (const message of messages) {
if (message && typeof message === 'object') {
const msgToolCalls = message.tool_calls;
if (msgToolCalls && Array.isArray(msgToolCalls)) {
toolCalls.push(...msgToolCalls);
}
}
}
return toolCalls.length > 0 ? toolCalls : null;
}
/**
* Extract token usage from a message's usage_metadata or response_metadata
* Returns token counts without setting span attributes
*/
export function extractTokenUsageFromMessage(message: LangChainMessage): {
inputTokens: number;
outputTokens: number;
totalTokens: number;
} {
const msg = message as Record<string, unknown>;
let inputTokens = 0;
let outputTokens = 0;
let totalTokens = 0;
// Extract from usage_metadata (newer format)
if (msg.usage_metadata && typeof msg.usage_metadata === 'object') {
const usage = msg.usage_metadata as Record<string, unknown>;
if (typeof usage.input_tokens === 'number') {
inputTokens = usage.input_tokens;
}
if (typeof usage.output_tokens === 'number') {
outputTokens = usage.output_tokens;
}
if (typeof usage.total_tokens === 'number') {
totalTokens = usage.total_tokens;
}
return { inputTokens, outputTokens, totalTokens };
}
// Fallback: Extract from response_metadata.tokenUsage
if (msg.response_metadata && typeof msg.response_metadata === 'object') {
const metadata = msg.response_metadata as Record<string, unknown>;
if (metadata.tokenUsage && typeof metadata.tokenUsage === 'object') {
const tokenUsage = metadata.tokenUsage as Record<string, unknown>;
if (typeof tokenUsage.promptTokens === 'number') {
inputTokens = tokenUsage.promptTokens;
}
if (typeof tokenUsage.completionTokens === 'number') {
outputTokens = tokenUsage.completionTokens;
}
if (typeof tokenUsage.totalTokens === 'number') {
totalTokens = tokenUsage.totalTokens;
}
}
}
return { inputTokens, outputTokens, totalTokens };
}
/**
* Extract model and finish reason from a message's response_metadata
*/
export function extractModelMetadata(span: Span, message: LangChainMessage): void {
const msg = message as Record<string, unknown>;
if (msg.response_metadata && typeof msg.response_metadata === 'object') {
const metadata = msg.response_metadata as Record<string, unknown>;
if (metadata.model_name && typeof metadata.model_name === 'string') {
span.setAttribute(GEN_AI_RESPONSE_MODEL_ATTRIBUTE, metadata.model_name);
}
if (metadata.finish_reason && typeof metadata.finish_reason === 'string') {
span.setAttribute(GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE, [metadata.finish_reason]);
}
}
}
/**
* Extract tools from compiled graph structure
*
* Tools are stored in: compiledGraph.builder.nodes.tools.runnable.tools
*/
export function extractToolsFromCompiledGraph(compiledGraph: CompiledGraph): unknown[] | null {
if (!compiledGraph.builder?.nodes?.tools?.runnable?.tools) {
return null;
}
const tools = compiledGraph.builder?.nodes?.tools?.runnable?.tools;
if (!tools || !Array.isArray(tools) || tools.length === 0) {
return null;
}
// Extract name, description, and schema from each tool's lc_kwargs
return tools.map((tool: LangGraphTool) => ({
name: tool.lc_kwargs?.name,
description: tool.lc_kwargs?.description,
schema: tool.lc_kwargs?.schema,
}));
}
/**
* Set response attributes on the span
*/
export function setResponseAttributes(span: Span, inputMessages: LangChainMessage[] | null, result: unknown): void {
// Extract messages from result
const resultObj = result as { messages?: LangChainMessage[] } | undefined;
const outputMessages = resultObj?.messages;
if (!outputMessages || !Array.isArray(outputMessages)) {
return;
}
// Get new messages (delta between input and output)
/* v8 ignore start - coverage gets confused by this somehow */
const inputCount = inputMessages?.length ?? 0;
/* v8 ignore stop */
const newMessages = outputMessages.length > inputCount ? outputMessages.slice(inputCount) : [];
if (newMessages.length === 0) {
return;
}
// Extract and set tool calls from new messages BEFORE normalization
// (normalization strips tool_calls, so we need to extract them first)
const toolCalls = extractToolCalls(newMessages as Array<Record<string, unknown>>);
if (toolCalls) {
span.setAttribute(GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE, JSON.stringify(toolCalls));
}
// Normalize the new messages
const normalizedNewMessages = normalizeLangChainMessages(newMessages);
span.setAttribute(GEN_AI_RESPONSE_TEXT_ATTRIBUTE, JSON.stringify(normalizedNewMessages));
// Accumulate token usage across all messages
let totalInputTokens = 0;
let totalOutputTokens = 0;
let totalTokens = 0;
// Extract metadata from messages
for (const message of newMessages) {
// Accumulate token usage
const tokens = extractTokenUsageFromMessage(message);
totalInputTokens += tokens.inputTokens;
totalOutputTokens += tokens.outputTokens;
totalTokens += tokens.totalTokens;
// Extract model metadata (last message's metadata wins for model/finish_reason)
extractModelMetadata(span, message);
}
// Set accumulated token usage on span
if (totalInputTokens > 0) {
span.setAttribute(GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE, totalInputTokens);
}
if (totalOutputTokens > 0) {
span.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE, totalOutputTokens);
}
if (totalTokens > 0) {
span.setAttribute(GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE, totalTokens);
}
}