const {GoogleGenAI, Type} = require('@google/genai');
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
async function generateFunctionDesc(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});
const get_album_sales = {
name: 'get_album_sales',
description: 'Gets the number of albums sold',
parameters: {
type: Type.OBJECT,
properties: {
albums: {
type: Type.ARRAY,
description: 'List of albums',
items: {
description: 'Album and its sales',
type: Type.OBJECT,
properties: {
album_name: {
type: Type.STRING,
description: 'Name of the music album',
},
copies_sold: {
type: Type.INTEGER,
description: 'Number of copies sold',
},
},
},
},
},
},
};
const sales_tool = {
functionDeclarations: [get_album_sales],
};
const prompt = `
At Stellar Sounds, a music label, 2024 was a rollercoaster. "Echoes of the Night", a debut synth-pop album,
surprisingly sold 350,000 copies, while veteran rock band "Crimson Tide's" latest, "Reckless Hearts",
lagged at 120,000. Their up-and-coming indie artist, "Luna Bloom's" EP, "Whispers of Dawn",
secured 75,000 sales. The biggest disappointment was the highly-anticipated rap album "Street Symphony"
only reaching 100,000 units. Overall, Stellar Sounds moved over 645,000 units this year, revealing unexpected
trends in music consumption.
`;
const response = await client.models.generateContent({
model: 'gemini-2.5-flash',
contents: prompt,
config: {
tools: [sales_tool],
temperature: 0,
},
});
const output = JSON.stringify(response.functionCalls, null, 2);
console.log(output);
// Example response:
// [FunctionCall(
// id=None,
// name="get_album_sales",
// args={
// "albums": [
// {"album_name": "Echoes of the Night", "copies_sold": 350000},
// {"copies_sold": 120000, "album_name": "Reckless Hearts"},
// {"copies_sold": 75000, "album_name": "Whispers of Dawn"},
// {"copies_sold": 100000, "album_name": "Street Symphony"},
// ]
// },
// )]
return output;
}