-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (118 loc) · 6.21 KB
/
main.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
import os
import openai
from dotenv import load_dotenv
from colorama import Fore, Back, Style
# load values from the .env file if it exists
load_dotenv()
# configure OpenAI
openai.api_key = os.getenv("OPENAI_API_KEY")
INSTRUCTIONS = """
You are an AI assistant that is an english Literature expert.
You know about litery techniques used in extracts, their effect on the readers and why a writer has used them. You can find these techniques in an extract and create me paragraph answers based on this framework:
Point: that is the starter sentence i give you that you must support and conclude
Evidence: find a quote in the extract I give you that supports the point - only use small section of the extract and not the whole thing.
Technique used: find what litery techniues are used in the quote you use that supports the point: do not use basic techniques such as 'descriptive language', specify the actual technique used.
Explain why the writer has used the technique: why does the writer uses this technique in the extract - and how does it support the point.
The effect of the technique on the user: hows does the given quote make the reader feel.
Find words used in the evidence you chose: what do those words mean and what do they connote to.
link back: conclude your answer with how the point is correct .
DO not use any external URls in your answer. Do not refer to any blogs in your answers.
Adjectives can not be personified.
When using contrast as a technique all of the parts in which the two oposing ides are found must be quoted.
When writing the paragraph, use the framework, but do not directly use it.
When writing the point, use something like: "The writer shows (your point)".
When using writing the evidence, write something like: "This can be seen in the quote (your quote)".
Descriptive language is a technique.
A word longer than a single word, cannot be a word.
When quoting direct words, state there type, between: nouns, adjectives, verbs and adverbs
Personification can only be a technique when a non-human thing is given a human trait.
A simile can only be a technique when two things are compared - comparing on thing - with a thing of a different type - using 'like' or 'as'.
Personification cannot give a non-human trait to a character.
"""
ANSWER_SEQUENCE = "\nAI:"
QUESTION_SEQUENCE = "\nHuman: "
TEMPERATURE = 0.5
MAX_TOKENS = 500
FREQUENCY_PENALTY = 0
PRESENCE_PENALTY = 0.6
# limits how many questions we include in the prompt
MAX_CONTEXT_QUESTIONS = 10
def get_response(prompt):
"""
Get a response from the model using the prompt
Parameters:
prompt (str): The prompt to use to generate the response
Returns the response from the model
"""
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=TEMPERATURE,
max_tokens=MAX_TOKENS,
top_p=1,
frequency_penalty=FREQUENCY_PENALTY,
presence_penalty=PRESENCE_PENALTY,
)
return response.choices[0].text
def get_moderation(question):
"""
Check the question is safe to ask the model
Parameters:
question (str): The question to check
Returns a list of errors if the question is not safe, otherwise returns None
"""
errors = {
"hate": "Content that expresses, incites, or promotes hate based on race, gender, ethnicity, religion, nationality, sexual orientation, disability status, or caste.",
"hate/threatening": "Hateful content that also includes violence or serious harm towards the targeted group.",
"self-harm": "Content that promotes, encourages, or depicts acts of self-harm, such as suicide, cutting, and eating disorders.",
"sexual": "Content meant to arouse sexual excitement, such as the description of sexual activity, or that promotes sexual services (excluding sex education and wellness).",
"sexual/minors": "Sexual content that includes an individual who is under 18 years old.",
"violence": "Content that promotes or glorifies violence or celebrates the suffering or humiliation of others.",
"violence/graphic": "Violent content that depicts death, violence, or serious physical injury in extreme graphic detail.",
}
response = openai.Moderation.create(input=question)
if response.results[0].flagged:
# get the categories that are flagged and generate a message
result = [
error
for category, error in errors.items()
if response.results[0].categories[category]
]
return result
return None
def main():
os.system("cls" if os.name == "nt" else "clear")
# keep track of previous questions and answers
previous_questions_and_answers = []
while True:
# ask the user for their question
new_question = input(
Fore.GREEN + Style.BRIGHT + "What can I get you?: " + Style.RESET_ALL
)
# check the question is safe
errors = get_moderation(new_question)
if errors:
print(
Fore.RED
+ Style.BRIGHT
+ "Sorry, you're question didn't pass the moderation check:"
)
for error in errors:
print(error)
print(Style.RESET_ALL)
continue
# build the previous questions and answers into the prompt
# use the last MAX_CONTEXT_QUESTIONS questions
context = ""
for question, answer in previous_questions_and_answers[-MAX_CONTEXT_QUESTIONS:]:
context += QUESTION_SEQUENCE + question + ANSWER_SEQUENCE + answer
# add the new question to the end of the context
context += QUESTION_SEQUENCE + new_question + ANSWER_SEQUENCE
# get the response from the model using the instructions and the context
response = get_response(INSTRUCTIONS + context)
# add the new question and answer to the list of previous questions and answers
previous_questions_and_answers.append((new_question, response))
# print the response
print(Fore.CYAN + Style.BRIGHT + "Here you go: " + Style.NORMAL + response)
if __name__ == "__main__":
main()