diff --git a/Package.swift b/Package.swift index 65e6abe..b9b0b18 100644 --- a/Package.swift +++ b/Package.swift @@ -28,9 +28,9 @@ let package = Package( .package(url: "https://github.com/StanfordBDHG/HealthKitOnFHIR", .upToNextMinor(from: "0.2.4")), .package(url: "https://github.com/StanfordSpezi/Spezi", from: "1.2.1"), .package(url: "https://github.com/StanfordSpezi/SpeziHealthKit.git", .upToNextMinor(from: "0.5.1")), - .package(url: "https://github.com/StanfordSpezi/SpeziLLM.git", .upToNextMinor(from: "0.7.0")), + .package(url: "https://github.com/StanfordSpezi/SpeziLLM.git", .upToNextMinor(from: "0.8.1")), .package(url: "https://github.com/StanfordSpezi/SpeziStorage.git", from: "1.0.0"), - .package(url: "https://github.com/StanfordSpezi/SpeziChat.git", .upToNextMinor(from: "0.1.8")), + .package(url: "https://github.com/StanfordSpezi/SpeziChat.git", .upToNextMinor(from: "0.2.0")), .package(url: "https://github.com/StanfordSpezi/SpeziSpeech.git", from: "1.0.0") ], targets: [ diff --git a/Sources/SpeziFHIRLLM/FHIRInterpretation/MultipleResources/FHIRGetResourceLLMFunction.swift b/Sources/SpeziFHIRLLM/FHIRInterpretation/MultipleResources/FHIRGetResourceLLMFunction.swift index 67c17dd..6770748 100644 --- a/Sources/SpeziFHIRLLM/FHIRInterpretation/MultipleResources/FHIRGetResourceLLMFunction.swift +++ b/Sources/SpeziFHIRLLM/FHIRInterpretation/MultipleResources/FHIRGetResourceLLMFunction.swift @@ -43,7 +43,7 @@ struct FHIRGetResourceLLMFunction: LLMFunction { _resources = Parameter( description: String(localized: "PARAMETER_DESCRIPTION"), - enumValues: Array(allResourcesFunctionCallIdentifiers) + enum: Array(allResourcesFunctionCallIdentifiers) ) } diff --git a/Sources/SpeziFHIRLLM/FHIRInterpretation/MultipleResources/FHIRMultipleResourceInterpreter.swift b/Sources/SpeziFHIRLLM/FHIRInterpretation/MultipleResources/FHIRMultipleResourceInterpreter.swift index c16a75e..aaf91b0 100644 --- a/Sources/SpeziFHIRLLM/FHIRInterpretation/MultipleResources/FHIRMultipleResourceInterpreter.swift +++ b/Sources/SpeziFHIRLLM/FHIRInterpretation/MultipleResources/FHIRMultipleResourceInterpreter.swift @@ -18,7 +18,7 @@ import SwiftUI private enum FHIRMultipleResourceInterpreterConstants { - static let chat = "FHIRMultipleResourceInterpreter.chat" + static let context = "FHIRMultipleResourceInterpreter.context" } @@ -65,7 +65,7 @@ public class FHIRMultipleResourceInterpreter { let llm = llmRunner(with: llmSchema) // Read initial conversation from storage - if let storedContext: Chat = try? localStorage.read(storageKey: FHIRMultipleResourceInterpreterConstants.chat) { + if let storedContext: LLMContext = try? localStorage.read(storageKey: FHIRMultipleResourceInterpreterConstants.context) { llm.context = storedContext } else { llm.context.append(systemMessage: FHIRPrompt.interpretMultipleResources.prompt) @@ -80,7 +80,7 @@ public class FHIRMultipleResourceInterpreter { @MainActor func queryLLM() { guard let llm, - llm.context.last?.role == .user || !(llm.context.contains(where: { $0.role == .assistant }) ) else { + llm.context.last?.role == .user || !(llm.context.contains(where: { $0.role == .assistant() }) ) else { return } @@ -96,7 +96,7 @@ public class FHIRMultipleResourceInterpreter { } // Store conversation to storage - try localStorage.store(llm.context, storageKey: FHIRMultipleResourceInterpreterConstants.chat) + try localStorage.store(llm.context, storageKey: FHIRMultipleResourceInterpreterConstants.context) } } diff --git a/Sources/SpeziFHIRLLM/FHIRInterpretation/MultipleResources/MultipleResourcesChatView.swift b/Sources/SpeziFHIRLLM/FHIRInterpretation/MultipleResources/MultipleResourcesChatView.swift index b472dee..33db7c6 100644 --- a/Sources/SpeziFHIRLLM/FHIRInterpretation/MultipleResources/MultipleResourcesChatView.swift +++ b/Sources/SpeziFHIRLLM/FHIRInterpretation/MultipleResources/MultipleResourcesChatView.swift @@ -28,12 +28,13 @@ public struct MultipleResourcesChatView: View { NavigationStack { Group { if let llm = multipleResourceInterpreter.llm { - let contextBinding = Binding { llm.context } set: { llm.context = $0 } + let contextBinding = Binding { llm.context.chat } set: { llm.context.chat = $0 } + ChatView( contextBinding, disableInput: llm.state.representation == .processing ) - .speak(llm.context, muted: !textToSpeech) + .speak(llm.context.chat, muted: !textToSpeech) .speechToolbarButton(muted: !$textToSpeech) .viewStateAlert(state: llm.state) .onChange(of: llm.context, initial: true) { _, _ in diff --git a/Sources/SpeziFHIRLLM/FHIRProcessor/FHIRResourceProcessor.swift b/Sources/SpeziFHIRLLM/FHIRProcessor/FHIRResourceProcessor.swift index 0fc419a..657671b 100644 --- a/Sources/SpeziFHIRLLM/FHIRProcessor/FHIRResourceProcessor.swift +++ b/Sources/SpeziFHIRLLM/FHIRProcessor/FHIRResourceProcessor.swift @@ -6,6 +6,7 @@ // SPDX-License-Identifier: MIT // +import Foundation import SpeziChat import SpeziFHIR import SpeziLLM @@ -21,6 +22,7 @@ class FHIRResourceProcessor { private let llmRunner: LLMRunner private let storageKey: String private let prompt: FHIRPrompt + private let lock = NSLock() var llmSchema: any LLMSchema @@ -59,14 +61,17 @@ class FHIRResourceProcessor { let chatStreamResult: String = try await llmRunner.oneShot( with: llmSchema, - chat: .init(systemMessages: [prompt.prompt(withFHIRResource: resource.jsonDescription)]) + context: .init(systemMessages: [prompt.prompt(withFHIRResource: resource.jsonDescription)]) ) guard let content = Content(chatStreamResult) else { throw FHIRResourceProcessorError.notParsableAsAString } - results[resource.id] = content + lock.withLock { + results[resource.id] = content + } + return content } }