10
10
HumanMessagePromptTemplate ,
11
11
SystemMessagePromptTemplate ,
12
12
)
13
+ from Hopfield import retrieval_info
13
14
14
15
15
- def answer_from_gpt (ques , context ):
16
+ def answer_from_gpt (ques , context , work ):
17
+
16
18
storage_context = StorageContext .from_defaults (persist_dir = './storage' )
17
19
index = load_index_from_storage (storage_context , index_id = "index_health" )
18
20
list_score = []
19
21
20
22
t = 0
21
23
for i in ques :
22
- my_context = context
24
+ my_context = context + work [ t ]
23
25
QA_PROMPT = get_systemprompt_template (my_context )
24
26
query_engine = index .as_query_engine (text_qa_template = QA_PROMPT )
25
27
response = query_engine .query (i )
@@ -32,21 +34,23 @@ def answer_from_gpt(ques, context):
32
34
return list_score
33
35
34
36
37
+
35
38
def get_systemprompt_template (exist_context ):
39
+
36
40
chat_text_qa_msgs = [
37
41
SystemMessagePromptTemplate .from_template (
38
42
exist_context
39
43
),
40
44
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
+ )
50
54
]
51
55
chat_text_qa_msgs_lc = ChatPromptTemplate .from_messages (chat_text_qa_msgs )
52
56
text_qa_template = Prompt .from_langchain_prompt (chat_text_qa_msgs_lc )
@@ -73,9 +77,13 @@ def generate_question(path):
73
77
question = []
74
78
for i in my_feature_list :
75
79
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 )
76
82
question .append (sentence )
83
+ related_work .append (retrieval [0 ])
84
+ print (retrieval [0 ])
77
85
78
- return question , my_feature_list
86
+ return question , related_work , my_feature_list
79
87
80
88
81
89
def count_subfolders (folder_path ):
@@ -86,39 +94,47 @@ def count_subfolders(folder_path):
86
94
if root != folder_path :
87
95
subfolder_count += 1
88
96
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_'
90
98
for i in range (subfolder_count ):
91
- path_rr = basepath + str ({i })
99
+ path_rr = basepath + str ({i })
92
100
subfolder_paths .append (path_rr )
93
101
94
102
return subfolder_count , subfolder_paths
95
103
96
104
97
- def load_doc (folder_path , question ):
105
+
106
+
107
+ def load_doc (folder_path ,question ,work ):
108
+ print (len (work ))
98
109
count , dict = count_subfolders (folder_path )
99
110
list_k = []
100
- openai .api_key = "Your Api"
101
111
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 ):
103
113
documents = SimpleDirectoryReader (dict [i ]).load_data ()
104
114
index = GPTVectorStoreIndex .from_documents (documents )
105
115
index .set_index_id ("index_health" )
106
116
index .storage_context .persist ('./storage' )
107
117
content = context
108
- list = answer_from_gpt (question , content )
118
+ list = answer_from_gpt (question , content , work )
109
119
list_k .append (list )
110
120
return list_k
111
121
112
122
123
+
113
124
if __name__ == '__main__' :
114
125
126
+
115
127
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 ' )
120
136
121
- with open (f'training2/combined10 .csv' , 'w' , newline = '' ) as file :
137
+ with open ('training/combined7 .csv' , 'w' , newline = '' ) as file :
122
138
writer = csv .writer (file )
123
139
# 首先写入特征行
124
140
writer .writerow (features_list )
0 commit comments