Skip to content

Commit a971922

Browse files
author
alcholiclg
committed
Merge branch 'main' of https://github.com/modelscope/ms-agent into feat/fin_research
Merge branch 'main' of https://github.com/modelscope/ms-agent into feat/fin_research
2 parents 8e0b575 + e32fc77 commit a971922

30 files changed

Lines changed: 10403 additions & 30 deletions

ms_agent/agent/llm_agent.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ms_agent.llm.llm import LLM
1515
from ms_agent.llm.utils import Message
1616
from ms_agent.memory import Memory, memory_mapping
17-
from ms_agent.memory.mem0ai import Mem0Memory
17+
from ms_agent.memory.mem0ai import Mem0Memory, SharedMemoryManager
1818
from ms_agent.rag.base import RAG
1919
from ms_agent.rag.utils import rag_mapping
2020
from ms_agent.tools import ToolManager
@@ -342,19 +342,19 @@ async def load_memory(self):
342342
service = self.config.llm.service
343343
config_dict = {
344344
'model':
345-
self.config.llm.model,
345+
_memory.summary_model,
346346
'provider':
347347
'openai',
348348
'openai_base_url':
349349
getattr(self.config.llm, f'{service}_base_url', None),
350350
'openai_api_key':
351351
getattr(self.config.llm, f'{service}_api_key', None),
352+
'max_tokens':
353+
_memory.max_tokens,
352354
}
353355
llm_config_obj = OmegaConf.create(config_dict)
354356
setattr(_memory, 'llm', llm_config_obj)
355-
356357
if memory_type == 'mem0':
357-
from ms_agent.memory.mem0ai import SharedMemoryManager
358358
shared_memory = SharedMemoryManager.get_shared_memory(
359359
_memory)
360360
self.memory_tools.append(shared_memory)
@@ -638,7 +638,7 @@ async def run_loop(self, messages: Union[List[Message], str],
638638
async for messages in self.step(messages):
639639
yield messages
640640
self.runtime.round += 1
641-
# save history
641+
# save memory and history
642642
self.save_memory(messages)
643643
self.save_history(messages)
644644

ms_agent/cli/run.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ def define_args(parsers: argparse.ArgumentParser):
7777
type=str,
7878
default=None,
7979
help='API key for accessing ModelScope api-inference services.')
80+
parser.add_argument(
81+
'--animation_mode',
82+
required=False,
83+
type=str,
84+
choices=['auto', 'human'],
85+
default=None,
86+
help='Animation mode for video_generate project: auto (default) or human.')
8087
parser.set_defaults(func=subparser_func)
8188

8289
def execute(self):
@@ -91,6 +98,10 @@ def execute(self):
9198
self.args.trust_remote_code) # noqa
9299
self.args.load_cache = strtobool(self.args.load_cache)
93100

101+
# Propagate animation mode via environment variable for downstream code agents
102+
if getattr(self.args, 'animation_mode', None):
103+
os.environ['MS_ANIMATION_MODE'] = self.args.animation_mode
104+
94105
config = Config.from_task(self.args.config)
95106

96107
if Config.is_workflow(config):

ms_agent/llm/anthropic_llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def _stream_format_output_message(self,
189189
index=len(current_message.tool_calls),
190190
type='function',
191191
tool_name=block.name,
192-
arguments=json5.dumps(block.input),
192+
arguments=block.input,
193193
)
194194
current_message.tool_calls.append(tool_call)
195195
used_tool_call_ids.add(tool_call_id)

ms_agent/memory/mem0ai.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ def _initialize_memory(self):
131131
self.config.llm.openai_base_url),
132132
'api_key':
133133
getattr(self.config, 'embedder_api_key',
134-
self.config.llm.openai_api_key),
135-
'max_tokens':
136-
getattr(self.config, 'max_tokens',
137-
self.config.llm.max_tokens),
134+
self.config.llm.openai_api_key)
138135
}
139136
},
140137
'llm': {
@@ -147,7 +144,10 @@ def _initialize_memory(self):
147144
self.config.llm.openai_base_url),
148145
'api_key':
149146
getattr(self.config, 'llm_api_key',
150-
self.config.llm.openai_api_key)
147+
self.config.llm.openai_api_key),
148+
'max_tokens':
149+
getattr(self.config, 'max_tokens',
150+
self.config.llm.max_tokens),
151151
}
152152
},
153153
}

ms_agent/tools/filesystem_tool.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,21 @@ async def get_tools(self):
6868
Tool(
6969
tool_name='read_file',
7070
server_name='file_system',
71-
description='Read the content of a file',
71+
description='Read the content of file(s)',
7272
parameters={
7373
'type': 'object',
7474
'properties': {
75-
'path': {
76-
'type': 'string',
77-
'description': 'The relative path of the file',
75+
'paths': {
76+
'type':
77+
'array',
78+
'items': {
79+
'type': 'string'
80+
},
81+
'description':
82+
'List of relative file path(s) to read',
7883
}
7984
},
80-
'required': ['path'],
85+
'required': ['paths'],
8186
'additionalProperties': False
8287
}),
8388
Tool(
@@ -154,20 +159,23 @@ async def write_file(self, path: str, content: str):
154159
except Exception as e:
155160
return f'Write file <{path}> failed, error: ' + str(e)
156161

157-
async def read_file(self, path: str):
158-
"""Read the content of a file.
162+
async def read_file(self, paths: list[str]):
163+
"""Read the content of file(s).
159164
160165
Args:
161-
path(`path`): The relative file path to read, a prefix dir will be automatically concatenated.
166+
paths(`list[str]`): List of relative file path(s) to read, a prefix dir will be automatically concatenated.
162167
163168
Returns:
164-
The file content or error message.
169+
Dictionary mapping file path(s) to their content or error messages.
165170
"""
166-
try:
167-
with open(os.path.join(self.output_dir, path), 'r') as f:
168-
return f.read()
169-
except Exception as e:
170-
return f'Read file <{path}> failed, error: ' + str(e)
171+
results = {}
172+
for path in paths:
173+
try:
174+
with open(os.path.join(self.output_dir, path), 'r') as f:
175+
results[path] = f.read()
176+
except Exception as e:
177+
results[path] = f'Read file <{path}> failed, error: ' + str(e)
178+
return str(results)
171179

172180
async def list_files(self, path: str = None):
173181
"""List all files in a directory.

ms_agent/tools/mcp_client.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# Copyright (c) Alibaba, Inc. and its affiliates.
2-
import asyncio
32
import os
43
from contextlib import AsyncExitStack
54
from datetime import timedelta
6-
from os import environb
75
from types import TracebackType
86
from typing import Any, Dict, Literal, Optional
97

@@ -15,7 +13,7 @@
1513
from ms_agent.llm.utils import Tool
1614
from ms_agent.tools.base import ToolBase
1715
from ms_agent.utils import enhance_error, get_logger
18-
from omegaconf import DictConfig, ListConfig
16+
from omegaconf import DictConfig
1917

2018
logger = get_logger()
2119

ms_agent/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.0.1rc0'
1+
__version__ = '2.0.0'

projects/code_scratch/coding.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ llm:
44
openai_api_key:
55
openai_base_url: https://dashscope.aliyuncs.com/compatible-mode/v1
66

7+
78
generation_config:
89
temperature: 0.2
910
top_k: 20

projects/video_generate/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Video Generate
2+
3+
一个“AI 科普短视频”工作流。支持全自动与人工协同两种模式,产生脚本、语音、插画/动画、字幕,并合成为成片。
4+
5+
## 快速检查(必读)
6+
7+
在首次运行前,建议完成以下检查:
8+
9+
1) 运行环境
10+
- Windows / Python 3.10+(推荐)
11+
- 已安装 FFmpeg,并添加到 PATH(ffmpeg -version 可执行)
12+
- Manim 可用(manim -h 可执行)
13+
14+
2) Python 依赖(若未安装)
15+
- 依赖在仓库 requirements 下,或按需安装:moviepy、Pillow、edge-tts、matplotlib 等
16+
17+
3) 资源文件(已随仓库提供)
18+
- 自定义字体与背景音乐:`projects/video_generate/core/asset/`
19+
- `bg_audio.mp3`
20+
- `字小魂扶摇手书(商用需授权).ttf`
21+
22+
4) 可选的 API Key(全自动模式常用)
23+
- MODELSCOPE_API_KEY:用于 ModelScope 模型调用
24+
25+
提示:未设置 Key 也可运行“只合成/人工模式”,但全自动模式可能因缺少 LLM 能力失败。
26+
27+
## 运行方式一:全自动模式(auto)
28+
29+
按主题从零到一自动生成并合成视频:
30+
31+
```powershell
32+
# 可选:设置 API Key
33+
$env:MODELSCOPE_API_KEY="你的ModelScopeKey"
34+
35+
# 运行三步工作流(脚本 → 素材 → 合成)
36+
ms-agent run --config "ms-agent/projects/video_generate/workflow.yaml" --query "主题" --animation_mode auto --trust_remote_code true
37+
```
38+
39+
输出将位于 `ms-agent/projects/video_generate/output/<主题>/`
40+
41+
## 运行方式二:人工模式(human)
42+
43+
适合需要人工把控动画的流程:自动产出“脚本/语音/插画/字幕/占位前景”,然后在“人工工作室”内逐段制作/审批前景动画,最终一键完整合成。
44+
45+
1) 先生成素材(不自动渲染 Manim)
46+
```powershell
47+
ms-agent run --config "ms-agent/projects/video_generate/workflow.yaml" --query "主题" --animation_mode human --trust_remote_code true
48+
```
49+
50+
2) 打开人工工作室(指向上一步生成的主题目录)
51+
```powershell
52+
# 确保将 ms-agent 包目录加入 PYTHONPATH
53+
$env:PYTHONPATH="项目本地目录\ms-agent"
54+
55+
# 以模块方式启动交互式工作室
56+
python -m projects.video_generate.core.human_animation_studio "项目本地目录\ms-agent\projects\video_generate\output\主题"
57+
```
58+
59+
在工作室中:
60+
- 1 查看待制作任务 → 2 开始制作动画 → 生成/改进 Manim 代码 → 创建预览 → 批准动画
61+
- 当所有片段完成后,系统会自动合并前景并执行“完整合成(背景+字幕+音频+前景+音乐)”生成成片
62+
63+
## 运行方式三:只合成(已有素材)
64+
65+
如果目录中已经有 `asset_info.json`(或你只想重新合成):
66+
67+
```powershell
68+
ms-agent run --config "ms-agent/projects/video_generate/workflow_from_assets.yaml" `
69+
--query "项目本地目录\ms-agent\projects\video_generate\output\<主题>\asset_info.json" `
70+
--animation_mode human `
71+
--trust_remote_code true
72+
```
73+
74+
该流程只执行合成,不会重新生成脚本/插画/动画。若存在已审批的透明前景(finals/scene_*_final.mov),将优先使用。
75+
76+
## 目录说明
77+
- `video_agent.py`:三步逻辑的 Agent 封装
78+
- `workflow.yaml`:三步编排;`workflow_from_assets.yaml`:只合成编排
79+
- `core/workflow.py`:主流程;`core/human_animation_studio.py`:人工工作室
80+
- `core/asset/`:字体与背景音乐
81+
- `output/`:运行产物
82+
- `scripts/compose_from_asset_info.py`:从现有 `asset_info.json` 直接合成的辅助脚本
83+
84+
## 常见问题
85+
- 退出码 1:
86+
- 检查是否缺少 MODELSCOPE_API_KEY(全自动模式常见)
87+
- 检查 ffmpeg / manim 是否可执行(PATH)
88+
- 查看终端最后 80 行日志定位具体异常
89+
- 字体/背景不一致:
90+
- 背景由 `create_manual_background` 生成,字体/音乐来自 `core/asset/`;确保该目录可读
91+
- TTS/事件循环冲突:
92+
- 已内置 loop-safe 处理;若仍报错,重试并贴出日志尾部
93+
94+
## 许可证与注意
95+
- 自定义字体文件标注为“商用需授权”,请在合规授权范围内使用
96+
- 背景音乐仅作示例,商业使用请更换或确保版权无虞

projects/video_generate/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)