diff --git a/src/anamnesisai/anamnesisai.py b/src/anamnesisai/anamnesisai.py index dd0b80e..0d2b2b7 100644 --- a/src/anamnesisai/anamnesisai.py +++ b/src/anamnesisai/anamnesisai.py @@ -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 diff --git a/src/anamnesisai/resource_templates.json b/src/anamnesisai/resource_templates.json new file mode 100644 index 0000000..c2bb444 --- /dev/null +++ b/src/anamnesisai/resource_templates.json @@ -0,0 +1,168 @@ +"comments": "patient" + { + "resourceType": "Patient", + "id": "", + "active": true, + "name": [ + { + "use": "official", + "text": "" + } + ], + "telecom": [ + { + "system": "phone", + "value": "" + }, + { + "system": "email", + "value": "" + } + ], + "birthDate": "", + "gender": "", + "maritalStatus": { + "coding": [ + { + "system": "", + "code": "" + } + ] + }, + "address": [ + { + "use": "home", + "text": "" + } + ] +} +"comments": "Practitioner" +{ + "resourceType": "Practitioner", + "id": "", + "active": true, + "name": [ + { + "use": "official", + "text": "" + } + ], + "telecom": [ + { + "system": "phone", + "value": "" + } + ] +} + +"comments": "Encounter" + +{ + "resourceType": "Encounter", + "id": "", + "status": "finished", + "subject": { + "reference": "Patient/" + }, + "participant": [ + { + "type": [ + { + "coding": [ + { + "system": "http://terminology.hl7.org/CodeSystem/encounter-participant-type", + "code": "TPA" + } + ] + } + ], + "actor": { + "reference": "Practitioner/" + } + } + ], + "reasonCode": [ + { + "coding": [ + { + "system": "", + "code": "" + } + ] + } + ] +} + +"comments": "Observation" + +{ + "resourceType": "Observation", + "id": "", + "status": "final", + "subject": { + "reference": "Patient/" + }, + "category": { + "coding": [ + { + "system": "", + "code": "" + } + ] + }, + "code": { + "coding": [ + { + "system": "", + "code": "" + } + ] + }, + "value": "" +} + +"comments": "DiagnosticReport" +{ + "resourceType": "DiagnosticReport", + "id": "" +} + +"comments": "Condition" + +{ + "resourceType": "Condition", + "id": "" +} + + +"comments":"MedicationRequest" + + +{ + "resourceType": "MedicationRequest", + "id": "" +} + +"comments": "CarePlan" + + { + "resourceType": "CarePlan", + "id": "" +} + +"comments": "Procedure" + + { + "resourceType": "Procedure", + "id": "" +} + +"comments": "AllergyIntolerance" + { + "resourceType": "AllergyIntolerance", + "id": "", + "patient": { + "reference": "Patient/" + } + } + diff --git a/src/anamnesisai/templates/allergy.json b/src/anamnesisai/templates/allergy.json new file mode 100644 index 0000000..84343a9 --- /dev/null +++ b/src/anamnesisai/templates/allergy.json @@ -0,0 +1,12 @@ +{ + "AllergyIntolerance": { + "resourceType": "AllergyIntolerance", + "id": "", + "patient": { + "reference": "Patient/" + }, + "substance": { + "coding": [] + } + } +} diff --git a/src/anamnesisai/templates/careplan.json b/src/anamnesisai/templates/careplan.json new file mode 100644 index 0000000..6071e87 --- /dev/null +++ b/src/anamnesisai/templates/careplan.json @@ -0,0 +1,4 @@ +{ + "resourceType": "CarePlan", + "id": "" +} diff --git a/src/anamnesisai/templates/condition.json b/src/anamnesisai/templates/condition.json new file mode 100644 index 0000000..06d88ae --- /dev/null +++ b/src/anamnesisai/templates/condition.json @@ -0,0 +1,4 @@ +{ + "resourceType": "Condition", + "id": "" +} diff --git a/src/anamnesisai/templates/diagnostic_report.json b/src/anamnesisai/templates/diagnostic_report.json new file mode 100644 index 0000000..dc94468 --- /dev/null +++ b/src/anamnesisai/templates/diagnostic_report.json @@ -0,0 +1,4 @@ +{ + "resourceType": "DiagnosticReport", + "id": "" +} diff --git a/src/anamnesisai/templates/encounter.json b/src/anamnesisai/templates/encounter.json new file mode 100644 index 0000000..d70447e --- /dev/null +++ b/src/anamnesisai/templates/encounter.json @@ -0,0 +1,35 @@ +{ + "resourceType": "Encounter", + "id": "", + "status": "finished", + "subject": { + "reference": "Patient/" + }, + "participant": [ + { + "type": [ + { + "coding": [ + { + "system": "http://terminology.hl7.org/CodeSystem/encounter-participant-type", + "code": "TPA" + } + ] + } + ], + "actor": { + "reference": "Practitioner/" + } + } + ], + "reasonCode": [ + { + "coding": [ + { + "system": "", + "code": "" + } + ] + } + ] +} diff --git a/src/anamnesisai/templates/medication_request.json b/src/anamnesisai/templates/medication_request.json new file mode 100644 index 0000000..3801772 --- /dev/null +++ b/src/anamnesisai/templates/medication_request.json @@ -0,0 +1,4 @@ +{ + "resourceType": "MedicationRequest", + "id": "" +} diff --git a/src/anamnesisai/templates/observation.json b/src/anamnesisai/templates/observation.json new file mode 100644 index 0000000..4ddd3aa --- /dev/null +++ b/src/anamnesisai/templates/observation.json @@ -0,0 +1,25 @@ +{ + "resourceType": "Observation", + "id": "", + "status": "final", + "subject": { + "reference": "Patient/" + }, + "category": { + "coding": [ + { + "system": "", + "code": "" + } + ] + }, + "code": { + "coding": [ + { + "system": "", + "code": "" + } + ] + }, + "value": "" +} diff --git a/src/anamnesisai/templates/patients.json b/src/anamnesisai/templates/patients.json new file mode 100644 index 0000000..41fb940 --- /dev/null +++ b/src/anamnesisai/templates/patients.json @@ -0,0 +1,37 @@ +{ + "resourceType": "Patient", + "id": "", + "active": true, + "name": [ + { + "use": "official", + "text": "" + } + ], + "telecom": [ + { + "system": "phone", + "value": "" + }, + { + "system": "email", + "value": "" + } + ], + "birthDate": "", + "gender": "", + "maritalStatus": { + "coding": [ + { + "system": "", + "code": "" + } + ] + }, + "address": [ + { + "use": "home", + "text": "" + } + ] +} diff --git a/src/anamnesisai/templates/practitioner.json b/src/anamnesisai/templates/practitioner.json new file mode 100644 index 0000000..54b4347 --- /dev/null +++ b/src/anamnesisai/templates/practitioner.json @@ -0,0 +1,17 @@ +{ + "resourceType": "Practitioner", + "id": "", + "active": true, + "name": [ + { + "use": "official", + "text": "" + } + ], + "telecom": [ + { + "system": "phone", + "value": "" + } + ] +} diff --git a/src/anamnesisai/templates/procedure.json b/src/anamnesisai/templates/procedure.json new file mode 100644 index 0000000..f2ba960 --- /dev/null +++ b/src/anamnesisai/templates/procedure.json @@ -0,0 +1,4 @@ +{ + "resourceType": "Procedure", + "id": "" +}