Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
dlarocque committed Jan 29, 2025
1 parent 6769c10 commit 708892a
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
120 changes: 120 additions & 0 deletions packages/vertexai/src/requests/stream-reader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import {
aggregateResponses,
deleteEmptyTextParts,
getResponseStream,
processStream
} from './stream-reader';
Expand All @@ -33,6 +34,7 @@ import {
GenerateContentResponse,
HarmCategory,
HarmProbability,
Part,
SafetyRating
} from '../types';

Expand Down Expand Up @@ -220,6 +222,23 @@ describe('processStream', () => {
}
expect(foundCitationMetadata).to.be.true;
});
it('removes empty text parts', async () => {
const fakeResponse = getMockResponseStreaming(
'streaming-success-empty-text-part.txt'
);
const result = processStream(fakeResponse as Response);
const aggregatedResponse = await result.response;
expect(aggregatedResponse.text()).to.equal('1');
expect(aggregatedResponse.candidates?.length).to.equal(1);
expect(aggregatedResponse.candidates?.[0].content.parts.length).to.equal(1);

// The chunk with the empty text part will still go through the stream
let numChunks = 0;
for await (const _ of result.stream) {
numChunks++;
}
expect(numChunks).to.equal(2);
});
});

describe('aggregateResponses', () => {
Expand Down Expand Up @@ -404,3 +423,104 @@ describe('aggregateResponses', () => {
});
});
});

describe('deleteEmptyTextParts', () => {
it('removes empty text parts from a single candidate', () => {
const parts: Part[] = [
{
text: ''
},
{
text: 'foo'
}
];
const generateContentResponse: GenerateContentResponse = {
candidates: [
{
index: 0,
content: {
role: 'model',
parts
}
}
]
};

deleteEmptyTextParts(generateContentResponse);
expect(generateContentResponse.candidates?.[0].content.parts).to.deep.equal(
[
{
text: 'foo'
}
]
);
});
it('removes empty text parts from all candidates', () => {
const parts: Part[] = [
{
text: ''
},
{
text: 'foo'
}
];
const generateContentResponse: GenerateContentResponse = {
candidates: [
{
index: 0,
content: {
role: 'model',
parts
}
},
{
index: 1,
content: {
role: 'model',
parts
}
}
]
};

deleteEmptyTextParts(generateContentResponse);
expect(generateContentResponse.candidates?.[0].content.parts).to.deep.equal(
[
{
text: 'foo'
}
]
);
expect(generateContentResponse.candidates?.[1].content.parts).to.deep.equal(
[
{
text: 'foo'
}
]
);
});
it('does not remove candidate even if all parts are removed', () => {
const parts: Part[] = [
{
text: ''
}
];
const generateContentResponse: GenerateContentResponse = {
candidates: [
{
index: 0,
content: {
role: 'model',
parts
}
}
]
};

deleteEmptyTextParts(generateContentResponse);
expect(generateContentResponse.candidates?.length).to.equal(1);
expect(generateContentResponse.candidates?.[0].content.parts).to.deep.equal(
[]
);
});
});
3 changes: 3 additions & 0 deletions packages/vertexai/test-utils/mock-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export function getMockResponseStreaming(
filename: string,
chunkLength: number = 20
): Partial<Response> {
if (!(filename in mocksLookup)) {
throw Error(`Mock response file not found: '${filename}'`);
}
const fullText = mocksLookup[filename];

return {
Expand Down

0 comments on commit 708892a

Please sign in to comment.