-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgemini_agent.py
More file actions
288 lines (242 loc) · 9.6 KB
/
Copy pathgemini_agent.py
File metadata and controls
288 lines (242 loc) · 9.6 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import os
from typing import Any, Dict, List
from dotenv import load_dotenv
from google import genai
from google.genai import types
# --- Setup & client ----------------------------------------------------------
load_dotenv()
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
raise RuntimeError("GEMINI_API_KEY is not set")
client = genai.Client(api_key=api_key)
# Default to Gemini 3 Flash preview (override with GEMINI_MODEL="gemini-3-pro-preview" if desired)
MODEL_ID = os.getenv("GEMINI_MODEL", "gemini-3-flash-preview")
# --- Custom functions (business logic) ---------------------------------------
def save_note(content: str, filename: str) -> str:
"""Append a note to a local text file."""
try:
with open(filename, "a", encoding="utf-8") as f:
f.write(content + "\n\n")
return f"Note saved to '{filename}'."
except Exception as e:
return f"Error saving note: {e!r}"
def analyze_sentiment(text: str) -> Dict[str, Any]:
"""Very simple rule-based sentiment analysis."""
text_lower = text.lower()
pos = ["good", "great", "excellent", "positive", "happy", "love"]
neg = ["bad", "terrible", "awful", "negative", "sad", "hate"]
score = sum(w in text_lower for w in pos) - sum(w in text_lower for w in neg)
label = "Positive" if score > 0 else "Negative" if score < 0 else "Neutral"
return {"sentiment": label, "score": score}
def generate_concise_summary(text_input: str, max_words: int) -> str:
"""Naive word-limited summary."""
words = text_input.split()
if max_words <= 0 or not words:
return ""
if len(words) <= max_words:
return text_input
return " ".join(words[:max_words]) + "..."
def web_research(query: str, url: str | None = None) -> Dict[str, Any]:
"""
Use Google Search + URL Context to get fresh info and summarize it.
This is where we integrate the google_search + url_context tools.
"""
tools = [
{"google_search": {}},
{"url_context": {}},
]
prompt_parts = [
f"Use Google Search to find recent, relevant information about: {query}.",
"Provide a short 3-bullet summary with one sentence per bullet.",
]
if url:
prompt_parts.append(f"Also read and use this URL as context if relevant: {url}")
prompt = " ".join(prompt_parts)
try:
resp = client.models.generate_content(
model=MODEL_ID,
contents=prompt,
config=types.GenerateContentConfig(tools=tools),
)
summary_text = resp.text or ""
return {"status": "ok", "summary": summary_text}
except Exception as e:
return {"status": "error", "error": str(e)}
# --- Function schemas --------------------------------------------------------
save_note_decl = types.FunctionDeclaration(
name="save_note",
description="Save a short research note to a local text file.",
parameters_json_schema={
"type": "object",
"properties": {
"content": {"type": "string", "description": "Text of the note."},
"filename": {
"type": "string",
"description": "File name, e.g. 'research_notes.txt'.",
},
},
"required": ["content", "filename"],
},
)
analyze_sentiment_decl = types.FunctionDeclaration(
name="analyze_sentiment",
description="Analyze the overall sentiment of the given text.",
parameters_json_schema={
"type": "object",
"properties": {
"text": {"type": "string", "description": "Text to analyze."}
},
"required": ["text"],
},
)
generate_concise_summary_decl = types.FunctionDeclaration(
name="generate_concise_summary",
description="Generate a short summary of the given text.",
parameters_json_schema={
"type": "object",
"properties": {
"text_input": {"type": "string", "description": "Source text."},
"max_words": {
"type": "integer",
"description": "Max words in summary.",
},
},
"required": ["text_input", "max_words"],
},
)
web_research_decl = types.FunctionDeclaration(
name="web_research",
description=(
"Search the web for recent information using Google Search and "
"optionally analyze a specific URL, then return a short summary."
),
parameters_json_schema={
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Topic or question to research.",
},
"url": {
"type": "string",
"description": "Optional URL to read/analyze.",
},
},
"required": ["query"],
},
)
custom_functions_tool = types.Tool(
function_declarations=[
save_note_decl,
analyze_sentiment_decl,
generate_concise_summary_decl,
web_research_decl,
]
)
# IMPORTANT: for the function-calling request, ONLY include function_declarations.
# Do NOT include google_search / url_context here, or you'll hit the 400 error.
TOOLS = [custom_functions_tool]
# --- Function-call dispatcher -------------------------------------------------
def run_custom_function(fc: types.FunctionCall | Any) -> Dict[str, Any]:
name = getattr(fc, "name", None)
call_obj = getattr(fc, "function_call", fc)
args = dict(getattr(call_obj, "args", {}) or {})
try:
if name == "save_note":
msg = save_note(args["content"], args["filename"])
return {"status": "ok", "message": msg}
if name == "analyze_sentiment":
analysis = analyze_sentiment(args["text"])
return {"status": "ok", "analysis": analysis}
if name == "generate_concise_summary":
summary = generate_concise_summary(
args["text_input"], int(args["max_words"])
)
return {"status": "ok", "summary": summary}
if name == "web_research":
return web_research(
query=args["query"],
url=args.get("url"),
)
return {"status": "error", "error": f"Unknown function '{name}'."}
except Exception as e:
return {"status": "error", "error": str(e)}
# --- Response text helper ----------------------------------------------------
def extract_text(response: types.GenerateContentResponse) -> str:
if getattr(response, "text", None):
return response.text
pieces: List[str] = []
for cand in response.candidates or []:
if cand.content and cand.content.parts:
for part in cand.content.parts:
if getattr(part, "text", None):
pieces.append(part.text)
return "\n".join(pieces).strip()
# --- Two-turn function-calling per user prompt -------------------------------
def handle_user_turn(history: List[types.Content], user_prompt: str) -> str:
# 1) User message
user_content = types.Content(
role="user",
parts=[types.Part.from_text(text=user_prompt)],
)
history.append(user_content)
# First turn: model decides which function(s) to call
try:
first = client.models.generate_content(
model=MODEL_ID,
contents=history,
config=types.GenerateContentConfig(
tools=TOOLS,
automatic_function_calling=types.AutomaticFunctionCallingConfig(
disable=True # we manage function calling manually
),
),
)
except Exception as e:
return f"Error calling the model: {e!r}"
history.append(first.candidates[0].content)
function_calls = first.function_calls or []
# Model answered directly (or only used its own tools inside web_research)
if not function_calls:
return extract_text(first)
# 2) Execute requested custom functions and send results back
tool_parts: List[types.Part] = []
for fc in function_calls:
result = run_custom_function(fc)
tool_parts.append(
types.Part.from_function_response(
name=getattr(fc, "name", None),
response={"result": result},
)
)
history.append(types.Content(role="tool", parts=tool_parts))
# Second turn: final answer
try:
second = client.models.generate_content(
model=MODEL_ID,
contents=history,
# Explicitly disable tool calling here too (avoids tool-config “leaking” across calls in some SDK versions)
config=types.GenerateContentConfig(
tools=[],
automatic_function_calling=types.AutomaticFunctionCallingConfig(
disable=True
),
),
)
except Exception as e:
return f"Error generating final answer: {e!r}"
history.append(second.candidates[0].content)
return extract_text(second)
# --- Conversation loop -------------------------------------------------------
def main() -> None:
print("AI Research Assistant ready.")
print("Type 'exit' to quit.\n")
history: List[types.Content] = []
while True:
user_input = input("You: ").strip()
if user_input.lower() in {"exit", "quit"}:
break
answer = handle_user_turn(history, user_input)
print(f"\nAssistant: {answer}\n")
if __name__ == "__main__":
main()