import os
import openai
import json
from dotenv import load_dotenv
print("openai version =", openai.__version__)
openai version = 0.28.0
load_dotenv("azure.env")
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.api_base = os.getenv("OPENAI_API_BASE")
openai.api_version = "2023-07-01-preview"
openai.api_type = "azure"
!wget http://docs.smarthealthit.org/dstu2-examples/examples/allergyintolerance-example.canonical.json
--2023-09-10 16:53:38-- http://docs.smarthealthit.org/dstu2-examples/examples/allergyintolerance-example.canonical.json Resolving docs.smarthealthit.org (docs.smarthealthit.org)... 185.199.111.153, 185.199.110.153, 185.199.109.153, ... Connecting to docs.smarthealthit.org (docs.smarthealthit.org)|185.199.111.153|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 2473 (2.4K) [application/json] Saving to: ‘allergyintolerance-example.canonical.json.1’ allergyintolerance- 100%[===================>] 2.42K --.-KB/s in 0s 2023-09-10 16:53:38 (127 MB/s) - ‘allergyintolerance-example.canonical.json.1’ saved [2473/2473]
file_path = "allergyintolerance-example.canonical.json"
try:
with open(file_path, "r") as json_file:
# Read the contents of the JSON file.
json_data = json_file.read()
# Now, json_data contains the JSON content as a string.
print("JSON data as a string:")
print(json_data)
except FileNotFoundError:
print(f"The file '{file_path}' was not found.")
except Exception as e:
print(f"An error occurred: {str(e)}")
JSON data as a string: {"category":"food","criticality":"CRITH","id":"example","identifier":[{"system":"http://acme.com/ids/patients/risks","value":"49476534"}],"lastOccurence":"2012-06","patient":{"reference":"Patient/example"},"reaction":[{"description":"Challenge Protocol. Severe Reaction to 1/8 cashew. Epinephrine administered","manifestation":[{"coding":[{"code":"39579001","display":"Anaphylactic reaction","system":"http://snomed.info/sct"}]}],"onset":"2012-06-12","severity":"severe","substance":{"coding":[{"code":"C3214954","display":"cashew nut allergenic extract Injectable Product","system":"http://www.nlm.nih.gov/research/umls/rxnorm"}]}},{"certainty":"likely","manifestation":[{"coding":[{"code":"64305001","display":"Urticaria","system":"http://snomed.info/sct"}]}],"onset":"2004","severity":"moderate"}],"recordedDate":"2014-10-09T14:58:00+11:00","recorder":{"reference":"Practitioner/example"},"resourceType":"AllergyIntolerance","status":"confirmed","substance":{"coding":[{"code":"227493005","display":"Cashew nuts","system":"http://snomed.info/sct"}]},"text":{"div":"<div><p><b>Generated Narrative with Details</b></p><p><b>id</b>: example</p><p><b>identifier</b>: 49476534</p><p><b>recordedDate</b>: 09/10/2014 2:58:00 PM</p><p><b>recorder</b>: <a>Practitioner/example</a></p><p><b>patient</b>: <a>Patient/example</a></p><p><b>substance</b>: Cashew nuts <span>(Details : {SNOMED CT code '227493005' = '227493005', given as 'Cashew nuts'})</span></p><p><b>status</b>: confirmed</p><p><b>criticality</b>: CRITH</p><p><b>type</b>: allergy</p><p><b>category</b>: food</p><p><b>lastOccurence</b>: 01/06/2012</p><blockquote><p><b>reaction</b></p><p><b>substance</b>: cashew nut allergenic extract Injectable Product <span>(Details : {RxNorm code 'C3214954' = '??', given as 'cashew nut allergenic extract Injectable Product'})</span></p><p><b>manifestation</b>: Anaphylactic reaction <span>(Details : {SNOMED CT code '39579001' = '39579001', given as 'Anaphylactic reaction'})</span></p><p><b>description</b>: Challenge Protocol. Severe Reaction to 1/8 cashew. Epinephrine administered</p><p><b>onset</b>: 12/06/2012</p><p><b>severity</b>: severe</p></blockquote><blockquote><p><b>reaction</b></p><p><b>certainty</b>: likely</p><p><b>manifestation</b>: Urticaria <span>(Details : {SNOMED CT code '64305001' = '64305001', given as 'Urticaria'})</span></p><p><b>onset</b>: 01/01/2004</p><p><b>severity</b>: moderate</p></blockquote></div>","status":"generated"},"type":"allergy"}
engine = "gpt-35-turbo-16k"
messages = [
{
"role": "assistant",
"content": "You are an export in FHIR document. You will analyse this FHIR document \
and generate a summary",
},
{"role": "user", "content": file_path},
]
response = openai.ChatCompletion.create(
deployment_id=engine,
messages=messages,
temperature=0,
max_tokens=2048,
)
response
<OpenAIObject chat.completion id=chatcmpl-7xHz0BUYr3xEP1CK5u42Rx0pRiowg at 0x7fcb6b652f70> JSON: { "id": "chatcmpl-7xHz0BUYr3xEP1CK5u42Rx0pRiowg", "object": "chat.completion", "created": 1694364834, "model": "gpt-35-turbo-16k", "prompt_annotations": [ { "prompt_index": 0, "content_filter_results": { "hate": { "filtered": false, "severity": "safe" }, "self_harm": { "filtered": false, "severity": "safe" }, "sexual": { "filtered": false, "severity": "safe" }, "violence": { "filtered": false, "severity": "safe" } } } ], "choices": [ { "index": 0, "finish_reason": "stop", "message": { "role": "assistant", "content": "This FHIR document is an example of an AllergyIntolerance resource in the FHIR format. It represents information about a patient's allergy or intolerance to a specific substance.\n\nThe document includes the following key information:\n\n- Identifier: A unique identifier for the allergy or intolerance.\n- ClinicalStatus: The clinical status of the allergy or intolerance, indicating whether it is active, inactive, resolved, or confirmed.\n- VerificationStatus: The verification status of the allergy or intolerance, indicating whether it is unconfirmed, confirmed, or refuted.\n- Type: The type of the allergy or intolerance, which can be either an allergy or an intolerance.\n- Category: The category of the allergy or intolerance, such as food, medication, or environmental.\n- Criticality: The criticality of the allergy or intolerance, indicating the potential impact on the patient's health.\n- Code: The code representing the specific substance that the patient is allergic or intolerant to.\n- Patient: A reference to the patient to whom the allergy or intolerance belongs.\n- Onset: The date or time when the allergy or intolerance was first identified or reported.\n- Recorder: A reference to the healthcare professional who recorded the allergy or intolerance.\n- Note: Additional notes or comments about the allergy or intolerance.\n\nThis FHIR document provides a structured representation of the allergy or intolerance information, making it easier to exchange and share this data between different healthcare systems." }, "content_filter_results": { "hate": { "filtered": false, "severity": "safe" }, "self_harm": { "filtered": false, "severity": "safe" }, "sexual": { "filtered": false, "severity": "safe" }, "violence": { "filtered": false, "severity": "safe" } } } ], "usage": { "completion_tokens": 283, "prompt_tokens": 40, "total_tokens": 323 } }
print(response["choices"][0]["message"]["content"])
This FHIR document is an example of an AllergyIntolerance resource in the FHIR format. It represents information about a patient's allergy or intolerance to a specific substance. The document includes the following key information: - Identifier: A unique identifier for the allergy or intolerance. - ClinicalStatus: The clinical status of the allergy or intolerance, indicating whether it is active, inactive, resolved, or confirmed. - VerificationStatus: The verification status of the allergy or intolerance, indicating whether it is unconfirmed, confirmed, or refuted. - Type: The type of the allergy or intolerance, which can be either an allergy or an intolerance. - Category: The category of the allergy or intolerance, such as food, medication, or environmental. - Criticality: The criticality of the allergy or intolerance, indicating the potential impact on the patient's health. - Code: The code representing the specific substance that the patient is allergic or intolerant to. - Patient: A reference to the patient to whom the allergy or intolerance belongs. - Onset: The date or time when the allergy or intolerance was first identified or reported. - Recorder: A reference to the healthcare professional who recorded the allergy or intolerance. - Note: Additional notes or comments about the allergy or intolerance. This FHIR document provides a structured representation of the allergy or intolerance information, making it easier to exchange and share this data between different healthcare systems.
!wget http://docs.smarthealthit.org/dstu2-examples/examples/careplan-example-f003-pharynx.canonical.json
--2023-09-10 16:54:08-- http://docs.smarthealthit.org/dstu2-examples/examples/careplan-example-f003-pharynx.canonical.json Resolving docs.smarthealthit.org (docs.smarthealthit.org)... 185.199.108.153, 185.199.109.153, 185.199.110.153, ... Connecting to docs.smarthealthit.org (docs.smarthealthit.org)|185.199.108.153|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 2562 (2.5K) [application/json] Saving to: ‘careplan-example-f003-pharynx.canonical.json.1’ careplan-example-f0 100%[===================>] 2.50K --.-KB/s in 0s 2023-09-10 16:54:08 (120 MB/s) - ‘careplan-example-f003-pharynx.canonical.json.1’ saved [2562/2562]
file_path = "careplan-example-f003-pharynx.canonical.json"
try:
with open(file_path, "r") as json_file:
# Read the contents of the JSON file.
json_data = json_file.read()
# Now, json_data contains the JSON content as a string.
print("JSON data as a string:")
print(json_data)
except FileNotFoundError:
print(f"The file '{file_path}' was not found.")
except Exception as e:
print(f"An error occurred: {str(e)}")
JSON data as a string: {"activity":[{"detail":{"category":{"coding":[{"code":"procedure","system":"http://hl7.org/fhir/care-plan-activity-category"}]},"code":{"coding":[{"code":"172960003","display":"Incision of retropharyngeal abscess","system":"http://snomed.info/sct"}]},"performer":[{"display":"E.M. van den broek","reference":"Practitioner/f001"}],"prohibited":true,"scheduledString":"2011-06-27T09:30:10+01:00","status":"completed"}}],"addresses":[{"display":"?????","reference":"Condition/f201"}],"contained":[{"description":"Retropharyngeal abscess removal","id":"goal","note":[{"text":"goal accomplished without complications"}],"resourceType":"Goal","status":"achieved","subject":{"display":"P. van de Heuvel","reference":"Patient/f001"}}],"goal":[{"reference":"#goal"}],"id":"f003","identifier":[{"system":"http://www.bmc.nl/zorgportal/identifiers/careplans","use":"official","value":"CP3953"}],"modified":"2013-06-27T09:30:10+01:00","participant":[{"member":{"display":"E.M. van den broek","reference":"Practitioner/f001"}}],"period":{"end":"2013-03-08T09:30:10+01:00","start":"2013-03-08T09:00:10+01:00"},"resourceType":"CarePlan","status":"completed","subject":{"display":"P. van de Heuvel","reference":"Patient/f001"},"text":{"div":"<div><p><b>Generated Narrative with Details</b></p><p><b>id</b>: f003</p><p><b>contained</b>: </p><p><b>identifier</b>: CP3953 (OFFICIAL)</p><p><b>subject</b>: <a>P. van de Heuvel</a></p><p><b>status</b>: completed</p><p><b>period</b>: 08/03/2013 9:00:10 AM --> 08/03/2013 9:30:10 AM</p><p><b>modified</b>: 27/06/2013 9:30:10 AM</p><p><b>addresses</b>: <a>?????</a></p><h3>Participants</h3><table><tr><td>-</td><td><b>Member</b></td></tr><tr><td>*</td><td><a>E.M. van den broek</a></td></tr></table><p><b>goal</b>: id: goal; P. van de Heuvel; description: Retropharyngeal abscess removal; status: achieved; Annotation: goal accomplished without complications</p><blockquote><p><b>activity</b></p><h3>Details</h3><table><tr><td>-</td><td><b>Category</b></td><td><b>Code</b></td><td><b>Status</b></td><td><b>Prohibited</b></td><td><b>Scheduled[x]</b></td><td><b>Performer</b></td></tr><tr><td>*</td><td>Procedure <span>(Details : {http://hl7.org/fhir/care-plan-activity-category code 'procedure' = 'Procedure)</span></td><td>Incision of retropharyngeal abscess <span>(Details : {SNOMED CT code '172960003' = '172960003', given as 'Incision of retropharyngeal abscess'})</span></td><td>completed</td><td>true</td><td>2011-06-27T09:30:10+01:00</td><td><a>E.M. van den broek</a></td></tr></table></blockquote></div>","status":"generated"}}
response = openai.ChatCompletion.create(
deployment_id=engine,
messages=messages,
temperature=0,
max_tokens=2048,
)
print(response["choices"][0]["message"]["content"])
This FHIR document is an example of an AllergyIntolerance resource in the FHIR format. It represents information about a patient's allergy or intolerance to a specific substance. The document includes the following key information: - Identifier: A unique identifier for the allergy or intolerance. - ClinicalStatus: The clinical status of the allergy or intolerance, indicating whether it is active, inactive, resolved, or confirmed. - VerificationStatus: The verification status of the allergy or intolerance, indicating whether it is unconfirmed, confirmed, or refuted. - Type: The type of the allergy or intolerance, which can be either an allergy or an intolerance. - Category: The category of the allergy or intolerance, such as food, medication, or environmental. - Criticality: The criticality of the allergy or intolerance, indicating the potential impact on the patient's health. - Code: The code representing the specific substance that the patient is allergic or intolerant to. - Patient: A reference to the patient to whom the allergy or intolerance belongs. - Onset: The date or time when the allergy or intolerance was first identified or reported. - Recorder: A reference to the healthcare professional who recorded the allergy or intolerance. - Note: Additional notes or comments about the allergy or intolerance. This FHIR document provides a structured representation of the allergy or intolerance information, making it easier to exchange and share this data between different healthcare systems.