mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-05-15 11:02:36 +02:00
refactor: brain memory
This commit is contained in:
parent
7c6b0325d8
commit
f2da313548
3 changed files with 50 additions and 4 deletions
|
|
@ -6,10 +6,12 @@
|
|||
@File : talk_action.py
|
||||
@Desc : Act as it’s a talk
|
||||
"""
|
||||
import json
|
||||
|
||||
from metagpt.actions import Action, ActionOutput
|
||||
from metagpt.config import CONFIG
|
||||
from metagpt.const import DEFAULT_LANGUAGE
|
||||
from metagpt.llm import LLMType
|
||||
from metagpt.logs import logger
|
||||
|
||||
|
||||
|
|
@ -63,6 +65,15 @@ class TalkAction(Action):
|
|||
return prompt
|
||||
|
||||
async def run(self, *args, **kwargs) -> ActionOutput:
|
||||
if CONFIG.LLM_TYPE == LLMType.METAGPT.value:
|
||||
rsp = await self.llm.aask(
|
||||
msg=self._talk,
|
||||
knowledge_msgs=[{"knowledge": self._knowledge}] if self._knowledge else None,
|
||||
history_msgs=json.loads(self._history_summary) if self._history_summary else None,
|
||||
)
|
||||
self._rsp = ActionOutput(content=rsp)
|
||||
return self._rsp
|
||||
|
||||
prompt = self.prompt
|
||||
rsp = await self.llm.aask(msg=prompt, system_msgs=[])
|
||||
logger.debug(f"PROMPT:{prompt}\nRESULT:{rsp}\n")
|
||||
|
|
|
|||
|
|
@ -309,4 +309,28 @@ class BrainMemory(pydantic.BaseModel):
|
|||
def is_history_available(self):
|
||||
return bool(self.history or self.historical_summary)
|
||||
|
||||
@property
|
||||
def history_text(self):
|
||||
if self.llm_type == LLMType.METAGPT.value:
|
||||
return self._get_metagpt_history_text()
|
||||
return self._get_openai_history_text()
|
||||
|
||||
def _get_metagpt_history_text(self):
|
||||
return BrainMemory.to_metagpt_history_format(self.history)
|
||||
|
||||
def _get_openai_history_text(self):
|
||||
if len(self.history) == 0 and not self.historical_summary:
|
||||
return ""
|
||||
texts = [self.historical_summary] if self.historical_summary else []
|
||||
for m in self.history[:-1]:
|
||||
if isinstance(m, Dict):
|
||||
t = Message(**m).content
|
||||
elif isinstance(m, Message):
|
||||
t = m.content
|
||||
else:
|
||||
continue
|
||||
texts.append(t)
|
||||
|
||||
return "\n".join(texts)
|
||||
|
||||
DEFAULT_TOKEN_SIZE = 500
|
||||
|
|
|
|||
|
|
@ -38,11 +38,22 @@ class BaseGPTAPI(BaseChatbot):
|
|||
rsp = self.completion(message)
|
||||
return self.get_choice_text(rsp)
|
||||
|
||||
async def aask(self, msg: str, system_msgs: Optional[list[str]] = None, generator: bool = False) -> str:
|
||||
async def aask(
|
||||
self,
|
||||
msg: str,
|
||||
system_msgs: Optional[list[str]] = None,
|
||||
history_msgs: Optional[list[dict[str, str]]] = None,
|
||||
knowledge_msgs: Optional[list[dict[str, str]]] = None,
|
||||
generator: bool = False,
|
||||
) -> str:
|
||||
message = []
|
||||
if system_msgs:
|
||||
message = self._system_msgs(system_msgs) + [self._user_msg(msg)]
|
||||
else:
|
||||
message = [self._default_system_msg(), self._user_msg(msg)]
|
||||
message = self._system_msgs(system_msgs)
|
||||
if knowledge_msgs:
|
||||
message.extend(knowledge_msgs)
|
||||
if history_msgs:
|
||||
message.extend(history_msgs)
|
||||
message.append(self._user_msg(msg))
|
||||
try:
|
||||
rsp = await self.acompletion_text(message, stream=True, generator=generator)
|
||||
except Exception as e:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue