Skip to content

feat(genai): Add GenAI SDK samples (1) #4093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 13, 2025
46 changes: 46 additions & 0 deletions genai/count-tokens/counttoken-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_counttoken_with_txt]
const {GoogleGenAI} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';

async function countTokens(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const response = await ai.models.countTokens({
model: 'gemini-2.0-flash',
contents: 'What is the highest mountain in Africa?',
});

console.log(response);

return response.totalTokens;
}
// [END googlegenaisdk_counttoken_with_txt]

module.exports = {
countTokens,
};
27 changes: 27 additions & 0 deletions genai/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "nodejs-genai-samples",
"private": true,
"license": "Apache-2.0",
"author": "Google LLC",
"engines": {
"node": ">=16.0.0"
},
"files": [
"*.js"
],
"scripts": {
"test": "c8 mocha -p -j 2 --timeout 2400000 test/*.test.js test/**/*.test.js"
},
"dependencies": {
"@google/genai": "^0.13.0",
"axios": "^1.6.2",
"supertest": "^7.0.0"
},
"devDependencies": {
"c8": "^10.0.0",
"chai": "^4.5.0",
"mocha": "^10.0.0",
"sinon": "^18.0.0",
"uuid": "^10.0.0"
}
}
28 changes: 28 additions & 0 deletions genai/test/counttoken-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../count-tokens/counttoken-with-txt.js');

describe('counttoken-with-txt', async () => {
it('should return the total token count for a text prompt', async () => {
const output = await sample.countTokens(projectId);
assert(output > 0);
});
});
28 changes: 28 additions & 0 deletions genai/test/textgen-sys-instr-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../text-generation/textgen-sys-instr-with-txt.js');

describe('textgen-sys-instr-with-txt', async () => {
it('should generate text content from a text prompt and with system instructions', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0 && output.includes('bagels'));
});
});
28 changes: 28 additions & 0 deletions genai/test/textgen-with-multi-img.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../text-generation/textgen-with-multi-img.js');

describe('textgen-with-multi-img', async () => {
it('should generate text content from a text prompt and multiple images', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0 && output.includes('blueberry'));
});
});
28 changes: 28 additions & 0 deletions genai/test/textgen-with-txt-img.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../text-generation/textgen-with-txt-img.js');

describe('textgen-with-txt-img', async () => {
it('should generate text content from a text prompt and an image', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0 && output.includes('image'));
});
});
28 changes: 28 additions & 0 deletions genai/test/textgen-with-txt-stream.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../text-generation/textgen-with-txt-stream.js');

describe('textgen-with-txt-stream', async () => {
it('should generate streaming text content from a text prompt', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0 && output.includes('sky'));
});
});
28 changes: 28 additions & 0 deletions genai/test/textgen-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../text-generation/textgen-with-txt.js');

describe('textgen-with-txt', async () => {
it('should generate text content from a text prompt', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0 && output.includes('AI'));
});
});
28 changes: 28 additions & 0 deletions genai/test/textgen-with-video.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../text-generation/textgen-with-video.js');

describe('textgen-with-video', async () => {
it('should generate text content from a text prompt and a video', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0 && output.includes('video'));
});
});
28 changes: 28 additions & 0 deletions genai/test/tools-func-desc-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../tools/tools-func-desc-with-txt.js');

describe('tools-func-desc-with-txt', async () => {
it('should generate a function call', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0);
});
});
57 changes: 57 additions & 0 deletions genai/text-generation/textgen-sys-instr-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_textgen_sys_instr_with_txt]
const {GoogleGenAI} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';

async function generateContent(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const prompt = `
User input: I like bagels.
Answer:
`;

const response = await ai.models.generateContent({
model: 'gemini-2.0-flash',
contents: prompt,
config: {
systemInstruction: [
'You are a language translator.',
'Your mission is to translate text in English to French.',
],
},
});

console.log(response.text);

return response.text;
}
// [END googlegenaisdk_textgen_sys_instr_with_txt]

module.exports = {
generateContent,
};
Loading
Loading