diff --git a/metagpt/reflect/__init__.py b/metagpt/reflect/__init__.py index 6cc738dbe..6acb42c9e 100644 --- a/metagpt/reflect/__init__.py +++ b/metagpt/reflect/__init__.py @@ -3,8 +3,10 @@ # @Desc : reflection module from metagpt.reflect import agent_reflect - +from metagpt.reflect import ga_prompt_generator __all__ = [ "agent_reflect", "LongTermMemory", + "ga_po" + "ga_prompt_generator" ] diff --git a/metagpt/actions/ga_action_base.py b/metagpt/reflect/ga_prompt_generator.py similarity index 95% rename from metagpt/actions/ga_action_base.py rename to metagpt/reflect/ga_prompt_generator.py index 01bdca6b6..6b7bdbad7 100644 --- a/metagpt/actions/ga_action_base.py +++ b/metagpt/reflect/ga_prompt_generator.py @@ -12,7 +12,7 @@ from metagpt.llm import DEFAULT_LLM # 特殊指令加入Prompt生成 -def final_response(prompt, special_instruction, example_output=None): +async def final_response(prompt, special_instruction, example_output=None): """ 通过将特殊指令加入Prompt生成最终的响应。 @@ -30,7 +30,7 @@ def final_response(prompt, special_instruction, example_output=None): if example_output: prompt += "Example output json:\n" prompt += '{"output": "' + str(example_output) + '"}' - return DEFAULT_LLM.ask(prompt) + return await DEFAULT_LLM.aask(prompt) # prompt填充模板 diff --git a/metagpt/reflect/reflect.py b/metagpt/reflect/reflect.py index 84ac5c604..218df4fea 100644 --- a/metagpt/reflect/reflect.py +++ b/metagpt/reflect/reflect.py @@ -2,25 +2,35 @@ # -*- coding: utf-8 -*- # @Desc : base class of reflection +import asyncio import json from metagpt.logs import logger import time -from metagpt.actions.ga_action_base import final_response +from ga_prompt_generator import final_response ''' 等待Agent和memory更新,保留相关引用但可以忽略。 ''' from metagpt.memory.ga_memory_storage import AgentMemory, MemoryBasic +import json +import time -def agent_reflect(agent): - A = generate_focus_point(agent.memories_list) + +async def agent_reflect(agent): + """ + 代理反思函数:生成关注点并生成洞察和证据 + + """ + A = await generate_focus_point(agent.memories_list) for i in A: - B = generate_insights_and_evidence( - agent, agent.memories_list, question=i) + B = await generate_insights_and_evidence(agent, agent.memories_list, question=i) -def generate_focus_point(memories_list, n=3): +async def generate_focus_point(memories_list: list[MemoryBasic], n=3): + """ + 生成关注点函数:根据记忆列表生成关注点 + """ wait_sorted_mem = [[i.accessed_time, i] for i in memories_list] sorted_memories = sorted(wait_sorted_mem, key=lambda x: x[0]) memorys = [i for created, i in sorted_memories] @@ -32,19 +42,22 @@ def generate_focus_point(memories_list, n=3): Given only the information above, what are {num_question} most salient high-level questions we can answer about the subjects grounded in the statements? ''' example_output = '["What should Jane do for lunch", "Does Jane like strawberry", "Who is Jane"]' - out = final_response(prompt.format(statements=statements, num_question=n), - "Output must be a list of str.", example_output) + out = await final_response(prompt.format(statements=statements, num_question=n), + "Output must be a list of str.", example_output) try: poi_dict = json.loads(out) - return (poi_dict['output']) + return poi_dict['output'] except ValueError: print(out) logger.error('无法返回正常结果') return out -def generate_insights_and_evidence(agent, memories_list, question, n=5): - agent_retrive(agent, question, 20, 10) +async def generate_insights_and_evidence(agent: Agent, memories_list: list[MemoryBasic], question: str, n=5): + """ + 生成洞察和证据函数:根据问题生成洞察和证据 + """ + await agent_retrieve(agent, question, 20, 10) statements = "" for count, mem in enumerate(memories_list): statements += f'{str(count)}. {mem.description}\n' @@ -53,7 +66,7 @@ def generate_insights_and_evidence(agent, memories_list, question, n=5): {statements} What {n} high-level insights can you infer from the above statements? - You should return a list of list[str,list] . The first element is the insight you have found.The second element is the + You should return a list of list[str,list]. The first element is the insight you have found. The second element is the ''' ret = final_response(prompt.format( @@ -63,27 +76,22 @@ def generate_insights_and_evidence(agent, memories_list, question, n=5): for insight, index in insight_list: agent.memory_list.append(Memory_basic( time.time(), None, insight, None, None)) - return (insight_list) + return insight_list except: - Logger.error('我们无法获得想要的返回。') + logger.error('我们无法获得想要的返回。') return ret -if __name__ == "__main__": +""" if __name__ == "__main__": # 例子,构建John Agent,实现retrive John_iss = "John Lin is a pharmacy shopkeeper at the Willow Market and Pharmacy who loves to help people. He is always looking for ways to make the process of getting medication easier for his customers; John Lin is living with his wife, Mei Lin, who is a college professor, and son, Eddy Lin, who is a student studying music theory; John Lin loves his family very much; John Lin has known the old couple next-door, Sam Moore and Jennifer Moore, for a few years; John Lin thinks Sam Moore is a kind and nice man; John Lin knows his neighbor, Yuriko Yamamoto, well; John Lin knows of his neighbors, Tamara Taylor and Carmen Ortiz, but has not met them before; John Lin and Tom Moreno are colleagues at The Willows Market and Pharmacy; John Lin and Tom Moreno are friends and like to discuss local politics together; John Lin knows the Moreno family somewhat well — the husband Tom Moreno and the wife Jane Moreno." John = AgentMemory( "John", John_iss, memory_path="agent_memories/John_memory.json") # John的相关信息:{'Had a friendly chat with Yuriko about her garden.': 2.4992317730827667, 'Helped Mrs. Moore carry groceries into her house.': 1.957656720441911, 'Discussed local politics with Tom Moreno.': 1.9458268038234035} - A = generate_focus_point(John.memories_list) - - for i in A: - B = generate_insights_and_evidence( - John, John.memories_list, question=A[0]) - print(type(B)) - print(B) + asyncio.run(agent_reflect(John)) ''' 这里是输出,list形式,返回给记忆。 [['The pharmacy is a friendly and helpful community.', [0, 2, 9, 12]], ['The pharmacy is a place where people come for more than just medication.', [3, 5, 13, 14]], ['The pharmacy is a place where people come for advice and conversation.', [0, 2, 6, 9, 12]], ['The pharmacy is a place where people come for assistance with daily tasks.', [3, 5, 13, 14]], ['The pharmacy is a place where people come for political discussions.', [1]]] ''' + """