-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
368 lines (281 loc) · 10.5 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import openai
import os, json
from bardapi import Bard
from dotenv import load_dotenv
from guides import *
load_dotenv()
openai.api_key = os.environ["openai_api_key"]
# openai.api_base = "https://chimeragpt.adventblocks.cc/api/v1"
def get_points(topic_sentence, n=5, support=True):
action = "supports" if support else "opposes"
PROMPT_GEN_POINTS = f"""Give {n} very different and important arguments based on an essay written that directly {action} the topic sentence. Do not give explanations. Do not include the introduction or conclusion. Do not introduce yourself. Follow the format of the example output exactly.
Consider potential arguemnts from the following aspects:
- Social
- Political
- Economic
- Cultural
- Religious
- Environmental
- Historical
- Scientific
- Psychological
- Technological
- Ethical
- Legal
- Moral
- Philosophical
EXAMPLE OUTPUT:
- [point]
- [point]
- [point]
...
TOPIC SENTENCE:\n"""
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": PROMPT_GEN_POINTS + topic_sentence},
],
temperature=0,
)
res = response["choices"][0]["message"]["content"].split("- ")
res = list(filter(lambda x: len(x) > 0, res))
res = [x.strip() for x in res]
res = [x if "\n" not in x else x.split("\n")[0].strip() for x in res]
return res
def get_example(topic_sentence, point, old_example="", feedback=""):
web_context = Bard().get_answer(f"""What is one recent examples that support the argument about the topic statement?
ARGUMENT:
{point}
TOPIC STATEMENT:
{topic_sentence}""")['content']
PROMPT_GEN_EXAMPLE = f"""Give a few specific examples with names supporting
the argument for the topic sentence, optionally using additional examples retrieved from the web as context.
Describe only the examples. Do not explain.
ARGUMENT:
{point}
TOPIC SENTENCE:
{topic_sentence}
ADDITIONAL EXAMPLES FROM THE WEB:
{web_context}
Example output:
Example 1: [Description of example 1]
Example 2: [Description of example 2]"""
PROMPT_GEN_EXAMPLE_F = f"""Give a few specific examples with names supporting
the argument for the topic sentence, optionally using additional examples retrieved from the web as context.
Describe only the examples. Do not explain.
ARGUMENT:
{point}
TOPIC SENTENCE:
{topic_sentence}
ADDITIONAL EXAMPLES FROM THE WEB:
{web_context}
Take into account these previous specific examples: {old_example}
Take into account the feedback given for this previous specific examples: {feedback}
Example output:
1. [Description of example 1]
2. [Description of example 2]"""
if feedback.strip() != "" and old_example.strip() != "":
PROMPT_GEN_EXAMPLE = PROMPT_GEN_EXAMPLE_F
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-16k",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": PROMPT_GEN_EXAMPLE},
],
temperature=0,
)
res = response["choices"][0]["message"]["content"]
return res
def get_feedback_point(topic_sentence, point):
PROMPT_FEEDBACK_POINT = f"""Provide feedback on the following main point or argument based on the topic sentence. Suggest an improvement for the main point or argument.
Argument: {point}
Topic sentence: {topic_sentence}"""
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": PROMPT_FEEDBACK_POINT},
],
temperature=0,
)
res = response["choices"][0]["message"]["content"]
return res
def get_feedback_example(topic_sentence, example, point):
PROMPT_FEEDBACK_EXAMPLE = f"""Provide feedback on the following examples supporting the argument about the topic sentence. Suggest better examples to support the argument.
Examples: {example}
Argument: {point}
Topic sentence: {topic_sentence}"""
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": PROMPT_FEEDBACK_EXAMPLE},
],
temperature=0,
)
res = response["choices"][0]["message"]["content"]
return res
def get_paragraph(topic_sentence, point, example, old_para="", feedback=""):
PROMPT_GEN_PARA = f"""Generate a body paragraph for an essay with the title based on the argument and the example. Use concise and complex language.
Closely follow the guide to generate a body paragraph.
GUIDE TO WRITE A BODY PARAGRAPH:
{BODY_GUIDE}
ESSAY TITLE:
{topic_sentence}
ARGUMENT:
{point}
EXAMPLE:
{example}"""
PROMPT_GEN_PARA_F = f"""Generate a body paragraph for an essay with the title based on the argument and the example. Use concise and complex language.
Closely follow the guide to generate a body paragraph.
GUIDE TO WRITE A BODY PARAGRAPH:
{BODY_GUIDE}
Improve on this body paragraph: {old_para}
Take into account the feedback given for this body paragraph: {feedback}
ESSAY TITLE:
{topic_sentence}
ARGUMENT:
{point}
EXAMPLE:
{example}"""
if feedback.strip() != "" and old_para.strip() != "":
PROMPT_GEN_PARA = PROMPT_GEN_PARA_F
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": PROMPT_GEN_PARA},
],
temperature=0,
)
res = response["choices"][0]["message"]["content"]
return res
def get_feedback_paragraph(topic_sentence, paragraph):
PROMPT_FEEDBACK_PARA = f"""Give feedback on how convincing and well-written the body paragraph was, and how well it followed the guide.
GUIDE:
{BODY_GUIDE}
TOPIC SENTENCE:
{topic_sentence}
BODY PARAGRAPH:
{paragraph}"""
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": PROMPT_FEEDBACK_PARA},
],
temperature=0,
)
res = response["choices"][0]["message"]["content"]
return res
def get_conclusion(topic_sentence, main_points, old_conclusion="", feedback=""):
main_points = [f"- {x}" for x in main_points]
points = "\n".join(main_points)
PROMPT_GEN_CONCLUSION = f"""Generate a one paragraph conclusion for an essay based on the title and main points. Use concise and complex language.
Closely follow the guide to generate a one paragraph conclusion.
GUIDE TO WRITE AN CONCLUSION:
{CONCLUSION_GUIDE}
TITLE:
{topic_sentence}
MAIN POINTS:
{points}"""
PROMPT_GEN_CONCLUSION_F = f"""Generate a one paragraph conclusion for an essay based on the title and main points. Use concise and complex language.
Closely follow the guide to generate a one paragraph conclusion.
GUIDE TO WRITE AN CONCLUSION:
{CONCLUSION_GUIDE}
Improve on this conclusion: {old_conclusion}
Take into account the feedback given for this conclusion: {feedback}
TITLE:
{topic_sentence}
MAIN POINTS:
{points}"""
if feedback.strip() != "" and old_conclusion.strip() != "":
PROMPT_GEN_CONCLUSION = PROMPT_GEN_CONCLUSION_F
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": PROMPT_GEN_CONCLUSION},
],
temperature=0,
)
res = response["choices"][0]["message"]["content"]
return res
def get_feedback_conclusion(topic_sentence, conclusion, main_points):
main_points = [f"- {x}" for x in main_points]
points = "\n".join(main_points)
PROMPT_FEEDBACK_CONCLUSION = f"""Give feedback on how convincing and well-written the one paragraph conclusion was, and how well it followed the guide.
GUIDE:
{CONCLUSION_GUIDE}
TOPIC SENTENCE:
{topic_sentence}
CONCLUSION:
{conclusion}
MAIN POINTS:
{points}"""
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": PROMPT_FEEDBACK_CONCLUSION},
],
temperature=0,
)
res = response["choices"][0]["message"]["content"]
return res
def get_introduction(topic_sentence, main_points, old_intro="", feedback=""):
main_points = [f"- {x}" for x in main_points]
points = "\n".join(main_points)
PROMPT_GEN_INTRO = f"""Generate a one paragraph introduction for an essay based on the title and main points, and guide to write an introduction. Use concise and complex language.
Closely follow the guide to generate a one paragraph introduction.
GUIDE TO WRITE AN INTRODUCTION:
{INTRO_GUIDE}
TITLE:
{topic_sentence}
MAIN POINTS:
{points}"""
PROMPT_GEN_INTRO_F = f"""Generate a one paragraph introduction for an essay based on the title and main points, and guide to write an introduction. Use concise and complex language.
Improve on this introduction: {old_intro}
Take into account the feedback given for this introduction: {feedback}
Closely follow the guide to generate a one paragraph introduction.
GUIDE TO WRITE AN INTRODUCTION:
{INTRO_GUIDE}
TITLE:
{topic_sentence}
MAIN POINTS:
{points}"""
if feedback.strip() != "" and old_intro.strip() != "":
PROMPT_GEN_INTRO = PROMPT_GEN_INTRO_F
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-16k",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": PROMPT_GEN_INTRO},
],
temperature=0,
)
res = response["choices"][0]["message"]["content"]
return res
def get_feedback_introduction(topic_sentence, intro, main_points):
main_points = [f"- {x}" for x in main_points]
points = "\n".join(main_points)
PROMPT_FEEDBACK_INTRO = f"""Give feedback on how convincing and well-written the one paragraph introduction was, and how well it followed the guide.
GUIDE:
{INTRO_GUIDE}
TOPIC SENTENCE:
{topic_sentence}
INTRODUCTION:
{intro}
MAIN POINTS:
{points}"""
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-16k",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": PROMPT_FEEDBACK_INTRO},
],
temperature=0,
)
res = response["choices"][0]["message"]["content"]
return res