Skip to content

Commit 8917ad5

Browse files
Merge pull request bytedance#23 from codingmokey2025/feat/optimze-video-gen-sample
optimize video-gen sample
2 parents d3ebae5 + f8cfeb8 commit 8917ad5

File tree

3 files changed

+117
-29
lines changed

3 files changed

+117
-29
lines changed

02-use-cases/video_gen/README.md

Lines changed: 115 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,25 @@
1010
- 上传成片到火山引擎 TOS,并返回可访问的签名 URL
1111

1212
核心组件:
13+
1314
- Agent 服务:`agent.py`,基于 `AgentkitSimpleApp`
1415
- 工具集:图片生成、视频生成、文件下载、视频拼接(MCP)、TOS 上传
1516
- 短期记忆:用于维持对话会话上下文
1617

1718
## 目录结构
1819

1920
```
20-
video-gen/
21+
video_gen/
2122
├── agent.py # Agent 应用入口,注册 MCP 工具并运行
2223
├── agent.yaml # Agent 配置:模型、系统指令与工具列表
2324
├── tool/ # 自定义工具
2425
│ ├── file_download.py # 批量下载文件到本地
2526
│ └── tos_upload.py # 上传文件到 TOS 并生成签名 URL
2627
├── requirements.txt # Python 依赖列表
28+
├── pyproject.toml # 项目配置(uv/pip 依赖与元数据)
29+
├── __init__.py # 包初始化文件
30+
├── .python-version # Python 版本声明(开发环境)
31+
├── README.md # 项目说明文档
2732
└── .dockerignore # Docker 构建忽略文件
2833
```
2934

@@ -32,8 +37,16 @@ video-gen/
3237
### 1. 安装依赖
3338

3439
```bash
35-
cd 02-use-cases/video-gen
36-
uv pip install -r requirements.txt
40+
cd 02-use-cases/video_gen
41+
# 若未安装 uv,请先安装(任选其一)
42+
# macOS / Linux(官方安装脚本)
43+
curl -LsSf https://astral.sh/uv/install.sh | sh
44+
# 或使用 Homebrew(macOS)
45+
brew install uv
46+
47+
# 初始化项目依赖
48+
uv sync
49+
source .venv/bin/activate
3750
```
3851

3952
额外要求:
@@ -49,52 +62,127 @@ uv pip install -r requirements.txt
4962
本地必需的环境变量(用于 TOS 上传):
5063

5164
```bash
52-
export VOLCENGINE_ACCESS_KEY=AK
53-
export VOLCENGINE_SECRET_KEY=SK
65+
# 务必导出 VOLCENGINE_ACCESS_KEY、VOLCENGINE_SECRET_KEY、DATABASE_TOS_BUCKET 环境变量
66+
export VOLCENGINE_ACCESS_KEY=<Your AK>
67+
export VOLCENGINE_SECRET_KEY=<Your SK>
68+
export DATABASE_TOS_BUCKET=<Your Bucket Name>
5469
# 可选:指定下载目录(不设置则默认使用项目根目录)
55-
export DOWNLOAD_DIR=/path/to/downloads
70+
export DOWNLOAD_DIR=/tmp # 配置视频下载目录
5671
```
5772

5873
TOS 存储桶说明:
74+
5975
- 默认使用 `tool/tos_upload.py` 中的 `bucket_name="agentkit-platform-{{your account_id}}"`
6076
- 如需自定义,可在调用工具时传入 `bucket_name`,或直接修改 `tool/tos_upload.py` 的默认参数为你的 Bucket 名称
6177

6278
### 4. 启动与部署
6379

64-
以本地方式运行(调试):
80+
#### 配置环境变量
6581

66-
```bash
67-
python agent.py
68-
# 服务默认监听 0.0.0.0:8000
82+
```
83+
export VOLCENGINE_ACCESS_KEY=<Your AK>
84+
export VOLCENGINE_SECRET_KEY=<Your SK>
85+
export DATABASE_TOS_BUCKET=<Your Bucket Name>
6986
```
7087

71-
部署到火山引擎 AgentKit(runtime):
88+
#### 以本地方式运行(调试):
7289

7390
```bash
74-
agentkit config \
75-
--agent_name video_gen \
76-
--entry_point 'agent.py' \
77-
--runtime_envs DATABASE_TOS_BUCKET=agentkit-platform-{{your account_id}} \ # example: agentkit-platform-12345678901234567890
78-
--launch_type cloud && \
79-
#--iam-role <your iam role>\
80-
agentkit launch
91+
uv run agent.py
92+
# 服务默认监听 0.0.0.0:8000
8193
```
8294

83-
## 使用与测试
95+
##### 本地调试接口调用示例
96+
97+
1. **获取应用名称**
98+
99+
通过 `list-apps` 接口获取当前运行的 Agent 名称,该名称与 `agent.yaml` 中的 `name` 保持一致,即 `storybook_illustrator`
100+
101+
```bash
102+
curl --location 'http://localhost:8000/list-apps'
103+
```
104+
2. **创建 Session**
84105

85-
通过 AgentKit 调用示例:
86-
- Prompt:后移射日,嫦娥奔月,吴刚伐木真人版
87-
- Prompt: 愚公移山与精卫填海绘本故事
88-
- Prompt: 射雕英雄传的真人版视频故事
89-
- Prompt: 凡人修仙传韩立结婴
90-
- Prompt: 凡人修仙传虚天殿大战,3D动漫风格
106+
使用获取到的应用名称(`storybook_illustrator`)创建会话。
91107

108+
```bash
109+
curl --location --request POST 'http://localhost:8000/apps/storybook_illustrator/users/u_123/sessions/s_123' \
110+
--header 'Content-Type: application/json' \
111+
--data ''
112+
```
113+
3. **发送消息**
114+
向 Agent 发起请求。
115+
116+
```bash
117+
curl --location 'http://localhost:8000/run_sse' \
118+
--header 'Content-Type: application/json' \
119+
--data '{
120+
"appName": "storybook_illustrator",
121+
"userId": "u_123",
122+
"sessionId": "s_123",
123+
"newMessage": {
124+
"role": "user",
125+
"parts": [{
126+
"text": "请根据寓言《狐假虎威》生成绘本故事视频"
127+
}]
128+
},
129+
"streaming": true
130+
}'
131+
```
132+
133+
##### 使用veadk web命令进行便捷调试
134+
135+
1. **启动veadk web服务**
136+
137+
```bash
138+
cd 02-use-cases/ # 要在use-cases根目录下执行
139+
veadk web --port 8000
140+
```
141+
2. **通过veadk web调用Agent**
142+
打开浏览器,访问 `http://localhost:8000`,输入 Prompt 后点击“Send”即可调用 Agent。
143+
144+
#### 部署到火山引擎 AgentKit(runtime):
145+
146+
1. 使用 `agentkit cli`命令部署到火山引擎 AgentKit(runtime):
92147

93148
```bash
94-
agentkit invoke '{"prompt": "请根据寓言《狐假虎威》生成绘本故事视频"}'
149+
agentkit config \
150+
--agent_name storybook_illustrator \
151+
--entry_point 'agent.py' \
152+
--runtime_envs DATABASE_TOS_BUCKET=<Your Bucket Name> \
153+
--launch_type cloud && agentkit launch
95154
```
96155

156+
2. 部署成功之后进入火山引擎 [AgentKit 控制台](https://console.volcengine.com/agentkit/region:agentkit+cn-beijing/runtime?pageSize=10&currentPage=1),点击 Runtime 查看部署的智能体 `storybook_illustrator`详情,获取公网访问域名(如`https://xxxxx.apigateway-cn-beijing.volceapi.com`)和Api Key,然后通过一下API进行测试
157+
158+
**创建 Session**
159+
```bash
160+
curl --location --request POST 'https://xxxxx.apigateway-cn-beijing.volceapi.com/apps/storybook_illustrator/users/u_123/sessions/s_124' \
161+
--header 'Content-Type: application/json' \
162+
--header 'Authorization: <your api key>' \
163+
--data ''
164+
```
165+
**发送消息**
166+
```bash
167+
curl --location 'https://xxxxx.apigateway-cn-beijing.volceapi.com/run_sse' \
168+
--header 'Authorization: <your api key>' \
169+
--header 'Content-Type: application/json' \
170+
--data '{
171+
"appName": "storybook_illustrator",
172+
"userId": "u_123",
173+
"sessionId": "s_124",
174+
"newMessage": {
175+
"role": "user",
176+
"parts": [{
177+
"text": "请根据寓言《狐假虎威》生成绘本故事视频"
178+
}]
179+
},
180+
"streaming": false
181+
}'
182+
```
183+
97184
期望行为:
185+
98186
- 自动生成 4 张分镜插画,并基于相邻分镜生成 3 段过渡视频
99187
- 启动本地 MCP 工具拼接为完整视频
100188
- 通过 TOS 上传生成签名 URL,并将该 URL 作为最终响应返回
@@ -107,4 +195,4 @@ agentkit invoke '{"prompt": "请根据寓言《狐假虎威》生成绘本故事
107195

108196
## 参考
109197

110-
- `agent.yaml` 的工作流程定义了从分镜生成到视频拼接与上传的完整链路
198+
- `agent.yaml` 的工作流程定义了从分镜生成到视频拼接与上传的完整链路
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tos>=2.8.7
22
veadk-python>=0.2.27
3-
google-adk >= 1.18.0
3+
google-adk>=1.18.0,<1.20.0
44
volcengine-python-sdk>=4.0.31
55
agentkit-sdk-python>=0.1.13

02-use-cases/video_gen/tool/tos_upload.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def upload_file_to_tos(
7171
>>> print(url)
7272
https://bucket.tos-cn-beijing.volces.com/video.mp4?X-Tos-Signature=...
7373
"""
74-
bucket_name = os.getenv("DATABASE_TOS_BUCKET", "aaa-bbb-ccc-ddd")
74+
bucket_name = os.getenv("DATABASE_TOS_BUCKET")
7575

7676
# Check if file exists
7777
if not os.path.exists(file_path):

0 commit comments

Comments
 (0)