Skip to content

Commit 6fdf714

Browse files
author
Guiners
committed
adding samples, test, lints
1 parent a3d808b commit 6fdf714

File tree

3 files changed

+142
-4
lines changed

3 files changed

+142
-4
lines changed
Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,108 @@
1-
//todo
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+
// [START googlegenaisdk_live_structured_output_with_txt]
16+
17+
'use strict';
18+
// todo not working
19+
const {OpenAI} = require('openai');
20+
const {GoogleAuth} = require('google-auth-library');
21+
22+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
23+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
24+
25+
const CalendarEventSchema = {
26+
type: 'object',
27+
properties: {
28+
name: {type: 'string'},
29+
date: {type: 'string'},
30+
participants: {
31+
type: 'array',
32+
items: {type: 'string'},
33+
},
34+
},
35+
required: ['name', 'date', 'participants'],
36+
};
37+
38+
async function generateContent(
39+
projectId = GOOGLE_CLOUD_PROJECT,
40+
location = GOOGLE_CLOUD_LOCATION
41+
) {
42+
console.log('[Init] Starting structured output sample...');
43+
console.log(`[Init] Project: ${projectId}, Location: ${location}`);
44+
45+
const auth = new GoogleAuth({
46+
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
47+
});
48+
const client = await auth.getClient();
49+
const token = await client.getAccessToken();
50+
51+
const ENDPOINT_ID = 'openapi';
52+
const baseURL = `https://${location}-aiplatform.googleapis.com/v1/projects/${projectId}/locations/${location}/endpoints/${ENDPOINT_ID}`;
53+
54+
console.log('[Auth] Successfully retrieved access token');
55+
56+
const ai = new OpenAI({
57+
apiKey: token,
58+
baseURL: baseURL,
59+
});
60+
61+
console.log('[Session] Sending structured output request...');
62+
63+
const completion = await ai.chat.completions.create({
64+
model: 'google/gemini-2.0-flash-001',
65+
messages: [
66+
{role: 'system', content: 'Extract the event information.'},
67+
{
68+
role: 'user',
69+
content: 'Alice and Bob are going to a science fair on Friday.',
70+
},
71+
],
72+
response_format: {
73+
type: 'json_schema',
74+
json_schema: {
75+
name: 'CalendarEvent',
76+
schema: CalendarEventSchema,
77+
},
78+
},
79+
});
80+
81+
const response = completion.choices[0].message;
82+
console.log('[Response] Raw structured output:', response);
83+
84+
let parsed;
85+
try {
86+
parsed = JSON.parse(response.content[0].text);
87+
} catch (err) {
88+
console.error('[Error] Failed to parse structured response:', err);
89+
throw err;
90+
}
91+
92+
console.log('[Parsed Response]', parsed);
93+
94+
// Example expected output:
95+
// {
96+
// name: 'science fair',
97+
// date: 'Friday',
98+
// participants: ['Alice', 'Bob']
99+
// }
100+
101+
return parsed;
102+
}
103+
104+
// [END googlegenaisdk_live_structured_output_with_txt]
105+
106+
module.exports = {
107+
generateContent,
108+
};

genai/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
"dependencies": {
1616
"@google/genai": "1.12.0",
1717
"axios": "^1.6.2",
18-
"supertest": "^7.0.0",
19-
"node-fetch": "^3.3.2"
18+
"google-auth-library": "^10.3.0",
19+
"node-fetch": "^3.3.2",
20+
"openai": "^5.19.1",
21+
"supertest": "^7.0.0"
2022
},
2123
"devDependencies": {
2224
"c8": "^10.0.0",
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
//todo
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('../live/live-structured-ouput-with-txt');
22+
23+
describe('live-structured-ouput-with-txt', () => {
24+
it('should generate audio content from a text prompt', async function () {
25+
this.timeout(18000);
26+
const output = await sample.generateContent(projectId);
27+
console.log('Generated output:', output);
28+
assert(output.length > 0);
29+
});
30+
});

0 commit comments

Comments
 (0)