-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_fix.py
More file actions
110 lines (86 loc) · 3.58 KB
/
simple_fix.py
File metadata and controls
110 lines (86 loc) · 3.58 KB
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
#!/usr/bin/env python3
"""
Simple script to regenerate embeddings without importing full NoteGraph
"""
import os
import sqlite3
import sqlite_vec
import numpy as np
def main():
print("🔧 Simple embeddings fix script...")
# Check if OpenAI API key is available
if not os.getenv('OPENAI_API_KEY'):
print("❌ OPENAI_API_KEY environment variable not found!")
print("Please set your OpenAI API key:")
print("export OPENAI_API_KEY='your-api-key-here'")
return False
try:
# OpenAI import and setup
from openai import OpenAI
client = OpenAI()
# Connect to database
conn = sqlite3.connect('note_graph.db')
conn.enable_load_extension(True)
# Load the sqlite-vec extension
extension_path = '/opt/homebrew/lib/python3.14/site-packages/sqlite_vec/vec0.dylib'
conn.load_extension(extension_path)
cursor = conn.cursor()
print("📚 Database connected successfully")
# Check current state
cursor.execute("SELECT COUNT(*) FROM notes")
notes_count = cursor.fetchone()[0]
print(f"📝 Found {notes_count} notes in database")
cursor.execute("SELECT COUNT(*) FROM note_embeddings")
embeddings_count = cursor.fetchone()[0]
print(f"🔢 Current embeddings: {embeddings_count}")
# Get all notes
cursor.execute("SELECT id, title, content FROM notes")
notes = cursor.fetchall()
if not notes:
print("❌ No notes found in database!")
return False
print(f"\n🔄 Regenerating embeddings for {len(notes)} notes...")
# Clear existing embeddings
cursor.execute("DELETE FROM note_embeddings")
conn.commit()
print("🧹 Cleared existing embeddings")
# Generate embeddings for each note using OpenAI
success_count = 0
for i, (note_id, title, content) in enumerate(notes, 1):
print(f"📊 Processing note {i}/{len(notes)}: {title[:50]}...")
try:
# Create full text for embedding
full_text = f"{title}\n\n{content}"
# Generate embedding using OpenAI text-embedding-3-small
response = client.embeddings.create(
model="text-embedding-3-small",
input=full_text
)
embedding = response.data[0].embedding
embedding_array = np.array(embedding, dtype=np.float32)
# Store embedding in database
embedding_blob = embedding_array.tobytes()
cursor.execute(
"INSERT INTO note_embeddings (note_id, embedding) VALUES (?, ?)",
(note_id, embedding_blob)
)
success_count += 1
print(f"✅ Successfully generated embedding for note {note_id} ({len(embedding)} dimensions)")
except Exception as e:
print(f"❌ Failed to generate embedding for note {note_id}: {e}")
conn.commit()
print(f"\n🎉 Successfully generated {success_count}/{len(notes)} embeddings!")
# Verify the embeddings
cursor.execute("SELECT COUNT(*) FROM note_embeddings")
final_count = cursor.fetchone()[0]
print(f"✅ Final embedding count: {final_count}")
conn.close()
return True
except Exception as e:
print(f"❌ Error fixing embeddings: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
success = main()
exit(0 if success else 1)