[Console]::InputEncoding = [System.Text.UTF8Encoding]::new($false) [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false) $OutputEncoding = [Console]::OutputEncoding $env:PYTHONIOENCODING = 'utf-8' $env:PYTHONUTF8 = '1' $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $ServerExe = 'llama-server.exe' # === 基础路径 / Base paths === $LlamaBin = 'D:\llama' $ModelsDir = 'E:\LMStudio\.lmstudio\models' $Preset = Join-Path $ScriptDir 'models-preset.ini' $Python = 'python' # === 网络 / Network === $LLAMA_Host = '127.0.0.1' $Port = 18003 $Timeout = '' $ThreadsHttp = '' # === Router 行为 / Router behavior === $Ttl = 300 $ModelsMax = 1 $RegeneratePreset = $true $ApiKey = '' $ApiKeyFile = '' $CacheRam = '' $EnableMetrics = $false $EnableUi = $false $NoUi = $false $SslCertFile = '' $SslKeyFile = '' $LlamaServer = Join-Path $LlamaBin $ServerExe if (-not (Test-Path $LlamaServer)) { throw "llama-server.exe not found: $LlamaServer" } Write-Host '============================================' Write-Host ' llama.cpp Router Server' Write-Host ' LM Studio-like router mode with ASR support' Write-Host '============================================' Write-Host '' Write-Host " llama-server: $LlamaServer" Write-Host " Models dir: $ModelsDir" Write-Host " Preset file: $Preset" Write-Host " Host: $LLAMA_Host" Write-Host " Port: $Port" Write-Host " TTL: ${Ttl}s" Write-Host " Models max: $ModelsMax" Write-Host '' if ($RegeneratePreset) { Write-Host 'Regenerating models preset / 重新生成模型预设...' & $Python (Join-Path $ScriptDir 'generate_models_preset.py') ` --models-dir $ModelsDir ` --output $Preset ` --select all ` --profile smart ` --alias-style section if ($LASTEXITCODE -ne 0) { throw 'Failed to generate models-preset.ini' } Write-Host '' } $Args = @( '--models-dir', $ModelsDir, '--models-preset', $Preset, '--models-autoload', '--models-max', "$ModelsMax", '--sleep-idle-seconds', "$Ttl", '--cache-idle-slots', '--host', $LLAMA_Host, '--port', "$Port" ) if ($ApiKey) { $Args += @('--api-key', $ApiKey) } if ($ApiKeyFile) { $Args += @('--api-key-file', $ApiKeyFile) } if ($Timeout) { $Args += @('--timeout', "$Timeout") } if ($CacheRam) { $Args += @('--cache-ram', "$CacheRam") } if ($ThreadsHttp) { $Args += @('--threads-http', "$ThreadsHttp") } if ($EnableMetrics) { $Args += '--metrics' } if ($EnableUi) { $Args += '--ui' } if ($NoUi) { $Args += '--no-ui' } if ($SslCertFile) { $Args += @('--ssl-cert-file', $SslCertFile) } if ($SslKeyFile) { $Args += @('--ssl-key-file', $SslKeyFile) } & $LlamaServer @Args exit $LASTEXITCODE