@@ -75,6 +75,13 @@ class ChatWidget extends StatefulWidget {
7575 State <ChatWidget > createState () => _ChatWidgetState ();
7676}
7777
78+ final class Location {
79+ final String city;
80+ final String state;
81+
82+ Location (this .city, this .state);
83+ }
84+
7885class _ChatWidgetState extends State <ChatWidget > {
7986 late final GenerativeModel _model;
8087 late final GenerativeModel _functionCallModel;
@@ -109,16 +116,13 @@ class _ChatWidgetState extends State<ChatWidget> {
109116 // This is a hypothetical API to return a fake weather data collection for
110117 // certain location
111118 Future <Map <String , Object ?>> fetchWeather (
112- double latitude,
113- double longitude,
119+ Location location,
114120 String date,
115121 ) async {
116122 // TODO(developer): Call a real weather API.
117123 // Mock response from the API. In developer live code this would call the
118124 // external API and return what that API returns.
119125 final apiResponse = {
120- 'location' : '$latitude , $longitude ' ,
121- 'date' : date,
122126 'temperature' : 38 ,
123127 'chancePrecipitation' : '56%' ,
124128 'cloudConditions' : 'partly-cloudy' ,
@@ -132,20 +136,14 @@ class _ChatWidgetState extends State<ChatWidget> {
132136 'Get the weather conditions for a specific city on a specific date.' ,
133137 parameters: {
134138 'location' : Schema .object (
135- description: 'The longitude and latitude of the city for which to get '
136- 'the weather. Must always be a nested object of '
137- '`longitude` and `latitude`. The values must be floats.' ,
139+ description: 'The name of the city and its state for which to get '
140+ 'the weather. Only cities in the USA are supported.' ,
138141 properties: {
139- 'latitude' : Schema .number (
140- format: 'float' ,
141- description: 'A numeric value indicating the latitude of the '
142- 'desired location between -90 and 90' ,
142+ 'city' : Schema .string (
143+ description: 'The city of the location.' ,
143144 ),
144- 'longitude' : Schema .number (
145- format: 'float' ,
146- description:
147- 'A numeric value indicating the longitude of the desired '
148- 'location between -180 and 180' ,
145+ 'state' : Schema .string (
146+ description: 'The state of the location.' ,
149147 ),
150148 },
151149 ),
@@ -341,17 +339,35 @@ class _ChatWidgetState extends State<ChatWidget> {
341339 _loading = true ;
342340 });
343341 try {
344- final content = [Content .text ('Create a list of 20 $subject .' )];
342+ final content = [
343+ Content .text (
344+ "For use in a children's card game, generate 10 animal-based "
345+ 'characters.' ,
346+ ),
347+ ];
348+
349+ final jsonSchema = Schema .object (
350+ properties: {
351+ 'characters' : Schema .array (
352+ items: Schema .object (
353+ properties: {
354+ 'name' : Schema .string (),
355+ 'age' : Schema .integer (),
356+ 'species' : Schema .string (),
357+ 'accessory' :
358+ Schema .enumString (enumValues: ['hat' , 'belt' , 'shoes' ]),
359+ },
360+ ),
361+ ),
362+ },
363+ optionalProperties: ['accessory' ],
364+ );
345365
346366 final response = await _model.generateContent (
347367 content,
348368 generationConfig: GenerationConfig (
349369 responseMimeType: 'application/json' ,
350- responseSchema: Schema .array (
351- items: Schema .string (
352- description: 'A single word that a player will need to guess.' ,
353- ),
354- ),
370+ responseSchema: jsonSchema,
355371 ),
356372 );
357373
@@ -537,9 +553,9 @@ class _ChatWidgetState extends State<ChatWidget> {
537553 Map <String , dynamic > location =
538554 functionCall.args['location' ]! as Map <String , dynamic >;
539555 var date = functionCall.args['date' ]! as String ;
540- var latitude = location['latitude' ] ! as double ;
541- var longitude = location['longitude' ] ! as double ;
542- final functionResult = await fetchWeather (latitude, longitude , date);
556+ var city = location['city' ] as String ;
557+ var state = location['state' ] as String ;
558+ final functionResult = await fetchWeather (Location (city, state) , date);
543559 // Send the response to the model so that it can use the result to
544560 // generate text for the user.
545561 response = await functionCallChat.sendMessage (
0 commit comments