Skip to content

Commit 4b8f228

Browse files
authored
feat(firebaseai): server prompt chat and function calling (#17972)
* add chat and function back to server prompt template * auto function calling for server prompt template * update with api decision * add tool structure * kts support * fix compile error for server template page * Add auto function calling to chat stream, and add test cases * add tools and toolConfig into template request * fix the template function name and update the schema override sample * add too config * some fix for e2e test * fix analyzer * tiny year change * bot review comment * fix analyzer and formatter * remove unrelated files from PR * fix formatter
1 parent bb1e04f commit 4b8f228

9 files changed

Lines changed: 728 additions & 113 deletions

File tree

packages/firebase_ai/firebase_ai/example/lib/pages/function_calling_page.dart

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import 'package:flutter/material.dart';
1616
import 'package:firebase_ai/firebase_ai.dart';
1717
import 'package:firebase_auth/firebase_auth.dart';
18+
import '../utils/function_call_utils.dart';
1819
import '../widgets/message_widget.dart';
1920

2021
class FunctionCallingPage extends StatefulWidget {
@@ -31,13 +32,6 @@ class FunctionCallingPage extends StatefulWidget {
3132
State<FunctionCallingPage> createState() => _FunctionCallingPageState();
3233
}
3334

34-
class Location {
35-
final String city;
36-
final String state;
37-
38-
Location(this.city, this.state);
39-
}
40-
4135
class _FunctionCallingPageState extends State<FunctionCallingPage> {
4236
late GenerativeModel _functionCallModel;
4337
late GenerativeModel _autoFunctionCallModel;
@@ -80,7 +74,7 @@ class _FunctionCallingPageState extends State<FunctionCallingPage> {
8074
'The date for which to get the weather. Date must be in the format: YYYY-MM-DD.',
8175
),
8276
},
83-
callable: _fetchWeatherCallable,
77+
callable: fetchWeatherCallable,
8478
);
8579
_autoFindRestaurantsTool = AutoFunctionDeclaration(
8680
name: 'findRestaurants',
@@ -231,16 +225,6 @@ class _FunctionCallingPageState extends State<FunctionCallingPage> {
231225
};
232226
}
233227

234-
Future<Map<String, Object?>> _fetchWeatherCallable(
235-
Map<String, Object?> args,
236-
) async {
237-
final locationData = args['location']! as Map<String, Object?>;
238-
final city = locationData['city']! as String;
239-
final state = locationData['state']! as String;
240-
final date = args['date']! as String;
241-
return fetchWeather(Location(city, state), date);
242-
}
243-
244228
void _initializeModel() {
245229
final generationConfig = GenerationConfig(
246230
thinkingConfig: _enableThinking
@@ -301,23 +285,6 @@ class _FunctionCallingPageState extends State<FunctionCallingPage> {
301285
);
302286
}
303287

304-
// This is a hypothetical API to return a fake weather data collection for
305-
// certain location
306-
Future<Map<String, Object?>> fetchWeather(
307-
Location location,
308-
String date,
309-
) async {
310-
// TODO(developer): Call a real weather API.
311-
// Mock response from the API. In developer live code this would call the
312-
// external API and return what that API returns.
313-
final apiResponse = {
314-
'temperature': 38,
315-
'chancePrecipitation': '56%',
316-
'cloudConditions': 'partly-cloudy',
317-
};
318-
return apiResponse;
319-
}
320-
321288
/// Actual function to demonstrate the function calling feature.
322289
final fetchWeatherTool = FunctionDeclaration(
323290
'fetchWeather',

0 commit comments

Comments
 (0)