Spaces:
Running
Running
File size: 2,773 Bytes
ab4828e f4c5010 ab4828e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | import os
import chromadb
from agent_plugins.rag_advanced import AdvancedRAG
class ApiAgent:
def __init__(self):
# Resolve path robustly to support local and cloud/containerized environments
possible_paths = [
os.path.join(os.path.dirname(__file__), '../../memory/vector_db'), # Local layout
os.path.join(os.path.dirname(__file__), '../memory/vector_db'), # Deployed layout
os.path.abspath('memory/vector_db') # Cwd layout
]
db_path = possible_paths[0]
for p in possible_paths:
if os.path.exists(p):
db_path = p
break
print(f"ApiAgent: Initializing ChromaDB at: {db_path}")
self.client = chromadb.PersistentClient(path=db_path)
self.collection = self.client.get_or_create_collection(name="aura_memory_vault")
self.rag = AdvancedRAG(self.collection)
def search_apis(self, query: str, top_k: int = 5) -> str:
"""Search the neural memory specifically for Public APIs."""
# Prepend to guide the vector search towards our API entries
enhanced_query = f"PUBLIC API ENTRY {query}"
results = self.rag.hybrid_search(enhanced_query, top_k=top_k * 2) # Get more to filter
# Filter for actual API entries
api_entries = [res for res in results if "PUBLIC API ENTRY:" in res]
if not api_entries:
return "No matching free APIs found in the neural memory vault."
# Deduplicate and limit
unique_apis = []
seen = set()
for entry in api_entries:
if entry not in seen:
seen.add(entry)
unique_apis.append(entry)
if len(unique_apis) >= top_k:
break
# Format nicely
output = "### Recommended Free Public APIs\n\n"
for entry in unique_apis:
lines = entry.split('\n')
name, cat, desc, auth, https, cors, url = "", "", "", "", "", "", ""
for line in lines:
if line.startswith("Name: "): name = line.replace("Name: ", "")
if line.startswith("Category: "): cat = line.replace("Category: ", "")
if line.startswith("Description: "): desc = line.replace("Description: ", "")
if line.startswith("Auth Required: "): auth = line.replace("Auth Required: ", "")
if line.startswith("URL: "): url = line.replace("URL: ", "")
output += f"**[{name}]({url})** ({cat})\n"
output += f"> {desc}\n"
output += f"> *Auth:* {auth}\n\n"
return output
|