Skip to content
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

Support use of Gemini built-in tools #40

Open
mathieubrunpicard opened this issue Mar 17, 2025 · 3 comments · May be fixed by #49
Open

Support use of Gemini built-in tools #40

mathieubrunpicard opened this issue Mar 17, 2025 · 3 comments · May be fixed by #49
Labels
enhancement New feature or request

Comments

@mathieubrunpicard
Copy link

mathieubrunpicard commented Mar 17, 2025

Hi,

It would be great if the gem would support Gemini's built-in tools like GoogleSearch.

To enable it in the API, the request is as follows:

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GOOGLE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
      "contents": [
          {
              "parts": [
                  {"text": "What is the current Google stock price?"}
              ]
          }
      ],
      "tools": [
          {
              "google_search": {}
          }
      ]
  }' > result.json

I was thinking of using it this way

chat = RubyLLM.chat(model: 'gemini-2.0-flash')
chat.with_google_search.ask "What is Google Stock Price Today"

I suggest to update providers/gemini/chat.rb with this method

def with_google_search
          @tools ||= []
    @tools << { google_search: {} }
    self
end

and refacto format_tools in providers/gemini/chat.rb

def format_tools(tools)
  return [] if tools.empty?

  formatted_tools = tools.map do |tool|
    if tool.is_a?(Hash) && tool.key?(:google_search)
      tool  
    else
      {
        functionDeclarations: [function_declaration_for(tool)]
      }
    end
  end

  formatted_tools.flatten
end

What do you think ?

@geneccx
Copy link

geneccx commented Mar 18, 2025

FWIW, I tried adding this functionality but it seems to hang when making tool queries when you have google_search and custom tools enabled.

@mathieubrunpicard
Copy link
Author

@geneccx Did you see what HTTP call was made when specifying the google_search tool ?

@mathieubrunpicard
Copy link
Author

Got something working.
Will open a PR :

chat = RubyLLM.chat(model: 'gemini-2.0-flash')
chat.with_google_search.ask "What is Google Stock Price Today"
=> 
#<RubyLLM::Message:0x0000000127f32f00
 @content=
  "Here's the information on Google's stock price today, March 19, 2025:\n\n**GOOG (Alphabet Inc. Class C):**\n\n*   The current price is $162.67 USD.\n*   It has decreased by -2.34% in the past 24 hours.\n\n**GOOGL (Alphabet Inc. Class A):**\n\n*   The previous close was $165.49.\n*   Today's high: $165.82\n*   Today's low: $162.78\n\nKeep in mind that real-time data may have a delay of at least 15 minutes.\n",
 @input_tokens=6,
 @model_id="gemini-2.0-flash",
 @output_tokens=153,
 @role=:assistant,
 @tool_call_id=nil,
 @tool_calls=nil>

@crmne crmne linked a pull request Mar 23, 2025 that will close this issue
@crmne crmne added the enhancement New feature or request label Mar 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants