feat(ai): 添加 AI 助手功能支持多模型对话

- 集成 OpenAI 兼容网关和 Ollama 原生 API 接口
- 新增 AI 测试页面支持流式对话和模型切换
- 配置开发环境同源反向代理解决浏览器 CORS 问题
- 添加环境变量配置支持 AI API 和 Ollama 接口设置
- 实现聊天历史记录、中断请求和参数调节功能
- 提供 Nginx 反向代理配置文档用于生产环境部署
This commit is contained in:
2026-02-27 22:15:41 +08:00
parent acec6570e1
commit b40326c3a9
9 changed files with 832 additions and 3 deletions

76
docs/AI_PROXY_NGINX.md Normal file
View File

@@ -0,0 +1,76 @@
# AI /ai-proxy Nginx 反代示例
前端页面 `src/views/ai/index.vue` 默认在开发环境使用 `AI_API_URL=/ai-proxy`,通过同源反代解决浏览器 CORS。
## 1) Vite 开发环境
项目已在 `vite.config.ts` 配置(默认目标可通过 `AI_PROXY_TARGET` 调整):
- `/ai-proxy/*` -> `https://ai-api.websoft.top/api/v1/*`
配合 `.env.development`
```bash
VITE_AI_API_URL=/ai-proxy
```
## 2) 生产环境Nginx 反代)
如果你的生产站点是 Nginx 托管静态文件,建议也加一条同源反代:
```nginx
location /ai-proxy/ {
proxy_pass https://ai-api.websoft.top/api/v1/;
proxy_http_version 1.1;
proxy_set_header Host ai-api.websoft.top;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# SSE/流式输出建议关闭缓存与缓冲
proxy_buffering off;
proxy_cache off;
# 如果你的 AI 网关开启了鉴权401 Not authenticated可以在反代层固定注入
# proxy_set_header Authorization "Bearer YOUR_AI_API_KEY";
}
```
然后把生产环境的 `VITE_AI_API_URL` 配置为:
```bash
VITE_AI_API_URL=/ai-proxy
```
## 2.1) Ollama 原生接口Nginx 反代)
如果你要直接用原生 Ollama`http://<host>:11434`),生产环境同样建议走同源反代(避免 CORS + https 混合内容):
```nginx
location /ollama-proxy/ {
proxy_pass http://47.119.165.234:11434/;
proxy_http_version 1.1;
proxy_set_header Host 47.119.165.234;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
proxy_cache off;
}
```
然后把 `VITE_OLLAMA_API_URL` 配置为:
```bash
VITE_OLLAMA_API_URL=/ollama-proxy
```
## 3) 关于 API Key
不建议把 Key 放在浏览器里。
推荐做法:
- Key 放在你自己的后端(或 Nginx里统一注入 / 鉴权;
- 前端只请求同源 `/ai-proxy/*`