File tree Expand file tree Collapse file tree 3 files changed +30
-5
lines changed Expand file tree Collapse file tree 3 files changed +30
-5
lines changed Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ import {
2323 RequestOptions
2424} from '../types' ;
2525import { Task , makeRequest } from '../requests/request' ;
26- import { addHelpers } from '../requests/response-helpers' ;
26+ import { createEnhancedContentResponse } from '../requests/response-helpers' ;
2727import { processStream } from '../requests/stream-reader' ;
2828import { ApiSettings } from '../types/internal' ;
2929
@@ -59,7 +59,7 @@ export async function generateContent(
5959 requestOptions
6060 ) ;
6161 const responseJson : GenerateContentResponse = await response . json ( ) ;
62- const enhancedResponse = addHelpers ( responseJson ) ;
62+ const enhancedResponse = createEnhancedContentResponse ( responseJson ) ;
6363 return {
6464 response : enhancedResponse
6565 } ;
Original file line number Diff line number Diff line change @@ -25,6 +25,26 @@ import {
2525} from '../types' ;
2626import { VertexAIError } from '../errors' ;
2727
28+ /**
29+ * Creates an EnhancedGenerateContentResponse object that has helper functions and
30+ * other modifications that improve usability.
31+ */
32+ export function createEnhancedContentResponse (
33+ response : GenerateContentResponse
34+ ) : EnhancedGenerateContentResponse {
35+ /**
36+ * The Vertex AI backend omits default values.
37+ * This causes the `index` property to be omitted from the first candidate in the
38+ * response, since it has index 0, and 0 is a default value.
39+ */
40+ if ( response . candidates && ! response . candidates [ 0 ] . index ) {
41+ response . candidates [ 0 ] . index = 0 ;
42+ }
43+
44+ let responseWithHelpers = addHelpers ( response ) ;
45+ return responseWithHelpers ;
46+ }
47+
2848/**
2949 * Adds convenience helper methods to a response object, including stream
3050 * chunks (as long as each chunk is a complete GenerateContentResponse JSON).
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ import {
2424 VertexAIErrorCode
2525} from '../types' ;
2626import { VertexAIError } from '../errors' ;
27- import { addHelpers } from './response-helpers' ;
27+ import { createEnhancedContentResponse } from './response-helpers' ;
2828
2929const responseLineRE = / ^ d a t a \: ( .* ) (?: \n \n | \r \r | \r \n \r \n ) / ;
3030
@@ -57,7 +57,10 @@ async function getResponsePromise(
5757 while ( true ) {
5858 const { done, value } = await reader . read ( ) ;
5959 if ( done ) {
60- return addHelpers ( aggregateResponses ( allResponses ) ) ;
60+ const enhancedResponse = createEnhancedContentResponse (
61+ aggregateResponses ( allResponses )
62+ ) ;
63+ return enhancedResponse ;
6164 }
6265 allResponses . push ( value ) ;
6366 }
@@ -72,7 +75,9 @@ async function* generateResponseSequence(
7275 if ( done ) {
7376 break ;
7477 }
75- yield addHelpers ( value ) ;
78+
79+ const enhancedResponse = createEnhancedContentResponse ( value ) ;
80+ return enhancedResponse ;
7681 }
7782}
7883
You can’t perform that action at this time.
0 commit comments