Skip to content

Commit

Permalink
fix: resolve linting
Browse files Browse the repository at this point in the history
  • Loading branch information
pattishin committed Dec 13, 2023
1 parent 0c96c8f commit d4a1a10
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 110 deletions.
62 changes: 31 additions & 31 deletions generative-ai/snippets/countTokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,41 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const { VertexAI } = require('@google-cloud/vertexai');
const {VertexAI} = require('@google-cloud/vertexai');

async function countTokens(
projectId = 'PROJECT_ID',
location = 'LOCATION_ID',
model = 'MODEL'
projectId = 'PROJECT_ID',
location = 'LOCATION_ID',
model = 'MODEL'
) {
// [START aiplatform_gemini_token_count]

/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'your-project-id';
// const location = 'us-central1';
// const model = 'gemini-pro';

// Initialize Vertex with your Cloud project and location
const vertex_ai = new VertexAI({ project: projectId, location: location });

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

const req = {
contents: [{ role: 'user', parts: [{ text: 'How are you doing today?' }] }],
};

const countTokensResp = await generativeModel.countTokens(req);
console.log('count tokens response: ', countTokensResp);
// [END aiplatform_gemini_token_count]
// [START aiplatform_gemini_token_count]

/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'your-project-id';
// const location = 'us-central1';
// const model = 'gemini-pro';

// Initialize Vertex with your Cloud project and location
const vertex_ai = new VertexAI({project: projectId, location: location});

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

const req = {
contents: [{role: 'user', parts: [{text: 'How are you doing today?'}]}],
};

const countTokensResp = await generativeModel.countTokens(req);
console.log('count tokens response: ', countTokensResp);

// [END aiplatform_gemini_token_count]
}

countTokens(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
console.error(err.message);
process.exitCode = 1;
});
10 changes: 5 additions & 5 deletions generative-ai/snippets/nonStreamingChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const {VertexAI} = require('@google-cloud/vertexai');

function wait(time) {

Check failure on line 17 in generative-ai/snippets/nonStreamingChat.js

View workflow job for this annotation

GitHub Actions / lint

'wait' is defined but never used
return new Promise(resolve => {
setTimeout(resolve, time);
setTimeout(resolve, time);
});
}

Expand All @@ -27,8 +27,8 @@ async function createNonStreamingChat(
) {
// TODO: Find better method. Setting delay to give api time to respond, otherwise it will 404
// await wait(10);
// [START aiplatform_gemini_multiturn_chat]

// [START aiplatform_gemini_multiturn_chat]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
Expand Down Expand Up @@ -63,11 +63,11 @@ async function createNonStreamingChat(
const result3 = await chat.sendMessage(chatInput3);
const response3 = result3.response.candidates[0].content.parts[0].text;
console.log('Chat bot: ', response3);

// [END aiplatform_gemini_multiturn_chat]
}

createNonStreamingChat(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
});
6 changes: 3 additions & 3 deletions generative-ai/snippets/nonStreamingContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ async function createNonStreamingContent(
location = 'LOCATION_ID',
model = 'MODEL'
) {
// [START aiplatform_gemini_function_calling]
// [START aiplatform_gemini_function_calling]

/**
* TODO(developer): Uncomment these variables before running the sample.
*/
Expand Down Expand Up @@ -54,7 +54,7 @@ async function createNonStreamingContent(
aggregatedResponse.candidates[0].content.parts[0].text;

console.log(fullTextResponse);

// [END aiplatform_gemini_function_calling]
}

Expand Down
6 changes: 3 additions & 3 deletions generative-ai/snippets/nonStreamingMultipartContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function createNonStreamingMultipartContent(
image = 'gs://generativeai-downloads/images/scones.jpg',
mimeType = 'image/jpeg'
) {
// [START aiplatform_gemini_get_started]
// [START aiplatform_gemini_get_started]

/**
* TODO(developer): Uncomment these variables before running the sample.
Expand Down Expand Up @@ -71,8 +71,8 @@ async function createNonStreamingMultipartContent(
aggregatedResponse.candidates[0].content.parts[0].text;

console.log(fullTextResponse);
// [END aiplatform_gemini_get_started]

// [END aiplatform_gemini_get_started]
}

createNonStreamingMultipartContent(...process.argv.slice(2)).catch(err => {
Expand Down
90 changes: 46 additions & 44 deletions generative-ai/snippets/safetySettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,61 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const { VertexAI, HarmCategory, HarmBlockThreshold } = require('@google-cloud/vertexai');
const {
VertexAI,
HarmCategory,
HarmBlockThreshold,
} = require('@google-cloud/vertexai');

async function createStreamContent(
) {
// [START aiplatform_gemini_safety_settings]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
const projectId = 'cloud-llm-preview1';
const location = 'us-central1';
const model = 'gemini-pro'
async function createStreamContent() {
// [START aiplatform_gemini_safety_settings]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
const projectId = 'cloud-llm-preview1';
const location = 'us-central1';
const model = 'gemini-pro';

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

// Instantiate the model
const generativeModel = vertexAI.preview.getGenerativeModel({
model: model,
// The following parameters are optional
// They can also be passed to individual content generation requests
safety_settings: [
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
},
],
generation_config: {max_output_tokens: 256},
});
// Instantiate the model
const generativeModel = vertexAI.preview.getGenerativeModel({
model: model,
// The following parameters are optional
// They can also be passed to individual content generation requests
safety_settings: [
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
},
],
generation_config: {max_output_tokens: 256},
});

const request = {
contents: [{ role: 'user', parts: [{ text: 'Tell me something dangerous.' }] }],
};
const request = {
contents: [{role: 'user', parts: [{text: 'Tell me something dangerous.'}]}],
};

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

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

// Log the text response as it streams
for await (const item of responseStream.stream) {
if (item.candidates[0].finishReason === 'SAFETY') {
console.log('This response stream terminated due to safety concerns.')
} else {
process.stdout.write(item.candidates[0].content.parts[0].text);
}
// Log the text response as it streams
for await (const item of responseStream.stream) {
if (item.candidates[0].finishReason === 'SAFETY') {
console.log('This response stream terminated due to safety concerns.');
} else {
process.stdout.write(item.candidates[0].content.parts[0].text);
}
// [END aiplatform_gemini_safety_settings]
}
// [END aiplatform_gemini_safety_settings]
}


createStreamContent(...process.argv.slice(3)).catch(err => {
console.error(err.message);
process.exitCode = 1;
console.error(err.message);
process.exitCode = 1;
});
13 changes: 5 additions & 8 deletions generative-ai/snippets/sendMultiModalPromptWithImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ async function sendMultiModalPromptWithImage(
location = 'LOCATION_ID',

Check failure on line 19 in generative-ai/snippets/sendMultiModalPromptWithImage.js

View workflow job for this annotation

GitHub Actions / lint

'location' is assigned a value but never used
model = 'MODEL'

Check failure on line 20 in generative-ai/snippets/sendMultiModalPromptWithImage.js

View workflow job for this annotation

GitHub Actions / lint

'model' is assigned a value but never used
) {
// [START aiplatform_gemini_single_turn_multi_image]



// [END aiplatform_gemini_single_turn_multi_image]
// [START aiplatform_gemini_single_turn_multi_image]
// [END aiplatform_gemini_single_turn_multi_image]
}

sendMultiModalPromptWithImage(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
console.error(err.message);
process.exitCode = 1;
});
7 changes: 2 additions & 5 deletions generative-ai/snippets/sendMultiModalPromptWithVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ async function sendMultiModalPromptWithImage(
location = 'LOCATION_ID',

Check failure on line 19 in generative-ai/snippets/sendMultiModalPromptWithVideo.js

View workflow job for this annotation

GitHub Actions / lint

'location' is assigned a value but never used
model = 'MODEL'

Check failure on line 20 in generative-ai/snippets/sendMultiModalPromptWithVideo.js

View workflow job for this annotation

GitHub Actions / lint

'model' is assigned a value but never used
) {
// [START aiplatform_gemini_single_turn_video]



// [START aiplatform_gemini_single_turn_video]
// [END aiplatform_gemini_single_turn_video]
}

sendMultiModalPromptWithImage(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
});
4 changes: 2 additions & 2 deletions generative-ai/snippets/streamChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function createStreamChat(
location = 'LOCATION_ID',
model = 'MODEL'
) {
// [START aiplatform_gemini_multiturn_chat]
// [START aiplatform_gemini_multiturn_chat]

/**
* TODO(developer): Uncomment these variables before running the sample.
Expand All @@ -43,7 +43,7 @@ async function createStreamChat(
for await (const item of result1.stream) {
console.log(item.candidates[0].content.parts[0].text);
}

// [END aiplatform_gemini_multiturn_chat]
}

Expand Down
4 changes: 2 additions & 2 deletions generative-ai/snippets/streamContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function createStreamContent(
model = 'MODEL'
) {
// [START aiplatform_gemini_function_calling]

/**
* TODO(developer): Uncomment these variables before running the sample.
*/
Expand Down Expand Up @@ -50,7 +50,7 @@ async function createStreamContent(
for await (const item of responseStream.stream) {
process.stdout.write(item.candidates[0].content.parts[0].text);
}

// [END aiplatform_gemini_function_calling]
}

Expand Down
4 changes: 2 additions & 2 deletions generative-ai/snippets/streamMultipartContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function createStreamMultipartContent(
mimeType = 'image/jpeg'
) {
// [START aiplatform_gemini_get_started]

/**
* TODO(developer): Uncomment these variables before running the sample.
*/
Expand Down Expand Up @@ -67,7 +67,7 @@ async function createStreamMultipartContent(
for await (const item of responseStream.stream) {
process.stdout.write(item.candidates[0].content.parts[0].text);
}

// [END aiplatform_gemini_get_started]
}

Expand Down
2 changes: 1 addition & 1 deletion generative-ai/snippets/test/countTokens.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Count tokens', async () => {
`node ./countTokens.js ${project} ${location} ${model}`
);

// Expect 6 tokens
// Expect 6 tokens
assert(output.match('totalTokens: 6'));
});
});
2 changes: 1 addition & 1 deletion generative-ai/snippets/test/nonStreamingChat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Generative AI NonStreaming Chat', async () => {
const output = execSync(
`node ./nonStreamingChat.js ${project} ${location} ${model}`
);

// Ensure that the beginning of the conversation is consistent
assert(output.match(/User: Hello/));
assert(output.match(/User: Can you tell me a scientific fun fact?/));
Expand Down
4 changes: 3 additions & 1 deletion generative-ai/snippets/test/safetySettings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ describe('Safety settings', async () => {
);

// Expect rejection due to safety concerns
assert(output.match('This response stream terminated due to safety concerns'));
assert(
output.match('This response stream terminated due to safety concerns')
);
});
});
1 change: 0 additions & 1 deletion generative-ai/snippets/test/streamContent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ describe('Generative AI Stream Content', () => {
const output = execSync(
`node ./streamContent.js ${project} ${location} ${model}`
);
;
// Ensure that the beginning of the conversation is consistent
assert(output.match(/Prompt:/));
assert(output.match(/What is Node.js/));
Expand Down
2 changes: 1 addition & 1 deletion generative-ai/snippets/test/streamMultipartContent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('Generative AI Stream Multipart Content', () => {
`node ./streamMultipartContent.js ${project} ${location} ${model} ${image}`
);
// Split up conversation output
const conversation = output.split('\n');
const conversation = output.split('\n');

// Ensure that the conversation is what we expect for this scone image
assert(conversation[0].match(/Prompt Text:/));
Expand Down

0 comments on commit d4a1a10

Please sign in to comment.