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', () => {
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,7 +74,7 @@ export class ChromeAdapter {
7574 }
7675 async generateContentOnDevice (
7776 request : GenerateContentRequest
78- ) : Promise < EnhancedGenerateContentResponse > {
77+ ) : Promise < Response > {
7978 const createOptions = this . onDeviceParams || { } ;
8079 createOptions . initialPrompts ??= [ ] ;
8180 const extractedInitialPrompts = ChromeAdapter . toInitialPrompts (
@@ -86,10 +85,20 @@ export class ChromeAdapter {
8685 createOptions . initialPrompts . push ( ...extractedInitialPrompts ) ;
8786 const session = await this . session ( createOptions ) ;
8887 const result = await session . prompt ( prompt . content ) ;
88+ return ChromeAdapter . toResponse ( result ) ;
89+ }
90+ private static toResponse ( text : string ) : Response {
8991 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 ;
93102 }
94103 async generateContentStreamOnDevice (
95104 request : GenerateContentRequest
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 ,
@@ -71,18 +70,15 @@ async function generateContentOnCloud(
7170 model : string ,
7271 params : GenerateContentRequest ,
7372 requestOptions ?: RequestOptions
74- ) : Promise < EnhancedGenerateContentResponse > {
75- const response = await makeRequest (
73+ ) : Promise < Response > {
74+ return makeRequest (
7675 model ,
7776 Task . GENERATE_CONTENT ,
7877 apiSettings ,
7978 /* stream */ false ,
8079 JSON . stringify ( params ) ,
8180 requestOptions
8281 ) ;
83- const responseJson : GenerateContentResponse = await response . json ( ) ;
84- const enhancedResponse = createEnhancedContentResponse ( responseJson ) ;
85- return enhancedResponse ;
8682}
8783
8884export async function generateContent (
@@ -92,17 +88,19 @@ export async function generateContent(
9288 chromeAdapter : ChromeAdapter ,
9389 requestOptions ?: RequestOptions
9490) : Promise < GenerateContentResult > {
95- let enhancedResponse ;
91+ let response ;
9692 if ( await chromeAdapter . isAvailable ( params ) ) {
97- enhancedResponse = await chromeAdapter . generateContentOnDevice ( params ) ;
93+ response = await chromeAdapter . generateContentOnDevice ( params ) ;
9894 } else {
99- enhancedResponse = await generateContentOnCloud (
95+ response = await generateContentOnCloud (
10096 apiSettings ,
10197 model ,
10298 params ,
10399 requestOptions
104100 ) ;
105101 }
102+ const responseJson : GenerateContentResponse = await response . json ( ) ;
103+ const enhancedResponse = createEnhancedContentResponse ( responseJson ) ;
106104 return {
107105 response : enhancedResponse
108106 } ;
You can’t perform that action at this time.
0 commit comments