umar-ts commited on
Commit
2492d3e
·
verified ·
1 Parent(s): deb89a3

Update service.py

Browse files
Files changed (1) hide show
  1. service.py +62 -23
service.py CHANGED
@@ -9,7 +9,7 @@ import json
9
  load_dotenv()
10
 
11
  # Access environment variables
12
- API_KEY = os.environ['CHAT_GPT_KEY']
13
 
14
  class Stage(Enum):
15
  INITIAL_ASSESSMENT = "Initial Assessment"
@@ -29,12 +29,13 @@ class GPT_Service:
29
  self.base_url = 'https://api.openai.com/v1/chat/completions'
30
 
31
  def request_gpt_for_response(self, system_prompt, userPrompt):
 
32
  headers = {
33
  'Content-Type': 'application/json',
34
  'Authorization': f'Bearer {self.api_key}'
35
  }
36
  data = {
37
- 'model': 'gpt-3.5-turbo-1106',
38
  'temperature': 0.3,
39
  'messages': [
40
  {
@@ -64,22 +65,75 @@ class GPT_Service:
64
  {f"Patient has the following medical history: {medicalHistory}" if medicalHistory is not None and medicalHistory != ""
65
  else ""}
66
  """
67
- print(patientData)
68
  return patientData
69
 
70
- def processOutputResults(self,jsonOutput):
 
 
 
 
 
 
 
 
71
  preprocessed_json_string = jsonOutput.replace('\n', '')
72
  data = json.loads(preprocessed_json_string)
73
- variants = [value for value in data.values()]
74
- # Fetch first three items from the array or pad with empty strings if not enough items are available
75
- paddedVariants = variants[:3] + [''] * (3 - len(variants))
76
- return paddedVariants
 
 
77
 
78
  def getInitialAssessment(self, keywords,medicalHistory):
79
  userPrompt= self.formulateUserPrompt(Stage.INITIAL_ASSESSMENT.value,keywords,medicalHistory)
80
  response= self.request_gpt_for_response(Prompts.INITIAL_ASSESSMENT_PROMPT.value,userPrompt)
81
  return self.processOutputResults(response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
 
 
83
  def getFollowUpAssessment(self, keywords, medicalHistory):
84
  userPrompt= self.formulateUserPrompt(Stage.FOLLOWUP_ASSESSMENT.value,keywords,medicalHistory)
85
  response= self.request_gpt_for_response(Prompts.FOLLOWUP_ASSESSMENT_PROMPT.value,userPrompt)
@@ -90,11 +144,6 @@ class GPT_Service:
90
  response= self.request_gpt_for_response(Prompts.EVALUATION_PROMPT.value,userPrompt)
91
  return self.processOutputResults(response)
92
 
93
- def getRecommendation(self, keywords, medicalHistory):
94
- userPrompt= self.formulateUserPrompt(Stage.RECOMMENDATION.value,keywords,medicalHistory)
95
- response= self.request_gpt_for_response(Prompts.RECOMMENDATION_PROMPT.value,userPrompt)
96
- return self.processOutputResults(response)
97
-
98
  def getDetailedEvaluation(self, keywords,medicalHistory):
99
  userPrompt= self.formulateUserPrompt(Stage.DETAILED_EVALUATION.value,keywords,medicalHistory)
100
  response= self.request_gpt_for_response(Prompts.DETAILED_EVALUATION_PROMPT.value,userPrompt)
@@ -108,14 +157,4 @@ class GPT_Service:
108
  def getDischargeSummary(self, keywords,medicalHistory):
109
  userPrompt= self.formulateUserPrompt(Stage.DISCHARGE_SUMMARY.value,keywords,medicalHistory)
110
  response = self.request_gpt_for_response(Prompts.DISCHARGE_SUMMARY_PROMPT.value,userPrompt)
111
- return self.processOutputResults(response)
112
-
113
- def getShortTermGoals(self, keywords,medicalHistory):
114
- userPrompt= self.formulateUserPrompt(Stage.SHORT_TERM_GOALS.value,keywords,medicalHistory)
115
- response = self.request_gpt_for_response(Prompts.SHORT_TERM_GOALS_PROMPT.value,userPrompt)
116
- return self.processOutputResults(response)
117
-
118
- def getLongTermGoals(self, keywords,medicalHistory):
119
- userPrompt= self.formulateUserPrompt(Stage.LONG_TERM_GOALS.value,keywords,medicalHistory)
120
- response = self.request_gpt_for_response(Prompts.LONG_TERM_GOALS_PROMPT.value,userPrompt)
121
  return self.processOutputResults(response)
 
9
  load_dotenv()
10
 
11
  # Access environment variables
12
+ API_KEY = os.environ['openAI_key']
13
 
14
  class Stage(Enum):
15
  INITIAL_ASSESSMENT = "Initial Assessment"
 
29
  self.base_url = 'https://api.openai.com/v1/chat/completions'
30
 
31
  def request_gpt_for_response(self, system_prompt, userPrompt):
32
+ print(f"System Prompt: {system_prompt}\n user Prompt: {userPrompt}\n")
33
  headers = {
34
  'Content-Type': 'application/json',
35
  'Authorization': f'Bearer {self.api_key}'
36
  }
37
  data = {
38
+ 'model': 'gpt-4-turbo',
39
  'temperature': 0.3,
40
  'messages': [
41
  {
 
65
  {f"Patient has the following medical history: {medicalHistory}" if medicalHistory is not None and medicalHistory != ""
66
  else ""}
67
  """
 
68
  return patientData
69
 
70
+ def formulateUserPromptForDetailAssessment(self, rawPatientInformation, medicalHistory):
71
+ patientData= f"""
72
+ Sentences: {rawPatientInformation}.
73
+ {f"Patient has the following medical history: {medicalHistory}" if medicalHistory is not None and medicalHistory != ""
74
+ else ""}
75
+ """
76
+ return patientData
77
+
78
+ def processOutputResults(self, jsonOutput):
79
  preprocessed_json_string = jsonOutput.replace('\n', '')
80
  data = json.loads(preprocessed_json_string)
81
+ result_list = []
82
+ for key, value in data.items():
83
+ for variant, sentence in value.items():
84
+ result_list.append(sentence)
85
+
86
+ return result_list
87
 
88
  def getInitialAssessment(self, keywords,medicalHistory):
89
  userPrompt= self.formulateUserPrompt(Stage.INITIAL_ASSESSMENT.value,keywords,medicalHistory)
90
  response= self.request_gpt_for_response(Prompts.INITIAL_ASSESSMENT_PROMPT.value,userPrompt)
91
  return self.processOutputResults(response)
92
+
93
+ def getShortTermGoals(self, keywords,medicalHistory):
94
+ userPrompt= self.formulateUserPrompt(Stage.SHORT_TERM_GOALS.value,keywords,medicalHistory)
95
+ response = self.request_gpt_for_response(Prompts.SHORT_TERM_GOALS_PROMPT.value,userPrompt)
96
+ return self.processOutputResults(response)
97
+
98
+ def getLongTermGoals(self, keywords,medicalHistory):
99
+ userPrompt= self.formulateUserPrompt(Stage.LONG_TERM_GOALS.value,keywords,medicalHistory)
100
+ response = self.request_gpt_for_response(Prompts.LONG_TERM_GOALS_PROMPT.value,userPrompt)
101
+ return self.processOutputResults(response)
102
+
103
+ def getRecommendation(self, keywords, medicalHistory):
104
+ userPrompt= self.formulateUserPrompt(Stage.RECOMMENDATION.value,keywords,medicalHistory)
105
+ response= self.request_gpt_for_response(Prompts.RECOMMENDATION_PROMPT.value,userPrompt)
106
+ return self.processOutputResults(response)
107
+
108
+ # Detail Patient Evaluation Prompt
109
+
110
+ def getDetailInitialAssessment(self,medicalHistory,sentences):
111
+ rawPatientAssessment = " ".join(sentences)
112
+ userPrompt= self.formulateUserPromptForDetailAssessment(rawPatientAssessment,medicalHistory)
113
+ response= self.request_gpt_for_response(Prompts.DETAIL_PATIENT_ASSESSMENT_INITIAL_ASSESSMENT_GOALS.value,userPrompt)
114
+ return response
115
+
116
+ def getDetailShortTermGoals(self,medicalHistory,sentences):
117
+ rawPatientAssessment = " ".join(sentences)
118
+ userPrompt= self.formulateUserPromptForDetailAssessment(rawPatientAssessment,medicalHistory)
119
+ response= self.request_gpt_for_response(Prompts.DETAIL_PATIENT_ASSESSMENT_SHORT_TERM_GOALS.value,userPrompt)
120
+ return response
121
+
122
+ def getDetailLongTermGoals(self,medicalHistory,sentences):
123
+ rawPatientAssessment = " ".join(sentences)
124
+ userPrompt= self.formulateUserPromptForDetailAssessment(rawPatientAssessment,medicalHistory)
125
+ response= self.request_gpt_for_response(Prompts.DETAIL_PATIENT_ASSESSMENT_LONG_TERM_GOALS.value,userPrompt)
126
+ return response
127
+
128
+ def getDetailRecommendation(self, medicalHistory,sentences):
129
+ rawPatientAssessment = " ".join(sentences)
130
+ userPrompt= self.formulateUserPromptForDetailAssessment(rawPatientAssessment,medicalHistory)
131
+ response= self.request_gpt_for_response(Prompts.DETAIL_PATIENT_ASSESSMENT_RECOMMENDATION_GOALS.value,userPrompt)
132
+ return response
133
+
134
 
135
+ # Extra helper functions
136
+
137
  def getFollowUpAssessment(self, keywords, medicalHistory):
138
  userPrompt= self.formulateUserPrompt(Stage.FOLLOWUP_ASSESSMENT.value,keywords,medicalHistory)
139
  response= self.request_gpt_for_response(Prompts.FOLLOWUP_ASSESSMENT_PROMPT.value,userPrompt)
 
144
  response= self.request_gpt_for_response(Prompts.EVALUATION_PROMPT.value,userPrompt)
145
  return self.processOutputResults(response)
146
 
 
 
 
 
 
147
  def getDetailedEvaluation(self, keywords,medicalHistory):
148
  userPrompt= self.formulateUserPrompt(Stage.DETAILED_EVALUATION.value,keywords,medicalHistory)
149
  response= self.request_gpt_for_response(Prompts.DETAILED_EVALUATION_PROMPT.value,userPrompt)
 
157
  def getDischargeSummary(self, keywords,medicalHistory):
158
  userPrompt= self.formulateUserPrompt(Stage.DISCHARGE_SUMMARY.value,keywords,medicalHistory)
159
  response = self.request_gpt_for_response(Prompts.DISCHARGE_SUMMARY_PROMPT.value,userPrompt)
 
 
 
 
 
 
 
 
 
 
160
  return self.processOutputResults(response)