Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lift to SpeziLLM v0.8, Data race fix #20

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct FHIRGetResourceLLMFunction: LLMFunction {

_resources = Parameter(
description: String(localized: "PARAMETER_DESCRIPTION"),
enumValues: Array(allResourcesFunctionCallIdentifiers)
enum: Array(allResourcesFunctionCallIdentifiers)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import SwiftUI


private enum FHIRMultipleResourceInterpreterConstants {
static let chat = "FHIRMultipleResourceInterpreter.chat"
static let context = "FHIRMultipleResourceInterpreter.context"
}


Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// SPDX-License-Identifier: MIT
//

import Foundation
import SpeziChat
import SpeziFHIR
import SpeziLLM
Expand All @@ -21,6 +22,7 @@ class FHIRResourceProcessor<Content: Codable & LosslessStringConvertible> {
private let llmRunner: LLMRunner
private let storageKey: String
private let prompt: FHIRPrompt
private let lock = NSLock()
var llmSchema: any LLMSchema


Expand Down Expand Up @@ -59,14 +61,17 @@ class FHIRResourceProcessor<Content: Codable & LosslessStringConvertible> {

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
}
}
Loading