Skip to content

Commit 7d7a3f4

Browse files
author
Guiners
committed
adding samples, test, lints
1 parent 7f48533 commit 7d7a3f4

6 files changed

Lines changed: 86 additions & 8 deletions
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.generateContent(projectId);
27+
assert.isTrue(output);
28+
});
29+
});

genai/text-generation/textgen-async-with-txt.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ async function generateContent(
3030
location: location,
3131
});
3232

33-
3433
const response = await ai.models.generateContent({
3534
model: 'gemini-2.5-flash',
36-
contents: 'Compose a song about the adventures of a time-traveling nuggets.',
35+
contents:
36+
'Compose a song about the adventures of a time-traveling nuggets.',
3737
config: {
3838
responseMimeType: 'text/plain',
39-
}
39+
},
4040
});
4141

4242
console.log(response.text);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 generateContent(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION
26+
) {
27+
const ai = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
31+
});
32+
33+
const chatSession = ai.chats.create({
34+
model: 'gemini-2.5-flash',
35+
});
36+
37+
for await (const chunk of await chatSession.sendMessageStream({
38+
message: 'Why lava is red?',
39+
})) {
40+
console.log(chunk.text);
41+
}
42+
43+
return true;
44+
}
45+
// [END googlegenaisdk_textgen_chat_stream_with_txt]
46+
47+
module.exports = {
48+
generateContent,
49+
};

genai/text-generation/textgen-chat-with-txt.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ async function generateContent(
3535
history: [
3636
{
3737
role: 'user',
38-
parts: [{ text: 'Hello' }],
38+
parts: [{text: 'Hello'}],
3939
},
4040
{
4141
role: 'model',
42-
parts: [{ text: 'Great to meet you. What would you like to know?' }],
42+
parts: [{text: 'Great to meet you. What would you like to know?'}],
4343
},
4444
],
4545
});

genai/text-generation/textgen-config-with-txt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async function generateContent(
4646
const response = await ai.models.generateContent({
4747
model: 'gemini-2.5-flash',
4848
contents: 'Why is the sky blue?',
49-
config: config
49+
config: config,
5050
});
5151

5252
console.log(response.text);

genai/text-generation/textgen-with-mute-video.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ async function generateContent(
3030
location: location,
3131
});
3232

33-
3433
const response = await ai.models.generateContent({
3534
model: 'gemini-2.5-flash',
3635
contents: [
@@ -40,7 +39,8 @@ async function generateContent(
4039
{
4140
fileData: {
4241
mimeType: 'video/mp4',
43-
fileUri: 'gs://cloud-samples-data/generative-ai/video/ad_copy_from_video.mp4',
42+
fileUri:
43+
'gs://cloud-samples-data/generative-ai/video/ad_copy_from_video.mp4',
4444
},
4545
},
4646
{

0 commit comments

Comments
 (0)