|
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 | +}; |
0 commit comments