71 lines
2.8 KiB
PowerShell
71 lines
2.8 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
本机启动「多比比」后端 (FastAPI / uvicorn)。
|
|
|
|
.DESCRIPTION
|
|
默认离线 mock 模式:不调智谱 LLM、不烧额度、不依赖外网。
|
|
自动用 conda 环境 pricebot 的 Python,并设好必需的环境变量
|
|
(DUOBIBI_DB_PATH / PYTHONUTF8 / DUOBIBI_MOCK_LLM),无需先 conda activate。
|
|
|
|
.EXAMPLE
|
|
.\run-backend.ps1 # 默认:mock 模式, 监听 127.0.0.1:8766
|
|
.\run-backend.ps1 -Reload # 改代码自动热重载 (开发用)
|
|
.\run-backend.ps1 -Lan # 监听 0.0.0.0,供真机/同局域网设备连
|
|
.\run-backend.ps1 -Real # 连真实智谱 LLM (需可用 key,否则 429)
|
|
.\run-backend.ps1 -Port 8888 # 换端口 (App 的 BASE_URL 也要同步改)
|
|
#>
|
|
param(
|
|
[int]$Port = 8766,
|
|
[switch]$Lan, # 监听 0.0.0.0 (真机联调); 默认仅本机 127.0.0.1
|
|
[switch]$Real, # 连真实 LLM; 默认离线 mock
|
|
[switch]$Reload # uvicorn 热重载 (改代码自动重启,开发用)
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
# 脚本所在目录就是 duobibi-server/,用它定位项目与 DB,不写死项目路径
|
|
$ServerDir = $PSScriptRoot
|
|
|
|
# pricebot 环境的 Python。若你的 conda 装在别处,改这一行即可。
|
|
$Python = "C:\Users\muzhiyuan\anaconda3\envs\pricebot\python.exe"
|
|
if (-not (Test-Path $Python)) {
|
|
Write-Error "找不到 pricebot 环境的 Python:`n $Python`n请确认 conda 环境存在 (conda env list),或修改本脚本里的 `$Python 路径。"
|
|
exit 1
|
|
}
|
|
|
|
# --- 必需的环境变量 ---
|
|
# DB 路径:不设的话代码默认 /opt/duobibi-server/data.db,Windows 上建库会失败
|
|
$env:DUOBIBI_DB_PATH = Join-Path $ServerDir "data.db"
|
|
# 让日志里的 ¥ / 中文不触发 Windows GBK 控制台的 UnicodeEncodeError
|
|
$env:PYTHONUTF8 = "1"
|
|
|
|
if ($Real) {
|
|
Remove-Item Env:\DUOBIBI_MOCK_LLM -ErrorAction SilentlyContinue
|
|
Write-Host "[mode ] 真实 LLM (需可用智谱 key,当前硬编码 key 可能 429)" -ForegroundColor Yellow
|
|
} else {
|
|
$env:DUOBIBI_MOCK_LLM = "1"
|
|
Write-Host "[mode ] 离线 mock (默认,不烧额度/不连外网)" -ForegroundColor Green
|
|
}
|
|
|
|
$BindHost = if ($Lan) { "0.0.0.0" } else { "127.0.0.1" }
|
|
|
|
Write-Host "[serve] http://${BindHost}:$Port" -ForegroundColor Cyan
|
|
Write-Host "[db ] $($env:DUOBIBI_DB_PATH)" -ForegroundColor DarkGray
|
|
if ($Lan) {
|
|
Write-Host "[lan ] 真机请连这台电脑的局域网 IP:$Port (需同一 WiFi + 防火墙放行该端口)" -ForegroundColor Yellow
|
|
}
|
|
Write-Host "[check] 另开一个窗口验证: Invoke-RestMethod http://127.0.0.1:$Port/health" -ForegroundColor DarkGray
|
|
Write-Host "[stop ] Ctrl+C 停止" -ForegroundColor DarkGray
|
|
Write-Host ""
|
|
|
|
# --- 启动 uvicorn (用 --app-dir 让 app.main 可解析,避免受当前工作目录影响) ---
|
|
$uvArgs = @(
|
|
"-m", "uvicorn", "app.main:app",
|
|
"--app-dir", $ServerDir,
|
|
"--host", $BindHost,
|
|
"--port", "$Port"
|
|
)
|
|
if ($Reload) { $uvArgs += "--reload" }
|
|
|
|
& $Python @uvArgs
|