项目介绍
本案例将指导你基于 DeepSeek API 构建一个实用的自动化内容创作工具(AI写作助手)。该工具适用于写作工作者、内容创作者、自媒体运营人员,可以通过少量提示快速生成高质量的内容大纲、创意标题、文章段落,甚至完整的博客或短文。
一、项目目标与应用场景
项目目标:
- 帮助用户快速生成各类创作内容,如:
- 内容标题和副标题生成
- 文章大纲结构设计
- 短文或博客的正文撰写
- 社交媒体文案与广告创意
应用场景:
- 自媒体写作
- 产品推广与营销文案
- 内容快速原型与文案测试
- 教育教学内容自动生成
二、技术实现方案与架构设计
技术栈
- Python 3.x
- DeepSeek API(使用 DeepSeek-Chat 或 DeepSeek-Coder 模型
- FastAPI 或 Flask 后端框架
- HTML/CSS/JavaScript 简易前端界面(也可使用纯命令行界面)
架构设计
用户 → 输入创作需求 → 后端服务(Python)→ DeepSeek API(内容生成)→ 前端展示结果
三、开发环境搭建
创建项目目录:
deepseek-ai-writer/
├── app.py
├── requirements.txt
├── .env
└── templates/
└── index.html
安装依赖:
pip install fastapi uvicorn requests python-dotenv jinja2
创建.env文件保存API密钥:
DEEPSEEK_API_KEY=你的API密钥
四、关键代码实现
后端API调用(app.py):
import os
import requests
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv("DEEPSEEK_API_KEY")
app = FastAPI()
templates = Jinja2Templates(directory="templates")
def generate_content(prompt):
url = "https://api.deepseek.com/chat/completions"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": "你是一个专业、高效的AI内容创作助手。"},
{"role": "user", "content": prompt}
],
"temperature": 0.8,
"max_tokens": 1000,
}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
result = response.json()
return result['choices'][0]['message']['content']
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.post("/generate")
async def generate(request: Request):
data = await request.json()
prompt = data.get("prompt", "")
content = generate_content(prompt)
return {"content": content}
五、前端界面(templates/index.html)
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>DeepSeek AI内容创作助手</title>
<script>
async function generateContent(){
let prompt = document.getElementById("prompt").value;
let response = await fetch('/generate',{
method:'POST',
headers:{'Content-Type':'application/json'},
body:JSON.stringify({prompt:prompt})
});
let result = await response.json();
document.getElementById("output").innerText = result.content;
}
</script>
</head>
<body>
<h2>DeepSeek AI 内容创作助手</h2>
<textarea id="prompt" rows="5" cols="60" placeholder="输入你的创作要求,如:写一篇关于人工智能的短文"></textarea><br>
<button onclick="generateContent()">生成内容</button>
<h3>生成结果:</h3>
<div id="output" style="white-space:pre-wrap;border:1px solid #ccc;padding:10px;"></div>
</body>
</html>
六、运行和测试
启动服务:
uvicorn app:app --reload
访问:http://localhost:8000
,输入创作需求测试生成效果。
七、功能优化与扩展建议
- 增加不同创作类型模板(如社交媒体文案、广告文案等)。
- 实现多轮对话迭代修改,用户可基于初始生成内容继续完善。
- 实现Markdown或富文本编辑器的集成,提升创作体验。
- 加入内容质量评估与优化功能。
八、实战小结与项目亮点
通过本实战案例,你可以学到:
- 如何基于 DeepSeek API 快速搭建一个实用的内容创作工具。
- 理解并掌握 AI 驱动的创作工具实际开发与部署流程。
- 将 AI工具深度融入创作流程,有效提升生产效率与内容质量。
九、课后练习与进阶思考
- 为你的写作助手添加风格控制功能(例如幽默、正式、通俗)。
- 尝试集成一个数据库,存储并管理用户历史生成的内容。
- 开发一个简单的用户登录系统,使用户可管理自己的创作成果。