Skip to content

Commit ff32df1

Browse files
authored
fix bugs in memory (#753)
1 parent 143aadf commit ff32df1

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

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/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.

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

0 commit comments

Comments
 (0)