File tree 4 files changed +49
-25
lines changed
packages/vertexai/src/methods 4 files changed +49
-25
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,19 +74,31 @@ 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
- const extractedInitialPrompts = ChromeAdapter . toInitialPrompts ( request . contents ) ;
80
+ const extractedInitialPrompts = ChromeAdapter . toInitialPrompts (
81
+ request . contents
82
+ ) ;
82
83
// Assumes validation asserted there is at least one initial prompt.
83
84
const prompt = extractedInitialPrompts . pop ( ) ! ;
84
85
createOptions . initialPrompts . push ( ...extractedInitialPrompts ) ;
85
86
const session = await this . session ( createOptions ) ;
86
87
const result = await session . prompt ( prompt . content ) ;
88
+ return ChromeAdapter . toResponse ( result ) ;
89
+ }
90
+ private static toResponse ( text : string ) : Response {
87
91
return {
88
- text : ( ) => result ,
89
- functionCalls : ( ) => undefined
90
- } ;
92
+ json : async ( ) => ( {
93
+ candidates : [
94
+ {
95
+ content : {
96
+ parts : [ { text } ]
97
+ }
98
+ }
99
+ ]
100
+ } )
101
+ } as Response ;
91
102
}
92
103
private static isOnDeviceRequest ( request : GenerateContentRequest ) : boolean {
93
104
// Returns false if the prompt is empty.
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 ,
@@ -51,18 +50,15 @@ async function generateContentOnCloud(
51
50
model : string ,
52
51
params : GenerateContentRequest ,
53
52
requestOptions ?: RequestOptions
54
- ) : Promise < EnhancedGenerateContentResponse > {
55
- const response = await makeRequest (
53
+ ) : Promise < Response > {
54
+ return makeRequest (
56
55
model ,
57
56
Task . GENERATE_CONTENT ,
58
57
apiSettings ,
59
58
/* stream */ false ,
60
59
JSON . stringify ( params ) ,
61
60
requestOptions
62
61
) ;
63
- const responseJson : GenerateContentResponse = await response . json ( ) ;
64
- const enhancedResponse = createEnhancedContentResponse ( responseJson ) ;
65
- return enhancedResponse ;
66
62
}
67
63
68
64
export async function generateContent (
@@ -72,17 +68,19 @@ export async function generateContent(
72
68
chromeAdapter : ChromeAdapter ,
73
69
requestOptions ?: RequestOptions
74
70
) : Promise < GenerateContentResult > {
75
- let enhancedResponse ;
71
+ let response ;
76
72
if ( await chromeAdapter . isAvailable ( params ) ) {
77
- enhancedResponse = await chromeAdapter . generateContentOnDevice ( params ) ;
73
+ response = await chromeAdapter . generateContentOnDevice ( params ) ;
78
74
} else {
79
- enhancedResponse = await generateContentOnCloud (
75
+ response = await generateContentOnCloud (
80
76
apiSettings ,
81
77
model ,
82
78
params ,
83
79
requestOptions
84
80
) ;
85
81
}
82
+ const responseJson : GenerateContentResponse = await response . json ( ) ;
83
+ const enhancedResponse = createEnhancedContentResponse ( responseJson ) ;
86
84
return {
87
85
response : enhancedResponse
88
86
} ;
You can’t perform that action at this time.
0 commit comments