Skip to content

Commit

Permalink
Consolidating UI + tuning LLM Assistnat prompt with few shot prompting
Browse files Browse the repository at this point in the history
  • Loading branch information
apgupta3303 committed Mar 12, 2024
1 parent 67abe87 commit d64b4bd
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 113 deletions.
36 changes: 16 additions & 20 deletions Intake/Allergy Records/AddAllergy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,28 @@ import SpeziFHIR
import SwiftUI

struct EditAllergyView: View {
@State private var index: Int
@Binding var item: AllergyItem
@Environment(DataStore.self) private var data
@Binding private var showingReaction: Bool
@Environment(NavigationPathWrapper.self) private var navigationPath
@Environment(\.presentationMode) var presentationMode

var body: some View {
NavigationView {
VStack(alignment: .leading, spacing: 10) {
@Bindable var data = data
TextField("Allergy Name", text: $data.allergyData[index].allergy)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding([.horizontal, .top])
ReactionSectionView(index: index)
Spacer()
saveButton
}
.navigationBarTitle("Allergy")
}
VStack(alignment: .leading, spacing: 10) {
let index = data.allergyData.firstIndex(of: item) ?? 0
@Bindable var data = data
TextField("Allergy Name", text: $data.allergyData[index].allergy)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding([.horizontal, .top])
ReactionSectionView(index: index)
Spacer()
saveButton
}
.navigationBarTitle("Allergy Name")

Check warning on line 35 in Intake/Allergy Records/AddAllergy.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Allergy Records/AddAllergy.swift#L25-L35

Added lines #L25 - L35 were not covered by tests
}

private var saveButton: some View {
Button(action: {
showingReaction = false
presentationMode.wrappedValue.dismiss()

Check warning on line 40 in Intake/Allergy Records/AddAllergy.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Allergy Records/AddAllergy.swift#L40

Added line #L40 was not covered by tests
}) {
Text("Save")
.foregroundColor(.white)
Expand All @@ -47,11 +48,6 @@ struct EditAllergyView: View {
}
.padding()
}

init(index: Int, showingReaction: Binding<Bool>) {
self._index = State(initialValue: index)
self._showingReaction = showingReaction
}
}


Expand Down
77 changes: 30 additions & 47 deletions Intake/Allergy Records/AllergyRecords.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct AllergyList: View {
@State private var selectedIndex = 0
@State private var showingChat = false
@State private var presentingAccount = false
@State private var newAllergy = AllergyItem(allergy: "", reaction: [])

Check warning on line 61 in Intake/Allergy Records/AllergyRecords.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Allergy Records/AllergyRecords.swift#L61

Added line #L61 was not covered by tests

@LLMSessionProvider<LLMOpenAISchema> var session: LLMOpenAISession

Expand All @@ -69,7 +70,6 @@ struct AllergyList: View {
.padding()
}
.sheet(isPresented: $showingChat, content: chatSheetView)
.sheet(isPresented: $showingReaction, content: editAllergySheetView)
} else {
ProgressView()
.task {
Expand All @@ -86,48 +86,61 @@ struct AllergyList: View {
Form {
Section(header: Text("What are your current allergies?")) {
allergyEntries
addAllergyButton
Text("*Click the details to view/edit the reactions")
.font(.caption)
.foregroundColor(.gray)
}
}
.navigationBarItems(trailing: EditButton())
.navigationTitle("Allergies")
.navigationBarItems(trailing: addAllergyButton)

Check warning on line 95 in Intake/Allergy Records/AllergyRecords.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Allergy Records/AllergyRecords.swift#L95

Added line #L95 was not covered by tests
.navigationBarItems(trailing: NavigationLink(destination: AllergyLLMAssistant(presentingAccount: $presentingAccount)) {
Text("Chat")
})
}

private var allergyEntries: some View {
ForEach(0..<data.allergyData.count, id: \.self) { index in
allergyButton(index: index)
Group {
@Bindable var data = data
ForEach($data.allergyData) { $item in
NavigationLink(destination: EditAllergyView(item: $item)) {
Label(item.allergy, systemImage: "arrowtriangle.right")
.labelStyle(.titleOnly)
}
}
.onDelete(perform: delete)

Check warning on line 110 in Intake/Allergy Records/AllergyRecords.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Allergy Records/AllergyRecords.swift#L102-L110

Added lines #L102 - L110 were not covered by tests
}
.onDelete(perform: delete)
}

private var addAllergyButton: some View {
Button(action: addAllergyAction) {
HStack {
Image(systemName: "plus.circle.fill")
.accessibilityHidden(true)
Text("Add Field")
}
Button(action: {
let newAllergy = AllergyItem(allergy: "", reaction: [])
navigationPath.path.append(NavigationViews.newAllergy)
data.allergyData.append(newAllergy)
}) {
Image(systemName: "plus")
.accessibilityLabel(Text("Add_allergy"))

Check warning on line 121 in Intake/Allergy Records/AllergyRecords.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Allergy Records/AllergyRecords.swift#L115-L121

Added lines #L115 - L121 were not covered by tests
}
}

init() {
let systemPrompt = """
You are a helpful assistant that filters lists of allergies. You will be given\
an array of strings. Each string will be the name of a allergy.
an array of strings. Each string will be the name of a allergy, but we only want\
to keep the names of relevant allergies.

Check warning on line 129 in Intake/Allergy Records/AllergyRecords.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Allergy Records/AllergyRecords.swift#L128-L129

Added lines #L128 - L129 were not covered by tests
For example, if you are given the following list:
Mammography (procedure), Certification procedure (procedure), Cytopathology\
procedure, preparation of smear, genital source (procedure), Transplant of kidney\
(procedure),
Allergy to substance (finding), Latex (substance), Bee venom (substance), Mold (organism),\
House dust mite (organism)Animal dander (substance), Grass pollen (substance),\
Tree pollen (substance), Aspirin

Check warning on line 134 in Intake/Allergy Records/AllergyRecords.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Allergy Records/AllergyRecords.swift#L132-L134

Added lines #L132 - L134 were not covered by tests
you should return something like this:
Transplant of kidney, Mammography.
Latex, Bee venom, Mold, House dust mite, Animal dander, Grass pollen, Tree pollen, Aspirin
Another example would be if you are given the following list:
Animal dander (substance), Penicillin V, Peanut (substance)
you should return something like this:
Animal dander, Penicillin V, Peanut

Check warning on line 143 in Intake/Allergy Records/AllergyRecords.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Allergy Records/AllergyRecords.swift#L137-L143

Added lines #L137 - L143 were not covered by tests
In your response, return only the name of the allergy. Remove words in parenthesis
like (disorder), so "Aortic valve stenosis (disorder)" would turn to "Aortic valve stenosis".
Expand All @@ -146,35 +159,9 @@ struct AllergyList: View {
)
}

private func allergyEntryRow(index: Int) -> some View {
HStack {
Text(data.allergyData[index].allergy)
.foregroundColor(.black)
Spacer()
Image(systemName: "chevron.right")
.foregroundColor(.gray)
.accessibilityLabel(Text("DETAILS"))
}
}

private func allergyButton(index: Int) -> some View {
Button(action: {
self.selectedIndex = index
self.showingReaction = true
}) {
allergyEntryRow(index: index)
}
}

private func submitAction() {
navigationPath.path.append(NavigationViews.menstrual)
}

private func addAllergyAction() {
data.allergyData.append(AllergyItem(allergy: "", reaction: []))
self.selectedIndex = data.allergyData.count - 1
showingReaction = true
}

private func chatSheetView() -> some View {
LLMAssistantView(
Expand All @@ -184,10 +171,6 @@ struct AllergyList: View {
prompt: .constant("Pretend you are a nurse. Your job is to help the patient understand what allergies they have.")
)
}

private func editAllergySheetView() -> some View {
EditAllergyView(index: selectedIndex, showingReaction: $showingReaction)
}

private func removeTextWithinParentheses(from string: String) -> String {
let pattern = "\\s*\\([^)]+\\)"
Expand Down
2 changes: 2 additions & 0 deletions Intake/Home.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum NavigationViews: String {
case pdfs
case inspect
case general
case newAllergy
}

struct StartButton: View {
Expand Down Expand Up @@ -127,6 +128,7 @@ struct HomeView: View {
case .patient: EditPatientView()
case .pdfs: ScrollablePDF()
case .inspect: InspectSurgeryView(surgery: $data.surgeries[data.surgeries.count - 1], isNew: true)
case .newAllergy: EditAllergyView(item: $data.allergyData[data.allergyData.count - 1])
case .general: PatientInfo()
}
}
Expand Down
2 changes: 2 additions & 0 deletions Intake/LLMFiltering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class LLMFiltering {

func filterConditions() async throws {
let conditions = data.conditionData.map { $0.condition }
print(conditions)

Check warning on line 109 in Intake/LLMFiltering.swift

View check run for this annotation

Codecov / codecov/patch

Intake/LLMFiltering.swift#L109

Added line #L109 was not covered by tests
let filteredNames = try await self.LLMFilter(names: conditions)
let filteredConditions = data.conditionData.filter { self.containsAnyWords(item: $0.condition, words: filteredNames) }
var cleaned = filteredConditions
Expand All @@ -122,6 +123,7 @@ class LLMFiltering {

func filterAllergies() async throws {
let allergies = data.allergyData.map { $0.allergy }
print(allergies)

Check warning on line 126 in Intake/LLMFiltering.swift

View check run for this annotation

Codecov / codecov/patch

Intake/LLMFiltering.swift#L126

Added line #L126 was not covered by tests
let filteredNames = try await self.LLMFilter(names: allergies)
let filteredAllergies = data.allergyData.filter { self.containsAnyWords(item: $0.allergy, words: filteredNames) }
var cleaned = filteredAllergies
Expand Down
43 changes: 33 additions & 10 deletions Intake/Medical History/MedicalHistoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,14 @@ struct MedicalHistoryView: View {
Form {
Section(header: Text("Please list conditions you have had")) {
conditionEntries
addConditionButton
instructionText
}
}
.navigationTitle("Medical History")
.navigationBarItems(trailing: addConditionButton)

Check warning on line 66 in Intake/Medical History/MedicalHistoryView.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Medical History/MedicalHistoryView.swift#L66

Added line #L66 was not covered by tests
.navigationBarItems(trailing: NavigationLink(destination: MedicalHistoryLLMAssistant(presentingAccount: .constant(false))) {
Text("Chat")
})
.navigationBarItems(trailing: EditButton())
}

private var conditionEntries: some View {
Expand All @@ -92,9 +91,8 @@ struct MedicalHistoryView: View {
private var addConditionButton: some View {
Button(action: addConditionAction) {
HStack {
Image(systemName: "plus.circle.fill")
Image(systemName: "plus")

Check warning on line 94 in Intake/Medical History/MedicalHistoryView.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Medical History/MedicalHistoryView.swift#L94

Added line #L94 was not covered by tests
.accessibilityHidden(true)
Text("Add Field")
}
}
}
Expand All @@ -108,18 +106,43 @@ struct MedicalHistoryView: View {
.foregroundColor(.gray)
}

init() {
init() { // swiftlint:disable:this function_body_length

Check warning on line 109 in Intake/Medical History/MedicalHistoryView.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Medical History/MedicalHistoryView.swift#L109

Added line #L109 was not covered by tests
let systemPrompt = """
You are a helpful assistant that filters lists of conditions. You will be given\
an array of strings. Each string will be the name of a condition.
an array of strings. Each string will be the name of a condition, but we only want\
to keep the names of relevant conditions. By relevant, we do not want to add conditions\
that are not severe and super common such as colds and ear infections

Check warning on line 114 in Intake/Medical History/MedicalHistoryView.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Medical History/MedicalHistoryView.swift#L112-L114

Added lines #L112 - L114 were not covered by tests
For example, if you are given the following list:
Mammography (procedure), Certification procedure (procedure), Cytopathology\
procedure, preparation of smear, genital source (procedure), Transplant of kidney\
(procedure),
Atopic dermatitis, Acute viral pharyngitis (disorder), Otitis media, Perennial allergic rhinitis,\
Aortic valve stenosis (disorder), Streptococcal sore throat (disorder)

Check warning on line 118 in Intake/Medical History/MedicalHistoryView.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Medical History/MedicalHistoryView.swift#L117-L118

Added lines #L117 - L118 were not covered by tests
you should return something like this:
Transplant of kidney, Mammography.
Atopic dermatitis, Perennial allergic rhinitis, Aortic valve stenosis
Another example would be if you are given the following list:
Received higher education (finding), Body mass index 30+ - obesity (finding), Gout, Essential\
hypertension (disorder), Chronic kidney disease stage 1 (disorder), Disorder of kidney due to\
diabetes mellitus (disorder), Chronic kidney disease stage 2 (disorder), Microalbuminuria due\
to type 2 diabetes mellitus (disorder), Has a criminal record (finding), Refugee (person),\
Chronic kidney disease stage 3 (disorder), Proteinuria due to type 2 diabetes mellitus\
(disorder), Metabolic syndrome X (disorder), Prediabetes, Limited social contact (finding),\
Reports of violence in the environment (finding), Victim of intimate partner abuse (finding),\
Not in labor force (finding), Social isolation (finding), Acute viral pharyngitis (disorder),\
Unhealthy alcohol drinking behavior (finding), Anemia (disorder), Awaiting transplantation of\
kidney (situation), Chronic kidney disease stage 4 (disorder), Unemployed (finding), Ischemic\
heart disease (disorder), Abnormal findings diagnostic imaging heart+coronary circulat (finding),\
History of renal transplant (situation), Viral sinusitis (disorder), Malignant neoplasm of breast\
(disorder), Acute bronchitis (disorder)
you should return something like this:
Obesity, Gout, hypertension, Chronic kidney disease stage 1, Disorder of kidney due to\
diabetes mellitus, Chronic kidney disease stage 2, Microalbuminuria due to type 2 diabetes mellitus,\
Chronic kidney disease stage 3, Proteinuria due to type 2 diabetes mellitus, Metabolic syndrome X,\
Prediabetes, Victim of intimate partner abuse, Unhealthy alcohol drinking behavior, Anemi, Awaiting\
transplantation of kidney, Chronic kidney disease stage 4,Ischemic heart disease, \
Malignant neoplasm of breast

Check warning on line 145 in Intake/Medical History/MedicalHistoryView.swift

View check run for this annotation

Codecov / codecov/patch

Intake/Medical History/MedicalHistoryView.swift#L121-L145

Added lines #L121 - L145 were not covered by tests
In your response, return only the name of the condition. Remove words in parenthesis
like (disorder), so "Aortic valve stenosis (disorder)" would turn to "Aortic valve stenosis".
Expand Down
12 changes: 6 additions & 6 deletions Intake/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
},
"Add Field" : {

},
"Add_allergy" : {

},
"ADD_REACTION" : {

Expand Down Expand Up @@ -118,9 +121,6 @@
},
"Allergies:" : {

},
"Allergy" : {

},
"Allergy Assistant" : {

Expand Down Expand Up @@ -260,9 +260,6 @@
},
"DELETE_SURGERY" : {

},
"DETAILS" : {

},
"Do you currently smoke or have you smoked in the past?" : {

Expand Down Expand Up @@ -518,6 +515,9 @@
},
"No past surgeries" : {

},
"No reactions" : {

},
"No Reactions" : {

Expand Down
Loading

0 comments on commit d64b4bd

Please sign in to comment.