Update app.py
Browse files
app.py
CHANGED
@@ -181,69 +181,65 @@ def embedding_search_hf_spaces(query: str = "", limit: int = 3) -> Dict:
|
|
181 |
Dictionary containing search results with MCP information
|
182 |
"""
|
183 |
try:
|
184 |
-
|
185 |
pinecone_api_key = os.getenv('PINECONE_API_KEY')
|
186 |
openai_api_key = os.getenv('OPENAI_API_KEY')
|
187 |
-
|
188 |
if not pinecone_api_key or not openai_api_key:
|
|
|
189 |
return {
|
190 |
"error": "API keys not found",
|
191 |
"results": [],
|
192 |
"total": 0
|
193 |
}
|
194 |
-
|
195 |
-
# Initialize clients
|
196 |
pc = Pinecone(api_key=pinecone_api_key)
|
197 |
index = pc.Index("hf-mcp")
|
198 |
client = OpenAI(api_key=openai_api_key)
|
199 |
-
|
200 |
-
# Generate embedding using OpenAI
|
201 |
response = client.embeddings.create(
|
202 |
input=query,
|
203 |
model="text-embedding-3-large"
|
204 |
)
|
205 |
query_embedding = response.data[0].embedding
|
206 |
-
|
207 |
-
|
208 |
results = index.query(
|
209 |
namespace="",
|
210 |
vector=query_embedding,
|
211 |
top_k=limit
|
212 |
)
|
213 |
-
|
214 |
-
# Process results and get detailed information
|
215 |
space_results = []
|
216 |
if not results.matches:
|
|
|
217 |
return {
|
218 |
"results": [],
|
219 |
"total": 0
|
220 |
}
|
221 |
-
|
222 |
for match in results.matches:
|
223 |
space_id = match.id
|
224 |
try:
|
225 |
-
# Remove 'spaces/' prefix if present
|
226 |
repo_id = space_id.replace('spaces/', '')
|
227 |
-
|
228 |
-
# Get space information from HF API
|
229 |
space = api.space_info(repo_id)
|
230 |
space_info = {
|
231 |
"id": space.id,
|
232 |
"likes": space.likes,
|
233 |
"trending_score": space.trending_score,
|
234 |
"source": "huggingface",
|
235 |
-
"score": match.score
|
236 |
}
|
237 |
space_results.append(space_info)
|
238 |
except Exception as e:
|
|
|
239 |
continue
|
240 |
-
|
241 |
return {
|
242 |
"results": space_results,
|
243 |
"total": len(space_results)
|
244 |
}
|
245 |
-
|
246 |
except Exception as e:
|
|
|
247 |
return {
|
248 |
"error": str(e),
|
249 |
"results": [],
|
@@ -262,7 +258,7 @@ def embedding_search_smithery(query: str = "", limit: int = 3) -> Dict:
|
|
262 |
Dictionary containing search results with MCP information
|
263 |
"""
|
264 |
try:
|
265 |
-
|
266 |
from pinecone import Pinecone
|
267 |
from openai import OpenAI
|
268 |
import os
|
@@ -270,42 +266,39 @@ def embedding_search_smithery(query: str = "", limit: int = 3) -> Dict:
|
|
270 |
pinecone_api_key = os.getenv('PINECONE_API_KEY')
|
271 |
openai_api_key = os.getenv('OPENAI_API_KEY')
|
272 |
smithery_token = os.getenv('SMITHERY_TOKEN')
|
273 |
-
|
274 |
if not pinecone_api_key or not openai_api_key or not smithery_token:
|
|
|
275 |
return {
|
276 |
"error": "API keys not found",
|
277 |
"results": [],
|
278 |
"total": 0
|
279 |
}
|
280 |
-
|
281 |
-
# Initialize clients
|
282 |
pc = Pinecone(api_key=pinecone_api_key)
|
283 |
index = pc.Index("smithery-mcp")
|
284 |
client = OpenAI(api_key=openai_api_key)
|
285 |
-
|
286 |
-
# Generate embedding using OpenAI
|
287 |
response = client.embeddings.create(
|
288 |
input=query,
|
289 |
model="text-embedding-3-large"
|
290 |
)
|
291 |
query_embedding = response.data[0].embedding
|
292 |
-
|
293 |
-
|
294 |
results = index.query(
|
295 |
namespace="",
|
296 |
vector=query_embedding,
|
297 |
top_k=limit
|
298 |
)
|
299 |
-
|
300 |
-
# Process results and get detailed information from Smithery
|
301 |
server_results = []
|
302 |
if not results.matches:
|
|
|
303 |
return {
|
304 |
"results": [],
|
305 |
"total": 0
|
306 |
}
|
307 |
-
|
308 |
-
# Prepare headers for Smithery API
|
309 |
headers = {
|
310 |
'Authorization': f'Bearer {smithery_token}'
|
311 |
}
|
@@ -313,15 +306,14 @@ def embedding_search_smithery(query: str = "", limit: int = 3) -> Dict:
|
|
313 |
for match in results.matches:
|
314 |
server_id = match.id
|
315 |
try:
|
316 |
-
|
317 |
response = requests.get(
|
318 |
f'https://registry.smithery.ai/servers/{server_id}',
|
319 |
headers=headers
|
320 |
)
|
321 |
-
|
322 |
if response.status_code != 200:
|
|
|
323 |
continue
|
324 |
-
|
325 |
server = response.json()
|
326 |
server_info = {
|
327 |
"id": server.get('qualifiedName'),
|
@@ -329,18 +321,18 @@ def embedding_search_smithery(query: str = "", limit: int = 3) -> Dict:
|
|
329 |
"description": server.get('description'),
|
330 |
"likes": server.get('useCount', 0),
|
331 |
"source": "smithery",
|
332 |
-
"score": match.score
|
333 |
}
|
334 |
server_results.append(server_info)
|
335 |
except Exception as e:
|
|
|
336 |
continue
|
337 |
-
|
338 |
return {
|
339 |
"results": server_results,
|
340 |
"total": len(server_results)
|
341 |
}
|
342 |
-
|
343 |
except Exception as e:
|
|
|
344 |
return {
|
345 |
"error": str(e),
|
346 |
"results": [],
|
|
|
181 |
Dictionary containing search results with MCP information
|
182 |
"""
|
183 |
try:
|
184 |
+
print("[DEBUG] embedding_search_hf_spaces called")
|
185 |
pinecone_api_key = os.getenv('PINECONE_API_KEY')
|
186 |
openai_api_key = os.getenv('OPENAI_API_KEY')
|
187 |
+
print(f"[DEBUG] pinecone_api_key exists: {pinecone_api_key is not None}, openai_api_key exists: {openai_api_key is not None}")
|
188 |
if not pinecone_api_key or not openai_api_key:
|
189 |
+
print("[ERROR] API keys not found")
|
190 |
return {
|
191 |
"error": "API keys not found",
|
192 |
"results": [],
|
193 |
"total": 0
|
194 |
}
|
195 |
+
print("[DEBUG] Initializing Pinecone and OpenAI clients")
|
|
|
196 |
pc = Pinecone(api_key=pinecone_api_key)
|
197 |
index = pc.Index("hf-mcp")
|
198 |
client = OpenAI(api_key=openai_api_key)
|
199 |
+
print("[DEBUG] Generating embedding with OpenAI")
|
|
|
200 |
response = client.embeddings.create(
|
201 |
input=query,
|
202 |
model="text-embedding-3-large"
|
203 |
)
|
204 |
query_embedding = response.data[0].embedding
|
205 |
+
print(f"[DEBUG] Embedding generated: {type(query_embedding)}, len={len(query_embedding)}")
|
206 |
+
print("[DEBUG] Querying Pinecone index")
|
207 |
results = index.query(
|
208 |
namespace="",
|
209 |
vector=query_embedding,
|
210 |
top_k=limit
|
211 |
)
|
212 |
+
print(f"[DEBUG] Pinecone query results: {results}")
|
|
|
213 |
space_results = []
|
214 |
if not results.matches:
|
215 |
+
print("[DEBUG] No matches found in Pinecone results")
|
216 |
return {
|
217 |
"results": [],
|
218 |
"total": 0
|
219 |
}
|
|
|
220 |
for match in results.matches:
|
221 |
space_id = match.id
|
222 |
try:
|
|
|
223 |
repo_id = space_id.replace('spaces/', '')
|
224 |
+
print(f"[DEBUG] Fetching space info for repo_id: {repo_id}")
|
|
|
225 |
space = api.space_info(repo_id)
|
226 |
space_info = {
|
227 |
"id": space.id,
|
228 |
"likes": space.likes,
|
229 |
"trending_score": space.trending_score,
|
230 |
"source": "huggingface",
|
231 |
+
"score": match.score
|
232 |
}
|
233 |
space_results.append(space_info)
|
234 |
except Exception as e:
|
235 |
+
print(f"[ERROR] Error fetching space info for {space_id}: {str(e)}")
|
236 |
continue
|
|
|
237 |
return {
|
238 |
"results": space_results,
|
239 |
"total": len(space_results)
|
240 |
}
|
|
|
241 |
except Exception as e:
|
242 |
+
print(f"[CRITICAL ERROR] in embedding_search_hf_spaces: {str(e)}")
|
243 |
return {
|
244 |
"error": str(e),
|
245 |
"results": [],
|
|
|
258 |
Dictionary containing search results with MCP information
|
259 |
"""
|
260 |
try:
|
261 |
+
print("[DEBUG] embedding_search_smithery called")
|
262 |
from pinecone import Pinecone
|
263 |
from openai import OpenAI
|
264 |
import os
|
|
|
266 |
pinecone_api_key = os.getenv('PINECONE_API_KEY')
|
267 |
openai_api_key = os.getenv('OPENAI_API_KEY')
|
268 |
smithery_token = os.getenv('SMITHERY_TOKEN')
|
269 |
+
print(f"[DEBUG] pinecone_api_key exists: {pinecone_api_key is not None}, openai_api_key exists: {openai_api_key is not None}, smithery_token exists: {smithery_token is not None}")
|
270 |
if not pinecone_api_key or not openai_api_key or not smithery_token:
|
271 |
+
print("[ERROR] API keys not found")
|
272 |
return {
|
273 |
"error": "API keys not found",
|
274 |
"results": [],
|
275 |
"total": 0
|
276 |
}
|
277 |
+
print("[DEBUG] Initializing Pinecone and OpenAI clients")
|
|
|
278 |
pc = Pinecone(api_key=pinecone_api_key)
|
279 |
index = pc.Index("smithery-mcp")
|
280 |
client = OpenAI(api_key=openai_api_key)
|
281 |
+
print("[DEBUG] Generating embedding with OpenAI")
|
|
|
282 |
response = client.embeddings.create(
|
283 |
input=query,
|
284 |
model="text-embedding-3-large"
|
285 |
)
|
286 |
query_embedding = response.data[0].embedding
|
287 |
+
print(f"[DEBUG] Embedding generated: {type(query_embedding)}, len={len(query_embedding)}")
|
288 |
+
print("[DEBUG] Querying Pinecone index")
|
289 |
results = index.query(
|
290 |
namespace="",
|
291 |
vector=query_embedding,
|
292 |
top_k=limit
|
293 |
)
|
294 |
+
print(f"[DEBUG] Pinecone query results: {results}")
|
|
|
295 |
server_results = []
|
296 |
if not results.matches:
|
297 |
+
print("[DEBUG] No matches found in Pinecone results")
|
298 |
return {
|
299 |
"results": [],
|
300 |
"total": 0
|
301 |
}
|
|
|
|
|
302 |
headers = {
|
303 |
'Authorization': f'Bearer {smithery_token}'
|
304 |
}
|
|
|
306 |
for match in results.matches:
|
307 |
server_id = match.id
|
308 |
try:
|
309 |
+
print(f"[DEBUG] Fetching server info for server_id: {server_id}")
|
310 |
response = requests.get(
|
311 |
f'https://registry.smithery.ai/servers/{server_id}',
|
312 |
headers=headers
|
313 |
)
|
|
|
314 |
if response.status_code != 200:
|
315 |
+
print(f"[ERROR] Smithery API error for {server_id}: {response.status_code}")
|
316 |
continue
|
|
|
317 |
server = response.json()
|
318 |
server_info = {
|
319 |
"id": server.get('qualifiedName'),
|
|
|
321 |
"description": server.get('description'),
|
322 |
"likes": server.get('useCount', 0),
|
323 |
"source": "smithery",
|
324 |
+
"score": match.score
|
325 |
}
|
326 |
server_results.append(server_info)
|
327 |
except Exception as e:
|
328 |
+
print(f"[ERROR] Error fetching server info for {server_id}: {str(e)}")
|
329 |
continue
|
|
|
330 |
return {
|
331 |
"results": server_results,
|
332 |
"total": len(server_results)
|
333 |
}
|
|
|
334 |
except Exception as e:
|
335 |
+
print(f"[CRITICAL ERROR] in embedding_search_smithery: {str(e)}")
|
336 |
return {
|
337 |
"error": str(e),
|
338 |
"results": [],
|