File tree Expand file tree Collapse file tree 4 files changed +49
-25
lines changed
packages/vertexai/src/methods Expand file tree Collapse file tree 4 files changed +49
-25
lines changed Original file line number Diff line number Diff line change @@ -252,7 +252,15 @@ describe('ChromeAdapter', () => {
252252 { role : 'assistant' , content : text [ 1 ] }
253253 ]
254254 } ) ;
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+ } ) ;
256264 } ) ;
257265 it ( 'Extracts system prompt' , async ( ) => {
258266 const aiProvider = {
@@ -279,7 +287,15 @@ describe('ChromeAdapter', () => {
279287 initialPrompts : [ ] ,
280288 systemPrompt : onDeviceParams . systemPrompt
281289 } ) ;
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+ } ) ;
283299 } ) ;
284300 } ) ;
285301} ) ;
Original file line number Diff line number Diff line change 1818import { isChrome } from '@firebase/util' ;
1919import {
2020 Content ,
21- EnhancedGenerateContentResponse ,
2221 GenerateContentRequest ,
2322 InferenceMode ,
2423 Role
@@ -75,19 +74,31 @@ export class ChromeAdapter {
7574 }
7675 async generateContentOnDevice (
7776 request : GenerateContentRequest
78- ) : Promise < EnhancedGenerateContentResponse > {
77+ ) : Promise < Response > {
7978 const createOptions = this . onDeviceParams || { } ;
8079 createOptions . initialPrompts ??= [ ] ;
81- const extractedInitialPrompts = ChromeAdapter . toInitialPrompts ( request . contents ) ;
80+ const extractedInitialPrompts = ChromeAdapter . toInitialPrompts (
81+ request . contents
82+ ) ;
8283 // Assumes validation asserted there is at least one initial prompt.
8384 const prompt = extractedInitialPrompts . pop ( ) ! ;
8485 createOptions . initialPrompts . push ( ...extractedInitialPrompts ) ;
8586 const session = await this . session ( createOptions ) ;
8687 const result = await session . prompt ( prompt . content ) ;
88+ return ChromeAdapter . toResponse ( result ) ;
89+ }
90+ private static toResponse ( text : string ) : Response {
8791 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 ;
91102 }
92103 private static isOnDeviceRequest ( request : GenerateContentRequest ) : boolean {
93104 // Returns false if the prompt is empty.
Original file line number Diff line number Diff line change @@ -291,24 +291,23 @@ describe('generateContent()', () => {
291291 expect ( mockFetch ) . to . be . called ;
292292 } ) ;
293293 it ( 'on-device' , async ( ) => {
294- const expectedText = 'hi' ;
295294 const chromeAdapter = new ChromeAdapter ( ) ;
296295 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 (
298300 chromeAdapter ,
299301 'generateContentOnDevice'
300- ) . resolves ( {
301- text : ( ) => expectedText ,
302- functionCalls : ( ) => undefined
303- } ) ;
302+ ) . resolves ( mockResponse as Response ) ;
304303 const result = await generateContent (
305304 fakeApiSettings ,
306305 'model' ,
307306 fakeRequestParams ,
308307 chromeAdapter
309308 ) ;
310- expect ( result . response . text ( ) ) . to . equal ( expectedText ) ;
309+ expect ( result . response . text ( ) ) . to . include ( 'Mountain View, California' ) ;
311310 expect ( mockIsAvailable ) . to . be . called ;
312- expect ( mockGenerateContent ) . to . be . calledWith ( fakeRequestParams ) ;
311+ expect ( makeRequestStub ) . to . be . calledWith ( fakeRequestParams ) ;
313312 } ) ;
314313} ) ;
Original file line number Diff line number Diff line change 1616 */
1717
1818import {
19- EnhancedGenerateContentResponse ,
2019 GenerateContentRequest ,
2120 GenerateContentResponse ,
2221 GenerateContentResult ,
@@ -51,18 +50,15 @@ async function generateContentOnCloud(
5150 model : string ,
5251 params : GenerateContentRequest ,
5352 requestOptions ?: RequestOptions
54- ) : Promise < EnhancedGenerateContentResponse > {
55- const response = await makeRequest (
53+ ) : Promise < Response > {
54+ return makeRequest (
5655 model ,
5756 Task . GENERATE_CONTENT ,
5857 apiSettings ,
5958 /* stream */ false ,
6059 JSON . stringify ( params ) ,
6160 requestOptions
6261 ) ;
63- const responseJson : GenerateContentResponse = await response . json ( ) ;
64- const enhancedResponse = createEnhancedContentResponse ( responseJson ) ;
65- return enhancedResponse ;
6662}
6763
6864export async function generateContent (
@@ -72,17 +68,19 @@ export async function generateContent(
7268 chromeAdapter : ChromeAdapter ,
7369 requestOptions ?: RequestOptions
7470) : Promise < GenerateContentResult > {
75- let enhancedResponse ;
71+ let response ;
7672 if ( await chromeAdapter . isAvailable ( params ) ) {
77- enhancedResponse = await chromeAdapter . generateContentOnDevice ( params ) ;
73+ response = await chromeAdapter . generateContentOnDevice ( params ) ;
7874 } else {
79- enhancedResponse = await generateContentOnCloud (
75+ response = await generateContentOnCloud (
8076 apiSettings ,
8177 model ,
8278 params ,
8379 requestOptions
8480 ) ;
8581 }
82+ const responseJson : GenerateContentResponse = await response . json ( ) ;
83+ const enhancedResponse = createEnhancedContentResponse ( responseJson ) ;
8684 return {
8785 response : enhancedResponse
8886 } ;
You can’t perform that action at this time.
0 commit comments