Skip to content

Commit 40b043d

Browse files
authored
Merge branch 'main' into ci-make
2 parents 1bc84dc + ebc7aca commit 40b043d

34 files changed

+1298
-27
lines changed

.github/auto-label.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ path:
4545
eventarc: "eventarc"
4646
error-reporting: "clouderrorreporting"
4747
functions: "cloudfunctions"
48+
generative-ai: "genai"
4849
game-servers: "gameservices"
4950
healthcare: "healhcare"
5051
iam: "iam"
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: generative-ai-snippets
16+
on:
17+
push:
18+
branches:
19+
- main
20+
paths:
21+
- 'generative-ai/snippets/**'
22+
- '.github/workflows/generative-ai-snippets.yaml'
23+
pull_request:
24+
paths:
25+
- 'generative-ai/snippets/**'
26+
- '.github/workflows/generative-ai-snippets.yaml'
27+
pull_request_target:
28+
types: [labeled]
29+
paths:
30+
- 'generative-ai/snippets/**'
31+
- '.github/workflows/generative-ai-snippets.yaml'
32+
schedule:
33+
- cron: '0 0 * * 0'
34+
jobs:
35+
test:
36+
if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run'
37+
runs-on: ubuntu-latest
38+
timeout-minutes: 120
39+
permissions:
40+
contents: 'read'
41+
id-token: 'write'
42+
defaults:
43+
run:
44+
working-directory: 'generative-ai/snippets'
45+
steps:
46+
- uses: actions/[email protected]
47+
with:
48+
ref: ${{github.event.pull_request.head.sha}}
49+
- uses: 'google-github-actions/[email protected]'
50+
with:
51+
workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider'
52+
service_account: '[email protected]'
53+
create_credentials_file: 'true'
54+
access_token_lifetime: 600s
55+
- id: secrets
56+
uses: 'google-github-actions/get-secretmanager-secrets@v1'
57+
with:
58+
secrets: |-
59+
caip_id:nodejs-docs-samples-tests/nodejs-docs-samples-ai-platform-caip-project-id
60+
location:nodejs-docs-samples-tests/nodejs-docs-samples-ai-platform-location
61+
- uses: actions/[email protected]
62+
with:
63+
node-version: 16
64+
- name: Get npm cache directory
65+
id: npm-cache-dir
66+
shell: bash
67+
run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT}
68+
- uses: actions/cache@v3
69+
id: npm-cache
70+
with:
71+
path: ${{ steps.npm-cache-dir.outputs.dir }}
72+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
73+
restore-keys: |
74+
${{ runner.os }}-node-
75+
- name: install repo dependencies
76+
run: npm install
77+
working-directory: .
78+
- name: install directory dependencies
79+
run: npm install
80+
- run: npm run build --if-present
81+
- name: set env vars for scheduled run
82+
if: github.event.action == 'schedule'
83+
run: |
84+
echo "MOCHA_REPORTER_SUITENAME=generative-ai-snippets" >> $GITHUB_ENV
85+
echo "MOCHA_REPORTER_OUTPUT=${{github.run_id}}_sponge_log.xml" >> $GITHUB_ENV
86+
echo "MOCHA_REPORTER=xunit" >> $GITHUB_ENV
87+
- run: npm test
88+
env:
89+
LOCATION: ${{ steps.secrets.outputs.location }}
90+
CAIP_PROJECT_ID: ${{ steps.secrets.outputs.caip_id }}
91+
- name: upload test results for FlakyBot workflow
92+
if: github.event.action == 'schedule' && always()
93+
uses: actions/upload-artifact@v3
94+
env:
95+
MOCHA_REPORTER_OUTPUT: "${{github.run_id}}_sponge_log.xml"
96+
with:
97+
name: test-results
98+
path: generative-ai/snippets/${{ env.MOCHA_REPORTER_OUTPUT }}
99+
retention-days: 1
100+
flakybot:
101+
permissions:
102+
contents: 'read'
103+
id-token: 'write'
104+
if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail
105+
uses: ./.github/workflows/flakybot.yaml
106+
needs: [test]

.github/workflows/utils/workflows-secrets.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"iam/deny",
66
"security-center/snippets",
77
"storagetransfer",
8+
"generative-ai/snippets",
89
"vision"
910
]

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ monitoring/opencensus @GoogleCloudPlatform/nodejs-samples-reviewers
5050

5151
# Data & AI
5252
ai-platform @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
53+
generative-ai @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
5354
automl @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
5455
cloud-language @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
5556
contact-center-insights @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers

document-warehouse/create-document-schema.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,38 @@ async function main(
3131
const {DocumentSchemaServiceClient} =
3232
require('@google-cloud/contentwarehouse').v1;
3333

34+
const apiEndpoint =
35+
location === 'us'
36+
? 'contentwarehouse.googleapis.com'
37+
: `${location}-contentwarehouse.googleapis.com`;
38+
3439
// Create service client
35-
const serviceClient = new DocumentSchemaServiceClient();
40+
const serviceClient = new DocumentSchemaServiceClient({
41+
apiEndpoint: apiEndpoint,
42+
});
3643

3744
// Create Document Schema
3845
async function createDocumentSchema() {
39-
// Initialize request argument(s)
40-
const request = {};
41-
42-
// Property Definition
43-
const propertyDefinition = {};
44-
propertyDefinition.name = 'testPropertyDefinitionName'; // Must be unique within a document schema (case insensitive)
45-
propertyDefinition.displayName = 'searchable text';
46-
propertyDefinition.isSearchable = true;
47-
propertyDefinition.textTypeOptions = {};
48-
49-
// Document Schema
50-
const documentSchema = {};
51-
documentSchema.displayName = 'My Test Schema';
52-
documentSchema.propertyDefinitions = [propertyDefinition];
53-
54-
request.documentSchema = documentSchema;
55-
5646
// The full resource name of the location, e.g.:
5747
// projects/{project_number}/locations/{location}
58-
request.parent = `projects/${projectNumber}/locations/${location}`;
48+
const parent = `projects/${projectNumber}/locations/${location}`;
49+
// Initialize request argument(s)
50+
const request = {
51+
parent: parent,
52+
// Document Schema
53+
documentSchema: {
54+
displayName: 'My Test Schema',
55+
// Property Definition
56+
propertyDefinitions: [
57+
{
58+
name: 'testPropertyDefinitionName', // Must be unique within a document schema (case insensitive)
59+
displayName: 'searchable text',
60+
isSearchable: true,
61+
textTypeOptions: {},
62+
},
63+
],
64+
},
65+
};
5966

6067
// Make Request
6168
const response = serviceClient.createDocumentSchema(request);

document-warehouse/delete-document-schema.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,15 @@ async function main(
3434
const {DocumentSchemaServiceClient} =
3535
require('@google-cloud/contentwarehouse').v1;
3636

37+
const apiEndpoint =
38+
location === 'us'
39+
? 'contentwarehouse.googleapis.com'
40+
: `${location}-contentwarehouse.googleapis.com`;
41+
3742
// Create service client
38-
const serviceClient = new DocumentSchemaServiceClient();
43+
const serviceClient = new DocumentSchemaServiceClient({
44+
apiEndpoint: apiEndpoint,
45+
});
3946

4047
// Delete Document Schema
4148
async function deleteDocumentSchema() {

document-warehouse/fetch-acl.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ async function main(
3434
// Import from google cloud
3535
const {DocumentServiceClient} = require('@google-cloud/contentwarehouse').v1;
3636

37+
const apiEndpoint =
38+
location === 'us'
39+
? 'contentwarehouse.googleapis.com'
40+
: `${location}-contentwarehouse.googleapis.com`;
41+
3742
// Create service client
38-
const serviceClient = new DocumentServiceClient();
43+
const serviceClient = new DocumentServiceClient({apiEndpoint: apiEndpoint});
3944

4045
// Fetches access control policies on project or document level.
4146
async function fetchACL() {

document-warehouse/get-document-schema.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ async function main(
3232
const {DocumentSchemaServiceClient} =
3333
require('@google-cloud/contentwarehouse').v1;
3434

35+
const apiEndpoint =
36+
location === 'us'
37+
? 'contentwarehouse.googleapis.com'
38+
: `${location}-contentwarehouse.googleapis.com`;
39+
3540
// Create service client
36-
const serviceClient = new DocumentSchemaServiceClient();
41+
const serviceClient = new DocumentSchemaServiceClient({
42+
apiEndpoint: apiEndpoint,
43+
});
3744

3845
// Get Document Schema
3946
async function getDocumentSchema() {

document-warehouse/get-document.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,15 @@ async function main(
3333
// Import from google cloud
3434
const {DocumentServiceClient} = require('@google-cloud/contentwarehouse').v1;
3535

36+
const apiEndpoint =
37+
location === 'us'
38+
? 'contentwarehouse.googleapis.com'
39+
: `${location}-contentwarehouse.googleapis.com`;
40+
3641
// Create service client
37-
const serviceClient = new DocumentServiceClient();
42+
const serviceClient = new DocumentServiceClient({
43+
apiEndpoint: apiEndpoint,
44+
});
3845

3946
// Get Document Schema
4047
async function getDocument() {

document-warehouse/quickstart.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,16 @@ async function main(
3131
const {DocumentSchemaServiceClient, DocumentServiceClient} =
3232
require('@google-cloud/contentwarehouse').v1;
3333

34+
const apiEndpoint =
35+
location === 'us'
36+
? 'contentwarehouse.googleapis.com'
37+
: `${location}-contentwarehouse.googleapis.com`;
38+
3439
// Create service client
35-
const schemaClient = new DocumentSchemaServiceClient();
36-
const serviceClient = new DocumentServiceClient();
40+
const schemaClient = new DocumentSchemaServiceClient({
41+
apiEndpoint: apiEndpoint,
42+
});
43+
const serviceClient = new DocumentServiceClient({apiEndpoint: apiEndpoint});
3744

3845
// Get Document Schema
3946
async function quickstart() {

document-warehouse/search-document.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ async function main(
3232
// Import from google cloud
3333
const {DocumentServiceClient} = require('@google-cloud/contentwarehouse').v1;
3434

35+
const apiEndpoint =
36+
location === 'us'
37+
? 'contentwarehouse.googleapis.com'
38+
: `${location}-contentwarehouse.googleapis.com`;
39+
3540
// Create service client
36-
const serviceClient = new DocumentServiceClient();
41+
const serviceClient = new DocumentServiceClient({apiEndpoint: apiEndpoint});
3742

3843
// Get Document Schema
3944
async function searchDocuments() {

document-warehouse/set-acl.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ async function main(
3737
//Import service client from google cloud
3838
const {DocumentServiceClient} = require('@google-cloud/contentwarehouse').v1;
3939

40+
const apiEndpoint =
41+
location === 'us'
42+
? 'contentwarehouse.googleapis.com'
43+
: `${location}-contentwarehouse.googleapis.com`;
44+
4045
// Create client
41-
const serviceClient = new DocumentServiceClient();
46+
const serviceClient = new DocumentServiceClient({apiEndpoint: apiEndpoint});
4247

4348
// Set access control policies on project or document level.
4449
async function setACL() {

generative-ai/snippets/countTokens.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
const {VertexAI} = require('@google-cloud/vertexai');
16+
17+
async function countTokens(
18+
projectId = 'PROJECT_ID',
19+
location = 'LOCATION_ID',
20+
model = 'MODEL'
21+
) {
22+
// [START aiplatform_gemini_token_count]
23+
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const projectId = 'your-project-id';
28+
// const location = 'us-central1';
29+
// const model = 'chosen-genai-model';
30+
31+
// Initialize Vertex with your Cloud project and location
32+
const vertex_ai = new VertexAI({project: projectId, location: location});
33+
34+
// Instantiate the model
35+
const generativeModel = vertex_ai.preview.getGenerativeModel({
36+
model: model,
37+
});
38+
39+
const req = {
40+
contents: [{role: 'user', parts: [{text: 'How are you doing today?'}]}],
41+
};
42+
43+
const countTokensResp = await generativeModel.countTokens(req);
44+
console.log('count tokens response: ', countTokensResp);
45+
46+
// [END aiplatform_gemini_token_count]
47+
}
48+
49+
countTokens(...process.argv.slice(2)).catch(err => {
50+
console.error(err.message);
51+
process.exitCode = 1;
52+
});

0 commit comments

Comments
 (0)