SooryaKiranKakkarayil commited on
Commit
898219f
·
1 Parent(s): f725fb6

Switch agent model to Gemini via LiteLLM

Browse files
Files changed (2) hide show
  1. app.py +19 -17
  2. requirements.txt +2 -0
app.py CHANGED
@@ -11,6 +11,7 @@ import pandas as pd
11
  from smolagents import (
12
  CodeAgent,
13
  DuckDuckGoSearchTool,
 
14
  InferenceClientModel,
15
  VisitWebpageTool,
16
  tool,
@@ -210,40 +211,41 @@ class BasicAgent:
210
  """Same name and call interface as the template: agent(question) -> str."""
211
 
212
  def __init__(self):
213
- token = os.getenv("HF_TOKEN")
214
- if not token:
215
- raise RuntimeError("HF_TOKEN is not set. Add it as a Space *secret* (or export it locally).")
216
-
217
- self.model = GuardedInferenceClientModel(
218
- model_id=MODEL_ID,
219
- token=token,
220
- provider=HF_PROVIDER,
 
221
  temperature=0.1,
222
- max_tokens=1500,
223
  )
224
 
225
  visit = VisitWebpageTool()
226
- visit.max_output_length = MAX_PAGE_CHARS # honored by recent versions; harmless otherwise
227
 
228
  base = dict(
229
  tools=[RobustDuckDuckGoSearchTool(max_results=6, rate_limit=2.0), visit, read_file],
230
  model=self.model,
231
- additional_authorized_imports=["pandas", "numpy", "json", "csv", "io",
232
- "string", "fractions", "decimal", "openpyxl"],
 
 
233
  max_steps=MAX_STEPS,
234
  verbosity_level=1,
235
  )
236
- # Answer rules go in the system prompt when this smolagents version supports
237
- # `instructions`; otherwise they are prepended to every task.
238
  try:
239
  self.agent = CodeAgent(**base, instructions=ANSWER_RULES, max_print_outputs_length=4000)
240
  self.rules_in_system = True
241
  except TypeError:
242
  self.agent = CodeAgent(**base)
243
  self.rules_in_system = False
244
- print(f"BasicAgent initialized | model={MODEL_ID} | provider={HF_PROVIDER or 'auto'} | "
245
- f"max_steps={MAX_STEPS} | rules_in_system={self.rules_in_system}")
246
-
247
  def _build_task(self, question: str, file_name: str, file_path: str | None) -> str:
248
  parts = [question.strip()]
249
  if file_name and file_path:
 
11
  from smolagents import (
12
  CodeAgent,
13
  DuckDuckGoSearchTool,
14
+ LiteLLMModel,
15
  InferenceClientModel,
16
  VisitWebpageTool,
17
  tool,
 
211
  """Same name and call interface as the template: agent(question) -> str."""
212
 
213
  def __init__(self):
214
+ # Fetch Google Gemini API Key from Space secrets
215
+ gemini_key = os.getenv("GEMINI_API_KEY")
216
+ if not gemini_key:
217
+ raise RuntimeError("GEMINI_API_KEY is not set. Add it as a Space *secret*.")
218
+
219
+ # Initialize Gemini 2.5 Flash / 1.5 Flash using LiteLLMModel wrapper
220
+ self.model = LiteLLMModel(
221
+ model_id="gemini/gemini-2.5-flash", # or "gemini/gemini-1.5-flash"
222
+ api_key=gemini_key,
223
  temperature=0.1,
 
224
  )
225
 
226
  visit = VisitWebpageTool()
227
+ visit.max_output_length = MAX_PAGE_CHARS
228
 
229
  base = dict(
230
  tools=[RobustDuckDuckGoSearchTool(max_results=6, rate_limit=2.0), visit, read_file],
231
  model=self.model,
232
+ additional_authorized_imports=[
233
+ "pandas", "numpy", "json", "csv", "io",
234
+ "string", "fractions", "decimal", "openpyxl"
235
+ ],
236
  max_steps=MAX_STEPS,
237
  verbosity_level=1,
238
  )
239
+
 
240
  try:
241
  self.agent = CodeAgent(**base, instructions=ANSWER_RULES, max_print_outputs_length=4000)
242
  self.rules_in_system = True
243
  except TypeError:
244
  self.agent = CodeAgent(**base)
245
  self.rules_in_system = False
246
+
247
+ print(f"BasicAgent initialized with Gemini Flash | max_steps={MAX_STEPS}")
248
+
249
  def _build_task(self, question: str, file_name: str, file_path: str | None) -> str:
250
  parts = [question.strip()]
251
  if file_name and file_path:
requirements.txt CHANGED
@@ -9,6 +9,8 @@ huggingface_hub
9
  spaces
10
  markdownify
11
  smolagents[toolkit]
 
 
12
  # Which DuckDuckGo package smolagents imports depends on its version.
13
  # Installing both is harmless.
14
  ddgs
 
9
  spaces
10
  markdownify
11
  smolagents[toolkit]
12
+ litellm
13
+ google-generativeai
14
  # Which DuckDuckGo package smolagents imports depends on its version.
15
  # Installing both is harmless.
16
  ddgs