@@ -49,47 +49,41 @@ export class VertexAIInstrumentation extends InstrumentationBase<any> {
49
49
protected init ( ) : InstrumentationModuleDefinition < any > {
50
50
const vertexAIModule = new InstrumentationNodeModuleDefinition < any > (
51
51
"@google-cloud/vertexai" ,
52
- [ ">=0.2.1 " ] ,
52
+ [ ">=1.1.0 " ] ,
53
53
this . wrap . bind ( this ) ,
54
54
this . unwrap . bind ( this ) ,
55
55
) ;
56
56
57
57
return vertexAIModule ;
58
58
}
59
59
60
- private modelConfig : vertexAI . ModelParams = { model : "" } ;
61
-
62
- private setModel ( newValue : vertexAI . ModelParams ) {
63
- this . modelConfig = { ...newValue } ;
64
- }
65
-
66
60
public manuallyInstrument ( module : typeof vertexAI ) {
67
61
this . _diag . debug ( "Manually instrumenting @google-cloud/vertexai" ) ;
68
62
69
63
this . _wrap (
70
- module . VertexAI_Preview . prototype ,
71
- "getGenerativeModel " ,
72
- this . wrapperMethod ( "getGenerativeModel" ) ,
64
+ module . GenerativeModel . prototype ,
65
+ "generateContentStream " ,
66
+ this . wrapperMethod ( ) ,
73
67
) ;
74
68
this . _wrap (
75
69
module . GenerativeModel . prototype ,
76
- "generateContentStream " ,
77
- this . wrapperMethod ( "generateContentStream" ) ,
70
+ "generateContent " ,
71
+ this . wrapperMethod ( ) ,
78
72
) ;
79
73
}
80
74
81
75
private wrap ( module : typeof vertexAI , moduleVersion ?: string ) {
82
76
this . _diag . debug ( `Patching @google-cloud/vertexai@${ moduleVersion } ` ) ;
83
77
84
78
this . _wrap (
85
- module . VertexAI_Preview . prototype ,
86
- "getGenerativeModel " ,
87
- this . wrapperMethod ( "getGenerativeModel" ) ,
79
+ module . GenerativeModel . prototype ,
80
+ "generateContentStream " ,
81
+ this . wrapperMethod ( ) ,
88
82
) ;
89
83
this . _wrap (
90
84
module . GenerativeModel . prototype ,
91
- "generateContentStream " ,
92
- this . wrapperMethod ( "generateContentStream" ) ,
85
+ "generateContent " ,
86
+ this . wrapperMethod ( ) ,
93
87
) ;
94
88
95
89
return module ;
@@ -98,42 +92,21 @@ export class VertexAIInstrumentation extends InstrumentationBase<any> {
98
92
private unwrap ( module : typeof vertexAI , moduleVersion ?: string ) : void {
99
93
this . _diag . debug ( `Unpatching @google-cloud/vertexai@${ moduleVersion } ` ) ;
100
94
101
- this . _unwrap ( module . VertexAI_Preview . prototype , "getGenerativeModel" ) ;
102
95
this . _unwrap ( module . GenerativeModel . prototype , "generateContentStream" ) ;
96
+ this . _unwrap ( module . GenerativeModel . prototype , "generateContent" ) ;
103
97
}
104
98
105
- private wrapperMethod (
106
- wrappedMethodName : "getGenerativeModel" | "generateContentStream" ,
107
- ) {
99
+ private wrapperMethod ( ) {
108
100
// eslint-disable-next-line @typescript-eslint/no-this-alias
109
101
const plugin = this ;
110
102
// eslint-disable-next-line @typescript-eslint/ban-types
111
103
return ( original : Function ) => {
112
104
return function method (
113
- this : any ,
105
+ this : vertexAI . GenerativeModel ,
114
106
...args : ( vertexAI . GenerateContentRequest & vertexAI . ModelParams ) [ ]
115
107
) {
116
- if ( wrappedMethodName === "getGenerativeModel" ) {
117
- plugin . setModel ( args [ 0 ] ) ;
118
-
119
- return context . bind (
120
- context . active ( ) ,
121
- safeExecuteInTheMiddle (
122
- ( ) => {
123
- return context . with ( context . active ( ) , ( ) => {
124
- return original . apply ( this , args ) ;
125
- } ) ;
126
- } ,
127
- ( e ) => {
128
- if ( e ) {
129
- plugin . _diag . error ( "Error in VertexAI Instrumentation" , e ) ;
130
- }
131
- } ,
132
- ) ,
133
- ) ;
134
- }
135
-
136
108
const span = plugin . _startSpan ( {
109
+ instance : this ,
137
110
params : args [ 0 ] ,
138
111
} ) ;
139
112
@@ -157,8 +130,10 @@ export class VertexAIInstrumentation extends InstrumentationBase<any> {
157
130
}
158
131
159
132
private _startSpan ( {
133
+ instance,
160
134
params,
161
135
} : {
136
+ instance : vertexAI . GenerativeModel ;
162
137
params : vertexAI . GenerateContentRequest ;
163
138
} ) : Span {
164
139
const attributes : Attributes = {
@@ -167,28 +142,18 @@ export class VertexAIInstrumentation extends InstrumentationBase<any> {
167
142
} ;
168
143
169
144
try {
170
- attributes [ SpanAttributes . LLM_REQUEST_MODEL ] = this . modelConfig . model ;
171
-
172
- if (
173
- this . modelConfig . generation_config !== undefined &&
174
- typeof this . modelConfig . generation_config === "object"
175
- ) {
176
- if ( this . modelConfig . generation_config . max_output_tokens ) {
177
- attributes [ SpanAttributes . LLM_REQUEST_MAX_TOKENS ] =
178
- this . modelConfig . generation_config . max_output_tokens ;
179
- }
180
- if ( this . modelConfig . generation_config . temperature ) {
181
- attributes [ SpanAttributes . LLM_REQUEST_TEMPERATURE ] =
182
- this . modelConfig . generation_config . temperature ;
183
- }
184
- if ( this . modelConfig . generation_config . top_p ) {
185
- attributes [ SpanAttributes . LLM_REQUEST_TOP_P ] =
186
- this . modelConfig . generation_config . top_p ;
187
- }
188
- if ( this . modelConfig . generation_config . top_k ) {
189
- attributes [ SpanAttributes . LLM_TOP_K ] =
190
- this . modelConfig . generation_config . top_k ;
191
- }
145
+ attributes [ SpanAttributes . LLM_REQUEST_MODEL ] = instance [ "model" ] ;
146
+ attributes [ SpanAttributes . LLM_RESPONSE_MODEL ] = instance [ "model" ] ;
147
+
148
+ if ( instance [ "generationConfig" ] ) {
149
+ attributes [ SpanAttributes . LLM_REQUEST_MAX_TOKENS ] =
150
+ instance [ "generationConfig" ] . max_output_tokens ;
151
+ attributes [ SpanAttributes . LLM_REQUEST_TEMPERATURE ] =
152
+ instance [ "generationConfig" ] . temperature ;
153
+ attributes [ SpanAttributes . LLM_REQUEST_TOP_P ] =
154
+ instance [ "generationConfig" ] . top_p ;
155
+ attributes [ SpanAttributes . LLM_TOP_K ] =
156
+ instance [ "generationConfig" ] . top_k ;
192
157
}
193
158
194
159
if ( this . _shouldSendPrompts ( ) && "contents" in params ) {
@@ -213,7 +178,9 @@ export class VertexAIInstrumentation extends InstrumentationBase<any> {
213
178
. then ( async ( result ) => {
214
179
await this . _endSpan ( {
215
180
span,
216
- result : result as vertexAI . StreamGenerateContentResult ,
181
+ result : result as
182
+ | vertexAI . StreamGenerateContentResult
183
+ | vertexAI . GenerateContentResult ,
217
184
} ) ;
218
185
return new Promise < T > ( ( resolve ) => resolve ( result ) ) ;
219
186
} )
@@ -236,14 +203,11 @@ export class VertexAIInstrumentation extends InstrumentationBase<any> {
236
203
result,
237
204
} : {
238
205
span : Span ;
239
- result : vertexAI . StreamGenerateContentResult ;
206
+ result :
207
+ | vertexAI . StreamGenerateContentResult
208
+ | vertexAI . GenerateContentResult ;
240
209
} ) {
241
210
try {
242
- span . setAttribute (
243
- SpanAttributes . LLM_RESPONSE_MODEL ,
244
- this . modelConfig . model ,
245
- ) ;
246
-
247
211
const streamResponse = await result . response ;
248
212
249
213
if ( streamResponse . usageMetadata ?. totalTokenCount !== undefined )
@@ -252,20 +216,20 @@ export class VertexAIInstrumentation extends InstrumentationBase<any> {
252
216
streamResponse . usageMetadata . totalTokenCount ,
253
217
) ;
254
218
255
- if ( streamResponse . usageMetadata ?. candidates_token_count )
219
+ if ( streamResponse . usageMetadata ?. candidatesTokenCount )
256
220
span . setAttribute (
257
221
SpanAttributes . LLM_USAGE_COMPLETION_TOKENS ,
258
- streamResponse . usageMetadata . candidates_token_count ,
222
+ streamResponse . usageMetadata . candidatesTokenCount ,
259
223
) ;
260
224
261
- if ( streamResponse . usageMetadata ?. prompt_token_count )
225
+ if ( streamResponse . usageMetadata ?. promptTokenCount )
262
226
span . setAttribute (
263
227
SpanAttributes . LLM_USAGE_PROMPT_TOKENS ,
264
- streamResponse . usageMetadata . prompt_token_count ,
228
+ streamResponse . usageMetadata . promptTokenCount ,
265
229
) ;
266
230
267
231
if ( this . _shouldSendPrompts ( ) ) {
268
- streamResponse . candidates . forEach ( ( candidate , index ) => {
232
+ streamResponse . candidates ? .forEach ( ( candidate , index ) => {
269
233
if ( candidate . finishReason )
270
234
span . setAttribute (
271
235
`${ SpanAttributes . LLM_COMPLETIONS } .${ index } .finish_reason` ,
@@ -298,10 +262,10 @@ export class VertexAIInstrumentation extends InstrumentationBase<any> {
298
262
const result = parts
299
263
. map ( ( part ) => {
300
264
if ( part . text ) return part . text ;
301
- else if ( part . file_data )
302
- return part . file_data . file_uri + "-" + part . file_data . mime_type ;
303
- else if ( part . inline_data )
304
- return part . inline_data . data + "-" + part . inline_data . mime_type ;
265
+ else if ( part . fileData )
266
+ return part . fileData . fileUri + "-" + part . fileData . mimeType ;
267
+ else if ( part . inlineData )
268
+ return part . inlineData . data + "-" + part . inlineData . mimeType ;
305
269
else return "" ;
306
270
} )
307
271
. filter ( Boolean ) ;
0 commit comments