本项目为基于https://github.com/pipipi-pikachu/PPTist/issues/354#issuecomment-2863517189回答制作的ai生成后端,支持使用自定义url和模型。
基于 LangChain 和 FastAPI 的 AI 驱动 PPT 生成后端服务。
用于PPTist的ai后端生成ppt使用
对应pptist的分支57e21c3b4c28ce4195fbb20815f432d596c0e5c8
请使用对应版本的的pptist使用该后端
确保您的系统已安装 Python 3.13 或更高版本,并安装 uv。
# 使用 pip 安装
pip install uv
# 或使用 curl (Linux/macOS)
curl -LsSf https://astral.sh/uv/install.sh | sh
# 或使用 PowerShell (Windows)
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
使用 uv 安装项目依赖:
uv sync
复制环境变量模板:
cp .env.example .env
编辑 .env 文件,设置您的 API 配置:
# OpenAI API 配置
OPENAI_API_KEY=your-openai-api-key-here
OPENAI_BASE_URL=https://api.openai.com/v1
# AI 模型配置
DEFAULT_MODEL=gpt-4o-mini
DEFAULT_TEMPERATURE=0.7
# 服务器配置
HOST=0.0.0.0
PORT=8000
DEBUG=false
使用 uv 启动服务:
uv run main.py
或者使用 uvicorn:
uv run uvicorn main:app --host 0.0.0.0 --port 8000
服务将在 http://localhost:8000 启动。
打开浏览器访问 http://localhost:8000/docs 查看自动生成的 API 文档。
# 拉取源代码
git clone https://github.com/pipipi-pikachu/PPTist.git
# 切换分支
git check 57e21c3b4c28ce4195fbb20815f432d596c0e5c8
修改服务器地址:
PPTist\src\services\index.ts中修改SERVER_URL变量为本服务的地址src\views\Editor\AIPPTDialog.vue中,59行修改Select标签中的可选模型选项,145行的const model = ref('GLM-4-Flash')改为默认的模型为了在离线环境下使用ppt生成,以及放置自己制作的模板。
项目提供了将模板内的图片转换为base64的文件(原文件的模板中使用的是url的地址)
制作模板过程参考:https://github.com/pipipi-pikachu/PPTist/blob/master/doc/AIPPT.md
制作的模板json文件放在项目下的template文件夹中
ppist源码需要修改:
PPTist\src\services\index.ts中修改ASSET_URL变量为本服务的地址src\store\slides.ts中,55行的templates列表中增加自己的模板,并注意修改选择模板时的图片地址(可以在这里转换https://tool.chinaz.com/tools/imgtobase)GET /health
POST /tools/aippt_outline Content-Type: application/json { "model": "gpt-4o-mini", "language": "中文", "require": "人工智能在教育领域的应用", "stream": true }
POST /tools/aippt Content-Type: application/json { "model": "gpt-4o-mini", "language": "中文", "content": "# PPT标题\n## 章节1\n### 小节1\n- 内容1", "stream": true }
import requests
import json
# 生成大纲
def generate_outline():
response = requests.post(
"http://localhost:8000/tools/aippt_outline",
json={
"model": "gpt-4o-mini",
"language": "中文",
"require": "机器学习基础知识",
"stream": True
},
stream=True
)
for chunk in response.iter_content(decode_unicode=True):
if chunk:
print(chunk, end='')
# 生成PPT内容
def generate_content(outline):
response = requests.post(
"http://localhost:8000/tools/aippt",
json={
"model": "gpt-4o-mini",
"language": "中文",
"content": outline,
"stream": True
},
stream=True
)
for chunk in response.iter_content(decode_unicode=True):
if chunk.strip():
page_data = json.loads(chunk.strip())
print(json.dumps(page_data, ensure_ascii=False, indent=2))
运行提供的测试脚本:
uv run test_api.py
生成的 PPT 内容支持以下页面类型:
# PPT标题
## 章的名字
### 节的名字
- 内容1
- 内容2
- 内容3
{"type": "cover", "data": {"title": "标题", "text": "副标题"}}
{"type": "contents", "data": {"items": ["章节1", "章节2"]}}
{"type": "content", "data": {"title": "标题", "items": [{"title": "小标题", "text": "内容"}]}}
gpt-4o: OpenAI GPT-4 Omni 模型gpt-4o-mini: OpenAI GPT-4 Omni Mini 模型(默认)| 变量名 | 说明 | 默认值 |
|---|---|---|
OPENAI_API_KEY | OpenAI API 密钥 | 必填 |
OPENAI_BASE_URL | API 基础URL | https://api.openai.com/v1 |
DEFAULT_MODEL | 默认使用的 AI 模型 | gpt-4o-mini |
DEFAULT_TEMPERATURE | 模型创造性参数 | 0.7 |
HOST | 服务器监听地址 | 0.0.0.0 |
PORT | 服务器端口 | 8000 |
DEBUG | 调试模式开关 | false |
API 会返回相应的 HTTP 状态码和错误信息:
200: 请求成功400: 请求参数错误500: 服务器内部错误流式响应中的错误会以文本形式返回。
pptist-aibackend/ ├── main.py # 主应用文件 ├── test_api.py # API 测试脚本 ├── pyproject.toml # 项目配置和依赖 ├── .python-version # Python 版本锁定 ├── .env.example # 环境变量模板 └── README.md # 说明文档
您可以通过修改 main.py 中的模板和链来自定义 AI 行为:
outline_template 来调整大纲生成格式ppt_content_template 来调整内容生成格式temperature 参数来控制输出的创造性本项目采用 MIT 许可证。
欢迎提交 Issue 和 Pull Request 来改进这个项目。
如果您在使用过程中遇到问题,请:
https://github.com/pipipi-pikachu/PPTist/issues/354#issuecomment-2863517189