-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_parser.py
More file actions
89 lines (68 loc) · 3.18 KB
/
text_parser.py
File metadata and controls
89 lines (68 loc) · 3.18 KB
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
import google.generativeai as genai
from dotenv import load_dotenv
import os
from gemini_context_manager import GeminiContextManager
load_dotenv()
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
class TextParser:
def __init__(self):
context_manager = GeminiContextManager()
# configure system prompt
context_manager.add_context("user",
"""
You are a text parser. Your job is to parse the input string to extract various medical information, and parse it so that our backend engineer can use it to store the information in our database.
YOU MUST FOLLOW THE RESPONSE FORMAT EXACTLY.
response format:
{ "notes": [{{ "note": "~doctor's note here~", "note_date": "~date in the format of YYYY-MM-DD if month or date is a single digit number, pad it with 0~"}}],
"medicine": [{{ "med_name": "~medicine name here~", "med_dosage": "~medicine dosage here~", "med_frequency": "~medicine frequency here~", "med_date": "~medicine start date here~"}}, ...],
"vaccine": [{{ "vac_name": "~vaccine name here~", "vac_date": "~vaccine start date here~"}}, ...],
"lab_result": [{{ "lab_result": "~lab result here~", lab_date": "~lab date here~"}}],
"surgeries": [{{ "surgery": "~surgery description here~", "surgery_date": "~surgery date here~"}}],
"diagnosis": [{{ "diagnosis": "~diagnosis here~", "diagnosis_date": "~diagnosis date here~"}}],
"symptoms": [{{ "symptom": "~symptom here~", "symptom_date": "~symptom date here~"}}, ...],
}
IMPORTANT: OBEY THE PROVIDED FORMAT.
IMPORTANT: DO NOT INCLUDE ANY OTHER INFORMATION THAT IS NOT IN THE PROVIDED FORMAT.
IMPORTANT: IF NO INFORMATION IS PROVIDED, PUT None INSTEAD.
IMPORTANT: DO NOT RESPOND WITH MARKDOWN STYLE. ALWAYS RETURN BY PLAIN TEXT.
IMPORTANT: ONLY RETURN THE DICTONARY. DO NOT RETURN ANYTHING ELSE, BEFORE OR AFTER THE DICTONARY.
""")
context_manager.add_context("model","Certainly, I can help you with that.")
self.system_prompt = context_manager.get_context()
def cut_off_until_bracket(self, text):
bracket_index = text.find('{')
if bracket_index != -1:
return text[bracket_index:]
else:
return text
def parse(self, text, chat):
chat.history = self.system_prompt
response = chat.send_message(text)
print(response.text)
return self.cut_off_until_bracket(response.text)
def main():
parser = TextParser()
response = parser.parse("""
Patient ID: #123456
Name: John Doe
Date of Birth: 1990-05-15
Gender: Male
Height: 180 cm
Weight: 75 kg
Race: Caucasian
Ethnicity: Not specified
Prescription:
Medication: Ibuprofen
Dosage: 200 mg
Frequency: Twice daily
Start Date: 2024-02-10
notes:
Take Ibuprofen orally with a full glass of water after meals.
Do not exceed the recommended dosage of 400 mg per day.
In case of any adverse reactions, contact your healthcare provider immediately.
Doctor's Signature: Dr. Smith
""")
print(response)
if __name__ == "__main__":
main()