# 光伏功率预测API部署使用说明 ![](https://img.shields.io/badge/Python-3.10-blue) ![](https://img.shields.io/badge/Pytorch-2.6.0+cu124-blue) ![](https://img.shields.io/badge/FastAPI-0.138.0-red) > 本文档说明 `forecast-algorithm` 中功率预测算法的运行方式,以及通过 `main.py` 启动 FastAPI 服务并调用预测接口的方法。 image-20261026105801289 ## 1. 功能概览 | 类型 | 模型 | 输入长度 | 输出长度 | 时间分辨率 | 预测范围 | |---|---|---:|---:|---:|---:| | 超短期 | / | / | / | / | / | | 短期 | GRU / v24 | 288 | 288 | 15 分钟 | 3 天 | | 中期 | GRU / v24 | 960 | 960 | 15 分钟 | 10 天 | 预测结果写入 PostgreSQL: ```text 超短期:/ 短期:data_short_term_power_forecast_raw 中期:data_medium_term_power_forecast_raw ``` ## 2. 目录结构 ```text forecast-algorithm/ ├── main.py FastAPI 启动入口,提供预测 API 和首页 ├── README.md 项目与 API 使用说明 ├── requirements.txt 项目通用 Python 依赖 ├── config/ │ └── config.yaml 数据库、模型路径和 FastAPI 配置 ├── templates/ │ └── api_home.html API 单页使用说明首页 ├── assets/ │ └── API_home_page.png API 首页预览图 │ ├── medium_short_term/ 短期和中期功率预测 │ ├── README.md 短期/中期预测说明 │ ├── requirements.txt 模型运行依赖 │ ├── base/ │ │ └── base_model.py 模型基类 │ ├── layers/ │ │ └── layers.py 公共网络层 │ ├── model/ 模型结构 │ │ ├── GRU.py │ │ ├── DLinear.py │ │ ├── TSMixer.py │ │ ├── TCN.py │ │ ├── Mamba.py │ │ ├── CNN_LSTM.py │ │ ├── TransformerEn.py │ │ └── lightbgm.py │ ├── scripts/ │ │ ├── ModelLoading.py 模型与归一化器加载 │ │ ├── pipeline.py 训练数据处理管线 │ │ ├── prediction_short.py 短期预测入口 │ │ ├── prediction_medium.py 中期预测入口 │ │ └── run.sh 批量预测脚本 │ ├── utils/ │ │ ├── metrics_utils.py 评估指标 │ │ ├── processed.py 数据预处理 │ │ ├── train_utils.py 训练工具 │ │ └── weather_utils.py 气象数据处理 │ ├── test/ │ │ └── prediction_test.py 预测测试脚本 │ └── saved_model/ 模型权重目录 │ └── <场站>/ │ └── <模型>/ │ └── <序列长度>/ │ └── <特征版本>/ │ ├── best_model.pth │ ├── best_tpe_params.json │ └── scaler.pkl │ └── ultra_short_term/ 超短期光伏功率预测 ├── README.md 超短期预测说明 ├── requirements.txt 超短期预测依赖 ├── run.py 超短期预测入口 ├── run.sh 运行脚本 ├── install.sh 安装脚本 ├── ultra_short_pv.py 超短期预测主逻辑 ├── pv_inputs.py 输入数据构造 ├── pv_io.py 输入输出处理 ├── config.json 超短期默认配置 ├── deployment_config.json 部署配置 ├── input_schema.json 输入数据格式定义 ├── SHA256SUMS.json 模型文件校验信息 ├── configs/ │ └── sites.yaml 场站配置 ├── pv_forecast/ │ ├── __init__.py │ ├── ec_latest.py 最新 EC 气象数据 │ ├── features.py 特征处理 │ └── mspf_pv/ 光伏预测模型目录 ├── models/ │ ├── mspf_pv/ 超短期模型 │ └── mspf_pv_e10a/ 备选模型 ├── examples/ │ └── call_model.py 模型调用示例 ├── docs/ │ ├── 超短期光伏预测部署文档.docx │ └── 输入输出与接入说明.md ├── test_data/ │ ├── JMJD01_input.npz │ ├── JMJD01_reference.npz │ ├── YUWEI_input.npz │ ├── YUWEI_reference.npz │ ├── ZHONGXIN_input.npz │ └── ZHONGXIN_reference.npz ├── tests/ │ └── test_forecast.py ├── test.sh 测试运行脚本 ├── test_deployment.py 部署测试 └── output/ 超短期预测输出与测试日志 ``` ## 3. 支持场站 | 场站代码 | 场站名称 | 装机容量 | 纬度 | 经度 | |---|---|---:|---:|---:| | JMJD01 | 江门光伏场 | 4050 kW | 22.555907 | 113.135232 | | YUWEI | 北流煜炜光伏场 | 3000 kW | 22.615142 | 110.305066 | | ZHONGXIN | 清远忠信光伏场 | 2250 kW | 23.542156 | 112.996233 | | ... | | | | | ## 4. 运行环境 安装依赖: ```bash # python 3.10 # 1.pytorch 2.6.0+cu124 这里根据客户需求,推理时安装CPU版本也可以的。 # 支持centos / windows # centos 必须conda安装 conda install pandas,pillow,greenlet conda create -n oulipower python=3.10 -y conda activate oulipower # 2.安装基础环境 python -m pip install -r requirements.txt # 3.安装CPU版本的Pytorch #pip install torch==2.6.0 torchvision==0.21.0 torchaudio==2.6.0 --index-url https://download.pytorch.org/whl/cpu # 3.安装GPU版本的Pytorch pip install torch==2.6.0 torchvision==0.21.0 torchaudio==2.6.0 --index-url https://download.pytorch.org/whl/cu124 python -m pip install fastapi uvicorn psycopg2-binary python -m pip install -r medium_short_term/requirements.txt ``` ## 5. 数据库配置 ```sh 目前是测试数据库,手动导入数据库环境变量 export PGHOST="112.124.19.19" export PGPORT="5432" export PGDATABASE="electricity" export PGUSER="postgres" export PGPOSSWORD="ow9NV!lkf" ``` ## 6. 启动 FastAPI 服务 修改配置文件**config/config.yaml** ```yaml FastAPI: # 关于fast api的配置 host: "0.0.0.0" port: 8000 model: # 关于超短期,短期,中期模型权重和模型脚本路径配置 # 项目路径('/Users/hm/Desktop/forecast/forecast-algorithm')+算法路径('medium_short_term/scripts/prediction_short.py') short_algorithm_file: '/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/scripts/prediction_short.py' medium_algorithm_file: '/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/scripts/prediction_medium.py' # 项目路径('/Users/hm/Desktop/forecast/forecast-algorithm')+权重路径('medium_short_term/saved_model') saved_model_root: "/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/saved_model" ``` 在**forecast-algorithm**目录执行: ```sh python main.py ``` 默认监听: ```python http://0.0.0.0:8000 ``` 本机访问: ```text http://127.0.0.1:8000 ``` > 访问http://127.0.0.1:8000展示API主页信息 image-20261026105801289 ## 7. 调用短期预测接口 接口: ```text GET /api/v1/power/short ``` 参数: | 参数 | 必填 | 说明 | |---|---|---| | `projs` | 是 | 场站代码,例如 `JMJD01` | | `init_time` | 是 | 上海时间,格式 `YYYY-MM-DD HH:MM:SS` | 浏览器访问事例: > 预报2026-11-25:08:00:00(北京时间),短期功率预报,结果写入数据库 ```text http://127.0.0.1:8000/api/v1/power/short?projs=JMJD01&init_time=2026-11-25%2008:00:00 ``` 短期预测写入数据库: ```text public.data_short_term_power_forecast_raw ``` 写入记录数: ```text 288 条 ``` ## 8. 调用中期预测接口 接口: ```text GET /api/v1/power/medium ``` 参数: | 参数 | 必填 | 说明 | | ----------- | ---- | ------------------------------------ | | `projs` | 是 | 场站代码,例如 `JMJD01` | | `init_time` | 是 | 上海时间,格式 `YYYY-MM-DD HH:MM:SS` | 浏览器访问事例: > 预报2026-11-25:08:00:00(北京时间),中期功率预报,结果写入数据库 ```text http://127.0.0.1:8000/api/v1/power/medium?projs=JMJD01&init_time=2026-11-25%2008:00:00 ``` 中期预测写入数据库: ```text public.data_medium_term_power_forecast_raw ``` 写入记录数: ```text 960 条 ```