-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9aab38
commit 7acae84
Showing
12 changed files
with
376 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,62 @@ | ||
"""Main module.""" | ||
|
||
from langchain import OpenAI | ||
from langchain.utilities import SQLDatabase | ||
from langchain_experimental.sql import SQLDatabaseChain | ||
from langchain.prompts import SemanticSimilarityExampleSelector | ||
from langchain.embeddings import HuggingFaceEmbeddings | ||
from langchain.vectorstores import Chroma | ||
from langchain.prompts import FewShotPromptTemplate | ||
from langchain.chains.sql_database.prompt import PROMPT_SUFFIX, _mysql_prompt | ||
from langchain.prompts.prompt import PromptTemplate | ||
|
||
|
||
import getpass | ||
import os | ||
# from dotenv import load_dotenv | ||
# load_dotenv() | ||
|
||
|
||
import json | ||
|
||
|
||
with open("resource_templates.json", "r") as f: | ||
resource_templates = json.load(f) | ||
|
||
def db_chain(): | ||
db_user = "root" | ||
db_password = "root" | ||
db_host = "localhost" | ||
db_name = "Fhir" | ||
|
||
db = SQLDatabase.from_uri(f"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}", | ||
sample_rows_in_table_info=3) | ||
llm = OpenAI(openai_api_key=os.environ["API_KEY"], temperature=0.7) | ||
|
||
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2') | ||
to_vectorize = [" ".join(example.values()) for example in database] | ||
vectorstore = Chroma.from_texts(to_vectorize, embeddings, metadatas=database) # need to create a database file to share the format of sql database | ||
example_selector = SemanticSimilarityExampleSelector( | ||
vectorstore=vectorstore, | ||
k=2, | ||
) | ||
fhir_prompt = """You are a FHIR Resource generating expert. Given a converstion between doctor and patient, first create a syntactically correct FHIR resource in JSON Format as specified by the user then look at the results and return the FHIR resource to the input conversation. | ||
Never create random values for values that are not present in the conversation. You must only the columns that are needed to answer the question. Wrap each column name in backticks (`) to denote them as delimited identifiers. | ||
Generate the following FHIR resources based on the conversation and exam: | ||
Patient: Capture the patient's demographics and medical history details. | ||
Practitioner: Identify the doctor involved in the encounter. | ||
Encounter: Describe the patient-doctor interaction, including the reason for the visit. | ||
Observation: Record the patient's reported symptoms and the physical exam findings. | ||
Use clear and concise language for each resource. Maintain patient confidentiality and adhere to HIPAA regulations. Strive for accuracy and consistency in your FHIR structures. | ||
resource_template = resource_templates["resource"] | ||
No pre-amble. | ||
""" | ||
|
||
example_prompt = PromptTemplate( | ||
input_variables=["Conversation"], | ||
template="\nConversation: {Conversation}\nFhir: {Resource}\nResource Template: {ResourceTemplate}", | ||
) | ||
|
||
|
||
chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, prompt=few_shot_prompt) | ||
return chain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
"comments": "patient" | ||
{ | ||
"resourceType": "Patient", | ||
"id": "<Patient ID>", | ||
"active": true, | ||
"name": [ | ||
{ | ||
"use": "official", | ||
"text": "<Patient Full Name>" | ||
} | ||
], | ||
"telecom": [ | ||
{ | ||
"system": "phone", | ||
"value": "<Patient Phone Number>" | ||
}, | ||
{ | ||
"system": "email", | ||
"value": "<Patient Email Address>" | ||
} | ||
], | ||
"birthDate": "<Patient Date of Birth>", | ||
"gender": "<Patient Gender>", | ||
"maritalStatus": { | ||
"coding": [ | ||
{ | ||
"system": "<Marital Status Coding System>", | ||
"code": "<Marital Status Code>" | ||
} | ||
] | ||
}, | ||
"address": [ | ||
{ | ||
"use": "home", | ||
"text": "<Patient Address>" | ||
} | ||
] | ||
} | ||
"comments": "Practitioner" | ||
{ | ||
"resourceType": "Practitioner", | ||
"id": "<Practitioner ID>", | ||
"active": true, | ||
"name": [ | ||
{ | ||
"use": "official", | ||
"text": "<Practitioner Full Name>" | ||
} | ||
], | ||
"telecom": [ | ||
{ | ||
"system": "phone", | ||
"value": "<Practitioner Phone Number>" | ||
} | ||
] | ||
} | ||
|
||
"comments": "Encounter" | ||
|
||
{ | ||
"resourceType": "Encounter", | ||
"id": "<Encounter ID>", | ||
"status": "finished", | ||
"subject": { | ||
"reference": "Patient/<Patient ID>" | ||
}, | ||
"participant": [ | ||
{ | ||
"type": [ | ||
{ | ||
"coding": [ | ||
{ | ||
"system": "http://terminology.hl7.org/CodeSystem/encounter-participant-type", | ||
"code": "TPA" | ||
} | ||
] | ||
} | ||
], | ||
"actor": { | ||
"reference": "Practitioner/<Practitioner ID>" | ||
} | ||
} | ||
], | ||
"reasonCode": [ | ||
{ | ||
"coding": [ | ||
{ | ||
"system": "<Reason Code System>", | ||
"code": "<Reason Code>" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
|
||
"comments": "Observation" | ||
|
||
{ | ||
"resourceType": "Observation", | ||
"id": "<Observation ID>", | ||
"status": "final", | ||
"subject": { | ||
"reference": "Patient/<Patient ID>" | ||
}, | ||
"category": { | ||
"coding": [ | ||
{ | ||
"system": "<Observation Category Coding System>", | ||
"code": "<Observation Category Code>" | ||
} | ||
] | ||
}, | ||
"code": { | ||
"coding": [ | ||
{ | ||
"system": "<Observation Code System>", | ||
"code": "<Observation Code>" | ||
} | ||
] | ||
}, | ||
"value": "<Observation Value>" | ||
} | ||
|
||
"comments": "DiagnosticReport" | ||
{ | ||
"resourceType": "DiagnosticReport", | ||
"id": "<Placeholder: diisi setelah hasil pemeriksaan tersedia>" | ||
} | ||
|
||
"comments": "Condition" | ||
|
||
{ | ||
"resourceType": "Condition", | ||
"id": "<Placeholder: diisi setelah diagnosis tersedia>" | ||
} | ||
|
||
|
||
"comments":"MedicationRequest" | ||
|
||
|
||
{ | ||
"resourceType": "MedicationRequest", | ||
"id": "<Placeholder: diisi setelah prescription details tersedia>" | ||
} | ||
|
||
"comments": "CarePlan" | ||
|
||
{ | ||
"resourceType": "CarePlan", | ||
"id": "<Placeholder: diisi setelah care plan tersedia>" | ||
} | ||
|
||
"comments": "Procedure" | ||
|
||
{ | ||
"resourceType": "Procedure", | ||
"id": "<Placeholder: diisi setelah procedures tersedia>" | ||
} | ||
|
||
"comments": "AllergyIntolerance" | ||
{ | ||
"resourceType": "AllergyIntolerance", | ||
"id": "<AllergyIntolerance ID>", | ||
"patient": { | ||
"reference": "Patient/<Patient ID>" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"AllergyIntolerance": { | ||
"resourceType": "AllergyIntolerance", | ||
"id": "<AllergyIntolerance ID>", | ||
"patient": { | ||
"reference": "Patient/<Patient ID>" | ||
}, | ||
"substance": { | ||
"coding": [] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"resourceType": "CarePlan", | ||
"id": "<Placeholder: diisi setelah care plan tersedia>" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"resourceType": "Condition", | ||
"id": "<Placeholder: diisi setelah diagnosis tersedia>" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"resourceType": "DiagnosticReport", | ||
"id": "<Placeholder: diisi setelah hasil pemeriksaan tersedia>" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"resourceType": "Encounter", | ||
"id": "<Encounter ID>", | ||
"status": "finished", | ||
"subject": { | ||
"reference": "Patient/<Patient ID>" | ||
}, | ||
"participant": [ | ||
{ | ||
"type": [ | ||
{ | ||
"coding": [ | ||
{ | ||
"system": "http://terminology.hl7.org/CodeSystem/encounter-participant-type", | ||
"code": "TPA" | ||
} | ||
] | ||
} | ||
], | ||
"actor": { | ||
"reference": "Practitioner/<Practitioner ID>" | ||
} | ||
} | ||
], | ||
"reasonCode": [ | ||
{ | ||
"coding": [ | ||
{ | ||
"system": "<Reason Code System>", | ||
"code": "<Reason Code>" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"resourceType": "MedicationRequest", | ||
"id": "<Placeholder: diisi setelah prescription details tersedia>" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"resourceType": "Observation", | ||
"id": "<Observation ID>", | ||
"status": "final", | ||
"subject": { | ||
"reference": "Patient/<Patient ID>" | ||
}, | ||
"category": { | ||
"coding": [ | ||
{ | ||
"system": "<Observation Category Coding System>", | ||
"code": "<Observation Category Code>" | ||
} | ||
] | ||
}, | ||
"code": { | ||
"coding": [ | ||
{ | ||
"system": "<Observation Code System>", | ||
"code": "<Observation Code>" | ||
} | ||
] | ||
}, | ||
"value": "<Observation Value>" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"resourceType": "Patient", | ||
"id": "<Patient ID>", | ||
"active": true, | ||
"name": [ | ||
{ | ||
"use": "official", | ||
"text": "<Patient Full Name>" | ||
} | ||
], | ||
"telecom": [ | ||
{ | ||
"system": "phone", | ||
"value": "<Patient Phone Number>" | ||
}, | ||
{ | ||
"system": "email", | ||
"value": "<Patient Email Address>" | ||
} | ||
], | ||
"birthDate": "<Patient Date of Birth>", | ||
"gender": "<Patient Gender>", | ||
"maritalStatus": { | ||
"coding": [ | ||
{ | ||
"system": "<Marital Status Coding System>", | ||
"code": "<Marital Status Code>" | ||
} | ||
] | ||
}, | ||
"address": [ | ||
{ | ||
"use": "home", | ||
"text": "<Patient Address>" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"resourceType": "Practitioner", | ||
"id": "<Practitioner ID>", | ||
"active": true, | ||
"name": [ | ||
{ | ||
"use": "official", | ||
"text": "<Practitioner Full Name>" | ||
} | ||
], | ||
"telecom": [ | ||
{ | ||
"system": "phone", | ||
"value": "<Practitioner Phone Number>" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"resourceType": "Procedure", | ||
"id": "<Placeholder: diisi setelah procedures tersedia>" | ||
} |