From c204ee87071145ed7aa6214d635597eb0255d86d Mon Sep 17 00:00:00 2001 From: hongjiongteng Date: Tue, 5 Sep 2023 14:44:51 +0800 Subject: [PATCH 1/6] faiss init with kwargs --- metagpt/document_store/faiss_store.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/metagpt/document_store/faiss_store.py b/metagpt/document_store/faiss_store.py index 7833bc706..be4748b50 100644 --- a/metagpt/document_store/faiss_store.py +++ b/metagpt/document_store/faiss_store.py @@ -20,9 +20,10 @@ from metagpt.logs import logger class FaissStore(LocalStore): - def __init__(self, raw_data: Path, cache_dir=None, meta_col="source", content_col="output"): + def __init__(self, raw_data: Path, cache_dir=None, meta_col="source", content_col="output", embedding_conf=None): self.meta_col = meta_col self.content_col = content_col + self.embedding_conf = embedding_conf or {} super().__init__(raw_data, cache_dir) def _load(self) -> Optional["FaissStore"]: @@ -37,7 +38,7 @@ class FaissStore(LocalStore): return store def _write(self, docs, metadatas): - store = FAISS.from_texts(docs, OpenAIEmbeddings(openai_api_version="2020-11-07"), metadatas=metadatas) + store = FAISS.from_texts(docs, OpenAIEmbeddings(openai_api_version="2020-11-07", **self.embedding_conf), metadatas=metadatas) return store def persist(self): From 03019a304bfd342b2a0d5ed62b5a262bb513e8e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 6 Sep 2023 12:13:13 +0800 Subject: [PATCH 2/6] refactor: think --- metagpt/actions/talk_action.py | 1 + metagpt/provider/openai_api.py | 21 +++++++++++++-------- metagpt/roles/assistant.py | 14 +++++--------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/metagpt/actions/talk_action.py b/metagpt/actions/talk_action.py index 81caef013..4afed8014 100644 --- a/metagpt/actions/talk_action.py +++ b/metagpt/actions/talk_action.py @@ -44,6 +44,7 @@ class TalkAction(Action): f"Answer the following questions strictly in {language}, and the answers must follow the Markdown format.\n " f"{self._talk}" ) + logger.info(f"PROMPT: {prompt}") return prompt @property diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index bf2ca7f14..d84109f6a 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -276,16 +276,22 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): return response async def is_related(self, text1, text2): - command = f"{text1}\n{text2}\n\nIf the two sentences above are related, return [TRUE] brief and clear. Otherwise, return [FALSE]." + # command = f"{text1}\n{text2}\n\nIf the two sentences above are related, return [TRUE] brief and clear. Otherwise, return [FALSE]." + command = f"{text2}\n\nIs there any sentence above related to the following sentence: {text1}.\nIf is there any relevance, return [TRUE] brief and clear. Otherwise, return [FALSE] brief and clear." rsp = await self.aask(msg=command, system_msgs=[]) - result, _ = self.extract_info(rsp) - return result == "TRUE" + result = True if "TRUE" in rsp else False + p2 = text2.replace("\n", "") + p1 = text1.replace("\n", "") + logger.info(f"IS_RELATED:\nParagraph 1: {p2}\nParagraph 2: {p1}\nRESULT: {result}") + return result async def rewrite(self, sentence: str, context: str): - command = ( - f"{context}\n\nConsidering the content above, rewrite and return this sentence brief and clear:\n{sentence}" - ) + # command = ( + # f"{context}\n\nConsidering the content above, rewrite and return this sentence brief and clear:\n{sentence}" + # ) + command = f"{context}\n\nExtract relevant information from every preceding sentence and use it to succinctly supplement or rewrite the following text in brief and clear:\n{sentence}" rsp = await self.aask(msg=command, system_msgs=[]) + logger.info(f"REWRITE:\nCommand: {command}\nRESULT: {rsp}") return rsp @staticmethod @@ -316,8 +322,7 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): return windows @staticmethod - def extract_info(input_string): - pattern = r"\[([A-Z]+)\]:\s*(.+)" + def extract_info(input_string, pattern=r"\[([A-Z]+)\]:\s*(.+)"): match = re.match(pattern, input_string) if match: return match.group(1), match.group(2) diff --git a/metagpt/roles/assistant.py b/metagpt/roles/assistant.py index 87127cbab..ac80a4bc8 100644 --- a/metagpt/roles/assistant.py +++ b/metagpt/roles/assistant.py @@ -54,18 +54,15 @@ class Assistant(Role): last_talk = await self.refine_memory() if not last_talk: return False - prompt = f"Refer to this sentence:\n {last_talk}\n" + prompt = "" skills = self.skills.get_skill_list() for desc, name in skills.items(): - prompt += ( - f"If want you to do {desc}, return `[SKILL]: {name}` brief and clear. For instance: [SKILL]: {name}\n" - ) - prompt += "If the preceding text presents a complete question and solution, rewrite and return `[SOLUTION]: {problem}` brief and clear. For instance: [SOLUTION]: Solution for distributing watermelon\n" - prompt += "If the preceding text presents an unresolved issue and its corresponding discussion, rewrite and return `[PROBLEM]: {problem}` brief and clear. For instance: [PROBLEM]: How to distribute watermelon?\n" - prompt += "Otherwise, rewrite and return `[TALK]: {talk}` brief and clear. For instance: [TALK]: distribute watermelon" + prompt += f"If the text explicitly want you to {desc}, return `[SKILL]: {name}` brief and clear. For instance: [SKILL]: {name}\n" + prompt += 'Otherwise, return `[TALK]: {talk}` brief and clear. For instance: if {talk} is "xxxx" return [TALK]: xxxx\n\n' + prompt += f"Now what specific action is explicitly mentioned in the text: {last_talk}\n" logger.info(prompt) rsp = await self._llm.aask(prompt, []) - logger.info(rsp) + logger.info(f"THINK: {prompt}\n, THINK RESULT: {rsp}\n") return await self._plan(rsp, last_talk=last_talk) async def act(self) -> ActionOutput: @@ -90,7 +87,6 @@ class Assistant(Role): skill, text = Assistant.extract_info(input_string=rsp) handlers = { MessageType.Talk.value: self.talk_handler, - MessageType.Problem.value: self.talk_handler, MessageType.Skill.value: self.skill_handler, } handler = handlers.get(skill, self.talk_handler) From 4e0b2898a6a54993738448991699e81dd58bd577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 6 Sep 2023 12:24:20 +0800 Subject: [PATCH 3/6] refactor: think --- metagpt/roles/assistant.py | 1 - 1 file changed, 1 deletion(-) diff --git a/metagpt/roles/assistant.py b/metagpt/roles/assistant.py index ac80a4bc8..bef2cf53c 100644 --- a/metagpt/roles/assistant.py +++ b/metagpt/roles/assistant.py @@ -60,7 +60,6 @@ class Assistant(Role): prompt += f"If the text explicitly want you to {desc}, return `[SKILL]: {name}` brief and clear. For instance: [SKILL]: {name}\n" prompt += 'Otherwise, return `[TALK]: {talk}` brief and clear. For instance: if {talk} is "xxxx" return [TALK]: xxxx\n\n' prompt += f"Now what specific action is explicitly mentioned in the text: {last_talk}\n" - logger.info(prompt) rsp = await self._llm.aask(prompt, []) logger.info(f"THINK: {prompt}\n, THINK RESULT: {rsp}\n") return await self._plan(rsp, last_talk=last_talk) From c792cf09ecb642faf9bc628edf920b43847f83f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 6 Sep 2023 13:18:03 +0800 Subject: [PATCH 4/6] refactor: think --- metagpt/roles/assistant.py | 1 + 1 file changed, 1 insertion(+) diff --git a/metagpt/roles/assistant.py b/metagpt/roles/assistant.py index bef2cf53c..cd1932f82 100644 --- a/metagpt/roles/assistant.py +++ b/metagpt/roles/assistant.py @@ -93,6 +93,7 @@ class Assistant(Role): async def talk_handler(self, text, **kwargs) -> bool: history = self.memory.history_text + text = kwargs.get("last_talk") or text action = TalkAction( talk=text, knowledge=self.memory.get_knowledge(), history_summary=history, llm=self._llm, **kwargs ) From eec0fbde6d0a4563b166b5cab929a65bc70c518b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 6 Sep 2023 15:02:36 +0800 Subject: [PATCH 5/6] refactor: disable log --- metagpt/memory/brain_memory.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/memory/brain_memory.py b/metagpt/memory/brain_memory.py index 2195da566..f309b532e 100644 --- a/metagpt/memory/brain_memory.py +++ b/metagpt/memory/brain_memory.py @@ -72,7 +72,7 @@ class BrainMemory(pydantic.BaseModel): if not redis.is_valid() or not redis_key: return BrainMemory() v = await redis.get(key=redis_key) - logger.info(f"REDIS GET {redis_key} {v}") + logger.debug(f"REDIS GET {redis_key} {v}") if v: data = json.loads(v) bm = BrainMemory(**data) @@ -86,7 +86,7 @@ class BrainMemory(pydantic.BaseModel): return False v = self.json() await redis.set(key=redis_key, data=v, timeout_sec=timeout_sec) - logger.info(f"REDIS SET {redis_key} {v}") + logger.debug(f"REDIS SET {redis_key} {v}") self.is_dirty = False @staticmethod From b3be30bdad534836d1bdaa168ae2a8a9d9e42245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 6 Sep 2023 15:12:41 +0800 Subject: [PATCH 6/6] refactor: log --- metagpt/actions/skill_action.py | 3 +-- metagpt/actions/talk_action.py | 5 ++--- metagpt/provider/openai_api.py | 12 ++++++------ 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/metagpt/actions/skill_action.py b/metagpt/actions/skill_action.py index 758591fdd..f629cfcbf 100644 --- a/metagpt/actions/skill_action.py +++ b/metagpt/actions/skill_action.py @@ -40,9 +40,8 @@ class ArgumentsParingAction(Action): async def run(self, *args, **kwargs) -> ActionOutput: prompt = self.prompt - logger.info(prompt) rsp = await self.llm.aask(msg=prompt, system_msgs=[]) - logger.info(rsp) + logger.debug(f"SKILL:{prompt}\n, RESULT:{rsp}") self.args = ArgumentsParingAction.parse_arguments(skill_name=self.skill.name, txt=rsp) self.rsp = ActionOutput(content=rsp) return self.rsp diff --git a/metagpt/actions/talk_action.py b/metagpt/actions/talk_action.py index 4afed8014..0e3762798 100644 --- a/metagpt/actions/talk_action.py +++ b/metagpt/actions/talk_action.py @@ -44,7 +44,7 @@ class TalkAction(Action): f"Answer the following questions strictly in {language}, and the answers must follow the Markdown format.\n " f"{self._talk}" ) - logger.info(f"PROMPT: {prompt}") + logger.debug(f"PROMPT: {prompt}") return prompt @property @@ -64,9 +64,8 @@ class TalkAction(Action): async def run(self, *args, **kwargs) -> ActionOutput: prompt = self.prompt - logger.info(prompt) rsp = await self.llm.aask(msg=prompt, system_msgs=[]) - logger.info(rsp) + logger.debug(f"PROMPT:{prompt}\nRESULT:{rsp}\n") self._rsp = ActionOutput(content=rsp) return self._rsp diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index d84109f6a..863475f52 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -257,9 +257,9 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): else: command = f"Translate the above content into a summary of less than {max_words} words." msg = text + "\n\n" + command - logger.info(f"summary ask:{msg}") + logger.debug(f"summary ask:{msg}") response = await self.aask(msg=msg, system_msgs=[]) - logger.info(f"summary rsp: {response}") + logger.debug(f"summary rsp: {response}") return response async def get_context_title(self, text: str, max_words=5) -> str: @@ -270,9 +270,9 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): command = f"Translate the above summary into a {language} title of less than {max_words} words." summaries = [summary, command] msg = "\n".join(summaries) - logger.info(f"title ask:{msg}") + logger.debug(f"title ask:{msg}") response = await self.aask(msg=msg, system_msgs=[]) - logger.info(f"title rsp: {response}") + logger.debug(f"title rsp: {response}") return response async def is_related(self, text1, text2): @@ -282,7 +282,7 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): result = True if "TRUE" in rsp else False p2 = text2.replace("\n", "") p1 = text1.replace("\n", "") - logger.info(f"IS_RELATED:\nParagraph 1: {p2}\nParagraph 2: {p1}\nRESULT: {result}") + logger.info(f"IS_RELATED:\nParagraph 1: {p2}\nParagraph 2: {p1}\nRESULT: {result}\n") return result async def rewrite(self, sentence: str, context: str): @@ -291,7 +291,7 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): # ) command = f"{context}\n\nExtract relevant information from every preceding sentence and use it to succinctly supplement or rewrite the following text in brief and clear:\n{sentence}" rsp = await self.aask(msg=command, system_msgs=[]) - logger.info(f"REWRITE:\nCommand: {command}\nRESULT: {rsp}") + logger.info(f"REWRITE:\nCommand: {command}\nRESULT: {rsp}\n") return rsp @staticmethod