# JSON格式输出更新说明 ## 更新时间 2026-01-07 ## 更新内容 将创作灵感的输出格式从Markdown改为JSON格式,方便前端解析和使用。 ## JSON格式定义 ### 完整格式 ```json { "inspirations": [ { "id": 1, "title": "灵感标题", "description": "核心创意描述和执行建议", "reference_author": "参考视频作者", "reference_description": "参考视频描述片段", "url": "参考视频作者主页链接", "platform": "抖音", "tags": ["#标签1", "#标签2", "#标签3"], "keywords": ["关键词1", "关键词2", "关键词3"] } ] } ``` ### 字段说明 | 字段 | 类型 | 说明 | 示例 | |------|------|------|------| | `id` | number | 灵感编号 | 1 | | `title` | string | 灵感标题(15字以内) | "5分钟晨起唤醒操" | | `description` | string | 核心创意和执行建议的综合描述(100字以内) | "设计一套简单的晨起拉伸动作..." | | `reference_author` | string | 参考视频的作者名 | "健身教练小李" | | `reference_description` | string | 参考视频的描述片段(30字以内) | "每天5分钟,唤醒身体活力!..." | | `url` | string | 参考视频作者的主页链接 | "https://www.iesdouyin.com/share/user/xxxxx" | | `platform` | string | 平台名称,固定为"抖音" | "抖音" | | `tags` | array | 推荐使用的标签数组 | ["#健身", "#晨起运动"] | | `keywords` | array | 热门关键词数组 | ["晨起", "拉伸", "唤醒"] | ## 数据来源 ### 字段映射关系 JSON字段与视频数据的映射关系: | JSON字段 | 视频数据字段 | 说明 | |----------|-------------|------| | `reference_author` | `author` | 直接使用 | | `reference_description` | `description` | 截取前30字 | | `url` | `authorLink` | 直接使用 | | `platform` | 固定值 | "抖音" | | `tags` | `hashTags` | 直接使用 | | `keywords` | `hotWords` | 直接使用 | ### 示例数据 **视频数据**: ```json { "author": "清华大学", "description": "我会等! 等枯树生出芽,等你来清华!一起看外面世界到底多大~#清华大学 #我会等", "authorLink": "https://www.iesdouyin.com/share/user/100746744340?sec_uid=MS4wLjABAAAADPpMCOWGL4ujoEiYrjYKjDQsbWr8QBeW321UxLW-T5Q", "hashTags": ["#清华大学", "#我会等", "#校园", "#上岸"], "hotWords": ["世界", "我会", "等你", "上岸"] } ``` **生成的灵感**: ```json { "id": 1, "title": "名校励志短片", "description": "以名校为背景,拍摄励志短片,展现学生的奋斗历程和对未来的期待。使用航拍和特写镜头,配合励志音乐,传递正能量。", "reference_author": "清华大学", "reference_description": "我会等! 等枯树生出芽,等你来清华!...", "url": "https://www.iesdouyin.com/share/user/100746744340?sec_uid=MS4wLjABAAAADPpMCOWGL4ujoEiYrjYKjDQsbWr8QBeW321UxLW-T5Q", "platform": "抖音", "tags": ["#清华大学", "#我会等", "#校园", "#上岸"], "keywords": ["世界", "我会", "等你", "上岸"] } ``` ## 使用方法 ### 1. 通过API调用 ```bash curl -X POST "http://localhost:8001/api/agent" \ -H "Content-Type: application/json" \ -d '{ "query": "我想做一些校园相关的短视频", "max_iterations": 15 }' ``` ### 2. 解析返回结果 ```python import json import re # 从API返回的final_answer中提取JSON json_match = re.search(r'```json\s*(.*?)\s*```', final_answer, re.DOTALL) if json_match: data = json.loads(json_match.group(1)) inspirations = data['inspirations'] for inspiration in inspirations: print(f"ID: {inspiration['id']}") print(f"标题: {inspiration['title']}") print(f"描述: {inspiration['description']}") print(f"作者: {inspiration['reference_author']}") print(f"链接: {inspiration['url']}") print(f"标签: {', '.join(inspiration['tags'])}") print(f"关键词: {', '.join(inspiration['keywords'])}") print("-" * 80) ``` ### 3. 前端使用示例 ```javascript // 解析API返回的数据 const response = await fetch('/api/agent', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: '我想做一些校园相关的短视频', max_iterations: 15 }) }); const result = await response.json(); // 提取JSON格式的灵感数据 const jsonMatch = result.final_answer.match(/```json\s*([\s\S]*?)\s*```/); if (jsonMatch) { const data = JSON.parse(jsonMatch[1]); const inspirations = data.inspirations; // 渲染到页面 inspirations.forEach(item => { console.log(`${item.id}. ${item.title}`); console.log(`描述: ${item.description}`); console.log(`参考: @${item.reference_author}`); console.log(`链接: ${item.url}`); console.log(`标签: ${item.tags.join(', ')}`); }); } ``` ## 优势 ### 1. 易于解析 - 标准JSON格式,所有编程语言都支持 - 不需要复杂的文本解析 - 减少解析错误 ### 2. 结构化数据 - 字段明确,类型固定 - 便于数据库存储 - 便于前端渲染 ### 3. 易于扩展 - 可以轻松添加新字段 - 保持向后兼容 - 便于版本管理 ### 4. 便于集成 - 可以直接传递给前端 - 可以存储到数据库 - 可以导出为其他格式 ## 测试方法 ### 方法1:使用测试脚本 ```bash python test_json_format.py ``` 这个脚本会: 1. 调用Agent生成灵感 2. 验证JSON格式 3. 检查必需字段 4. 显示示例输出 ### 方法2:使用API测试 ```bash # 启动服务 python api.py # 调用接口 curl -X POST "http://localhost:8001/api/agent" \ -H "Content-Type: application/json" \ -d '{"query": "我想做一些美食相关的短视频", "max_iterations": 15}' ``` ## 注意事项 1. **JSON提取**:返回的 `final_answer` 是文本格式,需要从中提取JSON部分 2. **URL字段**:目前使用的是作者主页链接(authorLink),不是视频链接 3. **平台字段**:目前固定为"抖音" 4. **数组字段**:tags和keywords都是数组类型,可能为空数组 5. **字符限制**: - title: 15字以内 - description: 100字以内 - reference_description: 30字以内 ## 修改的文件 1. ✅ `prompts/agent_prompt.md` - 更新输出格式要求 2. ✅ `ai_agent.py` - 修改 `generate_creative_inspirations()` 函数 3. ✅ `test_json_format.py` - 新建JSON格式测试脚本 4. ✅ `JSON_FORMAT_UPDATE.md` - 本文件 ## 完整示例 ### 输入 ```json { "query": "我想做一些校园相关的短视频", "max_iterations": 15 } ``` ### 输出 ```json { "inspirations": [ { "id": 1, "title": "名校励志短片", "description": "以名校为背景,拍摄励志短片,展现学生的奋斗历程和对未来的期待。使用航拍和特写镜头,配合励志音乐,传递正能量。", "reference_author": "清华大学", "reference_description": "我会等! 等枯树生出芽,等你来清华!...", "url": "https://www.iesdouyin.com/share/user/100746744340", "platform": "抖音", "tags": ["#清华大学", "#我会等", "#校园", "#上岸"], "keywords": ["世界", "我会", "等你", "上岸"] }, { "id": 2, "title": "校园情感故事", "description": "记录校园中的温暖瞬间,展现同学之间的友谊和互助。通过真实的场景和自然的表演,传递校园正能量。", "reference_author": "Lily Peng", "reference_description": "男生的哪一个瞬间 让你觉得他特别有教养...", "url": "https://www.iesdouyin.com/share/user/3355314087006819", "platform": "抖音", "tags": ["#校园", "#温暖瞬间"], "keywords": ["特别", "一个", "瞬间", "男生"] } ] } ``` 所有修改已完成,可以开始使用JSON格式的输出了!