1
- import openai , os
2
- import pandas as pd
1
+ import openai
2
+ import os
3
3
import re
4
- from Hopfield import retrieval_info
4
+ import csv
5
5
from llama_index import GPTVectorStoreIndex , SimpleDirectoryReader
6
6
from llama_index import Prompt
7
7
from llama_index import StorageContext , load_index_from_storage
10
10
HumanMessagePromptTemplate ,
11
11
SystemMessagePromptTemplate ,
12
12
)
13
- import csv
14
-
15
-
16
-
17
13
18
- def answer_from_gpt (ques , context , work ):
19
14
15
+ def answer_from_gpt (ques , context ):
20
16
storage_context = StorageContext .from_defaults (persist_dir = './storage' )
21
17
index = load_index_from_storage (storage_context , index_id = "index_health" )
22
18
list_score = []
23
19
24
20
t = 0
25
21
for i in ques :
26
- my_context = context + work [ t ]
22
+ my_context = context
27
23
QA_PROMPT = get_systemprompt_template (my_context )
28
24
query_engine = index .as_query_engine (text_qa_template = QA_PROMPT )
29
25
response = query_engine .query (i )
@@ -36,23 +32,21 @@ def answer_from_gpt(ques, context, work):
36
32
return list_score
37
33
38
34
39
-
40
35
def get_systemprompt_template (exist_context ):
41
-
42
36
chat_text_qa_msgs = [
43
37
SystemMessagePromptTemplate .from_template (
44
38
exist_context
45
39
),
46
40
HumanMessagePromptTemplate .from_template (
47
- "Give the answer in jason format with only one number between 0 and 1 that is: 'score'\n "
48
- "The score number must be an decimals\n "
49
- "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 "
50
- "This is a patient‘s medical record. Context information in below\n "
51
- "---------------------\n "
52
- "{context_str}"
53
- "Given the context information, you are a helpful health consultant "
54
- "answer the question: {query_str}\n "
55
- )
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
+ )
56
50
]
57
51
chat_text_qa_msgs_lc = ChatPromptTemplate .from_messages (chat_text_qa_msgs )
58
52
text_qa_template = Prompt .from_langchain_prompt (chat_text_qa_msgs_lc )
@@ -79,13 +73,9 @@ def generate_question(path):
79
73
question = []
80
74
for i in my_feature_list :
81
75
sentence = f"Does the person described in the case have { i } symptoms? Do you think it is serious?"
82
- list_sentence = [sentence ]
83
- retrieval = retrieval_info (list_sentence , '/Users/jmy/Desktop/ai_for_health_final/' , 1 )
84
76
question .append (sentence )
85
- related_work .append (retrieval [0 ])
86
- print (retrieval [0 ])
87
77
88
- return question , related_work , my_feature_list
78
+ return question , my_feature_list
89
79
90
80
91
81
def count_subfolders (folder_path ):
@@ -96,65 +86,39 @@ def count_subfolders(folder_path):
96
86
if root != folder_path :
97
87
subfolder_count += 1
98
88
99
- basepath = '/Users/jmy/Desktop /ai_for_health_final/dataset_folder/health_report_'
89
+ basepath = '/Users/chongzhang/PycharmProjects /ai_for_health_final/dataset_folder/health_report_'
100
90
for i in range (subfolder_count ):
101
- path_rr = basepath + str ({i })
91
+ path_rr = basepath + str ({i })
102
92
subfolder_paths .append (path_rr )
103
93
104
94
return subfolder_count , subfolder_paths
105
95
106
96
107
- # def answer_from_gpt(ques,context):
108
- # # rebuild storage context
109
- # storage_context = StorageContext.from_defaults(persist_dir='./storage')
110
- # # load index
111
- # index = load_index_from_storage(storage_context, index_id="index_health")
112
- #
113
- # list_score = []
114
- # for i in ques:
115
- # QA_PROMPT = get_systemprompt_template(context)
116
- # query_engine = index.as_query_engine(text_qa_template=QA_PROMPT)
117
- # response = query_engine.query(i)
118
- # stt = str(response)
119
- #
120
- # score = extract_score(stt)
121
- # list_score.append(score)
122
- # print(score)
123
- #
124
- # return list_score
125
-
126
-
127
- def load_doc (folder_path ,question ,work ):
128
- print (len (work ))
129
- count ,dict = count_subfolders (folder_path )
97
+ def load_doc (folder_path , question ):
98
+ count , dict = count_subfolders (folder_path )
130
99
list_k = []
100
+ openai .api_key = "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"
131
101
context = 'Here is some additional professional health knowledge that can help you better analyze the report'
132
- for i in range (0 ,10000 ):
102
+ for i in range (0 , 1 ):
133
103
documents = SimpleDirectoryReader (dict [i ]).load_data ()
134
104
index = GPTVectorStoreIndex .from_documents (documents )
135
105
index .set_index_id ("index_health" )
136
106
index .storage_context .persist ('./storage' )
137
107
content = context
138
- list = answer_from_gpt (question , content , work )
108
+ list = answer_from_gpt (question , content )
139
109
list_k .append (list )
140
110
return list_k
141
111
142
112
143
-
144
113
if __name__ == '__main__' :
145
114
146
-
147
115
openai .api_key = os .environ .get ("OPENAI_API_KEY" )
148
- path = '/Users/jmy/Desktop /ai_for_health_final/label and feature/input_feature.txt'
149
- question , related_work , features_list = generate_question (path )
150
- folder_path = '/Users/jmy/Desktop /ai_for_health_final/dataset_folder'
151
- list = load_doc (folder_path , question , related_work )
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 )
152
120
153
- with open ('training/train.txt' , 'w' ) as file :
154
- for item in list :
155
- file .write ('' .join (str (item )) + '\n \n ' )
156
-
157
- with open ('training/combined7.csv' , 'w' , newline = '' ) as file :
121
+ with open (f'training2/combined10.csv' , 'w' , newline = '' ) as file :
158
122
writer = csv .writer (file )
159
123
# 首先写入特征行
160
124
writer .writerow (features_list )
@@ -163,10 +127,3 @@ def load_doc(folder_path,question,work):
163
127
writer .writerow (row )
164
128
165
129
print ("CSV file has been created successfully." )
166
-
167
-
168
-
169
-
170
-
171
-
172
-
0 commit comments