Skip to content

Commit

Permalink
did someone say resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
zoyagarg committed Mar 8, 2024
2 parents c27c394 + 52b4aa6 commit 4bc5328
Show file tree
Hide file tree
Showing 11 changed files with 443 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
{
"identity" : "spezillm",
"kind" : "remoteSourceControl",
"location" : "https://github.com/StanfordSpezi/SpeziLLM",
"location" : "https://github.com/StanfordSpezi/SpeziLLM.git",
"state" : {
"revision" : "6892c5dfe258371b6f3287f02b8fec57a611ba70",
"version" : "0.7.0"
Expand Down
48 changes: 48 additions & 0 deletions Intake/Allergy Records/AllergyLLMAssistant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ import SpeziLLMLocal
import SpeziLLMOpenAI
import SwiftUI

@Observable
class AllergyItemBox: Equatable {
var allergyItem: AllergyItem?

init() {}

static func == (lhs: AllergyItemBox, rhs: AllergyItemBox) -> Bool {
lhs.allergyItem == rhs.allergyItem
}
}

func getCurrentPatientAllergy(allergyList: [AllergyItem]) -> String? {
var allergyDetails = "The patient has several allergies described in the next sentences."

Expand All @@ -33,6 +44,30 @@ func getCurrentPatientAllergy(allergyList: [AllergyItem]) -> String? {
return allergyDetails.isEmpty ? nil : allergyDetails
}

struct UpdateAllergyFunction: LLMFunction {
static let name: String = "update_allergies"
static let description: String = """
If the patient wants to add an allergy and they've given you the allergy name and the reaction they have to the allergy, \
call the update_allergies function to add it.
"""

@Parameter(description: "The allergy name the patient wants to create.") var allergyName: String
@Parameter(description: "The reaction of the allergy the patient wants to create.") var allergyReaction: String

let allergyItemBox: AllergyItemBox

init(allergyItemBox: AllergyItemBox) {
self.allergyItemBox = allergyItemBox
}


func execute() async throws -> String? {
let updatedAllergy = AllergyItem(allergy: allergyName, reaction: [ReactionItem(reaction: allergyReaction)])
allergyItemBox.allergyItem = updatedAllergy
return nil
}
}

struct AllergyLLMAssistant: View {
@Environment(DataStore.self) private var data
@Environment(NavigationPathWrapper.self) private var navigationPath
Expand All @@ -43,6 +78,8 @@ struct AllergyLLMAssistant: View {
@State var showOnboarding = true
@State var greeting = true

@State var allergyItemBox: AllergyItemBox

var body: some View {
@Bindable var data = data

Expand All @@ -68,21 +105,32 @@ struct AllergyLLMAssistant: View {
}
greeting = false
}
.onChange(of: allergyItemBox.allergyItem) { _, newValue in
if let allergyItem = newValue {
data.allergyData.append(allergyItem)
}
}
}

init(presentingAccount: Binding<Bool>) {
self._presentingAccount = presentingAccount
let temporaryAllergyItemBox = AllergyItemBox()
self.allergyItemBox = temporaryAllergyItemBox
self._session = LLMSessionProvider(
schema: LLMOpenAISchema(
parameters: .init(
modelType: .gpt3_5Turbo,
systemPrompt: """
Pretend you are a nurse. Your job is to answer information about the patient's allergies.\
You have the ability to add a allergy if the patient tells you to by calling the update_allergies function.\
Only call the update_allergies function if the patient has given you both the allergy name and the allergy reaction type.\
You do not have the ability to delete an allergy from the patient's list.\
Please use everyday layman terms and avoid using complex medical terminology.\
Only ask one question or prompt at a time, and keep your questions brief (one to two short sentences).
"""
)
) {
UpdateAllergyFunction(allergyItemBox: temporaryAllergyItemBox)
}
)
}
Expand Down
6 changes: 5 additions & 1 deletion Intake/Allergy Records/AllergyRecords.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ import ModelsR4
import SpeziFHIR
import SwiftUI

struct AllergyItem: Identifiable {
struct AllergyItem: Identifiable, Equatable {
let id = UUID()
var allergy: String
var reaction: [ReactionItem]

static func == (lhs: AllergyItem, rhs: AllergyItem) -> Bool {
lhs.allergy == rhs.allergy
}
}

// struct ReactionViewDetails {
Expand Down
2 changes: 2 additions & 0 deletions Intake/Home.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum NavigationViews: String {
case medication
case chat
case concern
case inspect
}

struct HomeView: View {
Expand Down Expand Up @@ -97,6 +98,7 @@ struct HomeView: View {
case .medication: MedicationContentView()
case .menstrual: SocialHistoryQuestionView()
case .concern: SummaryView(chiefComplaint: $data.chiefComplaint)
case .inspect: InspectSurgeryView(surgery: $data.surgeries[data.surgeries.count - 1], isNew: true)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Intake/Intake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class DataStore {
var medicationData: Set<IntakeMedicationInstance> = []
var surgeries: [SurgeryItem] = []
var chiefComplaint: String = ""
var surgeriesLoaded = false
}

@main
Expand Down
56 changes: 56 additions & 0 deletions Intake/Medical History/MedicalHistoryLLMAssistant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ import SpeziLLMLocal
import SpeziLLMOpenAI
import SwiftUI

@Observable
class MedicalHistoryItemBox: Equatable {
var medicalHistoryItem: MedicalHistoryItem?

init() {}

static func == (lhs: MedicalHistoryItemBox, rhs: MedicalHistoryItemBox) -> Bool {
lhs.medicalHistoryItem == rhs.medicalHistoryItem
}
}


func getCurrentPatientMedicalHistory(medHistoryList: [MedicalHistoryItem]) -> String? {
var medHistoryDetails = "The patient has had several conditions in their medical history described in the following sentences."

Expand All @@ -34,6 +46,37 @@ func getCurrentPatientMedicalHistory(medHistoryList: [MedicalHistoryItem]) -> St
return medHistoryDetails.isEmpty ? nil : medHistoryDetails
}

struct UpdateMedicalHistoryFunction: LLMFunction {
static let name: String = "update_medical_history"
static let description: String = """
If the patient wants to add to their medical history and they've given you the condition name\
and if it's an active or inactive condition\
call the update_medical_history function to add it.
"""

@Parameter(description: "The medical history condition name the patient wants to create.") var condition: String
@Parameter(description: "If the condition is active or inactive.") var active: String

let medicalHistoryItemBox: MedicalHistoryItemBox

init(medicalHistoryItemBox: MedicalHistoryItemBox) {
self.medicalHistoryItemBox = medicalHistoryItemBox
}


func execute() async throws -> String? {
var activeBool: Bool
if active == "active" {
activeBool = true
} else {
activeBool = false
}
let updatedMedicalHistory = MedicalHistoryItem(condition: condition, active: activeBool)
medicalHistoryItemBox.medicalHistoryItem = updatedMedicalHistory
return nil
}
}

struct MedicalHistoryLLMAssistant: View {
@Environment(DataStore.self) private var data
@Environment(NavigationPathWrapper.self) private var navigationPath
Expand All @@ -44,6 +87,8 @@ struct MedicalHistoryLLMAssistant: View {
@State var showOnboarding = true
@State var greeting = true

@State var medicalHistoryItemBox: MedicalHistoryItemBox

var body: some View {
@Bindable var data = data

Expand All @@ -69,21 +114,32 @@ struct MedicalHistoryLLMAssistant: View {
}
greeting = false
}
.onChange(of: medicalHistoryItemBox.medicalHistoryItem) { _, newValue in
if let medicalHistoryItem = newValue {
data.conditionData.append(medicalHistoryItem)
}
}
}

init(presentingAccount: Binding<Bool>) {
self._presentingAccount = presentingAccount
let temporaryMedicalHistoryItemBox = MedicalHistoryItemBox()
self.medicalHistoryItemBox = temporaryMedicalHistoryItemBox
self._session = LLMSessionProvider(
schema: LLMOpenAISchema(
parameters: .init(
modelType: .gpt3_5Turbo,
systemPrompt: """
Pretend you are a nurse. Your job is to answer information about the patient's medical history.\
You have the ability to add a medical history condition by calling the update_medical_history function.\
Only call the update_medical_history function if you know both the condition name and if it's active or inactive.\
You do not have the ability to delete a medical history from the patient's list.\
Please use everyday layman terms and avoid using complex medical terminology.\
Only ask one question or prompt at a time, and keep your questions brief (one to two short sentences).
"""
)
) {
UpdateMedicalHistoryFunction(medicalHistoryItemBox: temporaryMedicalHistoryItemBox)
}
)
}
Expand Down
2 changes: 1 addition & 1 deletion Intake/Medical History/MedicalHistoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import ModelsR4
import SpeziFHIR
import SwiftUI

struct MedicalHistoryItem: Identifiable {
struct MedicalHistoryItem: Identifiable, Equatable {
var id = UUID()
var condition: String
var active: Bool
Expand Down
1 change: 1 addition & 0 deletions Intake/Medication View/MedicationLLMAssistant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ struct MedicationLLMAssistant: View {
modelType: .gpt3_5Turbo,
systemPrompt: """
Pretend you are a nurse. Your job is to answer information about the patient's medications.\
You do not have the ability to add or delete medications, so please tell the patient that.\
Please use everyday layman terms and avoid using complex medical terminology.\
Only ask one question or prompt at a time, and keep your questions brief (one to two short sentences).
"""
Expand Down
33 changes: 32 additions & 1 deletion Intake/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"sourceLanguage" : "en",
"strings" : {
"" : {

},
"*Check the box if you currently have the condition. /\n*Uncheck the box if you had the condition in the past" : {

},
Expand Down Expand Up @@ -208,6 +211,15 @@
},
"Download your medical records from your health system." : {

},
"End Date: %@" : {

},
"Edit Surgery" : {

},
"END_CALENDAR" : {

},
"Ex: Heavy bleeding on second day, fatigue..." : {

Expand Down Expand Up @@ -311,6 +323,9 @@
},
"List your current medications." : {

},
"Location" : {

},
"Main reason(s) for visit" : {

Expand Down Expand Up @@ -394,6 +409,9 @@
}
}
}
},
"New Surgery" : {

},
"Next" : {

Expand Down Expand Up @@ -461,12 +479,21 @@
},
"Pack years: %.2f" : {

},
"Performed" : {

},
"Please add your past surgeries" : {

},
"Please list conditions you have had" : {

},
"Primary Concern" : {

},
"Procedure" : {

},
"PROJECT_LICENSE_DESCRIPTION" : {
"localizations" : {
Expand Down Expand Up @@ -571,6 +598,9 @@
},
"Start" : {

},
"Status" : {

},
"Submit" : {

Expand Down Expand Up @@ -666,11 +696,12 @@
"What brings you in today? Identify your primary concern(s)." : {

},

"What is your surgical history?" : {

},
"Yes" : {

}
},
"version" : "1.0"
Expand Down
Loading

0 comments on commit 4bc5328

Please sign in to comment.