File size: 5,049 Bytes
2aa4095
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from google.genai import types
from google import genai
from gemini_llm import GeminiLLM

class GeminiToolDefination:
    def __init__(self, geminillm: GeminiLLM, result=None):
        self.geminillm = geminillm
        self.result = result

        self.gemini_thinking_declaration = {
            "name": "GeminiThinking",
            "description": (
                "Use this tool when the user's input suggests they are looking for thoughtful reflection, brainstorming, "
                "hypothetical reasoning, or deep analysis. This includes philosophical questions, complex scenarios, or tasks "
                "where the agent must 'think' or 'reflect' to provide a structured or creative response. "
                "When to use: if the user is asking 'what if', 'analyze', 'brainstorm', or 'explore possibilities'. "
                "Avoid this tool for factual, time-sensitive, or technical queries—use search or code tools instead."
            ),
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": (
                            "Queries asking for reasoning or deep thought. "
                            "Examples: 'What are some possible futures if humans colonize Mars?', "
                            "'Can you explore the societal effects of AI on human creativity?', "
                            "'Is ambition more helpful or harmful in life?'"
                        )
                    }
                },
                "required": ["query"]
            }
        }

        self.google_search_tool = {
            "name": "GoogleSearchTool",
            "description": (
                "Use this tool when the user is asking for specific, real-time, or factual information that may be outside the model's knowledge. "
                "This includes recent news, live events, product prices, names, or anything the model cannot confidently answer from training data alone. "
                "When to use: if the model would otherwise respond with 'I'm not sure', 'As of my last update', or 'I cannot browse the web'. "
                "Avoid this for reasoning or general knowledge that doesn't need live updates."
            ),
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": (
                            "Use for fact-seeking or current-event queries. "
                            "Examples: 'What is the weather in Delhi today?', 'Latest iPhone 16 release date in India', "
                            "'Who is the current CEO of Google?', 'How did the 2024 elections end?'"
                        )
                    }
                },
                "required": ["query"]
            }
        }

        self.gemini_code_execution_tool = {
            "name": "GeminiCodeExecutionTool",
            "description": (
                "Use this tool when the user's prompt involves programming—writing, debugging, explaining, or executing code. "
                "Only invoke this for well-scoped technical/code tasks that require functional accuracy or output simulation. "
                "When to use: if the user asks for code generation, debugging, implementation, or syntax correction. "
                "Avoid this for general tech explanations—those don't require execution."
            ),
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": (
                            "Programming-related prompts. "
                            "Examples: 'Write a Python function to reverse a list', 'Fix this error in my code: ...', "
                            "'What will this JavaScript function return?', 'Create a REST API in Flask'."
                        )
                    }
                },
                "required": ["query"]
            }
        }

        self.tools = types.Tool(function_declarations=[
            self.gemini_thinking_declaration,
            self.google_search_tool,
            self.gemini_code_execution_tool
        ])

        self.config = types.GenerateContentConfig(tools=[self.tools])

    def pick_the_tool(self, prompt_text: str):
        contents = [
            types.Content(
                role="user",
                parts=[types.Part(text=prompt_text)]
            )
        ]

        try:
            response = self.geminillm.client.models.generate_content(
                model=self.geminillm.model_id,
                config=self.config,
                contents=contents
            )
            self.result = response
            return self.result
        except Exception as e:
            raise RuntimeError(f"Failed to generate response with tools: {str(e)}")