Skip to content

Commit

Permalink
feat: Fhir template (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Satarupa22-SD authored Apr 28, 2024
1 parent 34a5082 commit 2047a20
Show file tree
Hide file tree
Showing 21 changed files with 1,231 additions and 4 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ jobs:
steps:
- uses: actions/checkout@v3

<<<<<<< HEAD
- name: Install MacOS extra tools and add extra setup
if: ${{ matrix.os == 'macos' }}
run: |
sudo mkdir -p /tmp
sudo chmod 777 /tmp
brew install gnu-sed
echo 'export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"' >> ~/.bashrc
echo 'export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"' >> ~/.bash_profile
- name: Prepare conda environment (windows)
if: ${{ matrix.os == 'windows' }}
run: |
$env:Path += ";C:\Program Files\Git\usr\bin"
sed -i s/python\ 3\.8/python\ ${{ matrix.python_version }}/ conda/dev.yaml
cat conda/dev.yaml
- name: Prepare conda environment
if: ${{ matrix.os != 'windows' }}
run: |
sed -i s/python\ 3\.8/python\ ${{ matrix.python_version }}/ conda/dev.yaml
cat conda/dev.yaml
=======
>>>>>>> upstream/main
- uses: conda-incubator/setup-miniconda@v3
with:
miniconda-version: "latest"
Expand Down
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/anamnesis.ai.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions conda/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ channels:
- nodefaults
- conda-forge
dependencies:
<<<<<<< HEAD
- python 3.8
=======
- python
>>>>>>> upstream/main
- pip
- poetry
- nodejs # used by semantic-release
- shellcheck
- pip:
<<<<<<< HEAD
# distlib issue
- paginate
=======
# distlib issues
- paginate
>>>>>>> upstream/main
940 changes: 937 additions & 3 deletions poetry.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ exclude = [

[tool.poetry.dependencies]
python = ">=3.8.1,<4"
<<<<<<< HEAD
langchain = "^0.1.12"
openai = "^1.14.1"
=======
sqlmodel = ">=0.0.16"
fhir-resources = ">=7.1.0"
>>>>>>> upstream/main


[tool.poetry.group.dev.dependencies]
Expand Down
65 changes: 64 additions & 1 deletion src/anamnesisai/anamnesisai.py
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
"""Anamnesis.ai."""
# from dotenv import load_dotenv
# load_dotenv()
import json
import os

from langchain import OpenAI
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.prompts import (
SemanticSimilarityExampleSelector,
)
from langchain.prompts.prompt import PromptTemplate
from langchain.utilities import SQLDatabase
from langchain.vectorstores import Chroma
from langchain_experimental.sql import SQLDatabaseChain

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
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 2047a20

Please sign in to comment.