Skip to content

FirebaseAI: template chat and function calling api#1416

Draft
cynthiajoan wants to merge 1 commit intomainfrom
firebaseai/template-chat-function
Draft

FirebaseAI: template chat and function calling api#1416
cynthiajoan wants to merge 1 commit intomainfrom
firebaseai/template-chat-function

Conversation

@cynthiajoan
Copy link
Contributor

Description

Provide details of the change, and generalize the change in the PR title above.


Testing

Describe how you've tested these changes.


Type of Change

Place an x the applicable box:

  • Bug fix. Add the issue # below if applicable.
  • New feature. A non-breaking change which adds functionality.
  • Other, such as a build process or documentation change.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the Firebase AI SDK by introducing a comprehensive chat session management system and integrating robust function calling capabilities. It allows developers to maintain conversational context across multiple turns, define and automatically execute functions based on AI model prompts, and configure these tools, thereby expanding the interactivity and utility of AI models within applications.

Highlights

  • New Chat Session Management: Introduced a new TemplateChatSession class to manage conversational turns with a template model, maintaining chat history and supporting both single message and streaming responses.
  • Function Calling API: Implemented logic within TemplateChatSession for automatic function execution based on TemplateTool and TemplateAutoFunctionDeclaration, enabling the model to interact with external tools.
  • Tooling Integration: Modified TemplateGenerativeModel methods (GenerateContentAsyncInternal, GenerateContentStreamAsyncInternal) to accept TemplateTool and TemplateToolConfig parameters, allowing the generative model to utilize defined tools.
  • Simplified Chat Initialization: Added a new StartChat method to TemplateGenerativeModel for easily instantiating a TemplateChatSession with specified parameters, history, and tools.
  • Tool Definition Classes: Created new classes (TemplateFunctionDeclaration, TemplateAutoFunctionDeclaration, TemplateTool, TemplateToolConfig) to define and configure tools and their associated functions for the generative model.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a TemplateChatSession for managing conversations with template-based models, including support for multi-turn function calling. It also adds the necessary TemplateTool and related classes for defining and using tools. The changes are well-structured. I've provided a few suggestions to improve API design, robustness, and code clarity, particularly around function call handling and serialization.

return new Dictionary<string, object>()
{
{ "name", Name },
{ "input_schema", _schemaObject != null ? _schemaObject.ToJson() : "" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

When _schemaObject is null, input_schema is serialized as an empty string (""). An empty string is not a valid JSON Schema and will likely cause a request failure. A more correct representation for a schema with no properties is an empty object ({}).

        { "input_schema", _schemaObject != null ? _schemaObject.ToJson() : new Dictionary<string, object>() }

Comment on lines +201 to +208
if (function.Callable != null)
{
result = await function.Callable(call.Args.ToDictionary(k => k.Key, k => k.Value));
}
else
{
result = null;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation for a null Callable on a TemplateAutoFunctionDeclaration will result in a null result for the function call. This could be confusing or hide an implementation error. It would be better to throw an informative exception if a function is called without a Callable implementation. The surrounding try-catch block will then handle this exception and provide a clear error message to the developer.

               if (function.Callable != null)
               {
                 result = await function.Callable(call.Args.ToDictionary(k => k.Key, k => k.Value));
               }
               else
               {
                 throw new InvalidOperationException($"Auto function '{function.Name}' is missing a callable implementation.");
               }

Comment on lines +73 to +90
public Func<IDictionary<string, object>, Task<IDictionary<string, object>>> Callable { get; }

/// <summary>
/// Constructs a TemplateAutoFunctionDeclaration.
/// </summary>
/// <param name="name">The name of the function.</param>
/// <param name="callable">The function to execute when requested by the model.</param>
/// <param name="parameters">Optional dictionary of parameters schema.</param>
/// <param name="optionalParameters">Optional list of parameter names that are not required.</param>
public TemplateAutoFunctionDeclaration(
string name,
Func<IDictionary<string, object>, Task<IDictionary<string, object>>> callable,
IDictionary<string, Schema> parameters = null,
IEnumerable<string> optionalParameters = null)
: base(name, parameters, optionalParameters)
{
Callable = callable;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Callable property on TemplateAutoFunctionDeclaration is defined to return a Task<IDictionary<string, object>>. This forces developers to wrap any simple return value (like a string or number) into a dictionary. For better flexibility and developer experience, consider changing the return type to Task<object>. The calling code in TemplateChatSession already handles an object result, so this change would make the API more ergonomic for developers implementing tool functions.

    public Func<IDictionary<string, object>, Task<object>> Callable { get; }

    /// <summary>
    /// Constructs a TemplateAutoFunctionDeclaration.
    /// </summary>
    /// <param name="name">The name of the function.</param>
    /// <param name="callable">The function to execute when requested by the model.</param>
    /// <param name="parameters">Optional dictionary of parameters schema.</param>
    /// <param name="optionalParameters">Optional list of parameter names that are not required.</param>
    public TemplateAutoFunctionDeclaration(
        string name,
        Func<IDictionary<string, object>, Task<object>> callable,
        IDictionary<string, Schema> parameters = null,
        IEnumerable<string> optionalParameters = null)
        : base(name, parameters, optionalParameters)
    {
      Callable = callable;
    }

Comment on lines +127 to +128
if (_functionDeclarations == null) return Enumerable.Empty<TemplateAutoFunctionDeclaration>();
return _functionDeclarations.OfType<TemplateAutoFunctionDeclaration>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The constructor for TemplateTool ensures that _functionDeclarations is never null. Therefore, the null check _functionDeclarations == null is redundant and can be removed for cleaner code.

      return _functionDeclarations.OfType<TemplateAutoFunctionDeclaration>();

internal Dictionary<string, object> ToJson()
{
var json = new Dictionary<string, object>();
if (_functionDeclarations != null && _functionDeclarations.Any())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The constructor for TemplateTool ensures that _functionDeclarations is never null. Therefore, the null check _functionDeclarations != null is redundant and can be removed for cleaner code.

      if (_functionDeclarations.Any())

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.

1 participant