Skip to content

Adding support for tool calling into the providers #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 19, 2025

Conversation

csells
Copy link
Contributor

@csells csells commented May 14, 2025

This change allows the user to handle tool calls during the processing of LLM requests, e.g.

class ChatPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(title: const Text(App.title)),
    body: LlmChatView(
      provider: GeminiProvider(
        model: GenerativeModel(
          model: 'gemini-2.0-flash',
          apiKey: geminiApiKey,
          tools: [
            Tool(
              functionDeclarations: [
                FunctionDeclaration(
                  'get_temperature',
                  'Get the current local temperature',
                  Schema.object(properties: {}),
                ),
                FunctionDeclaration(
                  'get_time',
                  'Get the current local time',
                  Schema.object(properties: {}),
                ),
              ],
            ),
          ],
        ),
        onFunctionCall: _onFunctionCall,
      ),
    ),
  );

  Future<Map<String, Object?>?> _onFunctionCall(
    FunctionCall functionCall,
  ) async {
    // note: just as an example, we're not actually calling any external APIs
    return switch (functionCall.name) {
      'get_temperature' => {'temperature': 60, 'unit': 'F'},
      'get_time' => {'time': DateTime(1970, 1, 1).toIso8601String()},
      _ => throw Exception('Unknown function call: ${functionCall.name}'),
    };
  }
}

issues that are fixed by this PR

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read the [Flutter Style Guide] recently, and have followed its advice.
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • All existing and new tests are passing.

@csells csells requested a review from domesticmouse May 14, 2025 23:58
@csells csells merged commit 2731950 into flutter:main May 19, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants