Update app.py
Browse files
app.py
CHANGED
@@ -29,19 +29,24 @@ def db_agent(query: str) -> str:
|
|
29 |
return "No transactions found for today."
|
30 |
return None
|
31 |
except sqlite3.OperationalError as e:
|
32 |
-
# Handle missing table or other DB errors
|
33 |
return f"Database error: {e}. Please initialize 'transactions' table in shop.db."
|
34 |
|
35 |
|
36 |
def web_search_agent(query: str) -> str:
|
37 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
|
47 |
def llm_agent(prompt: str) -> str:
|
|
|
29 |
return "No transactions found for today."
|
30 |
return None
|
31 |
except sqlite3.OperationalError as e:
|
|
|
32 |
return f"Database error: {e}. Please initialize 'transactions' table in shop.db."
|
33 |
|
34 |
|
35 |
def web_search_agent(query: str) -> str:
|
36 |
+
# Try fetching a snippet from SerpAPI, otherwise fallback to direct LLM
|
37 |
+
try:
|
38 |
+
resp = requests.get(
|
39 |
+
"https://serpapi.com/search",
|
40 |
+
params={"q": query, "api_key": os.getenv("SERPAPI_KEY")}
|
41 |
+
)
|
42 |
+
data = resp.json()
|
43 |
+
snippet = data.get("organic_results", [{}])[0].get("snippet", "").strip()
|
44 |
+
if snippet:
|
45 |
+
return llm_agent(f"Summarize: {snippet}")
|
46 |
+
except Exception:
|
47 |
+
pass
|
48 |
+
# Fallback for no snippet or errors
|
49 |
+
return llm_agent(query)
|
50 |
|
51 |
|
52 |
def llm_agent(prompt: str) -> str:
|