-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobsidian_chat.py
295 lines (241 loc) Β· 11.4 KB
/
obsidian_chat.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
#!/usr/bin/env python3
"""
Obsidian Chat - Chat with your Obsidian vault using Bhumi LLM client
Usage:
python obsidian_chat.py [--vault-path /path/to/vault]
"""
import os
import sys
import argparse
import glob
import platform
from pathlib import Path
from typing import List, Optional
import dotenv
from bhumi.base_client import BaseLLMClient, LLMConfig
# Load environment variables
dotenv.load_dotenv()
def find_obsidian_vault() -> List[str]:
"""Find Obsidian vault directories based on OS."""
possible_vaults = []
system = platform.system()
home = Path.home()
if system == "Darwin": # macOS
# Common locations on macOS
search_paths = [
home / "Documents" / "Obsidian",
home / "Documents",
home / "Library" / "Application Support" / "obsidian"
]
elif system == "Windows":
# Common locations on Windows
search_paths = [
home / "Documents" / "Obsidian",
home / "AppData" / "Local" / "Obsidian",
home / "AppData" / "Roaming" / "Obsidian"
]
else: # Linux and others
search_paths = [
home / "Documents" / "Obsidian",
home / ".obsidian",
home / ".config" / "obsidian"
]
# Check each path for vault directories
for path in search_paths:
if path.exists():
# Look for .obsidian directory which indicates a vault
if (path / ".obsidian").exists():
possible_vaults.append(str(path))
# Also check subdirectories for .obsidian folders
for subdir in path.iterdir():
if subdir.is_dir() and (subdir / ".obsidian").exists():
possible_vaults.append(str(subdir))
return possible_vaults
def get_vault_files(vault_path: str) -> List[str]:
"""Get all markdown files from the vault."""
markdown_files = []
# Use glob to find all markdown files
for extension in ['md', 'markdown']:
markdown_files.extend(glob.glob(f"{vault_path}/**/*.{extension}", recursive=True))
return markdown_files
def read_file_content(file_path: str) -> str:
"""Read and return the content of a file."""
try:
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
except Exception as e:
return f"Error reading file: {str(e)}"
async def chat_with_vault(vault_path: str, model: str = "jan/qwen2.5-coder-7b-instruct"):
"""Interactive chat session with the Obsidian vault content."""
print(f"π Loading Obsidian vault from: {vault_path}")
# Get all markdown files
files = get_vault_files(vault_path)
if not files:
print("β No markdown files found in the vault.")
return
print(f"π Found {len(files)} markdown files in your vault.")
# Configure Bhumi client
config = LLMConfig(
api_key="not-needed",
model=model,
max_tokens=1500,
)
# Create client
client = BaseLLMClient(config)
# Create a context with info about the vault
vault_info = f"This Obsidian vault contains {len(files)} markdown files.\n"
vault_info += "Here are some of the files:\n"
for i, file in enumerate(files[:10]): # Show first 10 files
relative_path = os.path.relpath(file, vault_path)
vault_info += f"- {relative_path}\n"
if len(files) > 10:
vault_info += f"... and {len(files) - 10} more files.\n"
print("\nπ€ Welcome to Obsidian Chat! Type 'exit' to quit or 'help' for commands.")
context = []
while True:
try:
user_input = input("\n㪠You: ")
if user_input.lower() in ['exit', 'quit']:
print("π Goodbye!")
break
if user_input.lower() == 'help':
print("\nCommands:")
print(" help - Show this help message")
print(" exit/quit - Exit the chat")
print(" search [query] - Search for files in your vault")
print(" read [filename] - Read a specific file")
print(" clear - Clear the conversation history")
continue
if user_input.lower().startswith('search '):
search_term = user_input[7:].strip()
results = []
for file in files:
if search_term.lower() in file.lower():
relative_path = os.path.relpath(file, vault_path)
results.append(relative_path)
if results:
print(f"\nFound {len(results)} matching files:")
for i, result in enumerate(results[:10]):
print(f" {i+1}. {result}")
if len(results) > 10:
print(f" ... and {len(results) - 10} more")
else:
print("No matching files found.")
continue
if user_input.lower().startswith('read '):
file_name = user_input[5:].strip()
found = False
for file in files:
if file_name.lower() in file.lower() or file_name.lower() in os.path.basename(file).lower():
content = read_file_content(file)
print(f"\nContent of {os.path.relpath(file, vault_path)}:")
print(f"------ BEGIN ------\n{content[:500]}")
if len(content) > 500:
print("...(content truncated)...")
print("------- END -------")
found = True
break
if not found:
print(f"File '{file_name}' not found. Use 'search' to find files.")
continue
if user_input.lower() == 'clear':
context = []
print("π§Ή Conversation history cleared.")
continue
# Process regular chat input
if not context:
# First message, set up the system context
context = [
{"role": "system", "content": f"You are a helpful assistant that can chat about the user's Obsidian vault. When the user asks about a specific file, automatically access and summarize its contents. {vault_info}"}
]
# Check if user is asking about a specific file or topic
mentioned_files = []
file_content = ""
for file in files:
file_basename = os.path.basename(file).lower()
file_name_without_ext = os.path.splitext(file_basename)[0].lower()
# Check if file name is mentioned in user input
if (file_name_without_ext in user_input.lower() or
file_basename in user_input.lower()):
content = read_file_content(file)
relative_path = os.path.relpath(file, vault_path)
file_content += f"\n--- Content of {relative_path} ---\n{content}\n"
mentioned_files.append(relative_path)
# Check for topic matches if no direct file mentions
if not mentioned_files and not user_input.lower().startswith(('search', 'read', 'help', 'exit', 'quit', 'clear')):
for file in files:
content = read_file_content(file)
# Simple check for topic relevance
words = user_input.lower().split()
relevant_words = [word for word in words if len(word) > 3 and word.lower() not in ["what", "where", "when", "which", "this", "that", "these", "those", "want", "have", "about", "from"]]
if any(word in content.lower() for word in relevant_words):
relative_path = os.path.relpath(file, vault_path)
if len(file_content) < 5000: # Limit total content
file_content += f"\n--- Content of {relative_path} that might be relevant ---\n{content[:800]}\n"
mentioned_files.append(relative_path)
# Add files content to the message if any were found
if mentioned_files:
enhanced_input = f"{user_input}\n\nRelevant file content:\n{file_content}"
context.append({"role": "user", "content": enhanced_input})
print(f"π Including content from: {', '.join(mentioned_files)}")
else:
# Add regular user message
context.append({"role": "user", "content": user_input})
# Stream response from Bhumi
print("\nπ€ Assistant: ", end="", flush=True)
full_response = ""
# Get streaming response
stream = await client.completion(context, stream=True)
# Process the streaming response correctly
async for chunk in stream:
full_response += chunk
print(chunk, end="", flush=True)
# Add a newline after streaming completes
print()
# Add assistant message to context
context.append({"role": "assistant", "content": full_response})
except KeyboardInterrupt:
print("\nπ Goodbye!")
break
except Exception as e:
print(f"β Error: {str(e)}")
def main():
parser = argparse.ArgumentParser(description="Chat with your Obsidian vault using Bhumi")
parser.add_argument("--vault-path", type=str, help="Path to your Obsidian vault")
parser.add_argument("--model", type=str, default="jan/qwen2.5-coder-7b-instruct",
help="Model to use with Bhumi")
args = parser.parse_args()
vault_path = args.vault_path
if not vault_path:
# Try to find vaults automatically
possible_vaults = find_obsidian_vault()
if not possible_vaults:
print("β No Obsidian vaults found. Please specify the path with --vault-path.")
sys.exit(1)
if len(possible_vaults) == 1:
vault_path = possible_vaults[0]
print(f"π Found Obsidian vault at: {vault_path}")
else:
print("π Found multiple Obsidian vaults:")
for i, path in enumerate(possible_vaults):
print(f"{i+1}. {path}")
choice = input("Please select a vault (number): ")
try:
index = int(choice) - 1
if 0 <= index < len(possible_vaults):
vault_path = possible_vaults[index]
else:
print("β Invalid selection. Exiting.")
sys.exit(1)
except ValueError:
print("β Invalid input. Exiting.")
sys.exit(1)
# Ensure the vault path exists
if not os.path.isdir(vault_path):
print(f"β Vault path does not exist: {vault_path}")
sys.exit(1)
# Run the chat interface
import asyncio
asyncio.run(chat_with_vault(vault_path, args.model))
if __name__ == "__main__":
main()