Spaces:
Sleeping
Sleeping
jesusgj
commited on
Commit
·
c2d4de7
1
Parent(s):
8781862
Modified files
Browse files
agent.py
CHANGED
|
@@ -8,6 +8,9 @@ from dotenv import load_dotenv
|
|
| 8 |
from markdownify import markdownify
|
| 9 |
from requests.exceptions import RequestException
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
def initialize_agent():
|
| 12 |
# Load environment variables from .env file
|
| 13 |
load_dotenv()
|
|
@@ -32,6 +35,8 @@ def initialize_agent():
|
|
| 32 |
Returns:
|
| 33 |
The content of the webpage converted to Markdown, or an error message if the request fails.
|
| 34 |
"""
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
# Send a GET request to the URL
|
| 37 |
response = requests.get(url)
|
|
@@ -44,7 +49,9 @@ def initialize_agent():
|
|
| 44 |
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
| 45 |
|
| 46 |
# Truncate the content to a maximum of 15000 characters
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
except RequestException as e:
|
| 50 |
return f"Error fetching the webpage: {str(e)}"
|
|
@@ -61,6 +68,8 @@ def initialize_agent():
|
|
| 61 |
Returns:
|
| 62 |
The search results, or an error message if the search fails.
|
| 63 |
"""
|
|
|
|
|
|
|
| 64 |
try:
|
| 65 |
client = serpapi.Client(api_key=os.environ.get("SERPAPI_API_KEY"))
|
| 66 |
results = client.search(q=query, engine="google")
|
|
@@ -80,9 +89,12 @@ def initialize_agent():
|
|
| 80 |
output += "\n**References:**\n"
|
| 81 |
for ref in ai_overview["references"]:
|
| 82 |
output += f"- [{ref['title']}]({ref['link']})\n"
|
|
|
|
| 83 |
return output
|
| 84 |
elif "organic_results" in results:
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
else:
|
| 87 |
return "No results found."
|
| 88 |
except Exception as e:
|
|
@@ -107,4 +119,4 @@ def initialize_agent():
|
|
| 107 |
)
|
| 108 |
return manager_agent
|
| 109 |
else:
|
| 110 |
-
return None
|
|
|
|
| 8 |
from markdownify import markdownify
|
| 9 |
from requests.exceptions import RequestException
|
| 10 |
|
| 11 |
+
search_cache = {}
|
| 12 |
+
webpage_cache = {}
|
| 13 |
+
|
| 14 |
def initialize_agent():
|
| 15 |
# Load environment variables from .env file
|
| 16 |
load_dotenv()
|
|
|
|
| 35 |
Returns:
|
| 36 |
The content of the webpage converted to Markdown, or an error message if the request fails.
|
| 37 |
"""
|
| 38 |
+
if url in webpage_cache:
|
| 39 |
+
return webpage_cache[url]
|
| 40 |
try:
|
| 41 |
# Send a GET request to the URL
|
| 42 |
response = requests.get(url)
|
|
|
|
| 49 |
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
| 50 |
|
| 51 |
# Truncate the content to a maximum of 15000 characters
|
| 52 |
+
result = markdown_content[:15000]
|
| 53 |
+
webpage_cache[url] = result
|
| 54 |
+
return result
|
| 55 |
|
| 56 |
except RequestException as e:
|
| 57 |
return f"Error fetching the webpage: {str(e)}"
|
|
|
|
| 68 |
Returns:
|
| 69 |
The search results, or an error message if the search fails.
|
| 70 |
"""
|
| 71 |
+
if query in search_cache:
|
| 72 |
+
return search_cache[query]
|
| 73 |
try:
|
| 74 |
client = serpapi.Client(api_key=os.environ.get("SERPAPI_API_KEY"))
|
| 75 |
results = client.search(q=query, engine="google")
|
|
|
|
| 89 |
output += "\n**References:**\n"
|
| 90 |
for ref in ai_overview["references"]:
|
| 91 |
output += f"- [{ref['title']}]({ref['link']})\n"
|
| 92 |
+
search_cache[query] = output
|
| 93 |
return output
|
| 94 |
elif "organic_results" in results:
|
| 95 |
+
result = str(results["organic_results"])
|
| 96 |
+
search_cache[query] = result
|
| 97 |
+
return result
|
| 98 |
else:
|
| 99 |
return "No results found."
|
| 100 |
except Exception as e:
|
|
|
|
| 119 |
)
|
| 120 |
return manager_agent
|
| 121 |
else:
|
| 122 |
+
return None
|