mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-17 15:35:21 +02:00
PEP8格式修改
This commit is contained in:
parent
e40e3bacc5
commit
da44e12677
3 changed files with 53 additions and 51 deletions
|
|
@ -23,16 +23,14 @@ class AgentFocusPt(STAction):
|
|||
except:
|
||||
return False
|
||||
|
||||
def _func_cleanup(self, llm_resp: str, prompt: str = "") -> list:
|
||||
def _func_cleanup(self, llm_resp: str, prompt: str = "") -> str:
|
||||
try:
|
||||
"""
|
||||
Cleanup handling has been completed for run_v2
|
||||
"""
|
||||
return llm_resp
|
||||
# llm_resp = "1) " + llm_resp.strip()
|
||||
# ret = []
|
||||
# for i in llm_resp.split("\n"):
|
||||
# ret += [i.split(") ")[-1]]
|
||||
# return ret
|
||||
except Exception as exp:
|
||||
print(f"cleanup 错误 {exp}")
|
||||
logger.error(f"{self.__class__.__name__} with error {exp}")
|
||||
|
||||
def _func_fail_default_resp(self) -> str:
|
||||
pass
|
||||
|
|
@ -70,7 +68,6 @@ class AgentInsightAndGuidance(STAction):
|
|||
|
||||
def _func_cleanup(self, llm_resp: str, prompt: str = "") -> dict:
|
||||
try:
|
||||
logger.info(f"Run prompt Insight get {llm_resp}")
|
||||
llm_resp = "1. " + llm_resp.strip()
|
||||
ret = dict()
|
||||
for i in llm_resp.split("\n"):
|
||||
|
|
@ -82,7 +79,7 @@ class AgentInsightAndGuidance(STAction):
|
|||
ret[thought] = evi_raw
|
||||
return ret
|
||||
except Exception as exp:
|
||||
logger.error(f"AGent Insight 报错{exp}")
|
||||
logger.error(f"{self.__class__.__name__} with error {exp}")
|
||||
|
||||
def _func_fail_default_resp(self) -> str:
|
||||
pass
|
||||
|
|
@ -121,10 +118,9 @@ class AgentEventTriple(STAction):
|
|||
cr = [i.strip() for i in cr.split(")")[0].split(",")]
|
||||
if len(cr) != 2:
|
||||
return cr[-2:]
|
||||
logger.info(f"cr结果为{cr}")
|
||||
return cr
|
||||
except Exception as exp:
|
||||
logger.error(f"AGent Triple 报错{exp}")
|
||||
logger.error(f"{self.__class__.__name__} with error {exp}")
|
||||
|
||||
def _func_fail_default_resp(self) -> str:
|
||||
pass
|
||||
|
|
@ -166,7 +162,8 @@ class AgentEventPoignancy(STAction):
|
|||
llm_resp = int(llm_resp.strip())
|
||||
return llm_resp
|
||||
except Exception as exp:
|
||||
logger.error(f"AGent Event poignancy 报错{exp}")
|
||||
logger.error(f"{self.__class__.__name__} with error {exp}")
|
||||
|
||||
def _func_fail_default_resp(self) -> str:
|
||||
pass
|
||||
|
||||
|
|
@ -209,7 +206,8 @@ class AgentChatPoignancy(STAction):
|
|||
llm_resp = int(llm_resp.strip())
|
||||
return llm_resp
|
||||
except Exception as exp:
|
||||
logger.error(f"AGent Chat poignancy 报错{exp}")
|
||||
logger.error(f"{self.__class__.__name__} with error {exp}")
|
||||
|
||||
def _func_fail_default_resp(self) -> str:
|
||||
pass
|
||||
|
||||
|
|
@ -251,7 +249,7 @@ class AgentPlanThoughtOnConvo(STAction):
|
|||
try:
|
||||
return llm_resp.split('"')[0].strip()
|
||||
except Exception as exp:
|
||||
logger.error(f"AGent PlanThought 报错{exp}")
|
||||
logger.error(f"{self.__class__.__name__} with error {exp}")
|
||||
|
||||
def _func_fail_default_resp(self) -> str:
|
||||
pass
|
||||
|
|
@ -290,7 +288,8 @@ class AgentMemoryOnConvo(STAction):
|
|||
try:
|
||||
return llm_resp.split('"')[0].strip()
|
||||
except Exception as exp:
|
||||
logger.error(f"AGent MemoryOnconvo 报错{exp}")
|
||||
logger.error(f"{self.__class__.__name__} with error {exp}")
|
||||
|
||||
def _func_fail_default_resp(self) -> str:
|
||||
pass
|
||||
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ def generate_insights_and_evidence(role, nodes, n=5):
|
|||
ret[thought] = evidence_node_id
|
||||
return ret
|
||||
except Exception as exp:
|
||||
logger.info(f"insight处理错误为{exp}")
|
||||
logger.error(f"generate_insights_and_evidence error:{exp}")
|
||||
return {"this is blank": "node_1"}
|
||||
|
||||
|
||||
|
|
@ -59,7 +59,6 @@ def generate_action_event_triple(act_desp, role):
|
|||
EXAMPLE OUTPUT:
|
||||
"🧈🍞"
|
||||
"""
|
||||
logger.info(f"event Triple 输入为:{act_desp}")
|
||||
run_event_triple = AgentEventTriple()
|
||||
result = run_event_triple.run(act_desp, role)
|
||||
return result
|
||||
|
|
@ -108,16 +107,15 @@ def run_reflect(role: "STRole"):
|
|||
# For each of the focal points, generate thoughts and save it in the
|
||||
# agent's memory.
|
||||
for focal_pt, nodes in retrieved.items():
|
||||
logger.info(f"检索结果为{nodes}")
|
||||
# xx = [i.embedding_key for i in nodes]
|
||||
# for xxx in xx: logger.info(xxx)
|
||||
xx = [i.embedding_key for i in nodes]
|
||||
for xxx in xx: logger.info(f"Nodes retrieved for {focal_pt} are {xxx}.")
|
||||
|
||||
thoughts = generate_insights_and_evidence(role, nodes, 5)
|
||||
# 生成的是字典类型
|
||||
for thought, evidence in thoughts.items():
|
||||
created = role.scratch.curr_time
|
||||
expiration = created + datetime.timedelta(days=30)
|
||||
s, p, o = generate_action_event_triple("("+thought+")", role)
|
||||
s, p, o = generate_action_event_triple("(" + thought + ")", role)
|
||||
keywords = set([s, p, o])
|
||||
thought_poignancy = generate_poig_score(role, "thought", thought)
|
||||
thought_embedding_pair = (thought, get_embedding(thought))
|
||||
|
|
@ -126,7 +124,7 @@ def run_reflect(role: "STRole"):
|
|||
created, expiration, s, p, o, thought, keywords,
|
||||
thought_poignancy, thought_embedding_pair, evidence
|
||||
)
|
||||
logger.info(f"主谓宾为:{s}{p}{o},描述为{thought}")
|
||||
logger.info(f"add thought memory: {thought}")
|
||||
|
||||
|
||||
def reflection_trigger(role: "STRole"):
|
||||
|
|
|
|||
|
|
@ -1,60 +1,65 @@
|
|||
|
||||
import pytest
|
||||
from examples.st_game.roles.st_role import STRole
|
||||
from examples.st_game.actions.run_reflect_action import AgentFocusPt, AgentInsightAndGuidance, AgentEventTriple, AgentEventPoignancy, AgentChatPoignancy, AgentPlanThoughtOnConvo, AgentMemoryOnConvo
|
||||
from examples.st_game.actions.run_reflect_action import AgentFocusPt, AgentInsightAndGuidance, AgentEventTriple, \
|
||||
AgentEventPoignancy, AgentChatPoignancy, AgentPlanThoughtOnConvo, AgentMemoryOnConvo
|
||||
from metagpt.logs import logger
|
||||
|
||||
|
||||
class TestReflectFunction:
|
||||
@pytest.fixture
|
||||
def init_agent(self):
|
||||
# 创建一个STRole实例,注意从GA中copy过来JSON文件
|
||||
role = STRole(sim_code="July1_the_ville_isabella_maria_klaus-step-3-11", start_date='February 13, 2023', curr_time='February 13, 2023, 14:53:10')
|
||||
"""
|
||||
init STRole form local json, set sim_code(path),curr_time & start_date
|
||||
"""
|
||||
role = STRole(sim_code="July1_the_ville_isabella_maria_klaus-step-3-11", start_date='February 13, 2023',
|
||||
curr_time='February 13, 2023, 14:53:10')
|
||||
return role
|
||||
|
||||
def test_function_focus_and_insight_action(self,init_agent):
|
||||
def test_function_focus_and_insight_action(self, init_agent):
|
||||
"""
|
||||
单个Action测试样例
|
||||
test for AgentFocusPt & AgentInsightAndGuidance
|
||||
"""
|
||||
logger.info(f"{__name__}函数启动")
|
||||
# run_focus = AgentFocusPt()
|
||||
# statements = "" # 这个statements 与 n 设置是遵循reflect里面实际设置# 来的,你写的时候可以对应代码看一下
|
||||
# out_put = run_focus.run(init_agent, statements, n=3)
|
||||
run_focus = AgentFocusPt()
|
||||
statements = ""
|
||||
run_focus.run(init_agent, statements, n=3)
|
||||
|
||||
"""
|
||||
这里有通过测试的结果,但是更多时候LLM生成的结果缺少了because of;考虑修改一下prompt
|
||||
result = {'Klaus Mueller and Maria Lopez have a close relationship because they have been friends for a long time and have a strong bond': [1, 2, 5, 9, 11, 14], 'Klaus Mueller has a crush on Maria Lopez': [8, 15, 24], 'Klaus Mueller is academically inclined and actively researching a topic': [13, 20], 'Klaus Mueller is socially active and acquainted with Isabella Rodriguez': [17, 21, 22], 'Klaus Mueller is organized and prepared': [19]}
|
||||
"""
|
||||
# run_insight = AgentInsightAndGuidance()
|
||||
# statements = "[user: Klaus Mueller has a close relationship with Maria Lopez, user:s Mueller and Maria Lopez have a close relationship, user: Klaus Mueller has a close relationship with Maria Lopez, user: Klaus Mueller has a close relationship with Maria Lopez, user: Klaus Mueller and Maria Lopez have a strong relationship, user: Klaus Mueller is a dormmate of Maria Lopez., user: Klaus Mueller and Maria Lopez have a strong bond, user: Klaus Mueller has a crush on Maria Lopez, user: Klaus Mueller and Maria Lopez have been friends for more than 2 years., user: Klaus Mueller has a close relationship with Maria Lopez, user: Klaus Mueller Maria Lopez is heading off to college., user: Klaus Mueller and Maria Lopez have a close relationship, user: Klaus Mueller is actively researching a topic, user: Klaus Mueller is close friends and classmates with Maria Lopez., user: Klaus Mueller is socially active, user: Klaus Mueller has a crush on Maria Lopez., user: Klaus Mueller and Maria Lopez have been friends for a long time, user: Klaus Mueller is academically inclined, user: For Klaus Mueller's planning: should remember to ask Maria Lopez about her research paper, as she found it interesting that he mentioned it., user: Klaus Mueller is acquainted with Isabella Rodriguez, user: Klaus Mueller is organized and prepared, user: Maria Lopez is conversing about conversing about Maria's research paper mentioned by Klaus, user: Klaus Mueller is conversing about conversing about Maria's research paper mentioned by Klaus, user: Klaus Mueller is a student, user: Klaus Mueller is a student, user: Klaus Mueller is conversing about two friends named Klaus Mueller and Maria Lopez discussing their morning plans and progress on a research paper before Maria heads off to college., user: Klaus Mueller is socially active, user: Klaus Mueller is socially active, user: Klaus Mueller is socially active and acquainted with Isabella Rodriguez, user: Klaus Mueller has a crush on Maria Lopez]"
|
||||
# run_insight.run(init_agent, statements, n=5)
|
||||
run_insight = AgentInsightAndGuidance()
|
||||
statements = "[user: Klaus Mueller has a close relationship with Maria Lopez, user:s Mueller and Maria Lopez have a close relationship, user: Klaus Mueller has a close relationship with Maria Lopez, user: Klaus Mueller has a close relationship with Maria Lopez, user: Klaus Mueller and Maria Lopez have a strong relationship, user: Klaus Mueller is a dormmate of Maria Lopez., user: Klaus Mueller and Maria Lopez have a strong bond, user: Klaus Mueller has a crush on Maria Lopez, user: Klaus Mueller and Maria Lopez have been friends for more than 2 years., user: Klaus Mueller has a close relationship with Maria Lopez, user: Klaus Mueller Maria Lopez is heading off to college., user: Klaus Mueller and Maria Lopez have a close relationship, user: Klaus Mueller is actively researching a topic, user: Klaus Mueller is close friends and classmates with Maria Lopez., user: Klaus Mueller is socially active, user: Klaus Mueller has a crush on Maria Lopez., user: Klaus Mueller and Maria Lopez have been friends for a long time, user: Klaus Mueller is academically inclined, user: For Klaus Mueller's planning: should remember to ask Maria Lopez about her research paper, as she found it interesting that he mentioned it., user: Klaus Mueller is acquainted with Isabella Rodriguez, user: Klaus Mueller is organized and prepared, user: Maria Lopez is conversing about conversing about Maria's research paper mentioned by Klaus, user: Klaus Mueller is conversing about conversing about Maria's research paper mentioned by Klaus, user: Klaus Mueller is a student, user: Klaus Mueller is a student, user: Klaus Mueller is conversing about two friends named Klaus Mueller and Maria Lopez discussing their morning plans and progress on a research paper before Maria heads off to college., user: Klaus Mueller is socially active, user: Klaus Mueller is socially active, user: Klaus Mueller is socially active and acquainted with Isabella Rodriguez, user: Klaus Mueller has a crush on Maria Lopez]"
|
||||
run_insight.run(init_agent, statements, n=5)
|
||||
|
||||
def test_event_triple_action(self,init_agent):
|
||||
def test_event_triple_action(self, init_agent):
|
||||
"""
|
||||
测试tripleAgent Action
|
||||
我们需要限制生成字数在15之内,生成字数没有限制的时候很容易跑通
|
||||
Prompt同样存在问题,但是我做了处理
|
||||
test for AgentEventTriple
|
||||
error:遇到复杂情况时(多个Object)无法正确归纳,
|
||||
solution: 限制MaxTokens为15
|
||||
"""
|
||||
run_triple = AgentEventTriple()
|
||||
statements = "(Klaus Mueller is academically inclined)"
|
||||
run_triple.run(statements,init_agent)
|
||||
pass
|
||||
run_triple.run(statements, init_agent)
|
||||
|
||||
def test_poignancy_action(self,init_agent):
|
||||
def test_poignancy_action(self, init_agent):
|
||||
"""
|
||||
测试两个关于poignancy的Action
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
def test_convo_action(self,init_agent):
|
||||
"""
|
||||
测试两个convo相关的类
|
||||
test for AgentEventPoignancy, AgentChatPoignancy
|
||||
done in reflect test
|
||||
"""
|
||||
pass
|
||||
|
||||
def test_convo_action(self, init_agent):
|
||||
"""
|
||||
test for AgentPlanThoughtOnConvo, AgentMemoryOnConvo
|
||||
TODO Undone
|
||||
"""
|
||||
pass
|
||||
|
||||
# 测试全部Reflection功能
|
||||
def test_reflect_function(self, init_agent):
|
||||
# 修改 近期 importace 确保Reflect机制能够触发
|
||||
"""
|
||||
test for reflection
|
||||
modify importance_trigger_curr to trigger reflect
|
||||
"""
|
||||
init_agent.scratch.importance_trigger_curr = -1
|
||||
init_agent.reflect()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue