Skip to content

Commit b1231b6

Browse files
Addressed review comments and updated tests accordingly
1 parent 1b2ff96 commit b1231b6

File tree

5 files changed

+197
-123
lines changed

5 files changed

+197
-123
lines changed

model-armor/snippets/sanitizeModelResponse.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
* @param {string} templateId - Identifier of the template to use for sanitization.
2323
* @param {string} modelResponse - The text response from a model that needs to be sanitized.
2424
*/
25-
async function main(projectId, locationId, templateId, modelResponse) {
25+
async function sanitizeModelResponse(
26+
projectId,
27+
locationId,
28+
templateId,
29+
modelResponse
30+
) {
2631
// [START modelarmor_sanitize_model_response]
2732
/**
2833
* TODO(developer): Uncomment these variables before running the sample.
@@ -37,21 +42,23 @@ async function main(projectId, locationId, templateId, modelResponse) {
3742
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
3843
});
3944

40-
async function sanitizeModelResponse() {
41-
const request = {
42-
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
43-
modelResponseData: {
44-
text: modelResponse,
45-
},
46-
};
45+
const request = {
46+
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
47+
modelResponseData: {
48+
text: modelResponse,
49+
},
50+
};
4751

48-
const [response] = await client.sanitizeModelResponse(request);
49-
console.log(JSON.stringify(response, null, 2));
50-
}
51-
52-
sanitizeModelResponse();
52+
const [response] = await client.sanitizeModelResponse(request);
53+
console.log(JSON.stringify(response, null, 2));
5354
// [END modelarmor_sanitize_model_response]
55+
return response;
5456
}
5557

56-
const args = process.argv.slice(2);
57-
main(...args).catch(console.error);
58+
module.exports = sanitizeModelResponse;
59+
60+
// TODO(developer): Uncomment below lines before running the sample.
61+
// sanitizeModelResponse(...process.argv.slice(2)).catch(err => {
62+
// console.error(err.message);
63+
// process.exitCode = 1;
64+
// });

model-armor/snippets/sanitizeModelResponseWithUserPrompt.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @param {string} modelResponse - The text response from a model that needs to be sanitized.
2424
* @param {string} userPrompt - The original user prompt that generated the model response.
2525
*/
26-
async function main(
26+
async function sanitizeModelResponseWithUserPrompt(
2727
projectId,
2828
locationId,
2929
templateId,
@@ -45,22 +45,24 @@ async function main(
4545
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
4646
});
4747

48-
async function sanitizeModelResponseWithUserPrompt() {
49-
const request = {
50-
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
51-
modelResponseData: {
52-
text: modelResponse,
53-
},
54-
userPrompt: userPrompt,
55-
};
48+
const request = {
49+
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
50+
modelResponseData: {
51+
text: modelResponse,
52+
},
53+
userPrompt: userPrompt,
54+
};
5655

57-
const [response] = await client.sanitizeModelResponse(request);
58-
console.log(JSON.stringify(response, null, 2));
59-
}
60-
61-
sanitizeModelResponseWithUserPrompt();
56+
const [response] = await client.sanitizeModelResponse(request);
57+
console.log(JSON.stringify(response, null, 2));
58+
return response;
6259
// [END modelarmor_sanitize_model_response_with_user_prompt]
6360
}
6461

65-
const args = process.argv.slice(2);
66-
main(...args).catch(console.error);
62+
module.exports = sanitizeModelResponseWithUserPrompt;
63+
64+
// TODO(developer): Uncomment below lines before running the sample.
65+
// sanitizeModelResponseWithUserPrompt(...process.argv.slice(2)).catch(err => {
66+
// console.error(err.message);
67+
// process.exitCode = 1;
68+
// });

model-armor/snippets/sanitizeUserPrompt.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
* @param {string} templateId - Identifier of the template to use for sanitization.
2323
* @param {string} userPrompt - The user's text prompt that needs to be sanitized.
2424
*/
25-
async function main(projectId, locationId, templateId, userPrompt) {
25+
async function sanitizeUserPrompt(
26+
projectId,
27+
locationId,
28+
templateId,
29+
userPrompt
30+
) {
2631
// [START modelarmor_sanitize_user_prompt]
2732
/**
2833
* TODO(developer): Uncomment these variables before running the sample.
@@ -37,21 +42,23 @@ async function main(projectId, locationId, templateId, userPrompt) {
3742
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
3843
});
3944

40-
async function sanitizeUserPrompt() {
41-
const request = {
42-
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
43-
userPromptData: {
44-
text: userPrompt,
45-
},
46-
};
45+
const request = {
46+
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
47+
userPromptData: {
48+
text: userPrompt,
49+
},
50+
};
4751

48-
const [response] = await client.sanitizeUserPrompt(request);
49-
console.log(JSON.stringify(response, null, 2));
50-
}
51-
52-
sanitizeUserPrompt();
52+
const [response] = await client.sanitizeUserPrompt(request);
53+
console.log(JSON.stringify(response, null, 2));
54+
return response;
5355
// [END modelarmor_sanitize_user_prompt]
5456
}
5557

56-
const args = process.argv.slice(2);
57-
main(...args).catch(console.error);
58+
module.exports = sanitizeUserPrompt;
59+
60+
// TODO(developer): Uncomment below lines before running the sample.
61+
// sanitizeUserPrompt(...process.argv.slice(2)).catch(err => {
62+
// console.error(err.message);
63+
// process.exitCode = 1;
64+
// });

model-armor/snippets/screenPdfFile.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
* @param {string} templateId - The template ID used for sanitization.
2323
* @param {string} pdfContentFilename - Path to a PDF file.
2424
*/
25-
async function main(projectId, locationId, templateId, pdfContentFilename) {
25+
async function screenPdfFile(
26+
projectId,
27+
locationId,
28+
templateId,
29+
pdfContentFilename
30+
) {
2631
// [START modelarmor_screen_pdf_file]
2732
/**
2833
* TODO(developer): Uncomment these variables before running the sample.
@@ -60,8 +65,14 @@ async function main(projectId, locationId, templateId, pdfContentFilename) {
6065

6166
const [response] = await client.sanitizeUserPrompt(request);
6267
console.log(JSON.stringify(response, null, 2));
68+
return response;
6369
// [END modelarmor_screen_pdf_file]
6470
}
6571

66-
const args = process.argv.slice(2);
67-
main(...args).catch(console.error);
72+
module.exports = screenPdfFile;
73+
74+
// TODO(developer): Uncomment below lines before running the sample.
75+
// screenPdfFile(...process.argv.slice(2)).catch(err => {
76+
// console.error(err.message);
77+
// process.exitCode = 1;
78+
// });

0 commit comments

Comments
 (0)