Skip to content

Commit 961ffa0

Browse files
authored
Update compute_health_score.py
1 parent feaf755 commit 961ffa0

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

compute_health_score.py

+39-23
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010
HumanMessagePromptTemplate,
1111
SystemMessagePromptTemplate,
1212
)
13+
from Hopfield import retrieval_info
1314

1415

15-
def answer_from_gpt(ques, context):
16+
def answer_from_gpt(ques, context, work):
17+
1618
storage_context = StorageContext.from_defaults(persist_dir='./storage')
1719
index = load_index_from_storage(storage_context, index_id="index_health")
1820
list_score = []
1921

2022
t = 0
2123
for i in ques:
22-
my_context = context
24+
my_context = context + work[t]
2325
QA_PROMPT = get_systemprompt_template(my_context)
2426
query_engine = index.as_query_engine(text_qa_template=QA_PROMPT)
2527
response = query_engine.query(i)
@@ -32,21 +34,23 @@ def answer_from_gpt(ques, context):
3234
return list_score
3335

3436

37+
3538
def get_systemprompt_template(exist_context):
39+
3640
chat_text_qa_msgs = [
3741
SystemMessagePromptTemplate.from_template(
3842
exist_context
3943
),
4044
HumanMessagePromptTemplate.from_template(
41-
"Give the answer in jason format with only one number between 0 and 1 that is: 'score'\n"
42-
"The score number must be an decimals\n"
43-
"This is the rule of answer: 0-0.2 is mild or none, 0.3-0.6 is moderate, and above 0.7 is severe.\n"
44-
"This is a patient‘s medical record. Context information in below\n"
45-
"---------------------\n"
46-
"{context_str}"
47-
"Given the context information, you are a helpful health consultant "
48-
"answer the question: {query_str}\n"
49-
)
45+
"Give the answer in jason format with only one number between 0 and 1 that is: 'score'\n"
46+
"The score number must be an decimals\n"
47+
"This is the rule of answer: 0-0.2 is mild or none, 0.3-0.6 is moderate, and above 0.7 is severe.\n"
48+
"This is a patient‘s medical record. Context information in below\n"
49+
"---------------------\n"
50+
"{context_str}"
51+
"Given the context information, you are a helpful health consultant "
52+
"answer the question: {query_str}\n"
53+
)
5054
]
5155
chat_text_qa_msgs_lc = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
5256
text_qa_template = Prompt.from_langchain_prompt(chat_text_qa_msgs_lc)
@@ -73,9 +77,13 @@ def generate_question(path):
7377
question = []
7478
for i in my_feature_list:
7579
sentence = f"Does the person described in the case have {i} symptoms? Do you think it is serious?"
80+
list_sentence = [sentence]
81+
retrieval = retrieval_info(list_sentence, '/Users/jmy/Desktop/ai_for_health_final/', 1)
7682
question.append(sentence)
83+
related_work.append(retrieval[0])
84+
print(retrieval[0])
7785

78-
return question, my_feature_list
86+
return question, related_work, my_feature_list
7987

8088

8189
def count_subfolders(folder_path):
@@ -86,39 +94,47 @@ def count_subfolders(folder_path):
8694
if root != folder_path:
8795
subfolder_count += 1
8896

89-
basepath = '/Users/chongzhang/PycharmProjects/ai_for_health_final/dataset_folder/health_report_'
97+
basepath = '/Users/jmy/Desktop/ai_for_health_final/dataset_folder/health_report_'
9098
for i in range(subfolder_count):
91-
path_rr = basepath + str({i})
99+
path_rr = basepath+str({i})
92100
subfolder_paths.append(path_rr)
93101

94102
return subfolder_count, subfolder_paths
95103

96104

97-
def load_doc(folder_path, question):
105+
106+
107+
def load_doc(folder_path,question,work):
108+
print(len(work))
98109
count, dict = count_subfolders(folder_path)
99110
list_k = []
100-
openai.api_key = "Your Api"
101111
context = 'Here is some additional professional health knowledge that can help you better analyze the report'
102-
for i in range(0, 1):
112+
for i in range(0,5000):
103113
documents = SimpleDirectoryReader(dict[i]).load_data()
104114
index = GPTVectorStoreIndex.from_documents(documents)
105115
index.set_index_id("index_health")
106116
index.storage_context.persist('./storage')
107117
content = context
108-
list = answer_from_gpt(question, content)
118+
list = answer_from_gpt(question, content, work)
109119
list_k.append(list)
110120
return list_k
111121

112122

123+
113124
if __name__ == '__main__':
114125

126+
115127
openai.api_key = os.environ.get("OPENAI_API_KEY")
116-
path = '/Users/chongzhang/PycharmProjects/ai_for_health_final/label and feature/input_feature.txt'
117-
question, features_list = generate_question(path)
118-
folder_path = '/Users/chongzhang/PycharmProjects/ai_for_health_final/dataset_folder'
119-
list = load_doc(folder_path, question)
128+
path = '/Users/jmy/Desktop/ai_for_health_final/label and feature/input_feature.txt'
129+
question, related_work, features_list = generate_question(path)
130+
folder_path = '/Users/jmy/Desktop/ai_for_health_final/dataset_folder'
131+
list = load_doc(folder_path, question, related_work)
132+
133+
with open('training/train.txt', 'w') as file:
134+
for item in list:
135+
file.write(''.join(str(item)) + '\n\n')
120136

121-
with open(f'training2/combined10.csv', 'w', newline='') as file:
137+
with open('training/combined7.csv', 'w', newline='') as file:
122138
writer = csv.writer(file)
123139
# 首先写入特征行
124140
writer.writerow(features_list)

0 commit comments

Comments
 (0)