# Edge AI Gateway 启动指南 ## 前置条件 ### 1. 安装 Go ```bash # macOS brew install go # Linux wget https://go.dev/dl/go1.23.0.linux-amd64.tar.gz sudo tar -C /usr/local -xzf go1.23.0.linux-amd64.tar.gz export PATH=$PATH:/usr/local/go/bin ``` ### 2. 安装 Ollama(本地推理引擎) ```bash # macOS brew install ollama # Linux curl -fsSL https://ollama.com/install.sh | sh # 拉取模型 ollama pull deepseek-r1:1.5b ``` ### 3. 安装 SQLite ```bash # macOS(自带) # Linux sudo apt install sqlite3 ``` ## 启动方式 ### 方式一:直接运行 ```bash # 1. 创建数据目录 mkdir -p /tmp/edgeai-data # 2. 启动 Ollama(另一个终端) ollama serve # 3. 启动 Edge AI Gateway EDGEAI_DB_PATH=/tmp/edgeai-data go run ./cmd/gateway/ --config configs/config.yaml ``` 服务器将在 `http://0.0.0.0:8080` 启动。 ### 方式二:编译后运行 ```bash # 1. 编译 go build -o bin/gateway ./cmd/gateway/ # 2. 启动 EDGEAI_DB_PATH=/tmp/edgeai-data ./bin/gateway --config configs/config.yaml ``` ### 方式三:Docker Compose ```bash cd deploy docker-compose up -d ``` 服务端口: - Gateway: http://localhost:8080 - Ollama: http://localhost:11434 - Prometheus: http://localhost:9090 - Grafana: http://localhost:3000 ## 配置 API Key 首次启动后需要创建 API Key: ```bash # 1. 计算 key 的 SHA-256 哈希 HASH=$(echo -n "your-api-key" | shasum -a 256 | awk '{print $1}') # 2. 插入数据库 sqlite3 /tmp/edgeai-data/auth.db "INSERT INTO api_keys (app_id, tenant_id, name, key_hash, allowed_models, allowed_priorities, is_admin, enabled) VALUES ('my-app', 'my-tenant', 'my-key', '$HASH', '[]', '[]', 1, 1);" # 3. 重启服务器使 key 生效 ``` ## 环境变量 | 变量 | 默认值 | 说明 | |------|--------|------| | `EDGEAI_DB_PATH` | `/var/lib/edgeai` | 数据库目录 | | `EDGEAI_SERVER_PORT` | `8080` | 服务端口 | | `EDGEAI_LOG_LEVEL` | `info` | 日志级别 | | `EDGEAI_CONFIG_PATH` | `configs/config.yaml` | 配置文件路径 | ## 验证 ```bash # 健康检查 curl http://localhost:8080/health # 查看模型(需 API Key) curl -H "Authorization: Bearer your-api-key" http://localhost:8080/v1/models # 非流式对话 curl -X POST http://localhost:8080/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer your-api-key" \ -d '{"model":"general-chat","messages":[{"role":"user","content":"你好"}]}' # 流式对话 curl -N -X POST http://localhost:8080/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer your-api-key" \ -d '{"model":"fast-chat","messages":[{"role":"user","content":"你好"}],"stream":true}' # Prometheus 指标 curl http://localhost:8080/metrics ``` ## 可选:启动 vLLM ```bash # 安装 vLLM pip install vllm # 启动 vLLM 服务(端口 8000) vllm serve --model deepseek-ai/deepseek-r1-1.5b --port 8000 # 然后在 Gateway 配置中使用 vllm-chat 模型 ``` ## 运行测试 ```bash # 单元测试 go test ./internal/... -count=1 # 集成测试 go test ./test/integration/ -count=1 # 全部测试 go test ./internal/... -count=1 && \ go test ./test/integration/ -count=1 && \ go test ./test/security/ -count=1 && \ go test ./test/compatibility/ -count=1 && \ go test ./test/performance/ -count=1 && \ go test ./test/chaos/ -count=1 ```