Skip to content

🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming

refactor: brain memory

莘权 马8b5d83956d7c
diff --git a/metagpt/memory/brain_memory.py b/metagpt/memory/brain_memory.py
index a995244a6..9878fa750 100644
--- a/metagpt/memory/brain_memory.py
+++ b/metagpt/memory/brain_memory.py
@@ -124,13 +124,15 @@ class BrainMemory(pydantic.BaseModel):
self.last_talk = None
return v
- async def summarize(self, llm, max_words=200, keep_language: bool = False, **kwargs):
+ async def summarize(self, llm, max_words=200, keep_language: bool = False, limit: int = -1, **kwargs):
if self.llm_type == LLMType.METAGPT.value:
return await self._metagpt_summarize(llm=llm, max_words=max_words, keep_language=keep_language, **kwargs)
- return await self._openai_summarize(llm=llm, max_words=max_words, keep_language=keep_language, **kwargs)
+ return await self._openai_summarize(
+ llm=llm, max_words=max_words, keep_language=keep_language, limit=limit, **kwargs
+ )
- async def _openai_summarize(self, llm, max_words=200, keep_language: bool = False, **kwargs):
+ async def _openai_summarize(self, llm, max_words=200, keep_language: bool = False, limit: int = -1, **kwargs):
max_token_count = DEFAULT_MAX_TOKENS
max_count = 100
texts = [self.historical_summary]
@@ -139,6 +141,8 @@ class BrainMemory(pydantic.BaseModel):
texts.append(m.content)
text = "\n".join(texts)
text_length = len(text)
+ if limit > 0 and text_length < limit:
+ return text
summary = ""
while max_count > 0:
if text_length < max_token_count:
diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py
index 231b568c7..9dbbaf7e5 100644
--- a/metagpt/provider/openai_api.py
+++ b/metagpt/provider/openai_api.py
@@ -22,7 +22,9 @@ from tenacity import (
)
from metagpt.config import CONFIG
+from metagpt.llm import LLMType
from metagpt.logs import logger
+from metagpt.memory.brain_memory import BrainMemory
from metagpt.provider.base_gpt_api import BaseGPTAPI
from metagpt.utils.cost_manager import Costs
from metagpt.utils.token_counter import (
@@ -261,6 +263,19 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter):
raise e
raise openai.error.OpenAIError("Exceeds the maximum retries")
+ async def get_summary(self, text: str, max_words=200, keep_language: bool = False, **kwargs) -> str:
+ """
+ Return string in the following format:
+ [
+ {"role": "system", "content": "You are a helpful assistant."},
+ {"role": "user", "content": "Knock knock."},
+ {"role": "assistant", "content": "Who's there?"},
+ {"role": "user", "content": "Orange."},
+ ]
+ """
+ memory = BrainMemory(llm_type=LLMType.OPENAI.value, historical_summary=text)
+ return await memory.summarize(llm=self._llm, max_length=max_words, keep_language=keep_language)
+
MAX_TRY = 5
@@ -269,4 +284,3 @@ if __name__ == "__main__":
as dfas sad lkf sdkl sakdfsdk sjd jsk sdl sk dd sd asd fa sdf sad dd
- .gitlab-ci.yml & base_test.py
"""
- OpenAIGPTAPI.split_texts(txt, 30)