refactor: brain memory

This commit is contained in:
莘权 马 2023-09-08 10:05:44 +08:00
parent 42d0281fbb
commit 8b5d83956d
2 changed files with 22 additions and 4 deletions

View file

@ -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:

View file

@ -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)