-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAURA_functions.py
179 lines (137 loc) · 5.83 KB
/
AURA_functions.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
import csv
import openai
from gingerit.gingerit import GingerIt
import os
import pandas
# Specify data sources
PI_data = 'PI_data.csv'
baseline_database = 'baseline_database.csv'
def read_key():
with open('key.txt') as file:
key = file.read()
return key
def send_prompt_davinci(prompt, key):
openai.api_key = key
model_engine = "text-davinci-003"
completion = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5
)
response = completion.choices[0].text
# print("\nPrompt to OpenAI server: " + prompt)
print(response)
def send_prompt_gpt35turbo_ref(conversation, key):
import openai
openai.api_key = key
model_id = 'gpt-3.5-turbo'
def ChatGPT_conversation(conversation):
response = openai.ChatCompletion.create(
model=model_id,
messages=conversation
)
# api_usage = response['usage']
# print('Total token consumed: {0}'.format(api_usage['total_tokens']))
# stop means complete
# print(response['choices'][0].finish_reason)
# print(response['choices'][0].index)
conversation.append({'role': response.choices[0].message.role, 'content': response.choices[0].message.content})
return conversation
conversation = []
conversation.append({'role': 'system', 'content': 'How may I help you?'})
conversation = ChatGPT_conversation(conversation)
print('{0}: {1}\n'.format(conversation[-1]['role'].strip(), conversation[-1]['content'].strip()))
while True:
prompt = input('User:')
conversation.append({'role': 'user', 'content': prompt})
conversation = ChatGPT_conversation(conversation)
print('{0}: {1}\n'.format(conversation[-1]['role'].strip(), conversation[-1]['content'].strip()))
def send_prompt_gpt35turbo(prompt, key):
# import openai
openai.api_key = key
model_id = 'gpt-3.5-turbo'
def ChatGPT_conversation(conversation):
response = openai.ChatCompletion.create(
model=model_id,
messages=conversation
)
# api_usage = response['usage']
# print('Total token consumed: {0}'.format(api_usage['total_tokens']))
# stop means complete
# print(response['choices'][0].finish_reason)
# print(response['choices'][0].index)
conversation.append({'role': response.choices[0].message.role, 'content': response.choices[0].message.content})
return conversation
conversation = []
conversation.append({'role': 'system', 'content': prompt})
conversation = ChatGPT_conversation(conversation)
print('{0}: {1}\n'.format(conversation[-1]['role'].strip(), conversation[-1]['content'].strip()))
def extract_equipment():
with open(PI_data) as file:
input_data = csv.DictReader(file) # Read PI data
equipment_list = []
# Generate list of equipment
for row in input_data:
equipment = row['equipment']
if equipment not in equipment_list:
equipment_list.append(equipment)
return equipment_list
def generate_abnormalities_sentence(abnormality_list, abnormality_count, prompt):
if abnormality_count == 1: # If only 1 issue detected
prompt = prompt + ' ' + abnormality_list[0]
elif abnormality_count == 2: # If 2 issues detected
prompt = prompt + ' ' + abnormality_list[0] + ' and ' + abnormality_list[1]
else: # If more than 2 issues detected
for issues in abnormality_list:
if issues == abnormality_list[-1]: # When reached the end of the list
prompt = prompt + ' and ' + issues
elif issues == abnormality_list[0]: # For the first issue in the list
prompt = prompt + ' ' + issues
else:
prompt = prompt + ', ' + issues
return prompt
# Input actual data for an equipment and output result whether the actual data is high, low or normal
def determine_attribute_status(equipment, attribute, actual_data):
# set default attribute status as normal
attribute_status = 'normal'
with open(baseline_database) as file:
database = csv.DictReader(file) # Generate dictionary from csv file
for row in database:
if equipment == row['equipment'] and attribute == row['attribute']:
if actual_data < int(row['baseline_min']):
attribute_status = 'low'
elif actual_data > int(row['baseline_max']):
attribute_status = 'high'
return attribute_status
def grammar_correction(prompt):
parser = GingerIt()
return parser.parse(prompt)
def generate_single_prompt(equipment):
# initialize abnormality count to 0
abnormality_count = 0
with open(PI_data) as file:
input_data = csv.DictReader(file) # Read PI data
abnormality_list = []
# equipment_list = extract_equipment()
# Initialize prompt
prompt = f'List possible causes and solutions for power plant {equipment} experiencing'
for row in input_data:
attribute = row['attribute']
actual_data = int(row['actual_data'])
status = determine_attribute_status(equipment, attribute, actual_data)
if status != 'normal':
abnormality_list.append(f'{status} {attribute}')
abnormality_count = abnormality_count + 1
# combine initialized prompt with discovered issue(s)
return generate_abnormalities_sentence(abnormality_list, abnormality_count, prompt)
def generate_multi_prompt():
prompt_list = []
equipment_list = extract_equipment();
# print(equipment_list)
for equipment in equipment_list:
prompt = generate_single_prompt(equipment)
prompt_list.append(prompt)
return prompt_list