Skip to content

Commit

Permalink
Update anamnesisai.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Satarupa22-SD committed Mar 18, 2024
1 parent e9aab38 commit 7acae84
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/anamnesisai/anamnesisai.py
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
168 changes: 168 additions & 0 deletions src/anamnesisai/resource_templates.json
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>"
}
}

12 changes: 12 additions & 0 deletions src/anamnesisai/templates/allergy.json
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": []
}
}
}
4 changes: 4 additions & 0 deletions src/anamnesisai/templates/careplan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"resourceType": "CarePlan",
"id": "<Placeholder: diisi setelah care plan tersedia>"
}
4 changes: 4 additions & 0 deletions src/anamnesisai/templates/condition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"resourceType": "Condition",
"id": "<Placeholder: diisi setelah diagnosis tersedia>"
}
4 changes: 4 additions & 0 deletions src/anamnesisai/templates/diagnostic_report.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"resourceType": "DiagnosticReport",
"id": "<Placeholder: diisi setelah hasil pemeriksaan tersedia>"
}
35 changes: 35 additions & 0 deletions src/anamnesisai/templates/encounter.json
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>"
}
]
}
]
}
4 changes: 4 additions & 0 deletions src/anamnesisai/templates/medication_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"resourceType": "MedicationRequest",
"id": "<Placeholder: diisi setelah prescription details tersedia>"
}
25 changes: 25 additions & 0 deletions src/anamnesisai/templates/observation.json
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>"
}
37 changes: 37 additions & 0 deletions src/anamnesisai/templates/patients.json
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>"
}
]
}
17 changes: 17 additions & 0 deletions src/anamnesisai/templates/practitioner.json
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>"
}
]
}
4 changes: 4 additions & 0 deletions src/anamnesisai/templates/procedure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"resourceType": "Procedure",
"id": "<Placeholder: diisi setelah procedures tersedia>"
}

0 comments on commit 7acae84

Please sign in to comment.