Skip to content

Make text() accessor handle mixed-parts responses #8229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
121 changes: 88 additions & 33 deletions packages/vertexai/src/requests/response-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,54 +40,85 @@ const fakeResponseText: GenerateContentResponse = {
]
};

const functionCallPart1 = {
functionCall: {
name: 'find_theaters',
args: {
location: 'Mountain View, CA',
movie: 'Barbie'
}
}
};

const functionCallPart2 = {
functionCall: {
name: 'find_times',
args: {
location: 'Mountain View, CA',
movie: 'Barbie',
time: '20:00'
}
}
};

const fakeResponseFunctionCall: GenerateContentResponse = {
candidates: [
{
index: 0,
content: {
role: 'model',
parts: [
{
functionCall: {
name: 'find_theaters',
args: {
location: 'Mountain View, CA',
movie: 'Barbie'
}
}
}
]
parts: [functionCallPart1]
}
}
]
};

const fakeResponseFunctionCalls: GenerateContentResponse = {
candidates: [
{
index: 0,
content: {
role: 'model',
parts: [functionCallPart1, functionCallPart2]
}
}
]
};

const fakeResponseMixed1: GenerateContentResponse = {
candidates: [
{
index: 0,
content: {
role: 'model',
parts: [{ text: 'some text' }, functionCallPart2]
}
}
]
};

const fakeResponseMixed2: GenerateContentResponse = {
candidates: [
{
index: 0,
content: {
role: 'model',
parts: [functionCallPart1, { text: 'some text' }]
}
}
]
};

const fakeResponseMixed3: GenerateContentResponse = {
candidates: [
{
index: 0,
content: {
role: 'model',
parts: [
{
functionCall: {
name: 'find_theaters',
args: {
location: 'Mountain View, CA',
movie: 'Barbie'
}
}
},
{
functionCall: {
name: 'find_times',
args: {
location: 'Mountain View, CA',
movie: 'Barbie',
time: '20:00'
}
}
}
{ text: 'some text' },
functionCallPart1,
{ text: ' and more text' }
]
}
}
Expand All @@ -109,19 +140,43 @@ describe('response-helpers methods', () => {
it('good response text', async () => {
const enhancedResponse = addHelpers(fakeResponseText);
expect(enhancedResponse.text()).to.equal('Some text and some more text');
expect(enhancedResponse.functionCalls()).to.be.undefined;
});
it('good response functionCall', async () => {
const enhancedResponse = addHelpers(fakeResponseFunctionCall);
expect(enhancedResponse.text()).to.equal('');
expect(enhancedResponse.functionCalls()).to.deep.equal([
fakeResponseFunctionCall.candidates?.[0].content.parts[0].functionCall
functionCallPart1.functionCall
]);
});
it('good response functionCalls', async () => {
const enhancedResponse = addHelpers(fakeResponseFunctionCalls);
expect(enhancedResponse.text()).to.equal('');
expect(enhancedResponse.functionCalls()).to.deep.equal([
functionCallPart1.functionCall,
functionCallPart2.functionCall
]);
});
it('good response text/functionCall', async () => {
const enhancedResponse = addHelpers(fakeResponseMixed1);
expect(enhancedResponse.functionCalls()).to.deep.equal([
functionCallPart2.functionCall
]);
expect(enhancedResponse.text()).to.equal('some text');
});
it('good response functionCall/text', async () => {
const enhancedResponse = addHelpers(fakeResponseMixed2);
expect(enhancedResponse.functionCalls()).to.deep.equal([
functionCallPart1.functionCall
]);
expect(enhancedResponse.text()).to.equal('some text');
});
it('good response text/functionCall/text', async () => {
const enhancedResponse = addHelpers(fakeResponseMixed3);
expect(enhancedResponse.functionCalls()).to.deep.equal([
fakeResponseFunctionCalls.candidates?.[0].content.parts[0].functionCall,
fakeResponseFunctionCalls.candidates?.[0].content.parts[1].functionCall
functionCallPart1.functionCall
]);
expect(enhancedResponse.text()).to.equal('some text and more text');
});
it('bad response safety', async () => {
const enhancedResponse = addHelpers(badFakeResponse);
Expand Down
16 changes: 11 additions & 5 deletions packages/vertexai/src/requests/response-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,19 @@ export function addHelpers(
}

/**
* Returns text of first candidate.
* Returns all text found in all parts of first candidate.
*/
export function getText(response: GenerateContentResponse): string {
if (response.candidates?.[0].content?.parts?.[0]?.text) {
return response.candidates[0].content.parts
.map(({ text }) => text)
.join('');
const textStrings = [];
if (response.candidates?.[0].content?.parts) {
for (const part of response.candidates?.[0].content?.parts) {
if (part.text) {
textStrings.push(part.text);
}
}
}
if (textStrings.length > 0) {
return textStrings.join('');
} else {
return '';
}
Comment on lines +99 to 103
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we remove the conditional and just return textStrings.join(''), since [].join('') === ''? Or do we prefer to make the return result more explicit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Andrew brought up that we're not 100% sure we want the delimiter to be '' when there's multiple text Parts and this is a placeholder for now until we find out, so maybe in the future we might make it a space, or a comma, which could cause some unintended output if we forgot we meant to return '' when there's no Parts.

Expand Down
Loading