File tree Expand file tree Collapse file tree 4 files changed +46
-24
lines changed
packages/vertexai/src/methods Expand file tree Collapse file tree 4 files changed +46
-24
lines changed Original file line number Diff line number Diff line change @@ -252,7 +252,15 @@ describe('ChromeAdapter', () => {
252
252
{ role : 'assistant' , content : text [ 1 ] }
253
253
]
254
254
} ) ;
255
- expect ( response . text ( ) ) . to . equal ( text [ 2 ] ) ;
255
+ expect ( await response . json ( ) ) . to . deep . equal ( {
256
+ candidates : [
257
+ {
258
+ content : {
259
+ parts : [ { text : text [ 2 ] } ]
260
+ }
261
+ }
262
+ ]
263
+ } ) ;
256
264
} ) ;
257
265
it ( 'Extracts system prompt' , async ( ) => {
258
266
const aiProvider = {
@@ -279,7 +287,15 @@ describe('ChromeAdapter', () => {
279
287
initialPrompts : [ ] ,
280
288
systemPrompt : onDeviceParams . systemPrompt
281
289
} ) ;
282
- expect ( response . text ( ) ) . to . equal ( text ) ;
290
+ expect ( await response . json ( ) ) . to . deep . equal ( {
291
+ candidates : [
292
+ {
293
+ content : {
294
+ parts : [ { text } ]
295
+ }
296
+ }
297
+ ]
298
+ } ) ;
283
299
} ) ;
284
300
} ) ;
285
301
} ) ;
Original file line number Diff line number Diff line change 18
18
import { isChrome } from '@firebase/util' ;
19
19
import {
20
20
Content ,
21
- EnhancedGenerateContentResponse ,
22
21
GenerateContentRequest ,
23
22
InferenceMode ,
24
23
Role
@@ -75,7 +74,7 @@ export class ChromeAdapter {
75
74
}
76
75
async generateContentOnDevice (
77
76
request : GenerateContentRequest
78
- ) : Promise < EnhancedGenerateContentResponse > {
77
+ ) : Promise < Response > {
79
78
const createOptions = this . onDeviceParams || { } ;
80
79
createOptions . initialPrompts ??= [ ] ;
81
80
const extractedInitialPrompts = ChromeAdapter . toInitialPrompts (
@@ -86,10 +85,20 @@ export class ChromeAdapter {
86
85
createOptions . initialPrompts . push ( ...extractedInitialPrompts ) ;
87
86
const session = await this . session ( createOptions ) ;
88
87
const result = await session . prompt ( prompt . content ) ;
88
+ return ChromeAdapter . toResponse ( result ) ;
89
+ }
90
+ private static toResponse ( text : string ) : Response {
89
91
return {
90
- text : ( ) => result ,
91
- functionCalls : ( ) => undefined
92
- } ;
92
+ json : async ( ) => ( {
93
+ candidates : [
94
+ {
95
+ content : {
96
+ parts : [ { text } ]
97
+ }
98
+ }
99
+ ]
100
+ } )
101
+ } as Response ;
93
102
}
94
103
async generateContentStreamOnDevice (
95
104
request : GenerateContentRequest
Original file line number Diff line number Diff line change @@ -291,24 +291,23 @@ describe('generateContent()', () => {
291
291
expect ( mockFetch ) . to . be . called ;
292
292
} ) ;
293
293
it ( 'on-device' , async ( ) => {
294
- const expectedText = 'hi' ;
295
294
const chromeAdapter = new ChromeAdapter ( ) ;
296
295
const mockIsAvailable = stub ( chromeAdapter , 'isAvailable' ) . resolves ( true ) ;
297
- const mockGenerateContent = stub (
296
+ const mockResponse = getMockResponse (
297
+ 'unary-success-basic-reply-short.json'
298
+ ) ;
299
+ const makeRequestStub = stub (
298
300
chromeAdapter ,
299
301
'generateContentOnDevice'
300
- ) . resolves ( {
301
- text : ( ) => expectedText ,
302
- functionCalls : ( ) => undefined
303
- } ) ;
302
+ ) . resolves ( mockResponse as Response ) ;
304
303
const result = await generateContent (
305
304
fakeApiSettings ,
306
305
'model' ,
307
306
fakeRequestParams ,
308
307
chromeAdapter
309
308
) ;
310
- expect ( result . response . text ( ) ) . to . equal ( expectedText ) ;
309
+ expect ( result . response . text ( ) ) . to . include ( 'Mountain View, California' ) ;
311
310
expect ( mockIsAvailable ) . to . be . called ;
312
- expect ( mockGenerateContent ) . to . be . calledWith ( fakeRequestParams ) ;
311
+ expect ( makeRequestStub ) . to . be . calledWith ( fakeRequestParams ) ;
313
312
} ) ;
314
313
} ) ;
Original file line number Diff line number Diff line change 16
16
*/
17
17
18
18
import {
19
- EnhancedGenerateContentResponse ,
20
19
GenerateContentRequest ,
21
20
GenerateContentResponse ,
22
21
GenerateContentResult ,
@@ -71,18 +70,15 @@ async function generateContentOnCloud(
71
70
model : string ,
72
71
params : GenerateContentRequest ,
73
72
requestOptions ?: RequestOptions
74
- ) : Promise < EnhancedGenerateContentResponse > {
75
- const response = await makeRequest (
73
+ ) : Promise < Response > {
74
+ return makeRequest (
76
75
model ,
77
76
Task . GENERATE_CONTENT ,
78
77
apiSettings ,
79
78
/* stream */ false ,
80
79
JSON . stringify ( params ) ,
81
80
requestOptions
82
81
) ;
83
- const responseJson : GenerateContentResponse = await response . json ( ) ;
84
- const enhancedResponse = createEnhancedContentResponse ( responseJson ) ;
85
- return enhancedResponse ;
86
82
}
87
83
88
84
export async function generateContent (
@@ -92,17 +88,19 @@ export async function generateContent(
92
88
chromeAdapter : ChromeAdapter ,
93
89
requestOptions ?: RequestOptions
94
90
) : Promise < GenerateContentResult > {
95
- let enhancedResponse ;
91
+ let response ;
96
92
if ( await chromeAdapter . isAvailable ( params ) ) {
97
- enhancedResponse = await chromeAdapter . generateContentOnDevice ( params ) ;
93
+ response = await chromeAdapter . generateContentOnDevice ( params ) ;
98
94
} else {
99
- enhancedResponse = await generateContentOnCloud (
95
+ response = await generateContentOnCloud (
100
96
apiSettings ,
101
97
model ,
102
98
params ,
103
99
requestOptions
104
100
) ;
105
101
}
102
+ const responseJson : GenerateContentResponse = await response . json ( ) ;
103
+ const enhancedResponse = createEnhancedContentResponse ( responseJson ) ;
106
104
return {
107
105
response : enhancedResponse
108
106
} ;
You can’t perform that action at this time.
0 commit comments