Skip to content

Commit

Permalink
Revert "Fixes warnings"
Browse files Browse the repository at this point in the history
This reverts commit 7bb83b6.
  • Loading branch information
vishnuravi committed Jan 28, 2025
1 parent 7bb83b6 commit bc72467
Showing 1 changed file with 7 additions and 99 deletions.
106 changes: 7 additions & 99 deletions Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ extension LLMLocalSession {
// swiftlint:disable:next identifier_name function_body_length
internal func _generate(continuation: AsyncThrowingStream<String, any Error>.Continuation) async {

Check failure on line 21 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Cyclomatic Complexity Violation: Function should have complexity 10 or less; currently complexity is 11 (cyclomatic_complexity)
#if targetEnvironment(simulator)
return await _mockGenerate(continuation: continuation) // swiftlint:disable:this return_value_from_void_function
// swiftlint:disable:next return_value_from_void_function
return await _mockGenerate(continuation: continuation)
#endif

guard let modelContainer = await self.modelContainer else {

Check warning on line 27 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package iOS (Debug, SpeziLLM-iOS.xcresult, SpeziLLM-iOS.xcresult) / Test using xcodebuild or run fastlane

code after 'return' will never be executed

Check warning on line 27 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package iOS (Release, SpeziLLM-iOS-Release.xcresult, SpeziLLM-iOS-Release.xc... / Test using xcodebuild or run fastlane

code after 'return' will never be executed

Check warning on line 27 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package visionOS (Debug, SpeziLLM-visionOS.xcresult, SpeziLLM-visionOS.xcres... / Test using xcodebuild or run fastlane

code after 'return' will never be executed

Check warning on line 27 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package visionOS (Release, SpeziLLM-visionOS-Release.xcresult, SpeziLLM-visi... / Test using xcodebuild or run fastlane

code after 'return' will never be executed

Check warning on line 27 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS (Debug, TestApp-iOS.xcresult, TestApp-iOS.xcresult) / Test using xcodebuild or run fastlane

code after 'return' will never be executed

Check warning on line 27 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS (Release, TestApp-iOS-Release.xcresult, TestApp-iOS-Release.xcresult) / Test using xcodebuild or run fastlane

code after 'return' will never be executed

Check warning on line 27 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS (Release, TestApp-iOS-Release.xcresult, TestApp-iOS-Release.xcresult) / Test using xcodebuild or run fastlane

code after 'return' will never be executed

Check warning on line 27 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iPadOS (Debug, TestApp-iPad.xcresult, TestApp-iPad.xcresult) / Test using xcodebuild or run fastlane

code after 'return' will never be executed

Check warning on line 27 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iPadOS (Release, TestApp-iPad-Release.xcresult, TestApp-iPad-Release.xcre... / Test using xcodebuild or run fastlane

code after 'return' will never be executed

Check warning on line 27 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iPadOS (Release, TestApp-iPad-Release.xcresult, TestApp-iPad-Release.xcre... / Test using xcodebuild or run fastlane

code after 'return' will never be executed

Check warning on line 27 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS (Debug, TestApp-visionOS.xcresult, TestApp-visionOS.xcresult) / Test using xcodebuild or run fastlane

code after 'return' will never be executed

Check warning on line 27 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS (Release, TestApp-visionOS-Release.xcresult, TestApp-visionOS-Re... / Test using xcodebuild or run fastlane

code after 'return' will never be executed

Check warning on line 27 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS (Release, TestApp-visionOS-Release.xcresult, TestApp-visionOS-Re... / Test using xcodebuild or run fastlane

code after 'return' will never be executed
Self.logger.error("SpeziLLMLocal: Failed to load `modelContainer`")
await finishGenerationWithError(LLMLocalError.modelNotFound, on: continuation)
Expand Down Expand Up @@ -61,12 +63,12 @@ extension LLMLocalSession {
)

do {
// swiftlint:disable:next closure_body_length
let result = try await modelContainer.perform { modelContext in

Check failure on line 67 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Superfluous Disable Command Violation: SwiftLint rule 'closure_body_length' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)

Check warning on line 67 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS (Debug, TestApp-iOS.xcresult, TestApp-iOS.xcresult) / Test using xcodebuild or run fastlane

Superfluous Disable Command Violation: SwiftLint rule 'closure_body_length' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)

Check warning on line 67 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iPadOS (Debug, TestApp-iPad.xcresult, TestApp-iPad.xcresult) / Test using xcodebuild or run fastlane

Superfluous Disable Command Violation: SwiftLint rule 'closure_body_length' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)

Check warning on line 67 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS (Debug, TestApp-visionOS.xcresult, TestApp-visionOS.xcresult) / Test using xcodebuild or run fastlane

Superfluous Disable Command Violation: SwiftLint rule 'closure_body_length' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)
let result = try MLXLMCommon.generate(
input: modelInput,
parameters: parameters,
context: modelContext
) { tokens in
context: modelContext) { tokens in

Check failure on line 71 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Multiline Arguments Brackets Violation: Multiline arguments should have their surrounding brackets in a new line (multiline_arguments_brackets)

Check warning on line 71 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS (Debug, TestApp-iOS.xcresult, TestApp-iOS.xcresult) / Test using xcodebuild or run fastlane

Multiline Arguments Brackets Violation: Multiline arguments should have their surrounding brackets in a new line (multiline_arguments_brackets)

Check warning on line 71 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iPadOS (Debug, TestApp-iPad.xcresult, TestApp-iPad.xcresult) / Test using xcodebuild or run fastlane

Multiline Arguments Brackets Violation: Multiline arguments should have their surrounding brackets in a new line (multiline_arguments_brackets)

Check warning on line 71 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS (Debug, TestApp-visionOS.xcresult, TestApp-visionOS.xcresult) / Test using xcodebuild or run fastlane

Multiline Arguments Brackets Violation: Multiline arguments should have their surrounding brackets in a new line (multiline_arguments_brackets)
if Task.isCancelled {
return .stop
}
Expand All @@ -89,9 +91,9 @@ extension LLMLocalSession {
}
}
}

return .more
}
}

Check failure on line 96 in Sources/SpeziLLMLocal/LLMLocalSession+Generate.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Closure End Indentation Violation: Closure end should have the same indentation as the line that started it; expected 16, got 20 (closure_end_indentation)

// Yielding every Nth token may result in missing the final tokens.
let reaminingTokens = result.tokens.count % schema.parameters.displayEveryNTokens
Expand Down Expand Up @@ -128,100 +130,6 @@ extension LLMLocalSession {
}
}

private func prepareModelInput(modelContainer: ModelContainer) async -> LMInput? {
let messages = if await !self.customContext.isEmpty {
await self.customContext
} else {
await self.context.formattedChat
}

return try? await modelContainer.perform { modelContext in
if let chatTemplate = self.schema.parameters.chatTemplate {
let tokens = try modelContext.tokenizer.applyChatTemplate(messages: messages, chatTemplate: chatTemplate)
return LMInput(text: .init(tokens: MLXArray(tokens)))
} else {
return try await modelContext.processor.prepare(input: .init(messages: messages))
}
}
}

private func generateResponse(
modelContainer: ModelContainer,
modelInput: LMInput,
continuation: AsyncThrowingStream<String, any Error>.Continuation
) async {
let parameters = GenerateParameters(
temperature: schema.samplingParameters.temperature,
topP: schema.samplingParameters.topP,
repetitionPenalty: schema.samplingParameters.penaltyRepeat,
repetitionContextSize: schema.samplingParameters.repetitionContextSize
)

do {
let result = try await modelContainer.perform { modelContext in
try MLXLMCommon.generate(
input: modelInput,
parameters: parameters,
context: modelContext,
callback: { [weak self] tokens in
self?.handleGeneratedTokens(
tokens: tokens,
modelContext: modelContext,
continuation: continuation
)
}
)
}

Self.logger.debug(
"""
SpeziLLMLocal:
Prompt Tokens per second: \(result.promptTokensPerSecond, privacy: .public)
Generation tokens per second: \(result.tokensPerSecond, privacy: .public)
"""
)

await MainActor.run {
continuation.finish()
state = .ready
}
} catch {
Self.logger.error("SpeziLLMLocal: Generation ended with error: \(error)")
await finishGenerationWithError(LLMLocalError.generationError, on: continuation)
}
}

private func handleGeneratedTokens(
tokens: [Int],
modelContext: ModelContext,
continuation: AsyncThrowingStream<String, any Error>.Continuation
) -> MLXLMCommon.GenerateCallback.Action {
if Task.isCancelled {
return .stop
}

if tokens.count >= self.schema.parameters.maxOutputLength {
Self.logger.debug("SpeziLLMLocal: Max output length exceeded.")
return .stop
}

if tokens.count.isMultiple(of: schema.parameters.displayEveryNTokens) {
let lastTokens = Array(tokens.suffix(schema.parameters.displayEveryNTokens))
let text = modelContext.tokenizer.decode(tokens: lastTokens)

Self.logger.debug("SpeziLLMLocal: Yielded token: \(text, privacy: .public)")
continuation.yield(text)

if schema.injectIntoContext {
Task { @MainActor in
context.append(assistantOutput: text)
}
}
}

return .more
}

private func _mockGenerate(continuation: AsyncThrowingStream<String, any Error>.Continuation) async {
let tokens = [
"Mock ", "Message ", "from ", "SpeziLLM! ",
Expand Down

0 comments on commit bc72467

Please sign in to comment.