-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenai_linker.py
347 lines (282 loc) · 12 KB
/
genai_linker.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
genai_linker.py - Create links between notes using GenAI for relevance analysis
This script reads markdown notes from an Obsidian vault and:
1. Extracts titles and summaries from notes
2. Uses OpenAI GPT to find relationships between notes with explanations
3. Adds a "Related Notes (GenAI)" section with links and explanations
Features:
- Intelligent relevance scoring
- Natural language explanations for relationships
- Avoids duplicating existing links
Author: Jonathan Care <jonc@lacunae.org>
"""
import os
import sys
import re
import json
import random
from dotenv import load_dotenv
from openai import OpenAI
import utils
import signal_handler
# Load environment variables from .env file
load_dotenv()
# Initialize the OpenAI client with the API key
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def load_notes(vault_path=None):
"""Load all notes from the vault."""
if not vault_path:
vault_path = os.getenv("OBSIDIAN_VAULT_PATH")
if not vault_path:
print("Error: No vault path provided. Set OBSIDIAN_VAULT_PATH in .env")
sys.exit(1)
# Dictionary to store notes
notes = {}
# Walk through all directories and files in the vault
for root, dirs, files in os.walk(vault_path):
# Skip hidden directories
dirs[:] = [d for d in dirs if not d.startswith('.')]
for file in files:
if file.endswith(".md"):
try:
path = os.path.join(root, file)
with open(path, "r", encoding="utf-8") as f:
content = f.read()
notes[path] = {
"filename": file,
"content": content
}
except Exception as e:
print(f"Error reading {file}: {str(e)}")
print(f"Loaded {len(notes)} notes from vault")
return notes
def extract_titles_and_summaries(notes):
"""Extract titles and create summaries for each note."""
summaries = {}
for path, note in notes.items():
# Handle different note formats
if isinstance(note, dict) and "content" in note:
content = note["content"]
# Try to get filename from the note dictionary
if "filename" in note:
filename = note["filename"]
else:
# Extract filename from path
filename = os.path.basename(path)
else:
# In case note is directly a string
content = note
filename = os.path.basename(path)
# Extract title - first use H1 if available, else use filename
title = os.path.splitext(filename)[0]
h1_match = re.search(r'^#\s+(.+)$', content, re.MULTILINE)
if h1_match:
title = h1_match.group(1).strip()
# Create a summary from the first 500 characters, stopping at the nearest paragraph break
summary = content[:500]
last_para_break = summary.rfind("\n\n")
if last_para_break > 100: # Ensure we have at least 100 chars
summary = summary[:last_para_break]
# Store the title and summary
summaries[path] = {
"title": title,
"summary": summary.strip()
}
return summaries
def find_relevant_notes(target_path, notes, summaries, max_notes=5):
"""Find relevant notes for a target note using OpenAI API."""
target_note = notes[target_path]
# Handle different note formats for target note
if isinstance(target_note, dict) and "content" in target_note:
target_content = target_note["content"]
else:
# In case target_note is directly a string
target_content = target_note
target_summary = summaries[target_path]
# Get random sample of other notes (excluding the target)
other_paths = [path for path in notes.keys() if path != target_path]
# Limit to 20 random notes to keep API calls manageable
if len(other_paths) > 20:
other_paths = random.sample(other_paths, 20)
# Create the list of other note summaries
candidates = []
for path in other_paths:
candidates.append({
"path": path,
"title": summaries[path]["title"],
"summary": summaries[path]["summary"]
})
# Skip if we have no candidates
if not candidates:
return []
try:
# Prepare the prompt for the API
prompt = {
"target_note": {
"title": target_summary["title"],
"content": target_content[:2000] # Limit content to first 2000 chars
},
"candidate_notes": candidates
}
# Call the OpenAI API
response = client.chat.completions.create(
model="gpt-4o",
response_format={"type": "json_object"},
messages=[
{
"role": "system",
"content": """You are an assistant that finds relationships between notes in a knowledge base.
Analyze the target note and the candidate notes, and identify which candidates are most
relevant to the target. Provide a score from 1-10 (10 being highly related) and a brief
explanation. Return a JSON array with the path and score for each relevant note."""
},
{
"role": "user",
"content": f"""Find the most relevant notes to this target note:
Target: {json.dumps(prompt["target_note"])}
Candidates: {json.dumps(prompt["candidate_notes"])}
For each candidate, evaluate how related it is to the target note.
Choose at most {max_notes} notes that are most relevant.
Return a JSON array with "related_notes" containing:
- path (string)
- score (integer 1-10)
- reason (string, 1-2 sentences explaining the relationship)
Example:
{{
"related_notes": [
{{
"path": "/path/to/note.md",
"score": 8,
"reason": "Both notes discuss similar concepts and reference related theories."
}}
]
}}"""
}
]
)
# Parse the response
result = json.loads(response.choices[0].message.content)
# Extract the related notes
related_notes = result.get("related_notes", [])
# Only include notes with a score of at least 5
filtered_notes = [
{"path": note["path"], "score": note["score"], "reason": note["reason"]}
for note in related_notes
if note["score"] >= 5
]
return filtered_notes
except Exception as e:
print(f"Error finding relevant notes: {str(e)}")
return []
def save_notes(notes):
"""Save updated notes to disk."""
saved = 0
errors = 0
for path, note in notes.items():
try:
# Handle different note formats
if isinstance(note, dict) and "content" in note:
content = note["content"]
else:
# In case note is directly a string
content = note
with open(path, "w", encoding="utf-8") as f:
f.write(content)
saved += 1
except Exception as e:
print(f"Error saving {path}: {str(e)}")
errors += 1
print(f"Saved {saved} notes with {errors} errors")
return saved
def cleanup_before_exit():
"""Clean up resources before exiting."""
print("Performing cleanup before exit...")
print("GenAI linking tool interrupted. No files have been modified.")
print("Cleanup completed. Goodbye!")
def main():
"""Main function to run GenAI linking."""
# Set up clean interrupt handling
signal_handler.setup_interrupt_handling()
# Register cleanup function
signal_handler.register_cleanup_function(cleanup_before_exit)
vault_path = os.getenv("OBSIDIAN_VAULT_PATH")
if not vault_path:
print("Error: OBSIDIAN_VAULT_PATH not set in environment or .env file")
sys.exit(1)
print(f"Loading notes from vault: {vault_path}")
notes = load_notes(vault_path)
print("Extracting titles and summaries")
summaries = extract_titles_and_summaries(notes)
# Choose a random subset of notes to process
num_notes = min(10, len(notes)) # Process at most 10 notes for demonstration
note_paths = random.sample(list(notes.keys()), num_notes)
print(f"Processing {num_notes} random notes with GenAI linking")
updated = 0
skipped = 0
for path in note_paths:
try:
# Extract existing links to avoid duplicate linking
# Handle different note formats
if isinstance(notes[path], dict) and "content" in notes[path]:
content = notes[path]["content"]
else:
# In case the note is directly a string
content = notes[path]
# Ensure notes[path] is in the right format for later updates
notes[path] = {"content": content}
current_links = utils.extract_existing_links(content)
# Find relevant notes
relevant_notes = find_relevant_notes(path, notes, summaries)
if not relevant_notes:
continue
# Extract existing GenAI related notes section if it exists
section_text, _ = utils.extract_section(content, "## Related Notes (GenAI)")
existing_link_entries = []
if section_text:
existing_link_entries = section_text.split("\n")
# Create new link entries for relevant notes
new_link_entries = []
for rel_note in relevant_notes:
related_path = rel_note["path"]
# Handle both dictionary formats
if isinstance(notes[related_path], dict) and "filename" in notes[related_path]:
# Standard format from genai_linker.load_notes
related_filename = notes[related_path]["filename"]
note_name = os.path.splitext(related_filename)[0]
else:
# Alternative format or directly from obsidian_enhance.py
# Extract filename from the path
note_name = os.path.splitext(os.path.basename(related_path))[0]
# Skip if already linked in the document
if note_name in current_links:
continue
# Format the link with relevance score and reason
link_entry = f"- [[{note_name}]] (Score: {rel_note['score']}/10)\n - {rel_note['reason']}"
new_link_entries.append(link_entry)
# Add to current links to avoid duplicates in future iterations
current_links.append(note_name)
# If we have no entries to add and no existing entries, skip
if not new_link_entries and not existing_link_entries:
continue
# Merge existing and new link entries
all_link_entries = utils.merge_links(existing_link_entries, new_link_entries)
# Update the section in the content
updated_content = utils.replace_section(
content,
"## Related Notes (GenAI)",
"\n".join(all_link_entries)
)
# Save the updated content
notes[path]["content"] = updated_content
updated += 1
except Exception as e:
print(f"Error updating {path}: {str(e)}")
skipped += 1
print("Saving notes")
saved = save_notes(notes)
print(f"Added GenAI links to {updated} notes ({saved} saved)")
return saved
if __name__ == "__main__":
main()