File size: 4,487 Bytes
2b67e06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import sqlite3
import json
import os

# Database and dataset paths
DB_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../memory/vector_db/ai_knowledge.db'))
DATASET_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../memory/datasets/ai_tools_dataset.json'))

def init_db():
    os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    
    # Create the main tools table
    cursor.execute('''
    CREATE TABLE IF NOT EXISTS ai_tools (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        tool_name TEXT UNIQUE,
        category TEXT,
        description TEXT,
        pricing_model TEXT,
        official_website TEXT,
        raw_json TEXT
    )
    ''')
    
    # Create an FTS5 virtual table for fast full-text semantic-style search
    cursor.execute('''
    CREATE VIRTUAL TABLE IF NOT EXISTS ai_tools_fts USING fts5(
        tool_name,
        category,
        description,
        use_cases,
        strengths,
        recommended_for,
        workflow_tags,
        content=ai_tools,
        content_rowid=id
    )
    ''')
    
    # Create triggers to keep FTS table in sync with the main table
    cursor.execute('''
    CREATE TRIGGER IF NOT EXISTS tools_ai_insert AFTER INSERT ON ai_tools BEGIN
        INSERT INTO ai_tools_fts(rowid, tool_name, category, description, use_cases, strengths, recommended_for, workflow_tags)
        VALUES (
            new.id, 
            new.tool_name, 
            new.category, 
            new.description, 
            json_extract(new.raw_json, '$.use_cases'),
            json_extract(new.raw_json, '$.strengths'),
            json_extract(new.raw_json, '$.recommended_for'),
            json_extract(new.raw_json, '$.workflow_tags')
        );
    END;
    ''')
    
    conn.commit()
    return conn

def ingest_data(conn):
    if not os.path.exists(DATASET_PATH):
        print(f"Error: Dataset not found at {DATASET_PATH}")
        return
        
    with open(DATASET_PATH, 'r', encoding='utf-8') as f:
        tools = json.load(f)
        
    cursor = conn.cursor()
    inserted_count = 0
    
    for tool in tools:
        try:
            cursor.execute('''
            INSERT INTO ai_tools (tool_name, category, description, pricing_model, official_website, raw_json)
            VALUES (?, ?, ?, ?, ?, ?)
            ''', (
                tool.get('tool_name'),
                tool.get('category'),
                tool.get('description'),
                tool.get('pricing_model'),
                tool.get('official_website'),
                json.dumps(tool)
            ))
            inserted_count += 1
        except sqlite3.IntegrityError:
            # Tool already exists
            pass
            
    conn.commit()
    print(f"Ingested {inserted_count} new AI tools into the cognitive database.")

def search_tools(conn, query, limit=5):
    """
    Retrieval-Augmented Generation (RAG) backend utility function.
    Performs FTS match across all fields to retrieve the most relevant tools.
    """
    cursor = conn.cursor()
    
    # Format query for FTS5 (basic word match)
    # E.g. "instagram marketing" -> '"instagram" OR "marketing"'
    words = query.split()
    fts_query = " OR ".join([f'"{word}"' for word in words])
    
    print(f"\n--- AURA Search Results for: '{query}' ---")
    
    cursor.execute('''
    SELECT ai_tools.tool_name, ai_tools.category, ai_tools.description, ai_tools.raw_json 
    FROM ai_tools_fts 
    JOIN ai_tools ON ai_tools.id = ai_tools_fts.rowid
    WHERE ai_tools_fts MATCH ?
    ORDER BY rank
    LIMIT ?
    ''', (fts_query, limit))
    
    results = cursor.fetchall()
    
    if not results:
        print("No matching tools found.")
        return
        
    for idx, row in enumerate(results, 1):
        name, category, desc, raw = row
        tool_data = json.loads(raw)
        
        print(f"\n{idx}. {name} [{category}]")
        print(f"   Desc: {desc}")
        print(f"   Best for: {', '.join(tool_data.get('use_cases', []))}")
        print(f"   Pricing: {tool_data.get('pricing_model')}")

if __name__ == "__main__":
    print("Initializing AURA Vector Memory (SQLite FTS)...")
    db_conn = init_db()
    
    ingest_data(db_conn)
    
    # Test the ingestion with a couple of workflow queries
    search_tools(db_conn, "instagram marketing social media")
    search_tools(db_conn, "video editing avatars")
    
    db_conn.close()