Skip to content

Commit

Permalink
refactor: adjust samples to use global variables (GoogleCloudPlatform…
Browse files Browse the repository at this point in the history
  • Loading branch information
gryczj authored Jul 30, 2024
1 parent 64b3e1a commit 00fdd8a
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 156 deletions.
17 changes: 8 additions & 9 deletions ai-platform/snippets/predict-code-completion-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@

'use strict';

async function main(project, location = 'us-central1') {
async function main() {
// [START aiplatform_sdk_code_completion_comment]
// [START generativeaionvertexai_sdk_code_completion_comment]
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
* TODO(developer): Update these variables before running the sample.
*/
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';
const PROJECT_ID = process.env.CAIP_PROJECT_ID;
const LOCATION = 'us-central1';
const PUBLISHER = 'google';
const MODEL = 'code-gecko@001';
const aiplatform = require('@google-cloud/aiplatform');

// Imports the Google Cloud Prediction service client
Expand All @@ -37,15 +38,13 @@ async function main(project, location = 'us-central1') {
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};
const publisher = 'google';
const model = 'code-gecko@001';

// Instantiates a client
const predictionServiceClient = new PredictionServiceClient(clientOptions);

async function callPredict() {
// Configure the parent resource
const endpoint = `projects/${project}/locations/${location}/publishers/${publisher}/models/${model}`;
const endpoint = `projects/${PROJECT_ID}/locations/${LOCATION}/publishers/${PUBLISHER}/models/${MODEL}`;

const prompt = {
prefix:
Expand Down Expand Up @@ -88,4 +87,4 @@ process.on('unhandledRejection', err => {
process.exitCode = 1;
});

main(...process.argv.slice(2));
main();
17 changes: 8 additions & 9 deletions ai-platform/snippets/predict-code-completion-test-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

'use strict';

async function main(project, location = 'us-central1') {
async function main() {
// [START aiplatform_sdk_code_completion_test_function]
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
* TODO(developer): Update these variables before running the sample.
*/
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';
const PROJECT_ID = process.env.CAIP_PROJECT_ID;
const LOCATION = 'us-central1';
const PUBLISHER = 'google';
const MODEL = 'code-gecko@001';
const aiplatform = require('@google-cloud/aiplatform');

// Imports the Google Cloud Prediction service client
Expand All @@ -36,15 +37,13 @@ async function main(project, location = 'us-central1') {
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};
const publisher = 'google';
const model = 'code-gecko@001';

// Instantiates a client
const predictionServiceClient = new PredictionServiceClient(clientOptions);

async function callPredict() {
// Configure the parent resource
const endpoint = `projects/${project}/locations/${location}/publishers/${publisher}/models/${model}`;
const endpoint = `projects/${PROJECT_ID}/locations/${LOCATION}/publishers/${PUBLISHER}/models/${MODEL}`;

const prompt = {
prefix:
Expand Down Expand Up @@ -86,4 +85,4 @@ process.on('unhandledRejection', err => {
process.exitCode = 1;
});

main(...process.argv.slice(2));
main();
18 changes: 8 additions & 10 deletions ai-platform/snippets/predict-text-prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@

'use strict';

async function main(project, location = 'us-central1') {
async function main() {
// [START aiplatform_sdk_ideation]
// [START generativeaionvertexai_sdk_ideation]
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
* TODO(developer): Update these variables before running the sample.
*/
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';
const PROJECT_ID = process.env.CAIP_PROJECT_ID;
const LOCATION = 'us-central1';
const PUBLISHER = 'google';
const MODEL = 'text-bison@001';
const aiplatform = require('@google-cloud/aiplatform');

// Imports the Google Cloud Prediction service client
Expand All @@ -38,15 +39,12 @@ async function main(project, location = 'us-central1') {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

const publisher = 'google';
const model = 'text-bison@001';

// Instantiates a client
const predictionServiceClient = new PredictionServiceClient(clientOptions);

async function callPredict() {
// Configure the parent resource
const endpoint = `projects/${project}/locations/${location}/publishers/${publisher}/models/${model}`;
const endpoint = `projects/${PROJECT_ID}/locations/${LOCATION}/publishers/${PUBLISHER}/models/${MODEL}`;

const prompt = {
prompt:
Expand Down Expand Up @@ -85,4 +83,4 @@ process.on('unhandledRejection', err => {
process.exitCode = 1;
});

main(...process.argv.slice(2));
main();
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,9 @@ const {describe, it} = require('mocha');
const cp = require('child_process');
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const cwd = path.join(__dirname, '..');

const project = process.env.CAIP_PROJECT_ID;
const location = 'us-central1';

describe('AI platform predict code completion', () => {
it('should make predictions using a large language model', async () => {
const stdout = execSync(
`node ./predict-code-completion-comment.js ${project} ${location}`,
{
cwd,
}
);
const stdout = execSync('node ./predict-code-completion-comment.js', {cwd});
assert.match(stdout, /Get code completion response/);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,11 @@ const cp = require('child_process');
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const cwd = path.join(__dirname, '..');

const project = process.env.CAIP_PROJECT_ID;
const location = 'us-central1';

describe('AI platform predict code completion', () => {
it('should make predictions using a large language model', async () => {
const stdout = execSync(
`node ./predict-code-completion-test-function.js ${project} ${location}`,
{
cwd,
}
);
const stdout = execSync('node ./predict-code-completion-test-function.js', {
cwd,
});
assert.match(stdout, /Get code completion response/);
});
});
12 changes: 3 additions & 9 deletions ai-platform/snippets/test/predict-text-prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,11 @@ const cp = require('child_process');
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const cwd = path.join(__dirname, '..');

const project = process.env.CAIP_PROJECT_ID;
const location = 'us-central1';

describe('AI platform predict text prompt', () => {
it('should make predictions using a large language model', async () => {
const stdout = execSync(
`node ./predict-text-prompt.js ${project} ${location}`,
{
cwd,
}
);
const stdout = execSync('node ./predict-text-prompt.js', {
cwd,
});
assert.match(stdout, /Get text prompt response/);
});
});
16 changes: 8 additions & 8 deletions generative-ai/snippets/inference/nonStreamMultiModalityBasic.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ const {VertexAI} = require('@google-cloud/vertexai');
/**
* TODO(developer): Update these variables before running the sample.
*/
async function generateContent(
projectId = 'PROJECT_ID',
location = 'us-central1',
model = 'gemini-1.5-flash-001'
) {
const PROJECT_ID = process.env.CAIP_PROJECT_ID;
const LOCATION = 'us-central1';
const MODEL = 'gemini-1.5-flash-001';

async function generateContent() {
// Initialize Vertex AI
const vertexAI = new VertexAI({project: projectId, location: location});
const generativeModel = vertexAI.getGenerativeModel({model: model});
const vertexAI = new VertexAI({project: PROJECT_ID, location: LOCATION});
const generativeModel = vertexAI.getGenerativeModel({model: MODEL});

const request = {
contents: [
Expand Down Expand Up @@ -57,7 +57,7 @@ async function generateContent(
}
// [END generativeaionvertexai_non_stream_multimodality_basic]

generateContent(...process.argv.slice(2)).catch(err => {
generateContent().catch(err => {
console.error(err.message);
process.exitCode = 1;
});
16 changes: 8 additions & 8 deletions generative-ai/snippets/inference/nonStreamTextBasic.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ const {VertexAI} = require('@google-cloud/vertexai');
/**
* TODO(developer): Update these variables before running the sample.
*/
async function generateContent(
projectId = 'PROJECT_ID',
location = 'us-central1',
model = 'gemini-1.5-flash-001'
) {
const PROJECT_ID = process.env.CAIP_PROJECT_ID;
const LOCATION = process.env.LOCATION;
const MODEL = 'gemini-1.5-flash-001';

async function generateContent() {
// Initialize Vertex with your Cloud project and location
const vertexAI = new VertexAI({project: projectId, location: location});
const vertexAI = new VertexAI({project: PROJECT_ID, location: LOCATION});

// Instantiate the model
const generativeModel = vertexAI.getGenerativeModel({
model: model,
model: MODEL,
});

const request = {
Expand All @@ -52,7 +52,7 @@ async function generateContent(
}
// [END generativeaionvertexai_non_stream_text_basic]

generateContent(...process.argv.slice(2)).catch(err => {
generateContent().catch(err => {
console.error(err.message);
process.exitCode = 1;
});
16 changes: 8 additions & 8 deletions generative-ai/snippets/inference/streamMultiModalityBasic.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ const {VertexAI} = require('@google-cloud/vertexai');
/**
* TODO(developer): Update these variables before running the sample.
*/
async function generateContent(
projectId = 'PROJECT_ID',
location = 'us-central1',
model = 'gemini-1.5-flash-001'
) {
const PROJECT_ID = process.env.CAIP_PROJECT_ID;
const LOCATION = process.env.LOCATION;
const MODEL = 'gemini-1.5-flash-001';

async function generateContent() {
// Initialize Vertex AI
const vertexAI = new VertexAI({project: projectId, location: location});
const generativeModel = vertexAI.getGenerativeModel({model: model});
const vertexAI = new VertexAI({project: PROJECT_ID, location: LOCATION});
const generativeModel = vertexAI.getGenerativeModel({model: MODEL});

const request = {
contents: [
Expand Down Expand Up @@ -59,7 +59,7 @@ async function generateContent(
}
// [END generativeaionvertexai_stream_multimodality_basic]

generateContent(...process.argv.slice(2)).catch(err => {
generateContent().catch(err => {
console.error(err.message);
process.exitCode = 1;
});
16 changes: 8 additions & 8 deletions generative-ai/snippets/inference/streamTextBasic.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ const {VertexAI} = require('@google-cloud/vertexai');
/**
* TODO(developer): Update these variables before running the sample.
*/
async function generateContent(
projectId = 'PROJECT_ID',
location = 'us-central1',
model = 'gemini-1.5-flash-001'
) {
const PROJECT_ID = process.env.CAIP_PROJECT_ID;
const LOCATION = process.env.LOCATION;
const MODEL = 'gemini-1.5-flash-001';

async function generateContent() {
// Initialize Vertex with your Cloud project and location
const vertexAI = new VertexAI({project: projectId, location: location});
const vertexAI = new VertexAI({project: PROJECT_ID, location: LOCATION});

// Instantiate the model
const generativeModel = vertexAI.getGenerativeModel({
model: model,
model: MODEL,
});

const request = {
Expand All @@ -53,7 +53,7 @@ async function generateContent(
}
// [END generativeaionvertexai_stream_text_basic]

generateContent(...process.argv.slice(2)).catch(err => {
generateContent().catch(err => {
console.error(err.message);
process.exitCode = 1;
});
16 changes: 8 additions & 8 deletions generative-ai/snippets/safetySettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ const {
/**
* TODO(developer): Update these variables before running the sample.
*/
async function setSafetySettings(
projectId = 'PROJECT_ID',
location = 'us-central1',
model = 'gemini-1.5-flash-001'
) {
const PROJECT_ID = process.env.CAIP_PROJECT_ID;
const LOCATION = 'us-central1';
const MODEL = 'gemini-1.5-flash-001';

async function setSafetySettings() {
// Initialize Vertex with your Cloud project and location
const vertexAI = new VertexAI({project: projectId, location: location});
const vertexAI = new VertexAI({project: PROJECT_ID, location: LOCATION});

// Instantiate the model
const generativeModel = vertexAI.getGenerativeModel({
model: model,
model: MODEL,
// The following parameters are optional
// They can also be passed to individual content generation requests
safety_settings: [
Expand Down Expand Up @@ -75,7 +75,7 @@ async function setSafetySettings(
// [END aiplatform_gemini_safety_settings]
// [END generativeaionvertexai_gemini_safety_settings]

setSafetySettings(...process.argv.slice(2)).catch(err => {
setSafetySettings().catch(err => {
console.error(err.message);
process.exitCode = 1;
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,9 @@ const {describe, it} = require('mocha');
const cp = require('child_process');
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const projectId = process.env.CAIP_PROJECT_ID;
const location = process.env.LOCATION;
const model = 'gemini-1.5-flash-001';

describe('Generative AI Multimodal Text Inference', () => {
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'YOUR_LOCATION';
// const model = 'gemini-1.5-flash-001';

it('should generate text based on a prompt containing text, a video, and an image', async () => {
const output = execSync(
`node ./inference/nonStreamMultiModalityBasic.js ${projectId} ${location} ${model}`
);
const output = execSync('node ./inference/nonStreamMultiModalityBasic.js');
assert(output.length > 0);
});
});
Loading

0 comments on commit 00fdd8a

Please sign in to comment.