[go: nahoru, domu]

Skip to content

Commit

Permalink
remove function calling snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
cynthiajoan committed Jun 4, 2024
1 parent 8d3fa1f commit 1be27c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 89 deletions.
99 changes: 11 additions & 88 deletions vertexai/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,49 +93,23 @@ class _ChatWidgetState extends State<ChatWidget> {
_functionCallModel = FirebaseVertexAI.instance.generativeModel(
model: 'gemini-1.5-flash-preview-0514',
tools: [
Tool(functionDeclarations: [exchangeRateTool]),
Tool(functionDeclarations: [
FunctionDeclaration(
'fetchCurrentWeather',
'Returns the weather in a given location.',
Schema(SchemaType.object, properties: {
'location': Schema(SchemaType.string,
description: 'A location name, like "London".'),
}, requiredProperties: [
'location'
]))
])
],
);
_chat = _model.startChat();
});
}

Future<Map<String, Object?>> findExchangeRate(
Map<String, Object?> arguments,
) async =>
// This hypothetical API returns a JSON such as:
// {"base":"USD","date":"2024-04-17","rates":{"SEK": 0.091}}
{
'date': arguments['currencyDate'],
'base': arguments['currencyFrom'],
'rates': <String, Object?>{arguments['currencyTo']! as String: 0.091},
};

final exchangeRateTool = FunctionDeclaration(
'findExchangeRate',
'Returns the exchange rate between currencies on given date.',
Schema(
SchemaType.object,
properties: {
'currencyDate': Schema(
SchemaType.string,
description: 'A date in YYYY-MM-DD format or '
'the exact value "latest" if a time period is not specified.',
),
'currencyFrom': Schema(
SchemaType.string,
description: 'The currency code of the currency to convert from, '
'such as "USD".',
),
'currencyTo': Schema(
SchemaType.string,
description: 'The currency code of the currency to convert to, '
'such as "USD".',
),
},
),
);

Future<void> initFirebase() async {
await Firebase.initializeApp();
}
Expand Down Expand Up @@ -228,20 +202,6 @@ class _ChatWidgetState extends State<ChatWidget> {
: Theme.of(context).colorScheme.primary,
),
),
IconButton(
tooltip: 'function calling Test',
onPressed: !_loading
? () async {
await _testFunctionCalling();
}
: null,
icon: Icon(
Icons.functions,
color: _loading
? Theme.of(context).colorScheme.secondary
: Theme.of(context).colorScheme.primary,
),
),
IconButton(
tooltip: 'image prompt',
onPressed: !_loading
Expand Down Expand Up @@ -426,43 +386,6 @@ class _ChatWidgetState extends State<ChatWidget> {
}
}

Future<void> _testFunctionCalling() async {
setState(() {
_loading = true;
});
final chat = _functionCallModel.startChat();
const prompt = 'How much is 50 US dollars worth in Swedish krona?';

// Send the message to the generative model.
var response = await chat.sendMessage(Content.text(prompt));

final functionCalls = response.functionCalls.toList();
// When the model response with a function call, invoke the function.
if (functionCalls.isNotEmpty) {
final functionCall = functionCalls.first;
final result = switch (functionCall.name) {
// Forward arguments to the hypothetical API.
'findExchangeRate' => await findExchangeRate(functionCall.args),
// Throw an exception if the model attempted to call a function that was
// not declared.
_ => throw UnimplementedError(
'Function not implemented: ${functionCall.name}',
)
};
// Send the response to the model so that it can use the result to generate
// text for the user.
response = await chat
.sendMessage(Content.functionResponse(functionCall.name, result));
}
// When the model responds with non-null text content, print it.
if (response.text case final text?) {
_generatedContent.add((image: null, text: text, fromUser: false));
setState(() {
_loading = false;
});
}
}

Future<void> _testCountToken() async {
setState(() {
_loading = true;
Expand Down
2 changes: 1 addition & 1 deletion vertexai/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.6
flutter_markdown: ^0.6.20
firebase_vertexai: ^0.1.0
firebase_vertexai: ^0.1.1
firebase_core: ^2.27.0
firebase_app_check: ^0.2.2+2

Expand Down

0 comments on commit 1be27c6

Please sign in to comment.