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

Feature/bump mlx #75

Merged
merged 29 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 23 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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/ml-explore/mlx-swift", from: "0.18.1"),
.package(url: "https://github.com/ml-explore/mlx-swift-examples", from: "1.16.0"),
LeonNissen marked this conversation as resolved.
Show resolved Hide resolved
.package(url: "https://github.com/ml-explore/mlx-swift-examples", from: "1.18.1"),
.package(url: "https://github.com/huggingface/swift-transformers", .upToNextMinor(from: "0.1.12")),
.package(url: "https://github.com/StanfordBDHG/OpenAI", .upToNextMinor(from: "0.2.9")),
.package(url: "https://github.com/StanfordSpezi/Spezi", from: "1.2.1"),
Expand Down
2 changes: 1 addition & 1 deletion Sources/SpeziLLM/Models/LLMContextEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
case tool(id: String, name: String)


var rawValue: String {
public var rawValue: String {

Check warning on line 49 in Sources/SpeziLLM/Models/LLMContextEntity.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziLLM/Models/LLMContextEntity.swift#L49

Added line #L49 was not covered by tests
LeonNissen marked this conversation as resolved.
Show resolved Hide resolved
switch self {
case .user: "user"
case .assistant: "assistant"
Expand Down

This file was deleted.

16 changes: 13 additions & 3 deletions Sources/SpeziLLMLocal/Configuration/LLMLocalParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@
}()
}


LeonNissen marked this conversation as resolved.
Show resolved Hide resolved
/// The to-be-used system prompt of the LLM
let systemPrompt: String?
/// Indicates the maximum output length generated by the LLM.
let maxOutputLength: Int

/// Additional End of Sequence tokens at which the generation will be stoped.
let extraEOSTokens: Set<String>
/// Interval for displaying output after every N tokens generated.
let displayEveryNTokens: Int
/// The chat template to use for the model in the Jinja format
let chatTemplate: String?
/// RNG seed of the LLM
let seed: UInt64?

LeonNissen marked this conversation as resolved.
Show resolved Hide resolved

/// Creates the ``LLMLocalParameters`` which wrap the underlying llama.cpp `llama_model_params` C struct.
/// Is passed to the underlying llama.cpp model in order to configure the LLM.
Expand All @@ -36,15 +40,21 @@
/// - maxOutputLength: The maximum output length generated by the Spezi LLM, defaults to `512`.
/// - extraEOSTokens: Additional tokens to use for end of string
/// - displayEveryNTokens: Interval for displaying output after every N tokens generated, defaults to `4`.
/// - seed: RNG seed of the LLM, defaults to a random seed.
/// - chatTemplate: Can be set to manually overwrite the chatTemplate within the `swift-transformers` package.
public init(
systemPrompt: String? = Defaults.defaultSystemPrompt,
maxOutputLength: Int = 512,
extraEOSTokens: Set<String> = [],
displayEveryNTokens: Int = 4
displayEveryNTokens: Int = 4,
seed: UInt64? = nil,
chatTemplate: String? = nil
) {
self.systemPrompt = systemPrompt
self.maxOutputLength = maxOutputLength
self.extraEOSTokens = extraEOSTokens
self.displayEveryNTokens = displayEveryNTokens
self.seed = seed
self.chatTemplate = chatTemplate

Check warning on line 58 in Sources/SpeziLLMLocal/Configuration/LLMLocalParameters.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziLLMLocal/Configuration/LLMLocalParameters.swift#L57-L58

Added lines #L57 - L58 were not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
LeonNissen marked this conversation as resolved.
Show resolved Hide resolved
// This source file is part of the Stanford Spezi open source project
//
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import SpeziLLM

extension LLMContext {
/// Formats the current ``LLMContext`` for compatibility with Transformers-based chat models.
///
/// - Returns: An array of dictionaries where each dictionary represents a message in the format:
/// - `role`: The role of the message (e.g., "user", "assistant"), derived from the `rawValue` of the entry's `role`.
/// - `content`: The textual content of the message.
public func formatForTransformersChat() -> [[String: String]] {
LeonNissen marked this conversation as resolved.
Show resolved Hide resolved
self.map { entry in
[
"role": entry.role.rawValue,
"content": entry.content
]
}
}

Check warning on line 24 in Sources/SpeziLLMLocal/Helpers/LLMContext+formatForTransformersChat.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziLLMLocal/Helpers/LLMContext+formatForTransformersChat.swift#L17-L24

Added lines #L17 - L24 were not covered by tests
}
18 changes: 14 additions & 4 deletions Sources/SpeziLLMLocal/LLMLocalPlatform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import Spezi
import SpeziFoundation
import SpeziLLM

#if targetEnvironment(simulator)
import OSLog
#endif

/// LLM execution platform of an ``LLMLocalSchema``.
///
Expand Down Expand Up @@ -58,18 +60,26 @@

public nonisolated func configure() {
#if targetEnvironment(simulator)
assertionFailure("SpeziLLMLocal: Code cannot be run on simulator.")
#endif
Logger(
subsystem: "Spezi",
category: "LLMLocalPlatform"
).warning("SpeziLLMLocal is only supported on physical devices. Use `LLMMockPlatform` instead.")

Check warning on line 66 in Sources/SpeziLLMLocal/LLMLocalPlatform.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziLLMLocal/LLMLocalPlatform.swift#L63-L66

Added lines #L63 - L66 were not covered by tests
LeonNissen marked this conversation as resolved.
Show resolved Hide resolved
#else
if let cacheLimit = configuration.cacheLimit {
MLX.GPU.set(cacheLimit: cacheLimit * 1024 * 1024)
}
if let memoryLimit = configuration.memoryLimit {
MLX.GPU.set(memoryLimit: memoryLimit.limit, relaxed: memoryLimit.relaxed)
}
#endif
}

public nonisolated func callAsFunction(with llmSchema: LLMLocalSchema) -> LLMLocalSession {
public nonisolated func callAsFunction(with llmSchema: LLMLocalSchema) -> some LLMSession {

Check warning on line 77 in Sources/SpeziLLMLocal/LLMLocalPlatform.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziLLMLocal/LLMLocalPlatform.swift#L77

Added line #L77 was not covered by tests
#if targetEnvironment(simulator)
LeonNissen marked this conversation as resolved.
Show resolved Hide resolved
LLMLocalMockSession(self, schema: llmSchema)

Check warning on line 79 in Sources/SpeziLLMLocal/LLMLocalPlatform.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziLLMLocal/LLMLocalPlatform.swift#L79

Added line #L79 was not covered by tests
#else
LLMLocalSession(self, schema: llmSchema)
#endif
}

deinit {
Expand Down
Loading
Loading