230 lines
9.7 KiB
PowerShell
230 lines
9.7 KiB
PowerShell
# d0a1 pack - Hermes Agent (Windows)
|
|
# Agente de IA desde terminal o Telegram.
|
|
#
|
|
# Local: .\deploy.ps1 local -> terminal + http://localhost:11434 (Ollama)
|
|
# VPS: .\deploy.ps1 vps -> + gateway accesible desde fuera
|
|
# Stop: .\deploy.ps1 stop -> para Ollama
|
|
#
|
|
# Requiere: Docker Desktop para Windows (https://docs.docker.com/desktop/)
|
|
|
|
param(
|
|
[Parameter(Position = 0)]
|
|
[ValidateSet('local', 'vps', 'stop')]
|
|
[string]$Mode = 'local'
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
# Escape ANSI via [char]27 (compatible con Windows PowerShell 5.1 y PS 7+)
|
|
$Esc = [char]27
|
|
$Green = "$Esc[32m"; $Cyan = "$Esc[36m"; $Yellow = "$Esc[33m"; $Red = "$Esc[31m"; $Bold = "$Esc[1m"; $NC = "$Esc[0m"
|
|
if ($env:NO_COLOR) {
|
|
$Green = $Cyan = $Yellow = $Red = $Bold = $NC = ''
|
|
}
|
|
|
|
function Info($msg) { Write-Host "${Bold}${Green}>>>${NC} $msg" }
|
|
function Warn($msg) { Write-Host "${Bold}${Yellow}>>>${NC} $msg" }
|
|
function Err($msg) { Write-Host "${Bold}${Red}>>>${NC} $msg" }
|
|
function Ok($msg) { Write-Host "${Green}OK${NC} $msg" }
|
|
|
|
function Find-FreePort([int]$Start) {
|
|
for ($p = $Start; $p -le ($Start + 20); $p++) {
|
|
$inUse = Get-NetTCPConnection -LocalPort $p -State Listen -ErrorAction SilentlyContinue
|
|
if (-not $inUse) { return $p }
|
|
}
|
|
return $Start
|
|
}
|
|
|
|
Write-Host ''
|
|
Write-Host "${Bold}${Cyan}============================================================${NC}"
|
|
Write-Host "${Bold}${Cyan} d0a1 pack - Hermes Agent (Windows)${NC}"
|
|
Write-Host "${Bold}${Cyan} Agente de IA desde terminal o Telegram.${NC}"
|
|
Write-Host "${Bold}${Cyan}============================================================${NC}"
|
|
Write-Host ''
|
|
|
|
# --- Stop mode ------------------------------------------------------------
|
|
if ($Mode -eq 'stop') {
|
|
$running = docker ps --format '{{.Names}}' | Select-String -Quiet 'd0a1-hermes-ollama'
|
|
if ($running) {
|
|
docker stop d0a1-hermes-ollama | Out-Null
|
|
docker rm d0a1-hermes-ollama | Out-Null
|
|
Info 'Ollama parado.'
|
|
} else {
|
|
Write-Host 'Ollama no esta corriendo.'
|
|
}
|
|
exit 0
|
|
}
|
|
|
|
# --- Check Docker (para Ollama) -------------------------------------------
|
|
if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
|
|
Err 'Docker no esta instalado (necesario para Ollama).'
|
|
Write-Host ''
|
|
Write-Host 'Instala Docker Desktop para Windows:'
|
|
Write-Host ' https://docs.docker.com/desktop/setup/install/windows-install/'
|
|
exit 1
|
|
}
|
|
# En PS 5.1, *> $null sobre comandos nativos convierte stderr en errores
|
|
# terminantes con EAP=Stop (PS #4002). Se descarta via cmd /c.
|
|
$null = cmd /c "docker info >NUL 2>&1"
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Err 'Docker Desktop no responde. Abre Docker Desktop y espera a que arranque.'
|
|
exit 1
|
|
}
|
|
Ok "Docker: $((docker --version) -replace 'Docker version ','' -replace ',.*','')"
|
|
|
|
# --- Check RAM ------------------------------------------------------------
|
|
$TotalRamGb = [math]::Round((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1GB, 1)
|
|
if ($TotalRamGb -lt 3.5) {
|
|
Err "Se necesitan 4 GB de RAM minimo (detectado: $TotalRamGb GB)."
|
|
exit 1
|
|
}
|
|
Ok "RAM: $TotalRamGb GB"
|
|
|
|
# --- Tumbar deploy previo (antes de detectar puertos) -----------------------
|
|
cmd /c docker rm -f d0a1-hermes-ollama '>' NUL '2>&1'
|
|
Info 'Deploy previo detenido (rm; volumen con el modelo intacto).'
|
|
|
|
# --- GPU NVIDIA? (si hay, Ollama la usa y va 5-10x mas rapido) ---------------
|
|
$HasGpu = $false
|
|
if (Get-Command nvidia-smi -ErrorAction SilentlyContinue) {
|
|
$GpuInfo = & nvidia-smi --query-gpu=name,memory.total --format=csv,noheader 2>$null
|
|
if ($LASTEXITCODE -eq 0 -and $GpuInfo) {
|
|
$HasGpu = $true
|
|
$GpuName = ($GpuInfo -split ',')[0].Trim()
|
|
Info "GPU NVIDIA detectada: $GpuName (aceleracion activada)"
|
|
}
|
|
}
|
|
if (-not $HasGpu) {
|
|
Warn 'Sin GPU NVIDIA: modo CPU (~10-20 tokens/s). Con una GPU NVIDIA va 5-10x mas rapido.'
|
|
}
|
|
# --- Auto-detect Ollama port ------------------------------------------------
|
|
$OllamaPort = Find-FreePort 11434
|
|
|
|
if ($Mode -eq 'vps') {
|
|
Warn 'Modo: VPS'
|
|
Write-Host ''
|
|
Write-Host " ${Bold}¿No tienes VPS?${NC} Consigue uno con descuento:"
|
|
Write-Host " ${Cyan}https://d0a1.es/go/vultr/${NC} - Vultr"
|
|
Write-Host " ${Cyan}https://d0a1.es/go/digitalocean/${NC} - DigitalOcean (`$200 credit)"
|
|
Write-Host ''
|
|
}
|
|
if ($OllamaPort -ne 11434) { Warn "Puerto 11434 ocupado, usando $OllamaPort" }
|
|
Write-Host ''
|
|
|
|
# --- 1. Arrancar Ollama (Docker) -------------------------------------------
|
|
Warn '[1/3] Arrancando Ollama (IA local)...'
|
|
$running = docker ps --format '{{.Names}}' | Select-String -Quiet 'd0a1-hermes-ollama'
|
|
if (-not $running) {
|
|
$GpuArgs = @()
|
|
if ($HasGpu) { $GpuArgs = @('--gpus', 'all') }
|
|
docker run -d --name d0a1-hermes-ollama --memory 6g @GpuArgs `
|
|
-p "${OllamaPort}:11434" `
|
|
-v d0a1_hermes_ollama:/root/.ollama `
|
|
-e OLLAMA_HOST=0.0.0.0 `
|
|
--restart unless-stopped `
|
|
ollama/ollama:0.33.3 | Out-Null
|
|
if ($LASTEXITCODE -ne 0) { Err 'Fallo al arrancar el contenedor de Ollama.'; exit 1 }
|
|
}
|
|
|
|
Warn ' Esperando a que Ollama arranque...'
|
|
$MaxWait = 60; $Waited = 0
|
|
while ($true) {
|
|
# Descarte de stderr via cmd /c (ver nota de PS 5.1 / PS #4002 arriba)
|
|
cmd /c "docker exec d0a1-hermes-ollama ollama ps >NUL 2>&1"
|
|
if ($LASTEXITCODE -eq 0) { break }
|
|
Start-Sleep -Seconds 2
|
|
$Waited += 2
|
|
if ($Waited -ge $MaxWait) {
|
|
Err 'Ollama no arranco. Revisa: docker logs d0a1-hermes-ollama'
|
|
exit 1
|
|
}
|
|
}
|
|
Warn ' Descargando modelo (llama3.2:3b, ~2 GB)...'
|
|
# Si el usuario define $env:HTTPS_PROXY, Ollama lo usa para el pull (doc oficial):
|
|
$ProxyEnv = @()
|
|
if ($env:HTTPS_PROXY) { $ProxyEnv = @('-e', "HTTPS_PROXY=$env:HTTPS_PROXY", '-e', "HTTP_PROXY=$env:HTTP_PROXY") }
|
|
# --- Modelo ya presente? (volumen persistente: no re-descargar) ------------
|
|
$ModelList = docker exec d0a1-hermes-ollama ollama list 2>$null
|
|
if ("$ModelList" -match 'llama3.2:3b') {
|
|
Info 'Modelo llama3.2:3b ya descargado (volumen intacto). Omitiendo descarga.'
|
|
} else {
|
|
# --- Modelo: HF primero (CDN propio, rapido) y registry de Ollama como ultimo recurso ---
|
|
|
|
$Gguf = Join-Path $ScriptDir 'llama3.2-3b-q4.gguf'
|
|
$HfUrls = @(
|
|
'https://huggingface.co/bartowski/Llama-3.2-3B-Instruct-GGUF/resolve/main/Llama-3.2-3B-Instruct-Q4_K_M.gguf',
|
|
'https://huggingface.co/unsloth/Llama-3.2-3B-Instruct-GGUF/resolve/main/Llama-3.2-3B-Instruct-Q4_K_M.gguf'
|
|
)
|
|
Info 'Descargando modelo de IA (llama3.2:3b, ~2 GB)...'
|
|
foreach ($Url in $HfUrls) {
|
|
curl.exe -# -L --retry 3 --max-time 1800 -o "$Gguf" $Url
|
|
if ($LASTEXITCODE -eq 0 -and (Test-Path $Gguf) -and (Get-Item $Gguf).Length -gt 1900000000) { break }
|
|
Remove-Item "$Gguf" -Force -ErrorAction SilentlyContinue
|
|
if ($Url -ne $HfUrls[-1]) { Warn 'Mirror no accesible, probando el siguiente...' }
|
|
}
|
|
if ((Test-Path $Gguf) -and (Get-Item $Gguf).Length -gt 1900000000) {
|
|
Info 'Importando modelo en Ollama...'
|
|
docker cp "$Gguf" d0a1-hermes-ollama:/root/.ollama/import.gguf
|
|
docker exec d0a1-hermes-ollama sh -c 'printf "FROM /root/.ollama/import.gguf\\nPARAMETER num_ctx 16384\\n" > /tmp/mf && ollama create llama3.2:3b -f /tmp/mf'
|
|
$CreateOk = ($LASTEXITCODE -eq 0)
|
|
docker exec d0a1-hermes-ollama rm -f /root/.ollama/import.gguf | Out-Null
|
|
Remove-Item "$Gguf" -Force -ErrorAction SilentlyContinue
|
|
$PullOk = $CreateOk
|
|
if (-not $CreateOk) { Err 'Fallo al importar el modelo desde el GGUF.' }
|
|
} else {
|
|
# Ultimo recurso: pull directo (1 intento, ya sin esperar 3 rondas)
|
|
Warn 'HuggingFace no accesible. Probando el registry de Ollama (ultimo recurso)...'
|
|
docker exec @ProxyEnv d0a1-hermes-ollama ollama pull llama3.2:3b
|
|
if ($LASTEXITCODE -eq 0) {
|
|
$PullOk = $true
|
|
# Contexto ampliado (el default 4096 da 400 en Open WebUI con prompts largos)
|
|
docker exec d0a1-hermes-ollama sh -c 'printf "FROM llama3.2:3b\\nPARAMETER num_ctx 16384\\n" > /tmp/mf && ollama create llama3.2:3b -f /tmp/mf'
|
|
}
|
|
}
|
|
if (-not $PullOk) {
|
|
Err 'No se pudo descargar el modelo (ni HuggingFace ni registry).'
|
|
if (-not $env:HTTPS_PROXY) {
|
|
Write-Host ' Tu red bloquea ambos. Si tienes proxy/VPN:'
|
|
Write-Host ' $env:HTTPS_PROXY = "http://127.0.0.1:7890" # (puerto de tu proxy)'
|
|
Write-Host ' .\deploy.ps1 vps'
|
|
}
|
|
exit 1
|
|
}
|
|
}
|
|
Ok "Ollama corriendo en http://localhost:${OllamaPort}"
|
|
|
|
# --- 2. Instalar Hermes Agent ------------------------------------------------
|
|
Warn '[2/3] Instalando Hermes Agent...'
|
|
if (Get-Command hermes -ErrorAction SilentlyContinue) {
|
|
Ok "Hermes Agent ya instalado: $(cmd /c 'hermes --version 2>NUL')"
|
|
} else {
|
|
# En Windows: instala con winget o pip. El install.sh de Linux no aplica.
|
|
Write-Host ' Instalando con pip (requiere Python 3.10+)...'
|
|
pip install --user hermes-agent
|
|
if ($LASTEXITCODE -ne 0 -or -not (Get-Command hermes -ErrorAction SilentlyContinue)) {
|
|
Err 'Hermes Agent no se instalo. Instalalo manualmente:'
|
|
Write-Host ' https://hermes-agent.nousresearch.com/docs'
|
|
exit 1
|
|
}
|
|
Ok "Hermes Agent instalado: $(cmd /c 'hermes --version 2>NUL')"
|
|
}
|
|
|
|
# --- 3. Configurar ------------------------------------------------------------
|
|
Warn '[3/3] Configurando...'
|
|
Write-Host ''
|
|
Write-Host " ${Bold}Ejecuta estos comandos para terminar:${NC}"
|
|
Write-Host ''
|
|
Write-Host " ${Cyan}hermes setup${NC} - configuracion interactiva"
|
|
Write-Host " ${Cyan}hermes model${NC} - elige modelo (Ollama ya esta corriendo)"
|
|
Write-Host " ${Cyan}hermes gateway setup${NC} - conecta mensajeria (Telegram, etc.)"
|
|
Write-Host ''
|
|
|
|
if ($Mode -eq 'vps') {
|
|
Write-Host " ${Bold}Para VPS:${NC} configura el gateway para acceso remoto:"
|
|
Write-Host " ${Cyan}hermes gateway setup --portal${NC}"
|
|
Write-Host ''
|
|
}
|
|
|
|
Write-Host " ${Bold}Parar:${NC} .\deploy.ps1 stop"
|
|
Write-Host " ${Bold}¿Quieres mas?${NC} ${Cyan}https://d0a1.es/packs/${NC}"
|
|
Write-Host '' |