File size: 1,234 Bytes
39c0b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from dotenv import load_dotenv
from langchain_core.tools import tool
from google import genai
from google.genai import types

load_dotenv()


@tool("google_search_tool", parse_docstring=True)
def google_search_tool(query: str) -> str:
    """
    Performs a Google Search using Gemini's grounding tool and returns the grounded response text.

    Args:
        query (str): The search query.

    Returns:
        str: The grounded response text from Gemini's Google Search tool, or an error message if it fails.
    """
    try:
        # Configure the client
        client = genai.Client()
        # Define the grounding tool
        grounding_tool = types.Tool(
            google_search=types.GoogleSearch()
        )
        # Configure generation settings
        config = types.GenerateContentConfig(
            tools=[grounding_tool]
        )
        # Make the request
        response = client.models.generate_content(
            model="gemini-2.5-flash",
            contents=query,
            config=config,
        )

        print("\n\ngoogle_search_tool\n\nresponse.text:\n\n"+response.text+"\n\n")

        return response.text
    except Exception as e:
        return f"Error performing Google Search: {str(e)}"