-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_calling.js
79 lines (66 loc) · 2.97 KB
/
functions_calling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { GoogleGenerativeAI } from '@google/generative-ai';
import dotenv from 'dotenv';
import { searchAttractionsFunctionDeclaration, searchAttractionsFunction } from './attractions_service.js';
import { searchHotelsFunctionDeclaration, searchHotelsFunction } from './hotels_service.js';
import { searchFlightsFunctionDeclaration, searchFlightsFunction } from './flights_service.js';
dotenv.config();
const functions = {
searchAttractions: searchAttractionsFunction,
searchFlights: searchFlightsFunction,
searchHotels: searchHotelsFunction
};
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const generativeModel = genAI.getGenerativeModel({
model: 'gemini-1.5-flash',
tools: {
functionDeclarations: [searchAttractionsFunctionDeclaration, searchFlightsFunctionDeclaration, searchHotelsFunctionDeclaration]
}
});
const chat = generativeModel.startChat();
async function processChat(prompt) {
try {
const result = await chat.sendMessage(prompt);
const call = result.response.functionCalls()[0];
if (!call) {
console.log('No function calls received from the model');
return;
}
const apiResponse = await functions[call.name](call.args);
const detailedPrompt = formatResponse(call.name, apiResponse);
const finalResult = await chat.sendMessage(detailedPrompt);
console.log('Final response:', finalResult.response.text());
} catch (error) {
console.error('Error:', error);
}
}
function formatResponse(functionName, apiResponse) {
const formatters = {
searchFlights: (res) => `
Flight search results:
${res.summary}
Top 3 Flights:
${res.top3Summary}
`,
searchHotels: (res) => `
Hotel search results:
Found ${res.totalHotels} hotels in ${res.cityId} from ${res.dates.checkin} to ${res.dates.checkout} for ${res.adults} adults in ${
res.rooms
} room(s).
Top 3 Hotels:
${res.top3Hotels.map((hotel, i) => `${i + 1}. ${hotel.name} - Rating: ${hotel.rating}, Lowest Price: $${hotel.lowestPrice}`).join('\n')}
`,
searchAttractions: (res) => `
Top attractions in ${res.city}:
${res.attractions.map((attr, i) => `${i + 1}. ${attr.name} - Rating: ${attr.rating}, Address: ${attr.address}`).join('\n')}
`,
default: (res) => `Search results:\n${JSON.stringify(res, null, 2)}`
};
const formatter = formatters[functionName] || formatters.default;
return `${formatter(apiResponse)}\nPlease provide a detailed response that includes this information.`;
}
// Example usage:
// For attractions:
//processChat('what are the most popular attractions in Tokyo, Japan?');
// For flights:
// processChat('find flights from Berlin to Tokyo on January 18, 2025 for 1 passenger in Economy class');
// processChat('find hotels in New York from January 25 to January 26, 2025 for 2 adults in 1 room');