generated from intersystems-community/iris-fhir-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathirisfhirclient.py
186 lines (158 loc) · 7.14 KB
/
irisfhirclient.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import json
from fhirpy import SyncFHIRClient
from tabulate import tabulate
from fhirpy.base.searchset import Raw
import uuid
import base64
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.llms import OpenAI
from langchain.chains import RetrievalQA
from langchain.document_loaders import TextLoader
from langchain.document_loaders import DirectoryLoader
contentType = "application/fhir+json"
def FormatResource(resource, data, opt):
rows = []
if opt == 1:
if resource == "Patient":
for rowval in data:
row = {
"id": rowval.get('id'),
"lastName": rowval.get_by_path('name.0.family'),
"firstName": rowval.get_by_path('name.0.given.0'),
"birthDate": rowval.get_by_path('birthDate'),
"gender": rowval.get_by_path('gender'),
"address": {
"line": rowval.get_by_path('address.0.line'),
"city": rowval.get_by_path('address.0.city'),
"state": rowval.get_by_path('address.0.state'),
"postalCode": rowval.get_by_path('address.0.postalCode'),
"country": rowval.get_by_path('address.0.country')
},
"communicationLanguage": rowval.get_by_path('communication.0.language.text'),
"phone": rowval.get_by_path('telecom.0.value')
}
rows.append(row)
elif resource == "Observation":
for rowval in data:
row = {
"id": rowval.get('id'),
"category": rowval.get_by_path('category.0.coding.0.code'),
"code": rowval.get_by_path('code.coding.0.code'),
"value": rowval.get_by_path('valueQuantity.value'),
"uom": rowval.get_by_path('valueQuantity.code'),
"date": rowval.get('effectiveDateTime'),
"patientId": rowval.get_by_path('subject.reference')
}
rows.append(row)
elif resource == 'DocumentReference':
for rowval in data:
row = {
"id": rowval.get('id'),
"patientId": rowval.get_by_path('subject.reference'),
"practitionerId": rowval.get_by_path('author.0.reference'),
"base64payload": rowval.get_by_path('content.0.attachment.data'),
"mimeType": rowval.get_by_path('content.0.attachment.contentType'),
"updatedDate": rowval.get_by_path('meta.lastUpdated'),
}
rows.append(row)
elif resource == 'Encounter':
for rowval in data:
row = {
"id": rowval.get('id'),
"status": rowval.get_by_path('subject.reference'),
"type": rowval.get_by_path('type.0.text'),
"practitionerId": rowval.get_by_path('participant.0.individual.reference'),
"practitionerName": rowval.get_by_path('participant.0.individual.display'),
"patientId": rowval.get_by_path('subject.reference'),
"patientName": rowval.get_by_path('subject.display'),
"start": rowval.get_by_path('period.start'),
"end": rowval.get_by_path('period.end'),
"updatedDate": rowval.get_by_path('meta.lastUpdated'),
}
rows.append(row)
return rows
def GetResource(resource, id, url, api_key):
# Get Connection
client = SyncFHIRClient(url=url, extra_headers={
"Content-Type": contentType, "x-api-key": api_key})
data = ""
try:
if len(id) > 0:
data = client.resources(resource).search(_id=id).fetch()
else:
data = client.resources(resource).fetch()
except Exception as e:
print("Error :" + str(e))
rows = FormatResource(resource, data, 1)
# print(rows)
return json.dumps(rows)
def GetPatientResources(resource, patientId, url, api_key):
# Get Connection
cclient = SyncFHIRClient(url=url, extra_headers={
"Content-Type": contentType, "x-api-key": api_key})
try:
data = cclient.resources(resource).search(patient=patientId).fetch()
except:
print("Unable to get Resource Type")
return
rows = FormatResource(resource, data, 1)
# print(rows)
return json.dumps(rows)
def CreateDocumentForPatient(patientId, practitionerId, base64payload, mimeType, url, api_key):
headers = {"Content-Type": contentType, "x-api-key": api_key}
client = SyncFHIRClient(url=url, extra_headers=headers)
patient = client.resources('Patient').search(_id=patientId).first()
practitioner = client.resources(
'Practitioner').search(_id=practitionerId).first()
docref = client.resource("DocumentReference")
docref["status"] = "current"
docref["id"] = str(uuid.uuid4())
docref["content"] = [{
"attachment": {
"contentType": mimeType,
"data": base64payload
}
}]
base64_bytes = base64payload.encode('utf-8')
message_bytes = base64.b64decode(base64_bytes)
summary_content = json.loads(message_bytes.decode('utf-8'))['summary']
# write to txt files for document search
lines = [summary_content]
with open('/home/irisowner/irisdev/summaries/' + docref["id"] + '.txt', 'w') as f:
f.writelines(lines)
docref['author'] = [practitioner.to_reference()]
docref['subject'] = patient.to_reference()
try:
resp = docref.save()
except Exception as e:
return "Error while creating DocumentReference:" + str(e)
return json.dumps({"id": docref["id"]})
def QueryDocs(query):
loader = DirectoryLoader(
'/home/irisowner/irisdev/summaries/', glob="./*.txt", loader_cls=TextLoader)
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000, chunk_overlap=200)
texts = text_splitter.split_documents(documents)
persist_directory = 'db'
embedding = OpenAIEmbeddings()
vectordb = Chroma.from_documents(documents=texts,
embedding=embedding,
persist_directory=persist_directory)
retriever = vectordb.as_retriever()
qa_chain = RetrievalQA.from_chain_type(llm=OpenAI(),
chain_type="stuff",
retriever=retriever,
return_source_documents=True)
llm_response = qa_chain(query)
return json.dumps(process_llm_response(llm_response))
def process_llm_response(llm_response):
resp = dict()
resp['response'] = llm_response['result']
resp['sourceDocIds'] = []
for source in llm_response["source_documents"]:
resp['sourceDocIds'].append(
(source.metadata['source']).split('/')[-1].split('.')[0])
return resp