jesusgj commited on
Commit
e812f22
·
1 Parent(s): 5291928

Modified files

Browse files
Files changed (2) hide show
  1. agent.py +29 -3
  2. requirements.txt +1 -1
agent.py CHANGED
@@ -8,7 +8,7 @@ from smolagents import InferenceClientModel
8
  from dotenv import load_dotenv
9
  from markdownify import markdownify
10
  from requests.exceptions import RequestException
11
- from langchain_community.utilities import SerpAPIWrapper
12
 
13
  def initialize_agent():
14
  # Load environment variables from .env file
@@ -63,8 +63,33 @@ def initialize_agent():
63
  The search results, or an error message if the search fails.
64
  """
65
  try:
66
- serpapi = SerpAPIWrapper()
67
- return serpapi.run(query)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  except Exception as e:
69
  return f"Error performing Google search: {str(e)}"
70
 
@@ -89,3 +114,4 @@ def initialize_agent():
89
  else:
90
  return None
91
 
 
 
8
  from dotenv import load_dotenv
9
  from markdownify import markdownify
10
  from requests.exceptions import RequestException
11
+ from serpapi import GoogleSearch
12
 
13
  def initialize_agent():
14
  # Load environment variables from .env file
 
63
  The search results, or an error message if the search fails.
64
  """
65
  try:
66
+ params = {
67
+ "q": query,
68
+ "api_key": os.environ.get("SERPAPI_API_KEY"),
69
+ }
70
+ search = GoogleSearch(params)
71
+ results = search.get_dict()
72
+ if "ai_overview" in results:
73
+ ai_overview = results["ai_overview"]
74
+ output = ""
75
+ for block in ai_overview.get("text_blocks", []):
76
+ if block["type"] == "paragraph":
77
+ output += block["snippet"] + "\n\n"
78
+ elif block["type"] == "heading":
79
+ output += f"### {block['snippet']}\n\n"
80
+ elif block["type"] == "list":
81
+ for item in block["list"]:
82
+ output += f"- **{item['title']}** {item['snippet']}\n"
83
+ output += "\n"
84
+ if "references" in ai_overview:
85
+ output += "\n**References:**\n"
86
+ for ref in ai_overview["references"]:
87
+ output += f"- [{ref['title']}]({ref['link']})\n"
88
+ return output
89
+ elif "organic_results" in results:
90
+ return str(results["organic_results"])
91
+ else:
92
+ return "No results found."
93
  except Exception as e:
94
  return f"Error performing Google search: {str(e)}"
95
 
 
114
  else:
115
  return None
116
 
117
+
requirements.txt CHANGED
@@ -6,4 +6,4 @@ gradio
6
  markdownify
7
  duckduckgo-search
8
  wikipedia
9
- serpapi-python
 
6
  markdownify
7
  duckduckgo-search
8
  wikipedia
9
+ serpapi