Skip to content

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

feat: +knowledge

莘权 马6e10cbb73bd1
diff --git a/metagpt/actions/talk_action.py b/metagpt/actions/talk_action.py
index 5485456c5..dab4873fb 100644
--- a/metagpt/actions/talk_action.py
+++ b/metagpt/actions/talk_action.py
@@ -4,18 +4,21 @@ from metagpt.logs import logger
class TalkAction(Action):
- def __init__(self, options, name: str = '', talk='', history_summary='', context=None, llm=None, **kwargs):
+ def __init__(self, options, name: str = '', talk='', history_summary='', knowledge='', context=None, llm=None, **kwargs):
context = context or {}
context["talk"] = talk
context["history_summery"] = history_summary
+ context["knowledge"] = knowledge
super(TalkAction, self).__init__(options=options, name=name, context=context, llm=llm)
self._talk = talk
self._history_summary = history_summary
+ self._knowledge = knowledge
self._rsp = None
@property
def prompt(self):
- prompt = f"{self._history_summary}\n\n"
+ prompt = f"{self._knowledge}\n\n"
+ prompt += f"{self._history_summary}\n\n"
if self._history_summary != "":
prompt += "According to the historical conversation above, "
language = self.options.get("language", "Chinese")
diff --git a/metagpt/memory/brain_memory.py b/metagpt/memory/brain_memory.py
index 68e930144..422c096f3 100644
--- a/metagpt/memory/brain_memory.py
+++ b/metagpt/memory/brain_memory.py
@@ -17,6 +17,7 @@ class BrainMemory(pydantic.BaseModel):
history: List[Message] = []
stack: List[Message] = []
solution: List[Message] = []
+ knowledge: List[Message] = []
def add_talk(self, msg: Message):
@@ -27,6 +28,10 @@ class BrainMemory(pydantic.BaseModel):
msg.add_tag(MessageType.Answer.value)
self.history.append(msg)
+ def get_knowledge(self) -> str:
+ texts = [k.content for k in self.knowledge]
+ return "\n".join(texts)
+
@property
def history_text(self):
if len(self.history) == 0:
diff --git a/metagpt/roles/assistant.py b/metagpt/roles/assistant.py
index e02005f31..c001d69f0 100644
--- a/metagpt/roles/assistant.py
+++ b/metagpt/roles/assistant.py
@@ -87,7 +87,8 @@ class Assistant(Role):
return await handler(text, **kwargs)
async def talk_handler(self, text, **kwargs) -> bool:
- action = TalkAction(options=self.options, talk=text, llm=self._llm, **kwargs)
+ action = TalkAction(options=self.options, talk=text, knowledge=self.memory.get_knowledge(), llm=self._llm,
+ **kwargs)
self.add_to_do(action)
return True
@@ -136,7 +137,7 @@ class Assistant(Role):
async def main():
options = Config().runtime_options
cost_manager = CostManager(**options)
- topic = "draw an apple"
+ topic = "what's apple"
role = Assistant(options=options, cost_manager=cost_manager, language="Chinese")
await role.talk(topic)
while True: