Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -35,41 +35,58 @@ class RaindropSearchBot:
|
|
35 |
logger.info("RaindropSearchBot initialized")
|
36 |
|
37 |
def generate_search_query(self, user_request: str) -> str:
|
|
|
38 |
logger.info(f"Generating search query for request: {user_request}")
|
39 |
-
prompt = f"""
|
40 |
-
Convert the following request into a focused search query for Raindrop.io.
|
41 |
-
Extract key concepts and create a search query that will work well with the Raindrop API.
|
42 |
-
Include relevant operators and consider both exact matches and related terms.
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
"""
|
48 |
|
49 |
try:
|
50 |
response = self.client.chat.completions.create(
|
51 |
model="gpt-4o-mini",
|
52 |
messages=[{"role": "user", "content": prompt}],
|
53 |
-
temperature=0.
|
54 |
-
max_tokens=
|
55 |
)
|
56 |
-
|
57 |
-
logger.info(f"Generated search query: {
|
58 |
-
return
|
59 |
except Exception as e:
|
60 |
logger.error(f"Error generating search query: {str(e)}")
|
61 |
-
|
|
|
62 |
|
63 |
def search_raindrop(self, search_query: str) -> List[Dict]:
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
headers = {
|
67 |
"Authorization": f"Bearer {self.raindrop_api_token}",
|
68 |
"Content-Type": "application/json"
|
69 |
}
|
70 |
|
|
|
71 |
try:
|
72 |
-
# First, let's test the API connection and token
|
73 |
test_response = requests.get(
|
74 |
"https://api.raindrop.io/rest/v1/user",
|
75 |
headers=headers
|
@@ -78,16 +95,15 @@ class RaindropSearchBot:
|
|
78 |
if test_response.status_code != 200:
|
79 |
logger.error(f"API test failed: {test_response.text}")
|
80 |
return []
|
81 |
-
|
82 |
# Search parameters
|
83 |
params = {
|
84 |
-
"search":
|
85 |
"page": 0,
|
86 |
"perpage": 50,
|
87 |
"sort": "-created"
|
88 |
}
|
89 |
|
90 |
-
# Log the full request details
|
91 |
logger.info(f"Making request to Raindrop API with params: {params}")
|
92 |
|
93 |
# Make the search request
|
|
|
35 |
logger.info("RaindropSearchBot initialized")
|
36 |
|
37 |
def generate_search_query(self, user_request: str) -> str:
|
38 |
+
"""Convert user request to a tailored search query using OpenAI."""
|
39 |
logger.info(f"Generating search query for request: {user_request}")
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
prompt = f"""
|
42 |
+
Convert this request into a simple search query for finding bookmarks about this topic.
|
43 |
+
Use quotes for exact phrases and OR between alternatives.
|
44 |
+
Only return the search query itself, no explanations.
|
45 |
+
Keep it concise and focused.
|
46 |
|
47 |
+
Request: {user_request}
|
48 |
"""
|
49 |
|
50 |
try:
|
51 |
response = self.client.chat.completions.create(
|
52 |
model="gpt-4o-mini",
|
53 |
messages=[{"role": "user", "content": prompt}],
|
54 |
+
temperature=0.3, # Lower temperature for more focused results
|
55 |
+
max_tokens=100 # Reduced tokens since we only need the query
|
56 |
)
|
57 |
+
search_query = response.choices[0].message.content.strip()
|
58 |
+
logger.info(f"Generated search query: {search_query}")
|
59 |
+
return search_query
|
60 |
except Exception as e:
|
61 |
logger.error(f"Error generating search query: {str(e)}")
|
62 |
+
# Fallback to a simple search if query generation fails
|
63 |
+
return user_request
|
64 |
|
65 |
def search_raindrop(self, search_query: str) -> List[Dict]:
|
66 |
+
"""Search Raindrop.io with the generated query."""
|
67 |
+
logger.info(f"Original search query: {search_query}")
|
68 |
+
|
69 |
+
# Extract the actual search terms from between the backticks
|
70 |
+
try:
|
71 |
+
if "```" in search_query:
|
72 |
+
search_terms = search_query.split("```")[1].strip()
|
73 |
+
else:
|
74 |
+
search_terms = search_query.strip()
|
75 |
+
|
76 |
+
# Clean up the search terms
|
77 |
+
search_terms = search_terms.replace('\n', ' ').strip()
|
78 |
+
logger.info(f"Cleaned search terms: {search_terms}")
|
79 |
+
except Exception as e:
|
80 |
+
logger.error(f"Error processing search query: {e}")
|
81 |
+
search_terms = search_query
|
82 |
|
83 |
headers = {
|
84 |
"Authorization": f"Bearer {self.raindrop_api_token}",
|
85 |
"Content-Type": "application/json"
|
86 |
}
|
87 |
|
88 |
+
# Test the API connection first
|
89 |
try:
|
|
|
90 |
test_response = requests.get(
|
91 |
"https://api.raindrop.io/rest/v1/user",
|
92 |
headers=headers
|
|
|
95 |
if test_response.status_code != 200:
|
96 |
logger.error(f"API test failed: {test_response.text}")
|
97 |
return []
|
98 |
+
|
99 |
# Search parameters
|
100 |
params = {
|
101 |
+
"search": search_terms,
|
102 |
"page": 0,
|
103 |
"perpage": 50,
|
104 |
"sort": "-created"
|
105 |
}
|
106 |
|
|
|
107 |
logger.info(f"Making request to Raindrop API with params: {params}")
|
108 |
|
109 |
# Make the search request
|