@@ -4,12 +4,11 @@ const { expect } = require('chai')
4
4
const { describe, it, beforeEach, afterEach, before, after } = require ( 'mocha' )
5
5
const sinon = require ( 'sinon' )
6
6
7
- const LLMObsSpanWriter = require ( '../../../../src/llmobs/writers/spans' )
8
- const agent = require ( '../../../plugins/agent' )
9
7
const { withVersions } = require ( '../../../setup/mocha' )
10
8
const {
11
9
expectedLLMObsLLMSpanEvent,
12
- deepEqualWithMockValues
10
+ deepEqualWithMockValues,
11
+ useLlmObs
13
12
} = require ( '../../util' )
14
13
const chai = require ( 'chai' )
15
14
@@ -82,28 +81,7 @@ describe('integrations', () => {
82
81
}
83
82
84
83
describe ( 'vertexai' , ( ) => {
85
- before ( async ( ) => {
86
- sinon . stub ( LLMObsSpanWriter . prototype , 'append' )
87
-
88
- // reduce errors related to too many listeners
89
- process . removeAllListeners ( 'beforeExit' )
90
-
91
- return agent . load ( 'google-cloud-vertexai' , { } , {
92
- llmobs : {
93
- mlApp : 'test' ,
94
- agentlessEnabled : false
95
- }
96
- } )
97
- } )
98
-
99
- afterEach ( ( ) => {
100
- LLMObsSpanWriter . prototype . append . reset ( )
101
- } )
102
-
103
- after ( ( ) => {
104
- sinon . restore ( )
105
- return agent . close ( { ritmReset : false , wipe : true } )
106
- } )
84
+ const getEvents = useLlmObs ( { plugin : 'google-cloud-vertexai' } )
107
85
108
86
withVersions ( 'google-cloud-vertexai' , '@google-cloud/vertexai' , '>=1' , version => {
109
87
before ( ( ) => {
@@ -137,88 +115,76 @@ describe('integrations', () => {
137
115
useScenario ( { scenario : 'generate-content-single-response' } )
138
116
139
117
it ( 'makes a successful call' , async ( ) => {
140
- const checkTraces = agent . assertSomeTraces ( traces => {
141
- const span = traces [ 0 ] [ 0 ]
142
- const spanEvent = LLMObsSpanWriter . prototype . append . getCall ( 0 ) . args [ 0 ]
143
-
144
- const expected = expectedLLMObsLLMSpanEvent ( {
145
- span,
146
- spanKind : 'llm' ,
147
- modelName : 'gemini-1.5-flash-002' ,
148
- modelProvider : 'google' ,
149
- name : 'GenerativeModel.generateContent' ,
150
- inputMessages : getInputMessages ( 'Hello, how are you?' ) ,
151
- outputMessages : [
152
- {
153
- role : 'model' ,
154
- content : 'Hello! How can I assist you today?'
155
- }
156
- ] ,
157
- metadata : {
158
- temperature : 1 ,
159
- max_output_tokens : 50
160
- } ,
161
- tokenMetrics : { input_tokens : 35 , output_tokens : 2 , total_tokens : 37 } ,
162
- tags : { ml_app : 'test' , language : 'javascript' , integration : 'vertexai' }
163
- } )
164
-
165
- expect ( spanEvent ) . to . deepEqualWithMockValues ( expected )
166
- } )
167
-
168
118
await model . generateContent ( {
169
119
contents : [ { role : 'user' , parts : [ { text : 'Hello, how are you?' } ] } ]
170
120
} )
171
121
172
- await checkTraces
122
+ const { apmSpans, llmobsSpans } = await getEvents ( )
123
+ const expected = expectedLLMObsLLMSpanEvent ( {
124
+ span : apmSpans [ 0 ] ,
125
+ spanKind : 'llm' ,
126
+ modelName : 'gemini-1.5-flash-002' ,
127
+ modelProvider : 'google' ,
128
+ name : 'GenerativeModel.generateContent' ,
129
+ inputMessages : getInputMessages ( 'Hello, how are you?' ) ,
130
+ outputMessages : [
131
+ {
132
+ role : 'model' ,
133
+ content : 'Hello! How can I assist you today?'
134
+ }
135
+ ] ,
136
+ metadata : {
137
+ temperature : 1 ,
138
+ max_output_tokens : 50
139
+ } ,
140
+ tokenMetrics : { input_tokens : 35 , output_tokens : 2 , total_tokens : 37 } ,
141
+ tags : { ml_app : 'test' , language : 'javascript' , integration : 'vertexai' }
142
+ } )
143
+
144
+ expect ( llmobsSpans [ 0 ] ) . to . deepEqualWithMockValues ( expected )
173
145
} )
174
146
} )
175
147
176
148
describe ( 'tool calls' , ( ) => {
177
149
useScenario ( { scenario : 'generate-content-single-response-with-tools' } )
178
150
179
151
it ( 'makes a successful call' , async ( ) => {
180
- const checkTraces = agent . assertSomeTraces ( traces => {
181
- const span = traces [ 0 ] [ 0 ]
182
- const spanEvent = LLMObsSpanWriter . prototype . append . getCall ( 0 ) . args [ 0 ]
183
-
184
- const expected = expectedLLMObsLLMSpanEvent ( {
185
- span,
186
- spanKind : 'llm' ,
187
- modelName : 'gemini-1.5-flash-002' ,
188
- modelProvider : 'google' ,
189
- name : 'GenerativeModel.generateContent' ,
190
- inputMessages : getInputMessages ( 'what is 2 + 2?' ) ,
191
- outputMessages : [
192
- {
193
- role : 'model' ,
194
- content : '' ,
195
- tool_calls : [
196
- {
197
- name : 'add' ,
198
- arguments : {
199
- a : 2 ,
200
- b : 2
201
- }
202
- }
203
- ]
204
- }
205
- ] ,
206
- metadata : {
207
- temperature : 1 ,
208
- max_output_tokens : 50
209
- } ,
210
- tokenMetrics : { input_tokens : 20 , output_tokens : 3 , total_tokens : 23 } ,
211
- tags : { ml_app : 'test' , language : 'javascript' , integration : 'vertexai' }
212
- } )
213
-
214
- expect ( spanEvent ) . to . deepEqualWithMockValues ( expected )
215
- } )
216
-
217
152
await model . generateContent ( {
218
153
contents : [ { role : 'user' , parts : [ { text : 'what is 2 + 2?' } ] } ]
219
154
} )
220
155
221
- await checkTraces
156
+ const { apmSpans, llmobsSpans } = await getEvents ( )
157
+ const expected = expectedLLMObsLLMSpanEvent ( {
158
+ span : apmSpans [ 0 ] ,
159
+ spanKind : 'llm' ,
160
+ modelName : 'gemini-1.5-flash-002' ,
161
+ modelProvider : 'google' ,
162
+ name : 'GenerativeModel.generateContent' ,
163
+ inputMessages : getInputMessages ( 'what is 2 + 2?' ) ,
164
+ outputMessages : [
165
+ {
166
+ role : 'model' ,
167
+ content : '' ,
168
+ tool_calls : [
169
+ {
170
+ name : 'add' ,
171
+ arguments : {
172
+ a : 2 ,
173
+ b : 2
174
+ }
175
+ }
176
+ ]
177
+ }
178
+ ] ,
179
+ metadata : {
180
+ temperature : 1 ,
181
+ max_output_tokens : 50
182
+ } ,
183
+ tokenMetrics : { input_tokens : 20 , output_tokens : 3 , total_tokens : 23 } ,
184
+ tags : { ml_app : 'test' , language : 'javascript' , integration : 'vertexai' }
185
+ } )
186
+
187
+ expect ( llmobsSpans [ 0 ] ) . to . deepEqualWithMockValues ( expected )
222
188
} )
223
189
} )
224
190
@@ -227,44 +193,6 @@ describe('integrations', () => {
227
193
useScenario ( { scenario : 'generate-content-single-response' } )
228
194
229
195
it ( 'makes a successful call' , async ( ) => {
230
- const checkTraces = agent . assertSomeTraces ( traces => {
231
- const span = traces [ 0 ] [ 0 ]
232
- const spanEvent = LLMObsSpanWriter . prototype . append . getCall ( 0 ) . args [ 0 ]
233
-
234
- const inputMessages = [ ]
235
-
236
- if ( model . systemInstruction ) {
237
- inputMessages . push ( { role : 'system' , content : 'Please provide an answer' } )
238
- }
239
-
240
- inputMessages . push ( { role : 'user' , content : 'Foobar?' } )
241
- inputMessages . push ( { role : 'model' , content : 'Foobar!' } )
242
- inputMessages . push ( { content : 'Hello, how are you?' } )
243
-
244
- const expected = expectedLLMObsLLMSpanEvent ( {
245
- span,
246
- spanKind : 'llm' ,
247
- modelName : 'gemini-1.5-flash-002' ,
248
- modelProvider : 'google' ,
249
- name : 'ChatSession.sendMessage' ,
250
- inputMessages,
251
- outputMessages : [
252
- {
253
- role : 'model' ,
254
- content : 'Hello! How can I assist you today?'
255
- }
256
- ] ,
257
- metadata : {
258
- temperature : 1 ,
259
- max_output_tokens : 50
260
- } ,
261
- tokenMetrics : { input_tokens : 35 , output_tokens : 2 , total_tokens : 37 } ,
262
- tags : { ml_app : 'test' , language : 'javascript' , integration : 'vertexai' }
263
- } )
264
-
265
- expect ( spanEvent ) . to . deepEqualWithMockValues ( expected )
266
- } )
267
-
268
196
const chat = model . startChat ( {
269
197
history : [
270
198
{ role : 'user' , parts : [ { text : 'Foobar?' } ] } ,
@@ -274,7 +202,40 @@ describe('integrations', () => {
274
202
275
203
await chat . sendMessage ( [ { text : 'Hello, how are you?' } ] )
276
204
277
- await checkTraces
205
+ const { apmSpans, llmobsSpans } = await getEvents ( )
206
+
207
+ const inputMessages = [ ]
208
+
209
+ if ( model . systemInstruction ) {
210
+ inputMessages . push ( { role : 'system' , content : 'Please provide an answer' } )
211
+ }
212
+
213
+ inputMessages . push ( { role : 'user' , content : 'Foobar?' } )
214
+ inputMessages . push ( { role : 'model' , content : 'Foobar!' } )
215
+ inputMessages . push ( { content : 'Hello, how are you?' } )
216
+
217
+ const expected = expectedLLMObsLLMSpanEvent ( {
218
+ span : apmSpans [ 0 ] ,
219
+ spanKind : 'llm' ,
220
+ modelName : 'gemini-1.5-flash-002' ,
221
+ modelProvider : 'google' ,
222
+ name : 'ChatSession.sendMessage' ,
223
+ inputMessages,
224
+ outputMessages : [
225
+ {
226
+ role : 'model' ,
227
+ content : 'Hello! How can I assist you today?'
228
+ }
229
+ ] ,
230
+ metadata : {
231
+ temperature : 1 ,
232
+ max_output_tokens : 50
233
+ } ,
234
+ tokenMetrics : { input_tokens : 35 , output_tokens : 2 , total_tokens : 37 } ,
235
+ tags : { ml_app : 'test' , language : 'javascript' , integration : 'vertexai' }
236
+ } )
237
+
238
+ expect ( llmobsSpans [ 0 ] ) . to . deepEqualWithMockValues ( expected )
278
239
} )
279
240
} )
280
241
} )
0 commit comments