Skip to content

Commit

Permalink
fix(generative-ai): Use the non stream method instead (#3706)
Browse files Browse the repository at this point in the history
* fix(generative-ai): Use the non stream method instead

* Fix the grounding test

* Fix the grounding test 2

---------

Co-authored-by: Jennifer Davis <[email protected]>
Co-authored-by: Drew Brown <[email protected]>
  • Loading branch information
3 people authored Jul 12, 2024
1 parent afe1bc5 commit 4046b51
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async function generateContentWithGoogleSearchGrounding(
};

const result = await generativeModelPreview.generateContent(request);
const response = result.response;
const response = await result.response;
const groundingMetadata = response.candidates[0].groundingMetadata;
console.log(
'Response: ',
Expand Down
27 changes: 13 additions & 14 deletions generative-ai/snippets/nonStreamingContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,23 @@ async function createNonStreamingContent(
});

const request = {
contents: [{role: 'user', parts: [{text: 'What is Node.js?'}]}],
contents: [
{
role: 'user',
parts: [
{
text: 'Write a story about a magic backpack.',
},
],
},
],
};

console.log('Prompt:');
console.log(request.contents[0].parts[0].text);
console.log('Non-Streaming Response Text:');
console.log(JSON.stringify(request));

// Create the response stream
const responseStream = await generativeModel.generateContentStream(request);
const result = await generativeModel.generateContent(request);

// Wait for the response stream to complete
const aggregatedResponse = await responseStream.response;

// Select the text from the response
const fullTextResponse =
aggregatedResponse.candidates[0].content.parts[0].text;

console.log(fullTextResponse);
console.log(result.response.text);
}
// [END aiplatform_gemini_content_nonstreaming]
// [END generativeaionvertexai_gemini_content_nonstreaming]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ describe('Google search grounding', async () => {
const output = execSync(
`node ./grounding/groundingPublicDataBasic.js ${projectId} ${location} ${model}`
);
assert(output.match(/webSearchQueries.*why is the sky blue?/));
assert(output.match(/webSearchQueries.*Why is the sky blue?/));
});
});
8 changes: 3 additions & 5 deletions generative-ai/snippets/test/nonStreamingContent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ describe('Generative AI NonStreaming Content', () => {
// const location = 'YOUR_LOCATION';
// const model = 'gemini-1.5-flash-001';

it('should create nonstreaming content and begin the conversation the same in each instance', async () => {
it('should create nonstreaming content', async () => {
const output = execSync(
`node ./nonStreamingContent.js ${projectId} ${location} ${model}`
);

// Ensure that the beginning of the conversation is consistent
assert(output.match(/Prompt:/));
assert(output.match(/What is Node.js/));
assert(output.match(/Non-Streaming Response Text:/));
// Assert that the correct prompt was issued
assert(output.match(/Write a story about a magic backpack/));
});
});

0 comments on commit 4046b51

Please sign in to comment.