Skip to content

Initial implementation of google search for Gemini models #49

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mathieubrunpicard
Copy link

@mathieubrunpicard mathieubrunpicard commented Mar 19, 2025

Google Search Integration with Gemini Models

Issue

Reference: #40

Description

This PR adds built-in Google Search support for Gemini models by introducing a with_google_search method to the model.

Example Usage

# Without Google Search
chat = RubyLLM.chat(model: 'gemini-2.0-flash')
chat.ask("What is the google stock price?")

Example response

Unfortunately, I cannot give you the exact, real-time Google (Alphabet Inc.) stock price. Here's why:

  • Stock prices fluctuate constantly. The market changes every second.
  • I'm not a financial platform. I don't have access to live stock market data.

However, here's how you can find the current Google stock price:

  1. Google Search: Just type "Google stock price" into the Google search bar. It will usually display the current price and some basic information.
  2. Financial Websites: Go to reputable financial websites like:
    • Google Finance
    • Yahoo Finance
    • Bloomberg
    • MarketWatch
  3. Brokerage Accounts: If you have an account with a brokerage (like Fidelity, Schwab, Robinhood, etc.), the stock price will be displayed there.

Important Considerations:

  • Ticker Symbols: Google has two main classes of shares:
    • GOOGL: Alphabet Inc. (Class A)
    • GOOG: Alphabet Inc. (Class C)
  • After-Hours Trading: Keep in mind that prices may differ during after-hours trading.
  • Do Your Research: Never make investment decisions based solely on the current stock price. Research the company thoroughly.
# With Google Search enabled
chat.with_google_search.ask("What is the google stock price?")

Example response

Here's the latest information on Google (Alphabet Inc.) stock prices as of March 19, 2025:

Class A (GOOGL):

  • INDMoney reports the stock opened at $163.68 today, after a previous close of $164.29.
  • It reached a high of $165.82 and a low of $162.78 today.
  • Robinhood shows a high today of $165.93 and a low of $162.45.

Class C (GOOG):

  • TradingView indicates the current price is $167.47, a decrease of 2.34% in the last 24 hours.
  • Nasdaq reports a high of $166.44 and a low of $158.8 today.

Key Data (GOOGL):

  • Market Cap: $2.03T
  • Price-Earnings Ratio: 20.57
  • Dividend Yield: 0.36%

Please note: Real-time data may vary slightly across different sources.

Source

Implementation based on: https://ai.google.dev/gemini-api/docs/grounding?lang=rest

@bafraikin
Copy link

Cool !

@crmne crmne added the enhancement New feature or request label Mar 21, 2025
@crmne crmne linked an issue Mar 23, 2025 that may be closed by this pull request
Comment on lines +50 to +58
def with_google_search
raise UnsupportedFunctionsError, "Model #{@model.id} doesn't support function calling" unless @model.supports_functions
raise UnsupportedFunctionsError, "Google search is only supported with Gemini models" unless @model.provider == 'gemini'

@tools = [{
google_search: {}
}]
self
end
Copy link
Owner

Choose a reason for hiding this comment

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

chat.rb shouldn't have any code that's provider specific.

I think we should implement a provider overrides API, e.g. .with_provider_overrides({tools: :google_search}) This name is not set in stone, let's discuss naming and implementation.

Copy link
Author

Choose a reason for hiding this comment

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

What do you think of :

in lib/ruby_llm/chat.rb

def with_provider_overrides(overrides)
      unless @model.supports_functions
        raise UnsupportedFunctionsError, "Model #{@model.id} doesn't support function calling"
      end

      case overrides
      when Hash
        @tools = @provider.format_provider_overrides(overrides)
      when Array
        @tools = overrides
      else
        raise ArgumentError, 'Provider overrides must be a Hash or Array'
      end
      self
end

and then in gemini.rb

def format_provider_overrides(overrides)
        case overrides
        when { tools: :google_search }
          [{ google_search: {} }]
        else
          raise ArgumentError, "Unsupported provider override: #{overrides}"
        end
end

Copy link
Owner

Choose a reason for hiding this comment

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

The best interface IMO would be chat.with_tool(RubyLLM::Providers::Gemini::GoogleSearchTool)

Comment on lines 121 to +125
def execute_tool(tool_call)
if tool_call.name.to_sym == :google_search
return tool_call.arguments
end

Copy link
Owner

Choose a reason for hiding this comment

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

same here, no provider specific code.

Comment on lines +50 to +58
def with_google_search
raise UnsupportedFunctionsError, "Model #{@model.id} doesn't support function calling" unless @model.supports_functions
raise UnsupportedFunctionsError, "Google search is only supported with Gemini models" unless @model.provider == 'gemini'

@tools = [{
google_search: {}
}]
self
end
Copy link
Owner

Choose a reason for hiding this comment

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

The best interface IMO would be chat.with_tool(RubyLLM::Providers::Gemini::GoogleSearchTool)

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 this pull request may close these issues.

Support use of Gemini built-in tools
3 participants