spezillm
Large Language Model (LLM) module for the Spezi Ecosystem
Science Score: 77.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 3 DOI reference(s) in README -
✓Academic publication links
Links to: zenodo.org -
✓Committers with academic emails
2 of 11 committers (18.2%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (13.1%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
Large Language Model (LLM) module for the Spezi Ecosystem
Basic Info
- Host: GitHub
- Owner: StanfordSpezi
- License: mit
- Language: Swift
- Default Branch: main
- Homepage: https://swiftpackageindex.com/StanfordSpezi/SpeziLLM/documentation
- Size: 1.66 MB
Statistics
- Stars: 261
- Watchers: 15
- Forks: 39
- Open Issues: 17
- Releases: 36
Topics
Metadata Files
README.md
Spezi LLM
Overview
The Spezi LLM Swift Package includes modules that are helpful to integrate LLM-related functionality in your application. The package provides all necessary tools for local LLM execution, the usage of remote OpenAI-based LLMs, as well as LLMs running on Fog node resources within the local network.
|


OpenAI LLM Chat View|Language Model Download|Local LLM Chat View|
Setup
1. Add Spezi LLM as a Dependency
You need to add the SpeziLLM Swift package to your app in Xcode or Swift package.
[!IMPORTANT]
If your application is not yet configured to use Spezi, follow the Spezi setup article to set up the core Spezi infrastructure.
2. Follow the setup steps of the individual targets
As Spezi LLM contains a variety of different targets for specific LLM functionalities, please follow the additional setup guide in the respective target section of this README.
Targets
Spezi LLM provides a number of targets to help developers integrate LLMs in their Spezi-based applications: - SpeziLLM: Base infrastructure of LLM execution in the Spezi ecosystem. - SpeziLLMLocal: Local LLM execution capabilities directly on-device. Enables running open-source LLMs from Hugging Face like Meta's Llama2, Microsoft's Phi, Google's Gemma, or DeepSeek-R1, among others. See LLMLocalModel for a list of models tested with SpeziLLM. - SpeziLLMLocalDownload: Download and storage manager of local Language Models, including onboarding views. - SpeziLLMOpenAI: Integration with OpenAI's GPT models via using OpenAI's API service. - SpeziLLMFog: Discover and dispatch LLM inference jobs to Fog node resources within the local network.
The section below highlights the setup and basic use of the SpeziLLMLocal, SpeziLLMOpenAI, and SpeziLLMFog targets in order to integrate Language Models in a Spezi-based application.
[!NOTE]
To learn more about the usage of the individual targets, please refer to the DocC documentation of the package.
Spezi LLM Local
The target enables developers to easily execute medium-size Language Models (LLMs) locally on-device. The module allows you to interact with the locally run LLM via purely Swift-based APIs, no interaction with low-level code is necessary, building on top of the infrastructure of the SpeziLLM target.
[!IMPORTANT]
Spezi LLM Local is not compatible with simulators. The underlyingmlx-swiftrequires a modern Metal MTLGPUFamily and the simulator does not provide that.[!IMPORTANT] To use the LLM local target, some LLMs require adding the Increase Memory Limit entitlement to the project.
Setup
You can configure the Spezi Local LLM execution within the typical SpeziAppDelegate.
In the example below, the LLMRunner from the SpeziLLM target which is responsible for providing LLM functionality within the Spezi ecosystem is configured with the LLMLocalPlatform from the SpeziLLMLocal target. This prepares the LLMRunner to locally execute Language Models.
swift
class TestAppDelegate: SpeziAppDelegate {
override var configuration: Configuration {
Configuration {
LLMRunner {
LLMLocalPlatform()
}
}
}
}
SpeziLLMLocalDownload can be used to download an LLM from HuggingFace 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 {
var body: some View {
LLMLocalDownloadView(
model: .llama3_8B_4bit,
downloadDescription: "The Llama3 8B model will be downloaded",
) {
// Action to perform after the model is downloaded and the user presses the next button.
}
}
}
[!TIP] The
LLMLocalDownloadViewview can be included in your onboarding process using SpeziOnboarding as demonstrated in this example.
Usage
The code example below showcases the interaction with local LLMs through the the SpeziLLM LLMRunner, which is injected into the SwiftUI Environment via the Configuration shown above.
The LLMLocalSchema defines the type and configurations of the to-be-executed LLMLocalSession. This transformation is done via the LLMRunner that uses the LLMLocalPlatform. The inference via LLMLocalSession/generate() returns an AsyncThrowingStream that yields all generated String pieces.
```swift struct LLMLocalDemoView: View { @Environment(LLMRunner.self) var runner @State var responseText = ""
var body: some View {
Text(responseText)
.task {
// Instantiate the `LLMLocalSchema` to an `LLMLocalSession` via the `LLMRunner`.
let llmSession: LLMLocalSession = runner(
with: LLMLocalSchema(
model: .llama3_8B_4bit,
)
)
do {
for try await token in try await llmSession.generate() {
responseText.append(token)
}
} catch {
// Handle errors here. E.g., you can use `ViewState` and `viewStateAlert` from SpeziViews.
}
}
}
} ```
The 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
)
)
}
}
Offloading
To optimize inference performance and minimize resource consumption within the application, use the LLMLocalSession.offload() method. This function unloads the model from memory, thereby freeing up system resources when the model is not actively in use.
When further interaction with the model is required, calling either LLMLocalSession.setup() or LLMLocalSession.generate() will automatically reload the model into memory as needed.
[!NOTE]
To learn more about the usage of SpeziLLMLocal, please refer to the comprehensive DocC documentation.
Spezi LLM Open AI
A module that allows you to interact with GPT-based Large Language Models (LLMs) from OpenAI within your Spezi application.
SpeziLLMOpenAI provides a pure Swift-based API for interacting with the OpenAI GPT API, building on top of the infrastructure of the SpeziLLM target.
In addition, SpeziLLMOpenAI provides developers with a declarative Domain Specific Language to utilize OpenAI function calling mechanism. This enables a structured, bidirectional, and reliable communication between the OpenAI LLMs and external tools, such as the Spezi ecosystem.
Setup
In order to use OpenAI LLMs within the Spezi ecosystem, the SpeziLLM LLMRunner needs to be initialized in the Spezi Configuration with the LLMOpenAIPlatform. Only after, the LLMRunner can be used for inference of OpenAI LLMs.
See the SpeziLLM documentation for more details.
```swift import Spezi import SpeziLLM import SpeziLLMOpenAI
class LLMOpenAIAppDelegate: SpeziAppDelegate { override var configuration: Configuration { Configuration { LLMRunner { LLMOpenAIPlatform() } } } } ```
[!IMPORTANT] If using
SpeziLLMOpenAIon macOS, ensure to add theKeychain Access Groupsentitlement to the enclosing Xcode project via PROJECT_NAME > Signing&Capabilities > + Capability. The array of keychain groups can be left empty, only the base entitlement is required.
Usage
The code example below showcases the interaction with an OpenAI LLM through the the SpeziLLM LLMRunner, which is injected into the SwiftUI Environment via the Configuration shown above.
The LLMOpenAISchema defines the type and configurations of the to-be-executed LLMOpenAISession. This transformation is done via the LLMRunner that uses the LLMOpenAIPlatform. The inference via LLMOpenAISession/generate() returns an AsyncThrowingStream that yields all generated String pieces.
```swift import SpeziLLM import SpeziLLMOpenAI import SwiftUI
struct LLMOpenAIDemoView: View { @Environment(LLMRunner.self) var runner @State var responseText = ""
var body: some View {
Text(responseText)
.task {
// Instantiate the `LLMOpenAISchema` to an `LLMOpenAISession` via the `LLMRunner`.
let llmSession: LLMOpenAISession = runner(
with: LLMOpenAISchema(
parameters: .init(
modelType: .gpt4o,
systemPrompt: "You're a helpful assistant that answers questions from users.",
overwritingToken: "abc123"
)
)
)
do {
for try await token in try await llmSession.generate() {
responseText.append(token)
}
} catch {
// Handle errors here. E.g., you can use `ViewState` and `viewStateAlert` from SpeziViews.
}
}
}
} ```
[!NOTE]
To learn more about the usage of SpeziLLMOpenAI, please refer to the DocC documentation.
Spezi LLM Fog
The SpeziLLMFog target enables you to use LLMs running on Fog node computing resources within the local network. The fog nodes advertise their services via mDNS, enabling clients to discover all fog nodes serving a specific host within the local network.
SpeziLLMFog then dispatches LLM inference jobs dynamically to a random fog node within the local network and streams the response to surface it to the user.
[!IMPORTANT]
SpeziLLMFogrequires aSpeziLLMFogNodewithin the local network hosted on some computing resource that actually performs the inference requests.SpeziLLMFogprovides theSpeziLLMFogNodeDocker-based package that enables an easy setup of these fog nodes. See theFogNodedirectory on the root level of the SPM package as well as the respectiveREADME.mdfor more details.[!IMPORTANT]
SpeziLLMFogperforms dynamic discovery of available fog node services in the local network using Bonjour. To enable this functionality, the consuming application must configure the followingInfo.plistentries: -NSLocalNetworkUsageDescription(String): A description explaining why the app requires access to the local network. For example:"This app uses local network access to discover nearby services."-NSBonjourServices(Array<String>): Specifies the Bonjour service types the app is allowed to discover. For use withSpeziLLMFog, include the following entry: -_https._tcp(for discovering secured services via TLS) -_http._tcp(optional, for testing purposes only; discovers unsecured services)
Setup
In order to use Fog LLMs within the Spezi ecosystem, the SpeziLLM LLMRunner needs to be initialized in the Spezi Configuration with the LLMFogPlatform. Only after, the LLMRunner can be used for inference with Fog LLMs. See the SpeziLLM documentation for more details.
The LLMFogPlatform needs to be initialized with the custom root CA certificate that was used to sign the fog node web service certificate (see the FogNode/README.md documentation for more information). Copy the root CA certificate from the fog node as resource to the application using SpeziLLMFog and use it to initialize the LLMFogPlatform within the Spezi Configuration.
``swift
class LLMFogAppDelegate: SpeziAppDelegate {
private nonisolated static var caCertificateUrl: URL {
// Return local file URL of root CA certificate in the.crt` format
}
override var configuration: Configuration {
Configuration {
LLMRunner {
// Set up the Fog platform with the custom CA certificate
LLMRunner {
LLMFogPlatform(configuration: .init(connectionType: .http, authToken: .none))
// If required, specify `.https` connection type, including the certificate
}
}
}
}
} ```
In addition to set local network discovery entitlements described above, users must grant explicit authorization for local network access.
This authorization can be requested during the app’s onboarding process using LLMFogDiscoveryAuthorizationView.
It informs users about the need for local network access, prompts them to grant it, and attempts to verify the access status (note: the OS does not expose this information).
For detailed guidance on integrating the LLMFogDiscoveryAuthorizationView in an onboarding flow managed by [SpeziOnboarding](https://swiftpackageindex.com/stanfordspezi/spezionboarding), refer to the in-line documentation of the LLMFogDiscoveryAuthorizationView.
Usage
The code example below showcases the interaction with a Fog LLM through the the SpeziLLM LLMRunner, which is injected into the SwiftUI Environment via the Configuration shown above.
The LLMFogSchema defines the type and configurations of the to-be-executed LLMFogSession. This transformation is done via the LLMRunner that uses the LLMFogPlatform. The inference via LLMFogSession/generate() returns an AsyncThrowingStream that yields all generated String pieces.
The LLMFogSession automatically discovers all available LLM fog nodes within the local network upon setup and the dispatches the LLM inference jobs to the fog computing resource, streaming back the response and surfaces it to the user.
[!IMPORTANT]
TheLLMFogSchemaaccepts a closure that returns an authorization token that is passed with every request to the Fog node in theBearerHTTP field via theLLMFogParameters/init(modelType:overwritingAuthToken:systemPrompt:). The token is created via the closure upon every LLM inference request, as theLLMFogSessionmay be long lasting and the token could therefore expire. Ensure that the closure appropriately caches the token in order to prevent unnecessary token refresh roundtrips to external systems.
```swift struct LLMFogDemoView: View { @Environment(LLMRunner.self) var runner @State var responseText = ""
var body: some View {
Text(responseText)
.task {
// Instantiate the `LLMFogSchema` to an `LLMFogSession` via the `LLMRunner`.
let llmSession: LLMFogSession = runner(
with: LLMFogSchema(
parameters: .init(
modelType: .llama7B,
overwritingAuthToken: .none, // potentially overwrite default auth token from `LLMFogPlatform`
systemPrompt: "You're a helpful assistant that answers questions from users."
)
)
)
do {
for try await token in try await llmSession.generate() {
responseText.append(token)
}
} catch {
// Handle errors here. E.g., you can use `ViewState` and `viewStateAlert` from SpeziViews.
}
}
}
} ```
[!NOTE]
To learn more about the usage of SpeziLLMFog, please refer to the DocC documentation.
Contributing
Contributions to this project are welcome. Please make sure to read the contribution guidelines and the contributor covenant code of conduct first.
License
This project is licensed under the MIT License. See Licenses for more information.

Owner
- Name: Stanford Spezi
- Login: StanfordSpezi
- Kind: organization
- Repositories: 1
- Profile: https://github.com/StanfordSpezi
Citation (CITATION.cff)
# # This source file is part of the Stanford Spezi open source project # # SPDX-FileCopyrightText: 2022 Stanford University and the project authors (see CONTRIBUTORS.md) # # SPDX-License-Identifier: MIT # cff-version: 1.2.0 message: "If you use this software, please cite it as below." authors: - family-names: "Schmiedmayer" given-names: "Paul" orcid: "https://orcid.org/0000-0002-8607-9148" - family-names: "Zagar" given-names: "Philipp" orcid: "https://orcid.org/0009-0001-5934-2078" - family-names: "Ravi" given-names: "Vishnu" orcid: "https://orcid.org/0000-0003-0359-1275" - family-names: "Nissen" given-names: "Leon" orcid: "https://orcid.org/0009-0009-3884-0850" - family-names: "Letzelter" given-names: "Sébastien" - family-names: "Heidekrüger" given-names: "Paul" orcid: "https://orcid.org/0009-0000-8556-7353" - family-names: "Rao" given-names: "Adrit" orcid: "https://orcid.org/0000-0002-0780-033X" - family-names: "Riedman" given-names: "Nick" - family-names: "Kollmer" given-names: "Lukas" orcid: "https://orcid.org/0000-0002-1065-1320" - family-names: "Disho" given-names: "Joan" orcid: "https://orcid.org/0009-0000-9315-2449" - family-names: "Rosenblattl" given-names: "Max" title: "SpeziLLM" doi: 10.5281/zenodo.7954213 url: "https://github.com/StanfordSpezi/SpeziLLM"
GitHub Events
Total
- Create event: 29
- Release event: 7
- Issues event: 33
- Watch event: 104
- Delete event: 21
- Member event: 3
- Issue comment event: 142
- Push event: 185
- Pull request review event: 221
- Pull request review comment event: 227
- Pull request event: 54
- Fork event: 25
Last Year
- Create event: 29
- Release event: 7
- Issues event: 33
- Watch event: 104
- Delete event: 21
- Member event: 3
- Issue comment event: 142
- Push event: 185
- Pull request review event: 221
- Pull request review comment event: 227
- Pull request event: 54
- Fork event: 25
Committers
Last synced: 9 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Paul Schmiedmayer | P****r | 22 |
| Philipp Zagar | z****r@s****u | 12 |
| Vishnu Ravi | v****i@g****m | 6 |
| dependabot[bot] | 4****] | 4 |
| Leon Nissen | 5****n | 4 |
| Sébastien Letzelter | 3****z | 3 |
| Paul Heidekrüger | p****r@t****e | 2 |
| Adrit Rao | 6****o | 2 |
| nriedman | 1****n | 1 |
| Lukas Kollmer | h****y@l****e | 1 |
| Joan Disho | d****n@g****m | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 42
- Total pull requests: 94
- Average time to close issues: 5 months
- Average time to close pull requests: 11 days
- Total issue authors: 14
- Total pull request authors: 15
- Average comments per issue: 1.33
- Average comments per pull request: 1.88
- Merged pull requests: 66
- Bot issues: 1
- Bot pull requests: 10
Past Year
- Issues: 25
- Pull requests: 48
- Average time to close issues: about 1 month
- Average time to close pull requests: 13 days
- Issue authors: 11
- Pull request authors: 12
- Average comments per issue: 1.4
- Average comments per pull request: 2.02
- Merged pull requests: 26
- Bot issues: 1
- Bot pull requests: 7
Top Authors
Issue Authors
- philippzagar (16)
- PSchmiedmayer (11)
- bryan1anderson (2)
- Wall-E-59222 (2)
- Bourne-alt (1)
- LeonNissen (1)
- pedrodotavila (1)
- LeoLee429 (1)
- vishnuravi (1)
- luionTW (1)
- Turquoise-T (1)
- joshgalvan (1)
- riderx (1)
- dguo8412 (1)
Pull Request Authors
- philippzagar (25)
- PSchmiedmayer (24)
- dependabot[bot] (19)
- LeonNissen (12)
- vishnuravi (11)
- Seb-Ltz (8)
- paulhdk (7)
- AdritRao (3)
- bryan1anderson (2)
- lukaskollmer (2)
- max-rosenblattl (2)
- jdisho (2)
- aullman (1)
- nriedman (1)
- leehori19 (1)