Dmitry Kirsanov commited on
Commit
bf23f98
·
1 Parent(s): b89a17d

reconciling two lists

Browse files
Files changed (1) hide show
  1. understand.py +78 -22
understand.py CHANGED
@@ -11,6 +11,10 @@ class Document():
11
  def query(self, s):
12
  try:
13
  answer, truncated = self.LLM.send_question(s)
 
 
 
 
14
  return answer.strip()
15
  except Exception as e:
16
  print (e.message, e.args)
@@ -27,6 +31,7 @@ Write just one line with the description.
27
 
28
  <document>{self.values.get("FAD")}</document>"""))
29
 
 
30
  def query_summary(self):
31
  self.values.put("summary", self.query(
32
  f"""Below is a document inside <document></document>. It is a {self.values.get("document type")}.
@@ -37,8 +42,36 @@ Give a one-paragraph summary of the document's content. Write just one paragraph
37
 
38
  <document>{self.values.get("FAD")}</document>"""))
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def query_concepts(self):
41
- ret = self.query(
 
 
 
42
  f"""Below is a document inside <document></document>. It is a {self.values.get("document type")}.
43
 
44
  Summary of the document:
@@ -48,7 +81,7 @@ Read the entire document carefully. Remember everything you know about the conce
48
 
49
  Now, read it again, and compile a list of the main concepts that this document talks about. Output the line "CONCEPTS:" followed by a list of concepts, one per line.
50
 
51
- Then, read it one more time, and think about who are the actors implied. Who are the parties that this document is authored by, is governed by, is addressed to? Often, these parties are not explicitly stated in the document, so you will need to use your common sense. See if you can compile a minimal list of the main actors that the document is concerned with. Output the line "ACTORS:" followed by a list of concepts, one per line.
52
 
53
  For example, for an instruction manual for a chainsaw, you might want to output:
54
  CONCEPTS:
@@ -64,27 +97,50 @@ ACTORS:
64
  - Children and bystanders
65
  ...
66
 
 
 
67
  <document>{self.values.get("FAD")}</document>""")
68
-
69
- self.values.put("concepts", [])
70
- self.values.put("actors", [])
71
-
72
- lines = ret.splitlines()
73
- current_list = None
74
-
75
- for line in lines:
76
- line = line.strip()
77
- if line.strip() == "CONCEPTS:":
78
- current_list = "concepts"
79
- elif line.strip() == "ACTORS:":
80
- current_list = "actors"
81
- elif current_list is not None:
82
- if line.startswith("- "):
83
- item = line[2:].strip()
84
- else:
85
- item = line.strip()
86
- self.values.get(current_list).append(item)
87
- self.values.touch(current_list)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
 
90
  def ingest(self, filename):
 
11
  def query(self, s):
12
  try:
13
  answer, truncated = self.LLM.send_question(s)
14
+ # log
15
+ print(f"===Question: \"\"\"{s}\"\"\"")
16
+ print(f"===Answer: \"\"\"{answer}\"\"\"")
17
+ print(f"===Truncated? {truncated}")
18
  return answer.strip()
19
  except Exception as e:
20
  print (e.message, e.args)
 
31
 
32
  <document>{self.values.get("FAD")}</document>"""))
33
 
34
+
35
  def query_summary(self):
36
  self.values.put("summary", self.query(
37
  f"""Below is a document inside <document></document>. It is a {self.values.get("document type")}.
 
42
 
43
  <document>{self.values.get("FAD")}</document>"""))
44
 
45
+
46
+ def parse_lists(self, ret, what, whatlist):
47
+
48
+ lines = ret.splitlines()
49
+ inlist = False
50
+
51
+ for line in lines:
52
+ line = line.strip()
53
+ if line.strip()[-1] == ":":
54
+ if line.strip().upper() == what.upper():
55
+ inlist = True
56
+ else:
57
+ inlist = False
58
+ elif line.strip() == "":
59
+ inlist = False
60
+ elif inlist:
61
+ if line.startswith("- "):
62
+ item = line[2:].strip()
63
+ else:
64
+ item = line.strip()
65
+ whatlist.append(item)
66
+
67
+ print(f"===Parsed list {what}: {whatlist}")
68
+
69
+
70
  def query_concepts(self):
71
+ two_attempts = [{"actors": [], "concepts": []}, {"actors": [], "concepts": []}]
72
+
73
+ for attempt in two_attempts:
74
+ ret = self.query(
75
  f"""Below is a document inside <document></document>. It is a {self.values.get("document type")}.
76
 
77
  Summary of the document:
 
81
 
82
  Now, read it again, and compile a list of the main concepts that this document talks about. Output the line "CONCEPTS:" followed by a list of concepts, one per line.
83
 
84
+ Then, read it one more time, and think about who are the actors implied in the document. Who are the parties that this document is authored by, is governed by, is addressed to? Often, these parties are not explicitly stated in the document, so you will need to use your common sense. See if you can compile a minimal list of the main actors that the document is concerned with. Output the line "ACTORS:" followed by a list of actors, one per line.
85
 
86
  For example, for an instruction manual for a chainsaw, you might want to output:
87
  CONCEPTS:
 
97
  - Children and bystanders
98
  ...
99
 
100
+ Here's the document:
101
+
102
  <document>{self.values.get("FAD")}</document>""")
103
+
104
+ self.parse_lists(ret, "concepts", attempt["concepts"])
105
+ self.parse_lists(ret, "actors", attempt["actors"])
106
+
107
+ #reconcile two lists
108
+ for what in [
109
+ {"plural": "concepts", "singular": "concept", "full": "concepts that this document talks about"},
110
+ {"plural": "actors", "singular": "actor", "full": "actors described or implied in the document"}
111
+ ]:
112
+
113
+ if set(two_attempts[0][what["plural"]]) != set(two_attempts[1][what["plural"]]):
114
+ ret = self.query(
115
+ f"""Below is a document inside <document></document>. It is a {self.values.get("document type")}.
116
+
117
+ Summary of the document:
118
+ {self.values.get("summary")}
119
+
120
+ Two different readers have read this document and created different lists of the {what["full"]}. Your goal is to read these lists and the entire document carefully and then create a new combined list of the document's {what["plural"]}.
121
+
122
+ Compare the two lists:
123
+ - If they have the same {what["singular"]} but expressed differently, add a better expression of that {what["singular"]} to your final list.
124
+ - If any of the {what["plural"]} in these lists is actually irrelevant or confusing, drop it.
125
+ - If you see some {what["singular"]} in the document that both lists missed, add it.
126
+
127
+ First list of {what["plural"]}:
128
+ {"\n".join(["- " + x for x in two_attempts[0]["concepts"]])}
129
+
130
+ Second list of {what["plural"]}:
131
+ {"\n".join(["- " + x for x in two_attempts[1]["concepts"]])}
132
+
133
+ Output the line "{what["plural"].upper()}:" followed by your combined list of {what["plural"]}, one per line.
134
+
135
+ Here's the document:
136
+
137
+ <document>{self.values.get("FAD")}</document>""")
138
+ self.parse_lists(ret, what["plural"], two_attempts[0][what["plural"]])
139
+ print(f"===Reconciled {what["plural"]}: {two_attempts[0][what["plural"]]}")
140
+
141
+
142
+ self.values.put("concepts", two_attempts[0]["concepts"])
143
+ self.values.put("actors", two_attempts[0]["actors"])
144
 
145
 
146
  def ingest(self, filename):