Skip to content

Commit 51d8cee

Browse files
GuinersGuinersmsampathkumargericdong
authored
feat(genai): Sample/textgen part1 (#4160)
* adding samples and tests * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * Update genai/text-generation/textgen-async-with-txt.js Co-authored-by: Sampath Kumar <sam1990kumar@gmail.com> * Update genai/text-generation/textgen-chat-stream-with-txt.js Co-authored-by: Sampath Kumar <sam1990kumar@gmail.com> * adding samples, test, lints * adding samples, test, lints * fixing functions names --------- Co-authored-by: Guiners <rkoza@softserveinc.com> Co-authored-by: Sampath Kumar <sam1990kumar@gmail.com> Co-authored-by: Sampath Kumar <sampathm@google.com> Co-authored-by: Eric Dong <itseric@google.com>
1 parent 9fdf9b6 commit 51d8cee

12 files changed

+450
-3
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../text-generation/textgen-async-with-txt.js');
22+
23+
describe('textgen-async-with-txt', () => {
24+
it('should generate text content from a text prompt and with system instructions', async function () {
25+
this.timeout(100000);
26+
const output = await sample.generateText(projectId);
27+
assert(output.length > 0);
28+
});
29+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../text-generation/textgen-chat-stream-with-txt.js');
22+
23+
describe('textgen-chat-stream-with-txt', () => {
24+
it('should generate text content from a mute video', async function () {
25+
this.timeout(100000);
26+
const output = await sample.generateText(projectId);
27+
assert.isTrue(output);
28+
});
29+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../text-generation/textgen-chat-with-txt.js');
22+
23+
describe('textgen-chat-with-txt', () => {
24+
it('should generate chat content from a text prompt', async function () {
25+
this.timeout(100000);
26+
const output = await sample.generateText(projectId);
27+
assert(output.length > 0);
28+
});
29+
});

genai/test/textgen-code-with-pdf.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const sample = require('../text-generation/textgen-code-with-pdf.js');
2323
describe('textgen-code-with-pdf', () => {
2424
it('should generate text content from a pdf', async function () {
2525
this.timeout(100000);
26-
const output = await sample.generateContent(projectId);
26+
const output = await sample.generateText(projectId);
2727
assert(output.length > 0);
2828
});
2929
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../text-generation/textgen-config-with-txt.js');
22+
23+
describe('textgen-config-with-txt', () => {
24+
it('should generate text content from a text prompt with config', async function () {
25+
this.timeout(100000);
26+
const output = await sample.generateContent(projectId);
27+
assert(output.length > 0);
28+
});
29+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../text-generation/textgen-with-mute-video.js');
22+
23+
describe('textgen-with-mute-video', () => {
24+
it('should generate text content from a mute video', async function () {
25+
this.timeout(100000);
26+
const output = await sample.generateText(projectId);
27+
assert(output.length > 0);
28+
});
29+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2025 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+
'use strict';
16+
17+
// [START googlegenaisdk_textgen_async_with_txt]
18+
const {GoogleGenAI} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
23+
async function generateText(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION
26+
) {
27+
const client = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
31+
});
32+
33+
const response = await client.models.generateContent({
34+
model: 'gemini-2.5-flash',
35+
contents:
36+
'Compose a song about the adventures of a time-traveling squirrel.',
37+
config: {
38+
responseMimeType: 'text/plain',
39+
},
40+
});
41+
42+
console.log(response.text);
43+
44+
// Example response:
45+
// (Verse 1)
46+
// Sammy the nugget, a furry little friend
47+
// Had a knack for adventure, beyond all comprehend
48+
49+
return response.text;
50+
}
51+
52+
// [END googlegenaisdk_textgen_async_with_txt]
53+
54+
module.exports = {
55+
generateText,
56+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2025 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+
'use strict';
16+
17+
// [START googlegenaisdk_textgen_chat_stream_with_txt]
18+
const {GoogleGenAI} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
23+
async function generateText(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION
26+
) {
27+
const client = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
31+
});
32+
33+
const chatSession = client.chats.create({
34+
model: 'gemini-2.5-flash',
35+
});
36+
37+
for await (const chunk of await chatSession.sendMessageStream({
38+
message: 'Why is the sky blue?',
39+
})) {
40+
console.log(chunk.text);
41+
}
42+
// Example response:
43+
// The
44+
// sky appears blue due to a phenomenon called **Rayleigh scattering**. Here's
45+
// a breakdown of why:
46+
// ...
47+
return true;
48+
}
49+
50+
// [END googlegenaisdk_textgen_chat_stream_with_txt]
51+
52+
module.exports = {
53+
generateText,
54+
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2025 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+
'use strict';
16+
17+
// [START googlegenaisdk_textgen_chat_with_txt]
18+
const {GoogleGenAI} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
23+
async function generateText(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION
26+
) {
27+
const client = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
31+
});
32+
33+
const chatSession = client.chats.create({
34+
model: 'gemini-2.5-flash',
35+
history: [
36+
{
37+
role: 'user',
38+
parts: [{text: 'Hello'}],
39+
},
40+
{
41+
role: 'model',
42+
parts: [{text: 'Great to meet you. What would you like to know?'}],
43+
},
44+
],
45+
});
46+
47+
const response = await chatSession.sendMessage({message: 'Tell me a story.'});
48+
console.log(response.text);
49+
50+
// Example response:
51+
// Okay, here's a story for you:
52+
// ...
53+
54+
return response.text;
55+
}
56+
57+
// [END googlegenaisdk_textgen_chat_with_txt]
58+
59+
module.exports = {
60+
generateText,
61+
};

genai/text-generation/textgen-code-with-pdf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const {GoogleGenAI} = require('@google/genai');
2020
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
2121
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
2222

23-
async function generateContent(
23+
async function generateText(
2424
projectId = GOOGLE_CLOUD_PROJECT,
2525
location = GOOGLE_CLOUD_LOCATION
2626
) {
@@ -74,5 +74,5 @@ async function generateContent(
7474
// [END googlegenaisdk_textgen_code_with_pdf]
7575

7676
module.exports = {
77-
generateContent,
77+
generateText,
7878
};

0 commit comments

Comments
 (0)