forked from lyonzin/knowledge-rag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
621 lines (501 loc) · 20.9 KB
/
install.ps1
File metadata and controls
621 lines (501 loc) · 20.9 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
<#
.SYNOPSIS
Knowledge RAG System - Automated Installation Script
.DESCRIPTION
Installs and configures a local RAG (Retrieval-Augmented Generation) system
with ChromaDB, Ollama embeddings, and MCP integration for Claude Code.
.AUTHOR
Ailton Rocha (Lyon) - AI Operator
.VERSION
1.0.0
.REQUIREMENTS
- Windows 10/11
- Internet connection
- Administrator privileges (for some installations)
.USAGE
.\install.ps1 # Full installation
.\install.ps1 -SkipPython # Skip Python installation
.\install.ps1 -SkipOllama # Skip Ollama installation
.\install.ps1 -DocsPath "C:\Docs" # Custom documents path
#>
[CmdletBinding()]
param(
[switch]$SkipPython,
[switch]$SkipOllama,
[switch]$SkipIndex,
[string]$InstallPath = $PSScriptRoot, # Uses directory where script is located
[string]$DocsPath = "",
[switch]$Force
)
# ============================================================================
# CONFIGURATION
# ============================================================================
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
$CONFIG = @{
PythonVersion = "3.12"
PythonMinVersion = "3.11"
PythonInstallerUrl = "https://www.python.org/ftp/python/3.12.0/python-3.12.0-amd64.exe"
OllamaInstallerUrl = "https://ollama.com/download/OllamaSetup.exe"
EmbeddingModel = "nomic-embed-text"
RequiredPackages = @("chromadb", "pymupdf", "ollama", "mcp")
}
# ============================================================================
# HELPER FUNCTIONS
# ============================================================================
function Write-Banner {
$banner = @"
╔═══════════════════════════════════════════════════════════════════╗
║ ║
║ ██╗ ██╗███╗ ██╗ ██████╗ ██╗ ██╗██╗ ███████╗██████╗ ║
║ ██║ ██╔╝████╗ ██║██╔═══██╗██║ ██║██║ ██╔════╝██╔══██╗ ║
║ █████╔╝ ██╔██╗ ██║██║ ██║██║ █╗ ██║██║ █████╗ ██║ ██║ ║
║ ██╔═██╗ ██║╚██╗██║██║ ██║██║███╗██║██║ ██╔══╝ ██║ ██║ ║
║ ██║ ██╗██║ ╚████║╚██████╔╝╚███╔███╔╝███████╗███████╗██████╔╝ ║
║ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚══╝╚══╝ ╚══════╝╚══════╝╚═════╝ ║
║ ║
║ RAG SYSTEM INSTALLER v1.0 ║
║ Local Semantic Search for Claude Code ║
║ ║
╚═══════════════════════════════════════════════════════════════════╝
"@
Write-Host $banner -ForegroundColor Cyan
}
function Write-Step {
param([string]$Message, [string]$Status = "INFO")
$colors = @{
"INFO" = "Cyan"
"OK" = "Green"
"WARN" = "Yellow"
"ERROR" = "Red"
"SKIP" = "DarkGray"
}
$symbols = @{
"INFO" = "[*]"
"OK" = "[+]"
"WARN" = "[!]"
"ERROR" = "[-]"
"SKIP" = "[~]"
}
Write-Host "$($symbols[$Status]) " -ForegroundColor $colors[$Status] -NoNewline
Write-Host $Message
}
function Test-Administrator {
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
function Get-PythonPath {
# Check common Python installation paths
$paths = @(
"$env:LOCALAPPDATA\Programs\Python\Python312\python.exe",
"$env:LOCALAPPDATA\Programs\Python\Python311\python.exe",
"C:\Program Files\Python312\python.exe",
"C:\Program Files\Python311\python.exe",
"C:\Python312\python.exe",
"C:\Python311\python.exe"
)
foreach ($path in $paths) {
if (Test-Path $path) {
return $path
}
}
# Try to find via py launcher
try {
$pyPath = & py -3.12 -c "import sys; print(sys.executable)" 2>$null
if ($pyPath -and (Test-Path $pyPath)) {
return $pyPath
}
$pyPath = & py -3.11 -c "import sys; print(sys.executable)" 2>$null
if ($pyPath -and (Test-Path $pyPath)) {
return $pyPath
}
} catch {}
# Try PATH
try {
$pyPath = (Get-Command python -ErrorAction SilentlyContinue).Source
if ($pyPath) {
$version = & $pyPath --version 2>&1
if ($version -match "3\.(11|12)") {
return $pyPath
}
}
} catch {}
return $null
}
function Get-OllamaPath {
$paths = @(
"$env:LOCALAPPDATA\Programs\Ollama\ollama.exe",
"$env:PROGRAMFILES\Ollama\ollama.exe",
"C:\Program Files\Ollama\ollama.exe"
)
foreach ($path in $paths) {
if (Test-Path $path) {
return $path
}
}
try {
return (Get-Command ollama -ErrorAction SilentlyContinue).Source
} catch {
return $null
}
}
function Test-OllamaRunning {
try {
$response = Invoke-RestMethod -Uri "http://localhost:11434/api/tags" -Method Get -TimeoutSec 5
return $true
} catch {
return $false
}
}
function Start-OllamaService {
$ollamaPath = Get-OllamaPath
if (-not $ollamaPath) {
return $false
}
Write-Step "Starting Ollama service..." "INFO"
Start-Process -FilePath $ollamaPath -ArgumentList "serve" -WindowStyle Hidden
# Wait for service to start
$maxWait = 30
$waited = 0
while (-not (Test-OllamaRunning) -and $waited -lt $maxWait) {
Start-Sleep -Seconds 1
$waited++
}
return (Test-OllamaRunning)
}
# ============================================================================
# INSTALLATION STEPS
# ============================================================================
function Install-Python {
Write-Host "`n=== PYTHON INSTALLATION ===" -ForegroundColor Yellow
$pythonPath = Get-PythonPath
if ($pythonPath) {
$version = & $pythonPath --version 2>&1
Write-Step "Python found: $version at $pythonPath" "OK"
return $pythonPath
}
Write-Step "Python 3.11/3.12 not found. Installing..." "WARN"
$installerPath = "$env:TEMP\python-installer.exe"
Write-Step "Downloading Python installer..." "INFO"
Invoke-WebRequest -Uri $CONFIG.PythonInstallerUrl -OutFile $installerPath
Write-Step "Running Python installer (this may take a few minutes)..." "INFO"
$installArgs = "/quiet InstallAllUsers=1 PrependPath=1 Include_test=0"
Start-Process -FilePath $installerPath -ArgumentList $installArgs -Wait
Remove-Item $installerPath -Force -ErrorAction SilentlyContinue
# Refresh environment
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
$pythonPath = Get-PythonPath
if ($pythonPath) {
Write-Step "Python installed successfully!" "OK"
return $pythonPath
} else {
throw "Python installation failed. Please install Python 3.11 or 3.12 manually."
}
}
function Install-Ollama {
Write-Host "`n=== OLLAMA INSTALLATION ===" -ForegroundColor Yellow
$ollamaPath = Get-OllamaPath
if ($ollamaPath) {
Write-Step "Ollama found at $ollamaPath" "OK"
return $ollamaPath
}
Write-Step "Ollama not found. Installing..." "WARN"
$installerPath = "$env:TEMP\OllamaSetup.exe"
Write-Step "Downloading Ollama installer..." "INFO"
Invoke-WebRequest -Uri $CONFIG.OllamaInstallerUrl -OutFile $installerPath
Write-Step "Running Ollama installer..." "INFO"
Start-Process -FilePath $installerPath -ArgumentList "/S" -Wait
Remove-Item $installerPath -Force -ErrorAction SilentlyContinue
# Wait for installation to complete
Start-Sleep -Seconds 5
$ollamaPath = Get-OllamaPath
if ($ollamaPath) {
Write-Step "Ollama installed successfully!" "OK"
return $ollamaPath
} else {
throw "Ollama installation failed. Please install Ollama manually from https://ollama.com"
}
}
function Install-EmbeddingModel {
param([string]$OllamaPath)
Write-Host "`n=== EMBEDDING MODEL ===" -ForegroundColor Yellow
# Ensure Ollama is running
if (-not (Test-OllamaRunning)) {
if (-not (Start-OllamaService)) {
throw "Could not start Ollama service. Please start it manually."
}
}
Write-Step "Ollama service is running" "OK"
# Check if model exists
try {
$models = & $OllamaPath list 2>&1
if ($models -match $CONFIG.EmbeddingModel) {
Write-Step "Model '$($CONFIG.EmbeddingModel)' already installed" "OK"
return
}
} catch {}
Write-Step "Pulling embedding model '$($CONFIG.EmbeddingModel)'..." "INFO"
Write-Step "This may take a few minutes on first run..." "INFO"
& $OllamaPath pull $CONFIG.EmbeddingModel
if ($LASTEXITCODE -eq 0) {
Write-Step "Embedding model installed successfully!" "OK"
} else {
throw "Failed to pull embedding model"
}
}
function Setup-ProjectStructure {
Write-Host "`n=== PROJECT STRUCTURE ===" -ForegroundColor Yellow
# Create main directory
if (-not (Test-Path $InstallPath)) {
New-Item -ItemType Directory -Path $InstallPath -Force | Out-Null
Write-Step "Created: $InstallPath" "OK"
} else {
Write-Step "Directory exists: $InstallPath" "OK"
}
# Create subdirectories
# Note: documents/ is optional - add your own documents there
# The config.py expects data/chroma_db/ for vector storage
$dirs = @(
"mcp_server",
"documents",
"documents\security",
"documents\logscale",
"documents\development",
"documents\general",
"data",
"data\chroma_db",
".claude"
)
foreach ($dir in $dirs) {
$fullPath = Join-Path $InstallPath $dir
if (-not (Test-Path $fullPath)) {
New-Item -ItemType Directory -Path $fullPath -Force | Out-Null
}
}
Write-Step "Directory structure created" "OK"
}
function Setup-VirtualEnvironment {
param([string]$PythonPath)
Write-Host "`n=== VIRTUAL ENVIRONMENT ===" -ForegroundColor Yellow
$venvPath = Join-Path $InstallPath "venv"
$venvPython = Join-Path $venvPath "Scripts\python.exe"
$venvPip = Join-Path $venvPath "Scripts\pip.exe"
# Create venv if needed
if (-not (Test-Path $venvPython) -or $Force) {
Write-Step "Creating virtual environment..." "INFO"
& $PythonPath -m venv $venvPath
Write-Step "Virtual environment created" "OK"
} else {
Write-Step "Virtual environment exists" "OK"
}
# Upgrade pip
Write-Step "Upgrading pip..." "INFO"
& $venvPython -m pip install --upgrade pip --quiet
# Install packages
Write-Step "Installing dependencies..." "INFO"
foreach ($package in $CONFIG.RequiredPackages) {
Write-Step " Installing $package..." "INFO"
& $venvPip install $package --quiet
}
Write-Step "All dependencies installed!" "OK"
return $venvPython
}
function Create-SourceFiles {
Write-Host "`n=== SOURCE FILES ===" -ForegroundColor Yellow
# __init__.py
$initPath = Join-Path $InstallPath "mcp_server\__init__.py"
if (-not (Test-Path $initPath)) {
'"""Knowledge RAG MCP Server Package"""' | Out-File -FilePath $initPath -Encoding utf8
}
# config.py - only create if doesn't exist (preserve git version)
$configPath = Join-Path $InstallPath "mcp_server\config.py"
if (Test-Path $configPath) {
Write-Step "Found: config.py (using existing)" "OK"
} else {
Write-Step "config.py not found - please ensure you cloned the repository" "WARN"
}
# Check if ingestion.py and server.py exist
$ingestionPath = Join-Path $InstallPath "mcp_server\ingestion.py"
$serverPath = Join-Path $InstallPath "mcp_server\server.py"
if (-not (Test-Path $ingestionPath)) {
Write-Step "ingestion.py not found - please copy from source" "WARN"
} else {
Write-Step "Found: ingestion.py" "OK"
}
if (-not (Test-Path $serverPath)) {
Write-Step "server.py not found - please copy from source" "WARN"
} else {
Write-Step "Found: server.py" "OK"
}
}
function Setup-MCPConfiguration {
param([string]$VenvPython)
Write-Host "`n=== MCP CONFIGURATION ===" -ForegroundColor Yellow
# Use cmd /c wrapper to ensure working directory is set correctly
# (Claude Code may not respect the cwd property)
$escapedPath = $InstallPath.Replace("\", "\\")
$cmdArgs = "/c", "cd /d $InstallPath && .\venv\Scripts\python.exe -m mcp_server.server"
$mcpConfig = @{
mcpServers = @{
"knowledge-rag" = @{
type = "stdio"
command = "cmd"
args = @("/c", "cd /d $escapedPath && .\venv\Scripts\python.exe -m mcp_server.server")
env = @{}
}
}
}
$mcpJson = $mcpConfig | ConvertTo-Json -Depth 10
# Project-level config
$projectMcpPath = Join-Path $InstallPath ".claude\mcp.json"
$mcpJson | Out-File -FilePath $projectMcpPath -Encoding utf8
Write-Step "Created: .claude\mcp.json (project)" "OK"
# Global config
$globalClaudeDir = "$env:USERPROFILE\.claude"
if (-not (Test-Path $globalClaudeDir)) {
New-Item -ItemType Directory -Path $globalClaudeDir -Force | Out-Null
}
$globalMcpPath = Join-Path $globalClaudeDir "mcp.json"
# Merge with existing config if present
if (Test-Path $globalMcpPath) {
try {
$existingConfig = Get-Content $globalMcpPath -Raw | ConvertFrom-Json -AsHashtable
$existingConfig.mcpServers["knowledge-rag"] = $mcpConfig.mcpServers["knowledge-rag"]
$existingConfig | ConvertTo-Json -Depth 10 | Out-File -FilePath $globalMcpPath -Encoding utf8
Write-Step "Updated: ~/.claude/mcp.json (global)" "OK"
} catch {
$mcpJson | Out-File -FilePath $globalMcpPath -Encoding utf8
Write-Step "Created: ~/.claude/mcp.json (global)" "OK"
}
} else {
$mcpJson | Out-File -FilePath $globalMcpPath -Encoding utf8
Write-Step "Created: ~/.claude/mcp.json (global)" "OK"
}
}
function Run-InitialIndex {
param([string]$VenvPython)
Write-Host "`n=== INITIAL INDEXING ===" -ForegroundColor Yellow
# Check if there are documents to index
$docsPath = Join-Path $InstallPath "documents"
$docCount = (Get-ChildItem -Path $docsPath -Recurse -File | Where-Object {
$_.Extension -in @(".md", ".txt", ".pdf", ".py", ".json")
}).Count
if ($docCount -eq 0) {
Write-Step "No documents found to index. Add documents to: $docsPath" "WARN"
return
}
Write-Step "Found $docCount documents to index" "INFO"
# Ensure Ollama is running
if (-not (Test-OllamaRunning)) {
if (-not (Start-OllamaService)) {
Write-Step "Ollama not running. Start it and run indexing manually." "WARN"
return
}
}
Write-Step "Running initial indexing..." "INFO"
$indexScript = @"
import sys
sys.path.insert(0, r'$InstallPath')
from mcp_server.server import KnowledgeOrchestrator
orch = KnowledgeOrchestrator()
result = orch.index_all()
print(f"Indexed: {result['indexed']} files, {result['chunks_added']} chunks")
print(f"Categories: {result['categories']}")
"@
try {
$result = & $VenvPython -c $indexScript 2>&1
Write-Host $result
Write-Step "Indexing complete!" "OK"
} catch {
Write-Step "Indexing failed: $_" "ERROR"
}
}
function Show-Summary {
param([string]$VenvPython)
$summary = @"
╔═══════════════════════════════════════════════════════════════════╗
║ INSTALLATION COMPLETE! ║
╚═══════════════════════════════════════════════════════════════════╝
Installation Path: $InstallPath
Python: $VenvPython
Embedding Model: $($CONFIG.EmbeddingModel)
┌─────────────────────────────────────────────────────────────────┐
│ NEXT STEPS │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 1. Add documents to: $InstallPath\documents\
│ - security\ -> Security/pentest content
│ - logscale\ -> LogScale/CQL queries
│ - development\ -> Code/dev documentation
│ - general\ -> Other documents
│ │
│ 2. Restart Claude Code to load the MCP server │
│ │
│ 3. Available MCP Tools: │
│ - search_knowledge(query, max_results, category) │
│ - get_document(filepath) │
│ - reindex_documents(force) │
│ - list_categories() │
│ - list_documents(category) │
│ - get_index_stats() │
│ │
└─────────────────────────────────────────────────────────────────┘
IMPORTANT: Ollama must be running before using the RAG system!
Start Ollama with: ollama serve
"@
Write-Host $summary -ForegroundColor Green
}
# ============================================================================
# MAIN EXECUTION
# ============================================================================
try {
Write-Banner
# Check admin for installations
if (-not $SkipPython -or -not $SkipOllama) {
if (-not (Test-Administrator)) {
Write-Step "Some installations may require administrator privileges" "WARN"
}
}
# Step 1: Python
if ($SkipPython) {
Write-Step "Skipping Python installation" "SKIP"
$pythonPath = Get-PythonPath
if (-not $pythonPath) {
throw "Python 3.11/3.12 not found. Run without -SkipPython"
}
} else {
$pythonPath = Install-Python
}
# Step 2: Ollama
if ($SkipOllama) {
Write-Step "Skipping Ollama installation" "SKIP"
$ollamaPath = Get-OllamaPath
} else {
$ollamaPath = Install-Ollama
if ($ollamaPath) {
Install-EmbeddingModel -OllamaPath $ollamaPath
}
}
# Step 3: Project structure
Setup-ProjectStructure
# Step 4: Virtual environment
$venvPython = Setup-VirtualEnvironment -PythonPath $pythonPath
# Step 5: Source files
Create-SourceFiles
# Step 6: MCP configuration
Setup-MCPConfiguration -VenvPython $venvPython
# Step 7: Initial indexing
if (-not $SkipIndex) {
Run-InitialIndex -VenvPython $venvPython
} else {
Write-Step "Skipping initial indexing" "SKIP"
}
# Summary
Show-Summary -VenvPython $venvPython
Write-Host "Installation completed successfully!" -ForegroundColor Green
} catch {
Write-Host "`n[!] Installation failed: $_" -ForegroundColor Red
Write-Host "Please check the error and try again." -ForegroundColor Yellow
exit 1
}