From e40e3bacc56c4b42f279372677d3978e7733e645 Mon Sep 17 00:00:00 2001
From: didi <2020201387@ruc.edu.cn>
Date: Fri, 6 Oct 2023 01:44:45 +0800
Subject: [PATCH] =?UTF-8?q?Reflect=E6=B5=8B=E8=AF=95=E7=AC=AC=E4=B8=80?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
1. 能通过
2. insight不稳定
---
.../st_game/actions/run_reflect_action.py | 63 ++++++++++++-------
.../prompts/insight_and_evidence_v1.txt | 2 +-
examples/st_game/reflect/reflect.py | 27 ++++----
examples/st_game/tests/test_reflect.py | 25 +++++---
4 files changed, 74 insertions(+), 43 deletions(-)
diff --git a/examples/st_game/actions/run_reflect_action.py b/examples/st_game/actions/run_reflect_action.py
index 9c688421c..10e22dbcf 100644
--- a/examples/st_game/actions/run_reflect_action.py
+++ b/examples/st_game/actions/run_reflect_action.py
@@ -69,16 +69,20 @@ class AgentInsightAndGuidance(STAction):
return False
def _func_cleanup(self, llm_resp: str, prompt: str = "") -> dict:
- llm_resp = "1. " + llm_resp.strip()
- ret = dict()
- for i in llm_resp.split("\n"):
- row = i.split(". ")[-1]
- thought = row.split("(because of ")[0].strip()
- evi_raw = row.split("(because of ")[1].split(")")[0].strip()
- evi_raw = re.findall(r'\d+', evi_raw)
- evi_raw = [int(i.strip()) for i in evi_raw]
- ret[thought] = evi_raw
- return ret
+ 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"):
+ row = i.split(". ")[-1]
+ thought = row.split("(because of ")[0].strip()
+ evi_raw = row.split("(because of ")[1].split(")")[0].strip()
+ evi_raw = re.findall(r'\d+', evi_raw)
+ evi_raw = [int(i.strip()) for i in evi_raw]
+ ret[thought] = evi_raw
+ return ret
+ except Exception as exp:
+ logger.error(f"AGent Insight 报错{exp}")
def _func_fail_default_resp(self) -> str:
pass
@@ -112,9 +116,15 @@ class AgentEventTriple(STAction):
return True
def _func_cleanup(self, llm_resp: str, prompt: str = "") -> list:
- cr = llm_resp.strip()
- cr = [i.strip() for i in cr.split(")")[0].split(",")]
- return cr
+ try:
+ cr = llm_resp.strip()
+ 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}")
def _func_fail_default_resp(self) -> str:
pass
@@ -152,9 +162,11 @@ class AgentEventPoignancy(STAction):
return False
def _func_cleanup(self, llm_resp: str, prompt: str = "") -> int:
- llm_resp = int(llm_resp.strip())
- return llm_resp
-
+ try:
+ llm_resp = int(llm_resp.strip())
+ return llm_resp
+ except Exception as exp:
+ logger.error(f"AGent Event poignancy 报错{exp}")
def _func_fail_default_resp(self) -> str:
pass
@@ -193,9 +205,11 @@ class AgentChatPoignancy(STAction):
return False
def _func_cleanup(self, llm_resp: str, prompt: str = "") -> int:
- llm_resp = int(llm_resp.strip())
- return llm_resp
-
+ try:
+ llm_resp = int(llm_resp.strip())
+ return llm_resp
+ except Exception as exp:
+ logger.error(f"AGent Chat poignancy 报错{exp}")
def _func_fail_default_resp(self) -> str:
pass
@@ -234,7 +248,10 @@ class AgentPlanThoughtOnConvo(STAction):
return False
def _func_cleanup(self, llm_resp: str, prompt: str = "") -> str:
- return llm_resp.split('"')[0].strip()
+ try:
+ return llm_resp.split('"')[0].strip()
+ except Exception as exp:
+ logger.error(f"AGent PlanThought 报错{exp}")
def _func_fail_default_resp(self) -> str:
pass
@@ -270,8 +287,10 @@ class AgentMemoryOnConvo(STAction):
return False
def _func_cleanup(self, llm_resp: str, prompt: str = "") -> str:
- return llm_resp.split('"')[0].strip()
-
+ try:
+ return llm_resp.split('"')[0].strip()
+ except Exception as exp:
+ logger.error(f"AGent MemoryOnconvo 报错{exp}")
def _func_fail_default_resp(self) -> str:
pass
diff --git a/examples/st_game/prompts/insight_and_evidence_v1.txt b/examples/st_game/prompts/insight_and_evidence_v1.txt
index 579c81637..35671023b 100644
--- a/examples/st_game/prompts/insight_and_evidence_v1.txt
+++ b/examples/st_game/prompts/insight_and_evidence_v1.txt
@@ -8,5 +8,5 @@ Variables:
Input:
!!
-What !! high-level insights can you infer from the above statements? (example format: insight (because of 1, 5, 3))
+What !! high-level insights can you infer from the above statements? Please ensure it includes 'because of' and generates according to the example format.(example format: insight (because of 1, 5, 3)) .
1.
\ No newline at end of file
diff --git a/examples/st_game/reflect/reflect.py b/examples/st_game/reflect/reflect.py
index 761f3d943..5423e52bf 100644
--- a/examples/st_game/reflect/reflect.py
+++ b/examples/st_game/reflect/reflect.py
@@ -37,12 +37,14 @@ def generate_insights_and_evidence(role, nodes, n=5):
ret = run_insight_and_guidance.run(role, statements, n)
logger.info(ret)
+
try:
for thought, evi_raw in ret.items():
- evidence_node_id = [nodes[i].node_id for i in evi_raw]
+ evidence_node_id = [nodes[i].memory_id for i in evi_raw]
ret[thought] = evidence_node_id
return ret
- except:
+ except Exception as exp:
+ logger.info(f"insight处理错误为{exp}")
return {"this is blank": "node_1"}
@@ -57,6 +59,7 @@ 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
@@ -68,11 +71,11 @@ def generate_poig_score(role: "STRole", event_type, description):
if event_type == "event" or event_type == "thought":
run_event_poignancy = AgentEventPoignancy()
- return run_event_poignancy.run(role, description)[0]
+ return run_event_poignancy.run(role, description)
elif event_type == "chat":
run_chat_poignancy = AgentChatPoignancy()
return run_chat_poignancy.run(role,
- role.scratch.act_description)[0]
+ role.scratch.act_description)
def generate_planning_thought_on_convo(role, all_utt):
@@ -98,24 +101,24 @@ def run_reflect(role: "STRole"):
"""
# Reflection requires certain focal points. Generate that first.
focal_points = generate_focal_points(role, 3)
- # Retrieve the relevant Nodes object for each of the focal points.
+ # Retrieve the relevant Nodesobject for each of the focal points.
# has keys of focal points, and values of the associated Nodes.
retrieved = role.retrieve(focal_points)
# For each of the focal points, generate thoughts and save it in the
# agent's memory.
for focal_pt, nodes in retrieved.items():
-
- xx = [i.embedding_key for i in nodes]
- for xxx in xx: logger.info(xxx)
+ logger.info(f"检索结果为{nodes}")
+ # xx = [i.embedding_key for i in nodes]
+ # for xxx in xx: logger.info(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)
- keywords = {[s, p, o]}
+ 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))
@@ -193,7 +196,7 @@ def role_reflect(role: "STRole"):
created = role.scratch.curr_time
expiration = created + datetime.timedelta(days=30)
s, p, o = generate_action_event_triple(planning_thought, role)
- keywords = {[s, p, o]}
+ keywords = set([s, p, o])
thought_poignancy = generate_poig_score(role, "thought", planning_thought)
thought_embedding_pair = (planning_thought, get_embedding(planning_thought))
@@ -208,7 +211,7 @@ def role_reflect(role: "STRole"):
created = role.scratch.curr_time
expiration = created + datetime.timedelta(days=30)
s, p, o = generate_action_event_triple(memo_thought, role)
- keywords = {[s, p, o]}
+ keywords = set([s, p, o])
thought_poignancy = generate_poig_score(role, "thought", memo_thought)
thought_embedding_pair = (memo_thought, get_embedding(memo_thought))
diff --git a/examples/st_game/tests/test_reflect.py b/examples/st_game/tests/test_reflect.py
index 43139de4b..1593d9aaa 100644
--- a/examples/st_game/tests/test_reflect.py
+++ b/examples/st_game/tests/test_reflect.py
@@ -10,32 +10,41 @@ class TestReflectFunction:
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')
- logger.info(role.scratch.name)
- logger.info(f"记忆长度为{len(role.memory.storage)}")
return role
def test_function_focus_and_insight_action(self,init_agent):
"""
单个Action测试样例
"""
- run_focus = AgentFocusPt()
- statements = "" # 这个statements 与 n 设置是遵循reflect里面实际设置# 来的,你写的时候可以对应代码看一下
- run_focus.run(init_agent, statements, n=3)
+ logger.info(f"{__name__}函数启动")
+ # run_focus = AgentFocusPt()
+ # statements = "" # 这个statements 与 n 设置是遵循reflect里面实际设置# 来的,你写的时候可以对应代码看一下
+ # out_put = run_focus.run(init_agent, statements, n=3)
- run_insight = AgentInsightAndGuidance()
- # 这里主要需要查看,Looger.info(filling)
- # 完善代码
+ """
+ 这里有通过测试的结果,但是更多时候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)
def test_event_triple_action(self,init_agent):
"""
测试tripleAgent Action
+ 我们需要限制生成字数在15之内,生成字数没有限制的时候很容易跑通
+ Prompt同样存在问题,但是我做了处理
"""
+ run_triple = AgentEventTriple()
+ statements = "(Klaus Mueller is academically inclined)"
+ run_triple.run(statements,init_agent)
pass
def test_poignancy_action(self,init_agent):
"""
测试两个关于poignancy的Action
"""
+
pass
def test_convo_action(self,init_agent):