llamasrock commited on
Commit
75dd44b
·
verified ·
1 Parent(s): f06cd0f

Update app.py

Browse files

Try smolagents code agent with Serper google search

Files changed (1) hide show
  1. app.py +26 -24
app.py CHANGED
@@ -4,9 +4,8 @@ import requests
4
  import inspect
5
  import pandas as pd
6
  import os
7
- from smolagents import LiteLLMModel, CodeAgent, tool
8
  from google import genai
9
- # from google.genai.types import Tool, GoogleSearch
10
  from google.genai import types
11
  import asyncio
12
  import requests
@@ -15,6 +14,7 @@ import requests
15
  # --- Constants ---
16
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
17
  GEMINI_API_KEY = os.getenv("Gemini_API_key")
 
18
 
19
  # --- Basic Agent Definition ---
20
  # Agent capabilities required: Search the web, listen to audio recordings, watch YouTube videos (process the footage, not the transcript), work with Excel spreadsheets
@@ -22,16 +22,16 @@ GEMINI_API_KEY = os.getenv("Gemini_API_key")
22
 
23
  class BasicAgent:
24
  def __init__(self):
25
- # self.llm_model = LiteLLMModel(
26
- # model_id="gemini/gemini-2.0-flash", # you can see other model names here: https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models. It is important to prefix the name with "gemini/"
27
- # api_key=GEMINI_API_KEY,
28
- # max_tokens=8192
29
- # )
30
- # # self.google_search_tool = Tool(google_search = GoogleSearch())
31
- # self.google_search_tool = GoogleSearchTool()
32
- # self.agent = CodeAgent(model = self.llm_model, tools = [self.google_search_tool])
33
- # Define Google API client with GoogleSearch tool
34
- self.client = genai.Client(api_key=GEMINI_API_KEY)
35
 
36
  print("BasicAgent initialized.")
37
 
@@ -40,20 +40,22 @@ class BasicAgent:
40
  fixed_answer = "This is a default answer."
41
  # print(f"Agent returning fixed answer: {fixed_answer}")
42
  # return fixed_answer
43
- # return self.agent.run(question)
44
- # Use the Google GenAI client to run the question
45
  await asyncio.sleep(10)
46
- answer = self.client.models.generate_content(
47
- model='gemini-2.0-flash',
48
- contents=f'''Answer the following question in the format as requested. If the format not specified, then provide a single word/number/name.
49
- In your response, do not include anything other than your answer. {question}''',
50
- config=types.GenerateContentConfig(
51
- tools=[types.Tool(google_search=types.GoogleSearch()),
52
- types.Tool(code_execution=self.get_file)]
53
- )
54
- )
55
- return answer.text
 
 
56
 
 
57
  def get_file(self, task_id):
58
  '''
59
  Downloads the file associated with the given task_id, if one exists and is mapped.
 
4
  import inspect
5
  import pandas as pd
6
  import os
7
+ from smolagents import LiteLLMModel, CodeAgent, tool, GoogleSearchTool
8
  from google import genai
 
9
  from google.genai import types
10
  import asyncio
11
  import requests
 
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
  GEMINI_API_KEY = os.getenv("Gemini_API_key")
17
+ SERPER_API_KEY = os.getenv("Serper_API_key")
18
 
19
  # --- Basic Agent Definition ---
20
  # Agent capabilities required: Search the web, listen to audio recordings, watch YouTube videos (process the footage, not the transcript), work with Excel spreadsheets
 
22
 
23
  class BasicAgent:
24
  def __init__(self):
25
+ self.llm_model = LiteLLMModel(
26
+ model_id="gemini/gemini-2.0-flash", # you can see other model names here: https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models. It is important to prefix the name with "gemini/"
27
+ api_key=GEMINI_API_KEY,
28
+ max_tokens=8192
29
+ )
30
+ # self.google_search_tool = Tool(google_search = GoogleSearch())
31
+ self.google_search_tool = GoogleSearchTool()
32
+ self.agent = CodeAgent(model = self.llm_model, tools = [self.google_search_tool, self.get_file])
33
+ # # Define Google API client with GoogleSearch tool
34
+ # self.client = genai.Client(api_key=GEMINI_API_KEY)
35
 
36
  print("BasicAgent initialized.")
37
 
 
40
  fixed_answer = "This is a default answer."
41
  # print(f"Agent returning fixed answer: {fixed_answer}")
42
  # return fixed_answer
43
+
 
44
  await asyncio.sleep(10)
45
+ return self.agent.run(question, task_id)
46
+ # # Use the Google GenAI client to run the question
47
+ # answer = self.client.models.generate_content(
48
+ # model='gemini-2.0-flash',
49
+ # contents=f'''Answer the following question in the format as requested. If the format not specified, then provide a single word/number/name.
50
+ # In your response, do not include anything other than your answer. {question}''',
51
+ # config=types.GenerateContentConfig(
52
+ # tools=[types.Tool(google_search=types.GoogleSearch()),
53
+ # types.Tool(code_execution=self.get_file)]
54
+ # )
55
+ # )
56
+ # return answer.text
57
 
58
+ @tool
59
  def get_file(self, task_id):
60
  '''
61
  Downloads the file associated with the given task_id, if one exists and is mapped.