Spaces:
Build error
Build error
Commit
·
a258c1b
1
Parent(s):
d479dbc
Before google search
Browse files- app/services/message.py +1 -0
- app/utils/load_env.py +2 -0
- tools/search_engine.py +73 -0
app/services/message.py
CHANGED
@@ -13,6 +13,7 @@ from typing import List, Dict, Any, Optional
|
|
13 |
from app.utils.load_env import ACCESS_TOKEN, WHATSAPP_API_URL, GEMNI_API, OPENAI_API
|
14 |
from app.utils.system_prompt import system_prompt
|
15 |
|
|
|
16 |
# Load environment variables
|
17 |
load_dotenv()
|
18 |
|
|
|
13 |
from app.utils.load_env import ACCESS_TOKEN, WHATSAPP_API_URL, GEMNI_API, OPENAI_API
|
14 |
from app.utils.system_prompt import system_prompt
|
15 |
|
16 |
+
|
17 |
# Load environment variables
|
18 |
load_dotenv()
|
19 |
|
app/utils/load_env.py
CHANGED
@@ -16,6 +16,8 @@ ACCESS_TOKEN = os.getenv("ACCESS_TOKEN")
|
|
16 |
WHATSAPP_API_URL = os.getenv("WHATSAPP_API_URL")
|
17 |
OPENAI_API = os.getenv("OPENAI_API")
|
18 |
GEMNI_API = os.getenv("GEMINI_API")
|
|
|
|
|
19 |
|
20 |
# Debugging: Print the retrieved ACCESS_TOKEN (for development only)
|
21 |
if ENV == "development":
|
|
|
16 |
WHATSAPP_API_URL = os.getenv("WHATSAPP_API_URL")
|
17 |
OPENAI_API = os.getenv("OPENAI_API")
|
18 |
GEMNI_API = os.getenv("GEMINI_API")
|
19 |
+
CX_CODE = os.getenv("CX_CODE")
|
20 |
+
CUSTOM_SEARCH_API_KEY = os.getenv("CUSTOM_SEARCH_API_KEY")
|
21 |
|
22 |
# Debugging: Print the retrieved ACCESS_TOKEN (for development only)
|
23 |
if ENV == "development":
|
tools/search_engine.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
import logging
|
4 |
+
import httpx
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
from typing import List, Dict, Optional
|
7 |
+
|
8 |
+
from app.utils.load_env import CX_CODE, CUSTOM_SEARCH_API_KEY
|
9 |
+
# Configure logging
|
10 |
+
logging.basicConfig(level=logging.INFO)
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
+
|
13 |
+
# Load environment variables
|
14 |
+
load_dotenv()
|
15 |
+
|
16 |
+
async def google_search(query: str, num_results: str = "3") -> Optional[List[Dict[str, str]]]:
|
17 |
+
"""
|
18 |
+
Perform a Google search using the Custom Search JSON API.
|
19 |
+
|
20 |
+
Args:
|
21 |
+
query: The search query string.
|
22 |
+
num_results: The number of results to fetch as a string. Defaults to "10".
|
23 |
+
|
24 |
+
Returns:
|
25 |
+
Optional: A list of dictionaries containing the title and link of each search result.
|
26 |
+
Returns None if an error occurs or no results are found.
|
27 |
+
"""
|
28 |
+
if not query:
|
29 |
+
logger.error("Search query is empty.")
|
30 |
+
return None
|
31 |
+
|
32 |
+
try:
|
33 |
+
# Convert num_results to an integer
|
34 |
+
num_results_int = int(num_results)
|
35 |
+
if num_results_int > 10:
|
36 |
+
logger.warning("Google Custom Search API supports a maximum of 10 results per request.")
|
37 |
+
num_results_int = 10
|
38 |
+
elif num_results_int < 1:
|
39 |
+
logger.warning("num_results must be at least 1. Setting it to 1.")
|
40 |
+
num_results_int = 1
|
41 |
+
except ValueError:
|
42 |
+
logger.error("Invalid value for num_results. Must be a valid integer string.")
|
43 |
+
return None
|
44 |
+
|
45 |
+
# Define the URL and parameters
|
46 |
+
url = "https://www.googleapis.com/customsearch/v1"
|
47 |
+
params = {
|
48 |
+
"key": CUSTOM_SEARCH_API_KEY,
|
49 |
+
"cx": CX_CODE,
|
50 |
+
"q": query,
|
51 |
+
"num": num_results_int,
|
52 |
+
}
|
53 |
+
print(CUSTOM_SEARCH_API_KEY)
|
54 |
+
print(CX_CODE)
|
55 |
+
try:
|
56 |
+
# Make the HTTP request
|
57 |
+
async with httpx.AsyncClient() as client:
|
58 |
+
response = await client.get(url, params=params) # Properly encode query parameters
|
59 |
+
|
60 |
+
# Process the response
|
61 |
+
if response.status_code == 200:
|
62 |
+
results = response.json()
|
63 |
+
items = results.get("items", [])
|
64 |
+
return [{"title": item["title"], "link": item["link"]} for item in items]
|
65 |
+
|
66 |
+
else:
|
67 |
+
logger.error(f"Google Search API error: {response.status_code} - {response.text}")
|
68 |
+
return None
|
69 |
+
|
70 |
+
except httpx.RequestError as e:
|
71 |
+
logger.error("A network error occurred while performing the Google search.")
|
72 |
+
logger.error(f"Error details: {e}")
|
73 |
+
return None
|