start-llama-router.ps1 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. [Console]::InputEncoding = [System.Text.UTF8Encoding]::new($false)
  2. [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)
  3. $OutputEncoding = [Console]::OutputEncoding
  4. $env:PYTHONIOENCODING = 'utf-8'
  5. $env:PYTHONUTF8 = '1'
  6. $ErrorActionPreference = 'Stop'
  7. Set-StrictMode -Version Latest
  8. $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  9. $ServerExe = 'llama-server.exe'
  10. # === 基础路径 / Base paths ===
  11. $LlamaBin = 'D:\llama'
  12. $ModelsDir = 'E:\LMStudio\.lmstudio\models'
  13. $Preset = Join-Path $ScriptDir 'models-preset.ini'
  14. $Python = 'python'
  15. # === 网络 / Network ===
  16. $LLAMA_Host = '127.0.0.1'
  17. $Port = 18003
  18. $Timeout = ''
  19. $ThreadsHttp = ''
  20. # === Router 行为 / Router behavior ===
  21. $Ttl = 300
  22. $ModelsMax = 1
  23. $RegeneratePreset = $true
  24. $ApiKey = ''
  25. $ApiKeyFile = ''
  26. $CacheRam = ''
  27. $EnableMetrics = $false
  28. $EnableUi = $false
  29. $NoUi = $false
  30. $SslCertFile = ''
  31. $SslKeyFile = ''
  32. $LlamaServer = Join-Path $LlamaBin $ServerExe
  33. if (-not (Test-Path $LlamaServer)) {
  34. throw "llama-server.exe not found: $LlamaServer"
  35. }
  36. Write-Host '============================================'
  37. Write-Host ' llama.cpp Router Server'
  38. Write-Host ' LM Studio-like router mode with ASR support'
  39. Write-Host '============================================'
  40. Write-Host ''
  41. Write-Host " llama-server: $LlamaServer"
  42. Write-Host " Models dir: $ModelsDir"
  43. Write-Host " Preset file: $Preset"
  44. Write-Host " Host: $LLAMA_Host"
  45. Write-Host " Port: $Port"
  46. Write-Host " TTL: ${Ttl}s"
  47. Write-Host " Models max: $ModelsMax"
  48. Write-Host ''
  49. if ($RegeneratePreset) {
  50. Write-Host 'Regenerating models preset / 重新生成模型预设...'
  51. & $Python (Join-Path $ScriptDir 'generate_models_preset.py') `
  52. --models-dir $ModelsDir `
  53. --output $Preset `
  54. --select all `
  55. --profile smart `
  56. --alias-style section
  57. if ($LASTEXITCODE -ne 0) {
  58. throw 'Failed to generate models-preset.ini'
  59. }
  60. Write-Host ''
  61. }
  62. $Args = @(
  63. '--models-dir', $ModelsDir,
  64. '--models-preset', $Preset,
  65. '--models-autoload',
  66. '--models-max', "$ModelsMax",
  67. '--sleep-idle-seconds', "$Ttl",
  68. '--cache-idle-slots',
  69. '--host', $LLAMA_Host,
  70. '--port', "$Port"
  71. )
  72. if ($ApiKey) {
  73. $Args += @('--api-key', $ApiKey)
  74. }
  75. if ($ApiKeyFile) {
  76. $Args += @('--api-key-file', $ApiKeyFile)
  77. }
  78. if ($Timeout) {
  79. $Args += @('--timeout', "$Timeout")
  80. }
  81. if ($CacheRam) {
  82. $Args += @('--cache-ram', "$CacheRam")
  83. }
  84. if ($ThreadsHttp) {
  85. $Args += @('--threads-http', "$ThreadsHttp")
  86. }
  87. if ($EnableMetrics) {
  88. $Args += '--metrics'
  89. }
  90. if ($EnableUi) {
  91. $Args += '--ui'
  92. }
  93. if ($NoUi) {
  94. $Args += '--no-ui'
  95. }
  96. if ($SslCertFile) {
  97. $Args += @('--ssl-cert-file', $SslCertFile)
  98. }
  99. if ($SslKeyFile) {
  100. $Args += @('--ssl-key-file', $SslKeyFile)
  101. }
  102. & $LlamaServer @Args
  103. exit $LASTEXITCODE