Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/vertexai/src/methods/generate-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
RequestOptions
} from '../types';
import { Task, makeRequest } from '../requests/request';
import { addHelpers } from '../requests/response-helpers';
import { createEnhancedContentResponse } from '../requests/response-helpers';
import { processStream } from '../requests/stream-reader';
import { ApiSettings } from '../types/internal';

Expand Down Expand Up @@ -59,7 +59,7 @@ export async function generateContent(
requestOptions
);
const responseJson: GenerateContentResponse = await response.json();
const enhancedResponse = addHelpers(responseJson);
const enhancedResponse = createEnhancedContentResponse(responseJson);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you assign it to a named variable first here and in the other places to increase readability? That's fine with me, seems reasonable, just wondering.

Copy link
Contributor Author

@dlarocque dlarocque Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it improves readability only slightly. The main reason I like this style because it makes it easier to debug since I can step over and see the return value before returning to the calling function, and I can log the value before it's returned without having to move stuff around.

return {
response: enhancedResponse
};
Expand Down
20 changes: 20 additions & 0 deletions packages/vertexai/src/requests/response-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ import {
} from '../types';
import { VertexAIError } from '../errors';

/**
* Creates an EnhancedGenerateContentResponse object that has helper functions and
* other modifications that improve usability.
*/
export function createEnhancedContentResponse(
response: GenerateContentResponse
): EnhancedGenerateContentResponse {
/**
* The Vertex AI backend omits default values.
* This causes the `index` property to be omitted from the first candidate in the
* response, since it has index 0, and 0 is a default value.
*/
if (response.candidates && !response.candidates[0].index) {
response.candidates[0].index = 0;
}

const responseWithHelpers = addHelpers(response);
return responseWithHelpers;
}

/**
* Adds convenience helper methods to a response object, including stream
* chunks (as long as each chunk is a complete GenerateContentResponse JSON).
Expand Down
11 changes: 8 additions & 3 deletions packages/vertexai/src/requests/stream-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
VertexAIErrorCode
} from '../types';
import { VertexAIError } from '../errors';
import { addHelpers } from './response-helpers';
import { createEnhancedContentResponse } from './response-helpers';

const responseLineRE = /^data\: (.*)(?:\n\n|\r\r|\r\n\r\n)/;

Expand Down Expand Up @@ -57,7 +57,10 @@ async function getResponsePromise(
while (true) {
const { done, value } = await reader.read();
if (done) {
return addHelpers(aggregateResponses(allResponses));
const enhancedResponse = createEnhancedContentResponse(
aggregateResponses(allResponses)
);
return enhancedResponse;
}
allResponses.push(value);
}
Expand All @@ -72,7 +75,9 @@ async function* generateResponseSequence(
if (done) {
break;
}
yield addHelpers(value);

const enhancedResponse = createEnhancedContentResponse(value);
yield enhancedResponse;
}
}

Expand Down
Loading