Dmitry Kirsanov commited on
Commit
fbee5da
·
1 Parent(s): 8c80870

added prompts machinery

Browse files
Files changed (2) hide show
  1. prompts.py +217 -0
  2. understand.py +46 -59
prompts.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ prompts = {
4
+ "intro-structured-writing":
5
+ "To write clearly, it is important to follow certain standards. You are a helpful assistant who can answer questions and make suggestions about the best ways to convey complex concepts in writing.",
6
+
7
+ "intro-information-types":
8
+ """There are five information types in technical writing:
9
+ • reference
10
+ • concept
11
+ • principle
12
+ • process
13
+ • task""",
14
+
15
+ "recognize-information-types":
16
+ """To recognize the information type in a piece of content, identify:
17
+ • the question the content is trying to answer
18
+ • the purpose of the content
19
+ • the intended user response after reading the content
20
+
21
+ Tip: If the information type is unclear, treat it as reference.""",
22
+
23
+ "description-reference":
24
+ """Reference information states things that the reader needs to know. It provides the facts and specific details required for a subject.
25
+
26
+ Use reference information to answer:
27
+ • What are the facts?
28
+ • What does a [something] look like?
29
+ • What are [something’s] parts?""",
30
+
31
+ "titling-reference":
32
+ """Effective titles for reference information describe the item and anything that differentiates it from other items.""",
33
+
34
+ "description-concept":
35
+ """Concept information explains something that the user needs to understand.
36
+
37
+ Use concept information to answer:
38
+ • What is [something]?
39
+ • What does [something] mean?""",
40
+
41
+ "titling-concept":
42
+ """Effective titles for concept topics name the concept using the plural form of the noun, where one exists. The title does not include additional words such as “what is”, “about” or “definition of”.""",
43
+
44
+ "description-principle":
45
+ """Principle information advises the user on what they need to do or not do, and when.
46
+
47
+ Use principle information to answer:
48
+ • What should, or must I do?
49
+ • What should, or must I not do?""",
50
+
51
+ "titling-principle":
52
+ """A title for principle information identifies the principle and expresses the gravity of the principle. The gravity of a principle reflects its seriousness, from a general suggestion to a law.""",
53
+
54
+ "description-process":
55
+ """Process information illustrates how things work using a series of stages or phases.
56
+
57
+ Use process information to answer:
58
+ • What happens after what?
59
+ • Who does what?
60
+ • How does this work?
61
+
62
+ There are two types of process.
63
+ • Human process — What people do collectively to achieve an outcome
64
+ • Systems process — What happens within a system to describe how something works""",
65
+
66
+ "titling-process":
67
+ """Human process titles use the gerund (...ing) form of a verb followed by the plural form of the noun.
68
+
69
+ Systems process titles use the construction “how items work”, using the plural form of the noun, wherever possible. The word "work" is not mandatory. Regardless of the verb used, it must agree with the noun.""",
70
+
71
+ "description-task":
72
+ """Task information instructs the user on the steps to follow to achieve a specific result.
73
+
74
+ Use task information to answer:
75
+ • How do I do it?
76
+ • What do I do?""",
77
+
78
+ "titling-task":
79
+ """A title for task information is written as a command. An effective title for a task topic:
80
+ • states the subject matter covered in the task
81
+ • starts with an imperative (“command”) form of a verb, not its gerund (“...ing”)
82
+ • includes an article (“a”, “an”, or “the”)""",
83
+
84
+
85
+
86
+ "information-types":
87
+ """#intro-information-types
88
+
89
+ #recognize-information-types
90
+
91
+ 1. #description-reference
92
+
93
+ 2. #description-concept
94
+
95
+ 3. #description-principle
96
+
97
+ 4. #description-process
98
+
99
+ 5. #description-task
100
+ """,
101
+
102
+ "information-types-titling-full":
103
+ """#intro-information-types
104
+
105
+ #recognize-information-types
106
+
107
+ 1. #description-reference
108
+
109
+ #titling-reference
110
+
111
+ 2. #description-concept
112
+
113
+ #titling-concept
114
+
115
+ 3. #description-principle
116
+
117
+ #titling-principle
118
+
119
+ 4. #description-process
120
+
121
+ #titling-process
122
+
123
+ 5. #description-task
124
+
125
+ #titling-task""",
126
+
127
+
128
+ "information-types-titling":
129
+ """#intro-information-types
130
+
131
+ 1. #titling-reference
132
+
133
+ 2. #titling-concept
134
+
135
+ 3. #titling-principle
136
+
137
+ 4. #titling-process
138
+
139
+ 5. #titling-task""",
140
+
141
+
142
+
143
+
144
+ "q-chunk-information-type":
145
+ """Please determine the most likely content type of the chunk of content below. First, give one paragraph of your reasoning. Then, give a list of the information types applicable, one per line, in the order of most likely to least likely, so the first line contains the most likely information type.
146
+
147
+ CONTENT:
148
+
149
+ {content}""",
150
+
151
+
152
+ "q-title-chunk":
153
+ """Please suggest the most likely content type of the chunk of content below. The information type of this content is {information_type}. First, give your reasoning as a single paragraph. Then, on a separate line, give just the suggested title for the content, without quotes or prefixes.
154
+
155
+ CONTENT CHUNK:
156
+
157
+ {content}""",
158
+
159
+
160
+
161
+ "q-document-type": """Give a short noun phrase (without article) describing what this document is. Examples: "Textbook about interpersonal psychology", "State standard on oil rig equipment", "User manual for a smartphone app".
162
+
163
+ Write just one line with the description.""",
164
+
165
+
166
+ "fulldocument-intro": """Below is a document inside <document></document>. Please read this document carefully. Remember everything you know about the concepts it touches.""",
167
+ "q-document-summary": """Give a one-paragraph summary of the document's content. Write just one paragraph with the summary.""",
168
+
169
+
170
+ "q-concepts-actors": """Now, read the document 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.
171
+
172
+ 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.
173
+
174
+ For example, for an instruction manual for a chainsaw, you might want to output:
175
+ CONCEPTS:
176
+ - Power tool
177
+ - Safety
178
+ - Work area
179
+ - Saw chain
180
+ - Wood
181
+ ...
182
+ ACTORS:
183
+ - Chainsaw manufacturer
184
+ - Chainsaw user
185
+ - Children and bystanders
186
+ ...
187
+ """,
188
+
189
+
190
+ "": """""",
191
+ "": """""",
192
+ "": """""",
193
+
194
+
195
+
196
+
197
+
198
+
199
+ }
200
+
201
+ def prompt_replacement(matchobj):
202
+ return prompts[matchobj.group(0)[1:]]
203
+
204
+ def get_prompt_recursive(text):
205
+ if not('#' in text):
206
+ return text
207
+ else:
208
+ text = re.sub(r'#[a-z0-9-]+', prompt_replacement, text)
209
+ return get_prompt_recursive(text)
210
+
211
+ def get_prompt(ref):
212
+ text = "#" + ref if not(ref.startswith('#')) else ref
213
+
214
+ return get_prompt_recursive(text)
215
+
216
+ if __name__ == "__main__":
217
+ print(get_prompt("information-types-titling"))
understand.py CHANGED
@@ -4,6 +4,7 @@ from nltk.tokenize import sent_tokenize
4
  import nltk
5
  nltk.download('punkt')
6
 
 
7
  import parse
8
  import llm
9
  from versioneddict import VersionedDict
@@ -43,23 +44,21 @@ class Document():
43
 
44
 
45
  def query_doctype(self):
46
- self.values.put("document type", self.query(
47
- f"""Please read this document carefully. Remember everything you know about the concepts it touches.
48
 
49
- Give a short noun phrase (without article) describing what this document is. Examples: "Textbook about interpersonal psychology", "State standard on oil rig equipment", "User manual for a smartphone app".
50
 
51
- Write just one line with the description.
52
-
53
- <document>{self.values.get("FAD")}</document>"""))
54
 
55
 
56
  def query_summary(self):
57
- self.values.put("summary", self.query(
58
- f"""Below is a document inside <document></document>. It is a {self.values.get("document type")}.
59
 
60
- Read it carefully. Remember everything you know about the concepts it touches.
61
 
62
- Give a one-paragraph summary of the document's content. Write just one paragraph with the summary.
63
 
64
  <document>{self.values.get("FAD")}</document>"""))
65
 
@@ -96,35 +95,18 @@ Give a one-paragraph summary of the document's content. Write just one paragraph
96
  def query_concepts(self):
97
  two_attempts = [{"actors": [], "concepts": []}, {"actors": [], "concepts": []}]
98
 
99
- ret = self.query_n(2,
100
- f"""Below is a document inside <document></document>. It is a {self.values.get("document type")}.
 
 
101
 
102
- Summary of the document:
103
  {self.values.get("summary")}
104
 
105
- Read the entire document carefully. Remember everything you know about the concepts it touches.
106
-
107
- 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.
108
-
109
- 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.
110
-
111
- For example, for an instruction manual for a chainsaw, you might want to output:
112
- CONCEPTS:
113
- - Power tool
114
- - Safety
115
- - Work area
116
- - Saw chain
117
- - Wood
118
- ...
119
- ACTORS:
120
- - Chainsaw manufacturer
121
- - Chainsaw user
122
- - Children and bystanders
123
- ...
124
 
125
  Here's the document:
126
 
127
- <document>{self.values.get("FAD")}</document>""")
128
 
129
  two_attempts[0]["concepts"] = self.parse_lists(ret[0], "concepts")
130
  two_attempts[1]["concepts"] = self.parse_lists(ret[1], "concepts")
@@ -146,10 +128,11 @@ Here's the document:
146
  jackard[what['plural']] = len(set1.intersection(set2)) / len(set1.union(set2))
147
  if set1 != set2:
148
  print(f"===Two attempts of {what['plural']} are different, jaccard similarity: {jackard[what['plural']]}")
149
- ret = self.query(
150
- f"""Below is a document inside <document></document>. It is a {self.values.get("document type")}.
 
 
151
 
152
- Summary of the document:
153
  {self.values.get("summary")}
154
 
155
  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']}.
@@ -169,7 +152,7 @@ Output the line "{what['plural'].upper()}:" followed by your combined list of {w
169
 
170
  Here's the document:
171
 
172
- <document>{self.values.get("FAD")}</document>""")
173
  reconciled[what['plural']] = self.parse_lists(ret, what['plural'])
174
  print(f"===Reconciled {what['plural']}: {reconciled[what['plural']]}")
175
  else:
@@ -185,13 +168,12 @@ Here's the document:
185
 
186
 
187
  def generalize_actors(self):
188
- ret = self.query(
189
- f"""Below is a document inside <document></document>. It is a {self.values.get("document type")}.
 
 
190
 
191
- Summary of the document:
192
  {self.values.get("summary")}
193
-
194
- Read the entire document carefully. Remember everything you know about the concepts it touches.
195
 
196
  A reader of this document has compiled a list of actors mentioned or implied in the document. Here is their list:
197
 
@@ -201,7 +183,7 @@ Your goal is to generalize this list, replacing groups of actors playing the sam
201
 
202
  Output the line "ACTORS:" followed by your list of actors, one per line.
203
 
204
- <document>{self.values.get("FAD")}</document>""")
205
  generalized = self.parse_lists(ret, "actors")
206
  self.values.put("actors, generalized", generalized)
207
 
@@ -212,13 +194,12 @@ Output the line "ACTORS:" followed by your list of actors, one per line.
212
 
213
  print(f"=== Sentence markup: {markup}")
214
 
215
- ret = self.query_n(2,
216
- f"""Below is a document inside <document></document>. It is a {self.values.get("document type")}.
 
 
217
 
218
- Summary of the document:
219
  {self.values.get("summary")}
220
-
221
- Read the entire document carefully. Remember everything you know about the concepts it touches.
222
 
223
  A reader of this document has compiled a list of actors (parties) mentioned or implied in the document. Here is their list:
224
 
@@ -234,7 +215,7 @@ Add the relevant partes, separated by commas, inside the "audience" attribute of
234
 
235
  Output just the document with the audience markup sentence, but otherwise unchanged:
236
 
237
- <document>{markup}</document>""")
238
  annotated0, annotated1 = self.parse_document(ret[0]), self.parse_document(ret[1])
239
  diff = newline.join([str(x) for x in zip(annotated0.split('</sentence>'), annotated1.split('</sentence>')) if x[0] != x[1]])
240
  self.values.put("FAD with audience", annotated1)
@@ -243,26 +224,32 @@ Output just the document with the audience markup sentence, but otherwise unchan
243
  def split_document(self):
244
  audience = self.values.get("actors, generalized")[0]
245
  derivative_string = f"<document>{self.values.get('FAD with audience')}</document>"
 
 
246
  derivative = newline.join(etree.fromstring(derivative_string).xpath(f".//sentence[contains(@audience,'{audience}')]/text()"))
247
- ret = self.query(
248
- f"""Below is a document inside <document>...</document>. It is a {self.values.get("document type")}.
249
 
250
- Summary of the document:
251
- {self.values.get("summary")}
252
-
253
- Read the entire document carefully. Remember everything you know about the concepts it touches:
254
 
255
- <document>{self.values.get("FAD")}</document>
256
 
257
- An editor was tasked with creative a derivative document that is addressed to {audience}. The editor compiled all sentences from the document that have information relevant for {audience}. These sentences are dumped below the main document inside <derivative-document for="{audience}">...</derivative-document>:
258
 
259
- <derivative-document for="{audience}">{derivative}</derivative-document>
260
 
261
  Your goal is to edit the derivative document for structure and consistency. Rearrange its sentences in a way that makes sense for a reader. Group sentences into paragraphs. If necessary, write, or adapt from the original document, connecting sentences. Then, remove as many instances of "{audience}" as you can: since this entire document is now addressed to {audience}, you can just say "you" most of the time.
262
 
263
  Output just your revision inside <derivative-document>...</derivative-document>.
264
 
265
- """)
 
 
 
 
 
 
 
 
266
  self.values.put(f"""FAD for {self.values.get("actors, generalized")[0]}""", self.parse_document(ret))
267
 
268
 
 
4
  import nltk
5
  nltk.download('punkt')
6
 
7
+ import prompts
8
  import parse
9
  import llm
10
  from versioneddict import VersionedDict
 
44
 
45
 
46
  def query_doctype(self):
47
+ self.values.put("document type", self.query(prompts.get_prompt_recursive(
48
+ f"""#fulldocument-intro
49
 
50
+ #q-document-type
51
 
52
+ <document>{self.values.get("FAD")}</document>""")))
 
 
53
 
54
 
55
  def query_summary(self):
56
+ self.values.put("summary", self.query(prompts.get_prompt_recursive(
57
+ f"""#fulldocument-intro
58
 
59
+ This document is a {self.values.get("document type")}.
60
 
61
+ #q-document-summary
62
 
63
  <document>{self.values.get("FAD")}</document>"""))
64
 
 
95
  def query_concepts(self):
96
  two_attempts = [{"actors": [], "concepts": []}, {"actors": [], "concepts": []}]
97
 
98
+ ret = self.query_n(2, prompts.get_prompt_recursive(
99
+ f"""#fulldocument-intro
100
+
101
+ This document is a {self.values.get("document type")}. Here's a summary of the document:
102
 
 
103
  {self.values.get("summary")}
104
 
105
+ #q-concepts-actors
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  Here's the document:
108
 
109
+ <document>{self.values.get("FAD")}</document>"""))
110
 
111
  two_attempts[0]["concepts"] = self.parse_lists(ret[0], "concepts")
112
  two_attempts[1]["concepts"] = self.parse_lists(ret[1], "concepts")
 
128
  jackard[what['plural']] = len(set1.intersection(set2)) / len(set1.union(set2))
129
  if set1 != set2:
130
  print(f"===Two attempts of {what['plural']} are different, jaccard similarity: {jackard[what['plural']]}")
131
+ ret = self.query(prompts.get_prompt_recursive(
132
+ f"""#fulldocument-intro
133
+
134
+ This document is a {self.values.get("document type")}. Here's a summary of the document:
135
 
 
136
  {self.values.get("summary")}
137
 
138
  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']}.
 
152
 
153
  Here's the document:
154
 
155
+ <document>{self.values.get("FAD")}</document>"""))
156
  reconciled[what['plural']] = self.parse_lists(ret, what['plural'])
157
  print(f"===Reconciled {what['plural']}: {reconciled[what['plural']]}")
158
  else:
 
168
 
169
 
170
  def generalize_actors(self):
171
+ ret = self.query(prompts.get_prompt_recursive(
172
+ f"""#fulldocument-intro
173
+
174
+ This document is a {self.values.get("document type")}. Here's a summary of the document:
175
 
 
176
  {self.values.get("summary")}
 
 
177
 
178
  A reader of this document has compiled a list of actors mentioned or implied in the document. Here is their list:
179
 
 
183
 
184
  Output the line "ACTORS:" followed by your list of actors, one per line.
185
 
186
+ <document>{self.values.get("FAD")}</document>"""))
187
  generalized = self.parse_lists(ret, "actors")
188
  self.values.put("actors, generalized", generalized)
189
 
 
194
 
195
  print(f"=== Sentence markup: {markup}")
196
 
197
+ ret = self.query_n(2, prompts.get_prompt_recursive(
198
+ f"""#fulldocument-intro
199
+
200
+ This document is a {self.values.get("document type")}. Here's a summary of the document:
201
 
 
202
  {self.values.get("summary")}
 
 
203
 
204
  A reader of this document has compiled a list of actors (parties) mentioned or implied in the document. Here is their list:
205
 
 
215
 
216
  Output just the document with the audience markup sentence, but otherwise unchanged:
217
 
218
+ <document>{markup}</document>"""))
219
  annotated0, annotated1 = self.parse_document(ret[0]), self.parse_document(ret[1])
220
  diff = newline.join([str(x) for x in zip(annotated0.split('</sentence>'), annotated1.split('</sentence>')) if x[0] != x[1]])
221
  self.values.put("FAD with audience", annotated1)
 
224
  def split_document(self):
225
  audience = self.values.get("actors, generalized")[0]
226
  derivative_string = f"<document>{self.values.get('FAD with audience')}</document>"
227
+ sentences = re.findall(f'.*?(?=\</sentence>|$)', self.values.get('FAD with audience'))
228
+
229
  derivative = newline.join(etree.fromstring(derivative_string).xpath(f".//sentence[contains(@audience,'{audience}')]/text()"))
230
+ ret = self.query(prompts.get_prompt_recursive(
231
+ f"""#fulldocument-intro
232
 
233
+ This document is a {self.values.get("document type")}. Here's a summary of the document:
 
 
 
234
 
235
+ {self.values.get("summary")}
236
 
 
237
 
238
+ An editor was tasked with creating a derivative document that is addressed to {audience}. The editor compiled all sentences from the document that have information relevant for {audience}. These sentences are dumped below the main document inside <derivative-document for="{audience}">...</derivative-document>.
239
 
240
  Your goal is to edit the derivative document for structure and consistency. Rearrange its sentences in a way that makes sense for a reader. Group sentences into paragraphs. If necessary, write, or adapt from the original document, connecting sentences. Then, remove as many instances of "{audience}" as you can: since this entire document is now addressed to {audience}, you can just say "you" most of the time.
241
 
242
  Output just your revision inside <derivative-document>...</derivative-document>.
243
 
244
+ Here's the original document:
245
+
246
+ <document>{self.values.get("FAD")}</document>
247
+
248
+ Here's the derivative document:
249
+
250
+ <derivative-document for="{audience}">{derivative}</derivative-document>
251
+
252
+ """))
253
  self.values.put(f"""FAD for {self.values.get("actors, generalized")[0]}""", self.parse_document(ret))
254
 
255