Skip to content
5 changes: 4 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


repos:
- repo: https://github.com/pycqa/flake8.git
rev: 4.0.0
Expand All @@ -7,7 +9,8 @@ repos:
(?x)^(
thirdparty/|
examples/|
tests/run.py
tests/run.py|
ms_agent/utils/prompts.py
)$
- repo: https://github.com/PyCQA/isort.git
rev: 4.3.21
Expand Down
18 changes: 10 additions & 8 deletions ms_agent/agent/agent.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
llm:
service: modelscope
model: Qwen/Qwen3-235B-A22B
model: Qwen/Qwen3-235B-A22B-Instruct-2507
modelscope_api_key:
modelscope_base_url: https://api-inference.modelscope.cn/v1

Expand Down Expand Up @@ -31,20 +31,22 @@ prompt:

6. Do not call tools carelessly. Show your thoughts **as detailed as possible**.

For requests that require performing a specific task or retrieving information, you must use the following format:
7. Respond in the same language the user uses. If the user switches, switch accordingly.

For requests that require performing a specific task or retrieving information, you must use the following format in user language:
```
用户需要 ...
针对该需求,我进行了详细拆解和规划,需要按照如下步骤来解决问题:
The user needs to ...
I have analyzed this request in detail and broken it down into the following steps:
...
```
If you have tools which may help you to solve problems, follow this format to answer:
```
用户需要 ...
针对该需求,我进行了详细拆解和规划,需要按照如下步骤来解决问题:
The user needs to ...
I have analyzed this request in detail and broken it down into the following steps:
...
首先我应当选择...工具,由于该工具..., 该工具的入参需要...
First, I should use the [Tool Name] because [explain relevance]. The required input parameters are: ...
...
我仔细查看了工具返回值,该工具的返回值符合/不符合我的要求,我接下来需要...
I have carefully reviewed the tool's output. The result does/does not fully meet my expectations. Next, I need to ...
```

max_chat_round: 9999
Expand Down
6 changes: 3 additions & 3 deletions ms_agent/agent/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ms_agent.config import Config
from ms_agent.config.config import ConfigLifecycleHandler
from ms_agent.llm import Message
from omegaconf import DictConfig
from omegaconf import DictConfig, OmegaConf

DEFAULT_YAML = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'agent.yaml')
Expand Down Expand Up @@ -43,10 +43,10 @@ def __init__(self,
trust_remote_code: bool = False):
if config_dir_or_id is not None:
self.config: DictConfig = Config.from_task(config_dir_or_id, env)
elif config is not None and isinstance(config, DictConfig):
self.config: DictConfig = config
else:
self.config: DictConfig = Config.from_task(DEFAULT_YAML)
if config is not None and isinstance(config, DictConfig):
self.config = OmegaConf.merge(self.config, config)

if tag is None:
self.tag = getattr(config, 'tag', None) or self.DEFAULT_TAG
Expand Down
56 changes: 46 additions & 10 deletions ms_agent/agent/llm_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from ms_agent.callbacks import Callback, callbacks_mapping
from ms_agent.llm.llm import LLM
from ms_agent.llm.utils import Message, Tool
from ms_agent.memory import Memory, memory_mapping
from ms_agent.memory.mem0ai import Mem0Memory
from ms_agent.rag.base import RAG
from ms_agent.rag.utils import rag_mapping
from ms_agent.tools import ToolManager
Expand All @@ -20,7 +22,6 @@

from ..utils.utils import read_history, save_history
from .base import Agent
from .memory import Memory, memory_mapping
from .plan.base import Planer
from .plan.utils import planer_mapping
from .runtime import Runtime
Expand Down Expand Up @@ -229,20 +230,52 @@ async def _prepare_messages(
return messages

async def _prepare_memory(self):
"""Load and initialize memory components from the config."""
"""
Prepare memory

Initializes and appends memory tool instances based on the configuration provided in self.config.
Args:
self: The instance of the class containing this method. Expected to have:
- config: An object that may contain a memory attribute, which is a list of memory configurations.

Returns:
None

Raises:
AssertionError: If a specified memory type in the config does not exist in memory_mapping.
"""
if hasattr(self.config, 'memory'):
for _memory in (self.config.memory or []):
assert _memory.name in memory_mapping, (
f'{_memory.name} not in memory_mapping, '
memory_type = getattr(_memory, 'name', 'default_memory')
assert memory_type in memory_mapping, (
f'{memory_type} not in memory_mapping, '
f'which supports: {list(memory_mapping.keys())}')
if _memory.name == 'mem0':
from .memory.mem0ai import SharedMemoryManager

# Use LLM config if no special configuration is specified
llm_config = getattr(_memory, 'llm', None)
if llm_config is None:
service = self.config.llm.service
config_dict = {
'model':
self.config.llm.model,
'provider':
'openai',
'openai_base_url':
getattr(self.config.llm, f'{service}_base_url', None),
'openai_api_key':
getattr(self.config.llm, f'{service}_api_key', None),
}
llm_config_obj = OmegaConf.create(config_dict)
setattr(_memory, 'llm', llm_config_obj)

if memory_type == 'mem0':
from ms_agent.memory.mem0ai import SharedMemoryManager
shared_memory = SharedMemoryManager.get_shared_memory(
_memory)
self.memory_tools.append(shared_memory)
else:
self.memory_tools.append(
memory_mapping[_memory.name](_memory))
memory_mapping[memory_type](_memory))

async def _prepare_planer(self):
"""Load and initialize the planer component from the config."""
Expand Down Expand Up @@ -464,7 +497,9 @@ def _save_history(self, messages: List[Message], **kwargs):
user_id = memory_config.user_id
break
for memory_tool in self.memory_tools:
memory_tool._add_memories_from_conversation(messages, user_id)
if isinstance(memory_tool, Mem0Memory):
memory_tool._add_memories_from_conversation(
messages, user_id)

if not self.task or self.task == 'subtask':
return
Expand All @@ -487,8 +522,9 @@ def _save_memory(self, messages: List[Message], **kwargs):
if self.memory_tools:
agent_id = self.tag
for memory_tool in self.memory_tools:
memory_tool._add_memories_from_procedural(
messages, 'subagent', agent_id, 'procedural_memory')
if isinstance(memory_tool, Mem0Memory):
memory_tool._add_memories_from_procedural(
messages, 'subagent', agent_id, 'procedural_memory')
return

async def _run(self, messages: Union[List[Message], str],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Copyright (c) Alibaba, Inc. and its affiliates.
from .base import Memory
from .utils import memory_mapping
from .utils import DefaultMemory, memory_mapping
3 changes: 3 additions & 0 deletions ms_agent/agent/memory/base.py → ms_agent/memory/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
class Memory:
"""The memory refine tool"""

def __init__(self, config):
self.config = config

@abstractmethod
async def run(self, messages: List[Message]) -> List[Message]:
"""Refine the messages
Expand Down
Loading
Loading