
一个可落地的 AI 创作系统通常包含四层:输入理解层负责解析需求,提示生成层负责构造高质量指令,模型调用层负责统一对接文本、图像、音频等模型,编排与交付层负责批量生成、质量检查和结果输出。缺少任何一层,创作都会退化成“碰运气出图”或“手工复制粘贴”。
提示词不应散落在代码里,而应抽成模板。这样既能复用,也方便做 A/B 测试。
from dataclasses import dataclass
@dataclass
class PromptTemplate:
template: str
def render(self, **kwargs) -> str:
return self.template.format(**kwargs)
text_template = PromptTemplate(
"请以{style}风格,为{topic}写一段小红书文案,"
"包含核心卖点、使用场景和行动号召,不超过200字。"
)模板化的好处是:改风格只改参数,不改逻辑;批量生成时,只需替换 topic 和 style。
不同模型的调用方式不同。用统一接口封装,可以让上层业务不关心底层是哪个模型。
import asyncio
import hashlib
class AIClient:
async def generate_text(self, prompt: str) -> str:
await asyncio.sleep(0.1) # 模拟网络请求
return f"[文案] {prompt[:24]}..."
async def generate_image(self, prompt: str) -> str:
await asyncio.sleep(0.2)
digest = hashlib.md5(prompt.encode()).hexdigest()[:8]
return f"https://img.example/{digest}.png"统一接口之后,后续要接入新模型,只需实现同样的方法,不影响业务流程。
AI 创作往往需要批量产出。串行调用太慢,异步并发能显著提升效率。
class ContentPipeline:
def __init__(self, client: AIClient, template: PromptTemplate):
self.client = client
self.template = template
async def create_post(self, topic: str, style: str):
text_prompt = self.template.render(topic=topic, style=style)
text = await self.client.generate_text(text_prompt)
image_prompt = f"{style}风格,主题:{topic},高质量,适合社交媒体"
image_url = await self.client.generate_image(image_prompt)
return {"topic": topic, "text": text, "image": image_url}
async def batch_create(topics: list[str], style: str):
client = AIClient()
pipeline = ContentPipeline(client, text_template)
tasks = [pipeline.create_post(t, style) for t in topics]
return await asyncio.gather(*tasks)
if __name__ == "__main__":
results = asyncio.run(
batch_create(["夏季防晒", "便携咖啡机", "国风手账"], "清新种草")
)
for r in results:
print(r)这段代码展示了从模板渲染、文本生成、图像生成到批量并发的完整链路。实际项目中,只需把模拟调用替换成真实 API,并加入重试和限流。
生成结果不能直接交付,必须经过评估。可以从长度、关键词、敏感词、品牌规范等维度打分。
def evaluate(text: str, min_len: int = 30, banned: list[str] | None = None) -> int:
score = 0
if len(text) >= min_len:
score += 40
if any(k in text for k in ["限时", "优惠", "点击", "推荐"]):
score += 30
if banned and any(b in text for b in banned):
score -= 50
return max(0, min(100, score + 30))低于阈值的作品自动打回重生成,高于阈值的进入人工审核或直接发布。这样能保证批量输出的下限。
第一,缓存。相同提示和参数的结果应缓存,避免重复计费。第二,重试。网络抖动和模型限流很常见,要有指数退避重试。第三,日志。记录每次调用的提示、参数、耗时和结果,便于复盘。第四,成本控制。简单任务用轻量模型,复杂任务再上大模型。第五,安全合规。过滤敏感内容,检查字体、图片和肖像版权。
AI 创作不是替代人,而是放大人的创意和效率。和橘子学 AI 的系统课将这套方法拆成 600 集精讲和 150 个实战项目,覆盖图像、视频、文案、音频、数字人和自动化工作流。掌握模板化、统一接口、异步批量、质量评估和工程化治理,才能把工具变成系统,把系统变成稳定的生产力。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。