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

Adds new local models and updates documentation #97

Merged
merged 33 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b912e74
bump mlx to version 2.21.2
Jan 27, 2025
62b0cf2
Fixes warnings
vishnuravi Jan 28, 2025
7f13d57
Revert "Fixes warnings"
vishnuravi Jan 28, 2025
f3d54c0
Autofix warnings
vishnuravi Jan 28, 2025
1915b64
Suppress cyclomatic complexity
vishnuravi Jan 28, 2025
127a3d2
Update SpeziChat
vishnuravi Jan 29, 2025
6e55c08
Update Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift
vishnuravi Jan 30, 2025
11fcf00
Revert "Update Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift"
vishnuravi Jan 30, 2025
03d596a
Fix return from void function warning
vishnuravi Feb 1, 2025
ecf7a77
Refactor into private functions
vishnuravi Feb 2, 2025
2a2ea86
Refactor processing of remaining tokens into private function
vishnuravi Feb 2, 2025
b453e7a
Move error handling and parameter generation to private functions
vishnuravi Feb 2, 2025
66af686
Remove broken links to Apple documentation
vishnuravi Feb 2, 2025
aceabc3
Remove broken link from documentation
vishnuravi Feb 2, 2025
68f5362
Add license header
vishnuravi Feb 2, 2025
71980dc
Custom LinkSpector config
vishnuravi Feb 2, 2025
1790751
Add deepseek r1 distill qwen 1.5B model
vishnuravi Feb 2, 2025
4a78e5f
Add DeepSeek-R1-Distill-Qwen-7B-4bit and DeepSeek-R1-Distill-Llama-8B…
vishnuravi Feb 3, 2025
14f6a49
Adds LLMLocalPlatform to Configuration
vishnuravi Feb 3, 2025
4f88780
Update development team
vishnuravi Feb 3, 2025
b73c685
Merge branch 'main' into deepseek
vishnuravi Feb 3, 2025
970e3f8
Update example in docs
vishnuravi Feb 4, 2025
a28e4e4
Merge branch 'deepseek' of https://github.com/StanfordSpezi/SpeziLLM …
vishnuravi Feb 4, 2025
f27067e
Update docs for LLMLocalDownloadView
vishnuravi Feb 4, 2025
0eea68b
Update docs
vishnuravi Feb 5, 2025
d3cc941
Fix formatting in README
vishnuravi Feb 5, 2025
eaf1420
Update README.md
vishnuravi Feb 8, 2025
791fbfa
Update README per comments
vishnuravi Feb 8, 2025
824e4ba
Add Aloe Beta 8B and Med42 8B
vishnuravi Feb 11, 2025
96b6e47
Add Qwen2 7B 4bit
vishnuravi Feb 11, 2025
15154cf
Update README
vishnuravi Feb 11, 2025
fec3680
Fix qwen 1.5 case name
vishnuravi Feb 11, 2025
2535c21
Switch to GPT-4o
vishnuravi Feb 12, 2025
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
52 changes: 48 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ As Spezi LLM contains a variety of different targets for specific LLM functional

Spezi LLM provides a number of targets to help developers integrate LLMs in their Spezi-based applications:
- [SpeziLLM](https://swiftpackageindex.com/stanfordspezi/spezillm/documentation/spezillm): Base infrastructure of LLM execution in the Spezi ecosystem.
- [SpeziLLMLocal](https://swiftpackageindex.com/stanfordspezi/spezillm/documentation/spezillmlocal): Local LLM execution capabilities directly on-device. Enables running open-source LLMs like [Meta's Llama2 models](https://ai.meta.com/llama/).
- [SpeziLLMLocal](https://swiftpackageindex.com/stanfordspezi/spezillm/documentation/spezillmlocal): Local LLM execution capabilities directly on-device. Enables running open-source LLMs like [Meta's Llama2](https://ai.meta.com/llama/), [Microsoft's Phi](https://azure.microsoft.com/en-us/products/phi), [Google's Gemma](https://ai.google.dev/gemma), or [DeepSeek-R1](https://huggingface.co/deepseek-ai/DeepSeek-R1), among others.
- [SpeziLLMLocalDownload](https://swiftpackageindex.com/stanfordspezi/spezillm/documentation/spezillmlocaldownload): Download and storage manager of local Language Models, including onboarding views.
- [SpeziLLMOpenAI](https://swiftpackageindex.com/stanfordspezi/spezillm/documentation/spezillmopenai): Integration with OpenAI's GPT models via using OpenAI's API service.
- [SpeziLLMFog](https://swiftpackageindex.com/stanfordspezi/spezillm/documentation/spezillmfog): Discover and dispatch LLM inference jobs to Fog node resources within the local network.
Expand Down Expand Up @@ -82,6 +82,37 @@ class TestAppDelegate: SpeziAppDelegate {
}
```

[SpeziLLMLocalDownload](https://swiftpackageindex.com/stanfordspezi/spezillm/documentation/spezillmlocaldownload) can be used to download an LLM from [HuggingFace](https://huggingface.co/) and save it on the device for execution. The `LLMLocalDownloadView` provides an out-of-the-box onboarding view for downloading models locally.

```swift
struct LLMLocalOnboardingDownloadView: View {
@Environment(OnboardingNavigationPath.self) private var onboardingNavigationPath

var body: some View {
LLMLocalDownloadView(
model: .llama3_8B_4bit,
downloadDescription: "The Llama3 8B model will be downloaded",
) {
onboardingNavigationPath.nextStep()
}
}
}
```

This view can then be included in your onboarding process using [SpeziOnboarding](https://swiftpackageindex.com/stanfordspezi/spezionboarding/documentation).

```swift
struct LLMLocalDownloadApp: View {
@State private var path = NavigationPath()

var body: some View {
NavigationStack(path: $path) {
LLMLocalOnboardingDownloadView()
}
}
}
```

#### Usage

The code example below showcases the interaction with local LLMs through the the [SpeziLLM](https://swiftpackageindex.com/stanfordspezi/spezillm/documentation/spezillm) [`LLMRunner`](https://swiftpackageindex.com/stanfordspezi/spezillm/documentation/spezillm/llmrunner), which is injected into the SwiftUI `Environment` via the `Configuration` shown above.
Expand All @@ -100,7 +131,6 @@ struct LLMLocalDemoView: View {
let llmSession: LLMLocalSession = runner(
with: LLMLocalSchema(
model: .llama3_8B_4bit,
formatChat: LLMLocalSchema.PromptFormattingDefaults.llama3
)
)

Expand All @@ -116,8 +146,22 @@ struct LLMLocalDemoView: View {
}
```

The [`LLMChatViewSchema`](https://swiftpackageindex.com/stanfordspezi/spezillm/main/documentation/spezillm/llmchatviewschema) can be used to easily create a conversational chat interface for your chatbot application with a local LLM.

```swift
struct LLMLocalChatView: View {
var body: some View {
LLMChatViewSchema(
with: LLMLocalSchema(
model: .llama3_8B_4bit
)
)
}
}
```

> [!NOTE]
> To learn more about the usage of SpeziLLMLocal, please refer to the [DocC documentation](https://swiftpackageindex.com/stanfordspezi/spezillm/documentation/spezillmlocal).
> To learn more about the usage of SpeziLLMLocal, please refer to the comprehensive [DocC documentation](https://swiftpackageindex.com/stanfordspezi/spezillm/documentation/spezillmlocal).

### Spezi LLM Open AI

Expand Down Expand Up @@ -171,7 +215,7 @@ struct LLMOpenAIDemoView: View {
let llmSession: LLMOpenAISession = runner(
with: LLMOpenAISchema(
parameters: .init(
modelType: .gpt3_5Turbo,
modelType: .gpt4_Turbo,
systemPrompt: "You're a helpful assistant that answers questions from users.",
overwritingToken: "abc123"
)
Expand Down
12 changes: 12 additions & 0 deletions Sources/SpeziLLMLocal/Configuration/LLMLocalModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public enum LLMLocalModel {
case qwen205b4bit
/// OpenELM, 270 Million Parameters, Instruction-Tuned
case openelm270m4bit
/// DeepSeek R1 Distill Qwen 1.5 Billion Parameters, 8-bit Version
case deepseek_r1_distill_qwen_1_5b_8bit
/// DeepSeek R1 Distill Qwen 7 Billion Parameters, 4-bit Version
case deepseek_r1_distill_qwen_7b_4bit
/// DeepSeek R1 Distill Llama 8 Billion Parameters, 4-bit Version
case deepseek_r1_distill_llama_8b_4bit
/// Set the Huggingface ID of the model. e.g. "\<USER\>/\<MODEL\>"
case custom(id: String)

Expand Down Expand Up @@ -80,6 +86,12 @@ public enum LLMLocalModel {
return "mlx-community/Qwen1.5-0.5B-Chat-4bit"
case .openelm270m4bit:
return "mlx-community/OpenELM-270M-Instruct"
case .deepseek_r1_distill_qwen_1_5b_8bit:
return "mlx-community/DeepSeek-R1-Distill-Qwen-1.5B-8bit"
case .deepseek_r1_distill_qwen_7b_4bit:
return "mlx-community/DeepSeek-R1-Distill-Qwen-7B-4bit"
case .deepseek_r1_distill_llama_8b_4bit:
return "mlx-community/DeepSeek-R1-Distill-Llama-8B-4bit-mlx"
case .custom(let id):
return id
}
Expand Down
15 changes: 6 additions & 9 deletions Sources/SpeziLLMLocalDownload/LLMLocalDownloadView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import SwiftUI
///
/// It can be combined with the SpeziOnboarding `OnboardingStack` to create an easy onboarding flow within the application.
///
/// The ``LLMLocalDownloadView/init(downloadDescription:llmDownloadUrl:llmStorageUrl:action:)-9hraf`` initializer accepts a download description displayed in the view, the remote download `URL` of the LLM, the local storage `URL` of the downloaded model, as well as an action closure to move onto the next (onboarding) step.
/// The ``LLMLocalDownloadView/init(downloadDescription:llmDownloadUrl:llmStorageUrl:action:)-9hraf`` initializer accepts a download description displayed in the view, the `LLMLocalModel` representing the model to be downloaded, and an action closure to move onto the next (onboarding) step.
///
/// The heavy lifting of downloading and storing the model is done by the ``LLMLocalDownloadManager`` which exposes the current downloading state view the ``LLMLocalDownloadManager/state`` property of type ``LLMLocalDownloadManager/DownloadState``.
///
Expand All @@ -39,9 +39,8 @@ import SwiftUI
///
/// var body: some View {
/// LLMLocalDownloadView(
/// downloadDescription: "The Llama2 7B model will be downloaded",
/// llmDownloadUrl: LLMLocalDownloadManager.LLMUrlDefaults.llama2ChatModelUrl, // Download the Llama2 7B model
/// llmStorageUrl: .cachesDirectory.appending(path: "llm.gguf") // Store the downloaded LLM in the caches directory
/// model: .llama3_8B_4bit,
/// downloadDescription: "The Llama3 8B model will be downloaded"
/// ) {
/// onboardingNavigationPath.nextStep()
/// }
Expand Down Expand Up @@ -177,9 +176,8 @@ public struct LLMLocalDownloadView: View {
/// Creates a ``LLMLocalDownloadView`` that presents an onboarding view that helps with downloading the necessary LLM files from remote servers.
///
/// - Parameters:
/// - model: An `LLMLocalModel` representing the model to download.
/// - downloadDescription: Localized description of the to-be-downloaded model shown in the ``LLMLocalDownloadView``.
/// - llmDownloadUrl: The remote `URL` from where the LLM file should be downloaded.
/// - llmDownloadLocation: The local `URL` where the LLM file should be stored.
/// - action: The action that should be performed when pressing the primary button of the view.
public init(
model: LLMLocalModel,
Expand All @@ -196,9 +194,8 @@ public struct LLMLocalDownloadView: View {
/// Creates a ``LLMLocalDownloadView`` that presents an onboarding view that helps with downloading the necessary LLM files from remote servers.
///
/// - Parameters:
/// - model: An `LLMLocalModel` representing the model to download.
/// - downloadDescription: Description of the to-be-downloaded model shown in the ``LLMLocalDownloadView``.
/// - llmDownloadUrl: The remote `URL` from where the LLM file should be downloaded.
/// - llmDownloadLocation: The local `URL` where the LLM file should be stored.
/// - action: The action that should be performed when pressing the primary button of the view.
@_disfavoredOverload
public init<S: StringProtocol>(
Expand All @@ -218,7 +215,7 @@ public struct LLMLocalDownloadView: View {
#if DEBUG
#Preview {
LLMLocalDownloadView(
model: .phi3_4bit,
model: .llama3_8B_4bit,
downloadDescription: "LLM_DOWNLOAD_DESCRIPTION".localized(.module),
action: {}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ struct LLMLocalDownloadApp: View {
}
}
}
```

```swift
struct LLMLocalOnboardingDownloadView: View {
@Environment(OnboardingNavigationPath.self) private var onboardingNavigationPath

var body: some View {
LLMLocalDownloadView(
downloadDescription: "The Llama2 7B model will be downloaded",
llmDownloadUrl: LLMLocalDownloadManager.LLMUrlDefaults.llama2ChatModelUrl, // Download the Llama2 7B model
llmStorageUrl: .cachesDirectory.appending(path: "llm.gguf") // Store the downloaded LLM in the caches directory
model: .llama3_8B_4bit,
downloadDescription: "The Llama3 8B model will be downloaded"
) {
onboardingNavigationPath.nextStep()
}
Expand Down
1 change: 1 addition & 0 deletions Tests/UITests/TestApp/TestAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class TestAppDelegate: SpeziAppDelegate {
// No CA certificate (meaning no encrypted traffic) for development purposes, see `caCertificateUrl` above
LLMFogPlatform(configuration: .init(host: "spezillmfog.local", caCertificate: nil))
LLMOpenAIPlatform()
LLMLocalPlatform() // Note: Spezi LLM Local is not compatible with simulators.
}
}
}
Expand Down
Loading