File tree 3 files changed +30
-5
lines changed
3 files changed +30
-5
lines changed Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ import {
23
23
RequestOptions
24
24
} from '../types' ;
25
25
import { Task , makeRequest } from '../requests/request' ;
26
- import { addHelpers } from '../requests/response-helpers' ;
26
+ import { createEnhancedContentResponse } from '../requests/response-helpers' ;
27
27
import { processStream } from '../requests/stream-reader' ;
28
28
import { ApiSettings } from '../types/internal' ;
29
29
@@ -59,7 +59,7 @@ export async function generateContent(
59
59
requestOptions
60
60
) ;
61
61
const responseJson : GenerateContentResponse = await response . json ( ) ;
62
- const enhancedResponse = addHelpers ( responseJson ) ;
62
+ const enhancedResponse = createEnhancedContentResponse ( responseJson ) ;
63
63
return {
64
64
response : enhancedResponse
65
65
} ;
Original file line number Diff line number Diff line change @@ -25,6 +25,26 @@ import {
25
25
} from '../types' ;
26
26
import { VertexAIError } from '../errors' ;
27
27
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 ] . hasOwnProperty ( 'index' ) ) {
41
+ response . candidates [ 0 ] . index = 0 ;
42
+ }
43
+
44
+ const responseWithHelpers = addHelpers ( response ) ;
45
+ return responseWithHelpers ;
46
+ }
47
+
28
48
/**
29
49
* Adds convenience helper methods to a response object, including stream
30
50
* 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 {
24
24
VertexAIErrorCode
25
25
} from '../types' ;
26
26
import { VertexAIError } from '../errors' ;
27
- import { addHelpers } from './response-helpers' ;
27
+ import { createEnhancedContentResponse } from './response-helpers' ;
28
28
29
29
const responseLineRE = / ^ d a t a \: ( .* ) (?: \n \n | \r \r | \r \n \r \n ) / ;
30
30
@@ -57,7 +57,10 @@ async function getResponsePromise(
57
57
while ( true ) {
58
58
const { done, value } = await reader . read ( ) ;
59
59
if ( done ) {
60
- return addHelpers ( aggregateResponses ( allResponses ) ) ;
60
+ const enhancedResponse = createEnhancedContentResponse (
61
+ aggregateResponses ( allResponses )
62
+ ) ;
63
+ return enhancedResponse ;
61
64
}
62
65
allResponses . push ( value ) ;
63
66
}
@@ -72,7 +75,9 @@ async function* generateResponseSequence(
72
75
if ( done ) {
73
76
break ;
74
77
}
75
- yield addHelpers ( value ) ;
78
+
79
+ const enhancedResponse = createEnhancedContentResponse ( value ) ;
80
+ yield enhancedResponse ;
76
81
}
77
82
}
78
83
You can’t perform that action at this time.
0 commit comments