Support tools calling

Created on 16 April 2025, 4 months ago

Problem/Motivation

AI module has introduced support for tools in 1.1.x, and some provider (OpenAI, Anthropic) add their support too.

It would be good to add tools support in Gemini provider as well

✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡§πŸ‡¬Bulgaria valthebald Sofia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @valthebald
  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia

    Going to work on this during DDD25

  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia

    @jibla: it turns out that function calling is not yet supported by google-gemini-php. There is a PR https://github.com/google-gemini-php/client/pull/67 against beta branch, but not merged yet.

    Should we park the issue until PR is merged, or switch to vanilla Guzzle requests as suggested in related Google Vertex issue https://www.drupal.org/project/ai_provider_google_vertex/issues/3506263 πŸ› Vertex provider uses PredictionServiceClient and slows down response time. Active ?

  • πŸ‡¬πŸ‡ͺGeorgia jibla
  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia
  • πŸ‡¬πŸ‡ͺGeorgia jibla

    @valthebald exactly! :) I will take care of it, this week

  • Assigned to jibla
  • πŸ‡­πŸ‡ΊHungary balazswmann

    Any updates on this? This module still doesn't work with AI 1.1.0.

  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia

    @balazswmann technically, it does, but you can't use gemini as a chat with tools.
    @jibla do you need some help with that?

  • πŸ‡­πŸ‡ΊHungary balazswmann

    @valthebald

    technically, it does, but you can't use gemini as a chat with tools.

    Yes, I meant the chat and tools compatibility. That's exactly I'd need.

  • πŸ‡¬πŸ‡ͺGeorgia jibla

    @valthebald
    Actually if somebody from the community would suggest a MR, it would help. Otherwise I will try to push this in my team.

  • First commit to issue fork.
  • Merge request !21#3519469 "Support tools calling" β†’ (Open) created by yce
  • πŸ‡­πŸ‡ΊHungary yce

    Hi,

    I've created an MR to support the chat tools.

  • Pipeline finished with Failed
    27 days ago
    #559630
  • πŸ‡¬πŸ‡ͺGeorgia jibla

    @yce Thank you for the contribution!

    Before the code review of the MR, we need a test case to check with. As we don't have any automated tests for the providers yet, I would love if somebody tested this manually.

    I'll mark the issue as Needs Review.

  • πŸ‡ΊπŸ‡ΈUnited States dmundra Eugene, OR

    Attaching a patch file for the current MR to test with.

  • πŸ‡ΊπŸ‡ΈUnited States dmundra Eugene, OR

    Trying to test this with /admin/config/ai/explorers/chat_generator and the existing HtmlToMarkdown function. I ran into an error with the generator not supporting the 'Top K' being a float. Here is the error

    TypeError: Gemini\Data\GenerationConfig::__construct(): Argument #6 ($topK) must be of type ?int, float given, called in /var/www/html/web/modules/custom/gemini_provider/src/Plugin/AiProvider/GeminiProvider.php on line 260 in Gemini\Data\GenerationConfig->__construct() (line 39 of /var/www/html/vendor/google-gemini-php/client/src/Data/GenerationConfig.php)

    Switching that argument type, I was able to generate code but not execute the function. I will continue to try to test but if someone has a better approach please do share.

  • πŸ‡ΊπŸ‡ΈUnited States dmundra Eugene, OR

    An update, after enabling AI agents module and seeing that I can reference other tools like 'Calculator', I tried /admin/config/ai/explorers/chat_generator once again with xdebug and I can see that the code from the MR is calling the Calculator code it is just not outputting it to the screen as tool output. Maybe that is missing?

  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia

    Going to check this today and report the results

  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia

    I get this error: TypeError: Gemini\Data\GenerationConfig::__construct(): Argument #6 ($topK) must be of type ?int, float given, called in /var/www/html/web/modules/custom/gemini_provider/src/Plugin/AiProvider/GeminiProvider.php on line 260 in Gemini\Data\GenerationConfig->__construct() (line 39 of /var/www/html/vendor/google-gemini-php/client/src/Data/GenerationConfig.php).

    (probably related to the signature change in the library between the versions).

    Steps to reproduce:

    1. Enable AI Explorer module
    2. Go to AI Explorer > Chat generation
    3. Select any tool

    Here's my configuration:

  • πŸ‡ΊπŸ‡ΈUnited States dmundra Eugene, OR

    Actually the signature has not changed between version 1 and 2, here is version 1 line https://github.com/google-gemini-php/client/blob/1.0.15/src/Data/Generat... (still an ?int)

  • πŸ‡ΊπŸ‡ΈUnited States dmundra Eugene, OR

    I have this script that I am testing with drush and it calls the function but doesn't generate an output. I left a comment on the MR

    <?php
    
    use Drupal\ai\OperationType\Chat\ChatInput;
    use Drupal\ai\OperationType\Chat\ChatMessage;
    use Drupal\ai\OperationType\Chat\Tools\ToolsInput;
    
    $messages = new ChatInput([
      new ChatMessage('user', 'What is 20 * (4 + 3)?'),
    ]);
    $provider =  \Drupal::service('ai.provider')->createInstance('gemini');
    
    // Set a function call.
    $functions = [];
    $function_instances = [];
    $function_call = \Drupal::service("plugin.manager.ai.function_calls")->createInstance('ai:calculator');
    $function_instances[$function_call->getFunctionName()] = $function_call;
    $functions[] = $function_call->normalize();
    
    // Set chat tool.
    $messages->setChatTools(new ToolsInput($functions));
    
    /** @var \Drupal\ai\OperationType\Chat\ChatOutput $response */
    $response = $provider->chat($messages, 'gemini-2.5-flash', ['my-custom-call']);
    /** @var \Drupal\ai\OperationType\Chat\ChatMessage $return_message */
    $return_message = $response->getNormalized();
    echo $return_message->getText() . "\n";
    // Returns something like "140"
  • πŸ‡ΊπŸ‡ΈUnited States dmundra Eugene, OR

    I submitted a commit that captures the output of the function call if it is an instance of ExecutableFunctionCallInterface. Attaching new patch and inter diff.

  • πŸ‡ΊπŸ‡ΈUnited States dmundra Eugene, OR

    This solution https://www.drupal.org/project/ai/issues/3528549 ✨ Allow tool calling in streamed chat Active might make this redundant so something to watch as well.

  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia

    @dmundra: thanks for the update! Checking soon

Production build 0.71.5 2024