@@ -93,49 +93,23 @@ class _ChatWidgetState extends State<ChatWidget> {
9393 _functionCallModel = FirebaseVertexAI .instance.generativeModel (
9494 model: 'gemini-1.5-flash-preview-0514' ,
9595 tools: [
96- Tool (functionDeclarations: [exchangeRateTool]),
96+ Tool (functionDeclarations: [
97+ FunctionDeclaration (
98+ 'fetchCurrentWeather' ,
99+ 'Returns the weather in a given location.' ,
100+ Schema (SchemaType .object, properties: {
101+ 'location' : Schema (SchemaType .string,
102+ description: 'A location name, like "London".' ),
103+ }, requiredProperties: [
104+ 'location'
105+ ]))
106+ ])
97107 ],
98108 );
99109 _chat = _model.startChat ();
100110 });
101111 }
102112
103- Future <Map <String , Object ?>> findExchangeRate (
104- Map <String , Object ?> arguments,
105- ) async =>
106- // This hypothetical API returns a JSON such as:
107- // {"base":"USD","date":"2024-04-17","rates":{"SEK": 0.091}}
108- {
109- 'date' : arguments['currencyDate' ],
110- 'base' : arguments['currencyFrom' ],
111- 'rates' : < String , Object ? > {arguments['currencyTo' ]! as String : 0.091 },
112- };
113-
114- final exchangeRateTool = FunctionDeclaration (
115- 'findExchangeRate' ,
116- 'Returns the exchange rate between currencies on given date.' ,
117- Schema (
118- SchemaType .object,
119- properties: {
120- 'currencyDate' : Schema (
121- SchemaType .string,
122- description: 'A date in YYYY-MM-DD format or '
123- 'the exact value "latest" if a time period is not specified.' ,
124- ),
125- 'currencyFrom' : Schema (
126- SchemaType .string,
127- description: 'The currency code of the currency to convert from, '
128- 'such as "USD".' ,
129- ),
130- 'currencyTo' : Schema (
131- SchemaType .string,
132- description: 'The currency code of the currency to convert to, '
133- 'such as "USD".' ,
134- ),
135- },
136- ),
137- );
138-
139113 Future <void > initFirebase () async {
140114 await Firebase .initializeApp ();
141115 }
@@ -228,20 +202,6 @@ class _ChatWidgetState extends State<ChatWidget> {
228202 : Theme .of (context).colorScheme.primary,
229203 ),
230204 ),
231- IconButton (
232- tooltip: 'function calling Test' ,
233- onPressed: ! _loading
234- ? () async {
235- await _testFunctionCalling ();
236- }
237- : null ,
238- icon: Icon (
239- Icons .functions,
240- color: _loading
241- ? Theme .of (context).colorScheme.secondary
242- : Theme .of (context).colorScheme.primary,
243- ),
244- ),
245205 IconButton (
246206 tooltip: 'image prompt' ,
247207 onPressed: ! _loading
@@ -426,43 +386,6 @@ class _ChatWidgetState extends State<ChatWidget> {
426386 }
427387 }
428388
429- Future <void > _testFunctionCalling () async {
430- setState (() {
431- _loading = true ;
432- });
433- final chat = _functionCallModel.startChat ();
434- const prompt = 'How much is 50 US dollars worth in Swedish krona?' ;
435-
436- // Send the message to the generative model.
437- var response = await chat.sendMessage (Content .text (prompt));
438-
439- final functionCalls = response.functionCalls.toList ();
440- // When the model response with a function call, invoke the function.
441- if (functionCalls.isNotEmpty) {
442- final functionCall = functionCalls.first;
443- final result = switch (functionCall.name) {
444- // Forward arguments to the hypothetical API.
445- 'findExchangeRate' => await findExchangeRate (functionCall.args),
446- // Throw an exception if the model attempted to call a function that was
447- // not declared.
448- _ => throw UnimplementedError (
449- 'Function not implemented: ${functionCall .name }' ,
450- )
451- };
452- // Send the response to the model so that it can use the result to generate
453- // text for the user.
454- response = await chat
455- .sendMessage (Content .functionResponse (functionCall.name, result));
456- }
457- // When the model responds with non-null text content, print it.
458- if (response.text case final text? ) {
459- _generatedContent.add ((image: null , text: text, fromUser: false ));
460- setState (() {
461- _loading = false ;
462- });
463- }
464- }
465-
466389 Future <void > _testCountToken () async {
467390 setState (() {
468391 _loading = true ;
0 commit comments