Skip to content

Commit

Permalink
Swift 6 support (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukepistrol authored Nov 27, 2024
1 parent 4620016 commit 3af02c9
Show file tree
Hide file tree
Showing 34 changed files with 102 additions and 69 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ on:
branches:
- main

env:
DEVELOPER_DIR: /Applications/Xcode_16.1.app/Contents/Developer

jobs:
test-ios:
runs-on: macos-13
runs-on: macos-15

steps:
- uses: actions/checkout@v3

- name: Build and test
run: xcodebuild test -scheme OllamaKit -destination 'platform=iOS Simulator,name=iPhone 14 Pro'
run: xcodebuild test -scheme OllamaKit -destination 'platform=iOS Simulator,name=iPhone 16 Pro'

test-macos:
runs-on: macos-13
runs-on: macos-15

steps:
- uses: actions/checkout@v3
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ concurrency:
group: "pages"
cancel-in-progress: false

env:
DEVELOPER_DIR: /Applications/Xcode_16.1.app/Contents/Developer

jobs:
deploy:
runs-on: macos-13
runs-on: macos-15

environment:
name: github-pages
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.9
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
29 changes: 29 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "OllamaKit",
platforms: [
.iOS(.v15),
.macOS(.v12),
.macCatalyst(.v15)
],
products: [
.library(
name: "OllamaKit",
targets: ["OllamaKit"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-docc-plugin.git", .upToNextMajor(from: "1.3.0"))
],
targets: [
.target(
name: "OllamaKit",
dependencies: []),
.testTarget(
name: "OllamaKitTests",
dependencies: ["OllamaKit"]),
]
)
4 changes: 2 additions & 2 deletions Playground/OKPlayground.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.kevinhermawan.OKPlayground;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand Down Expand Up @@ -396,7 +396,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.kevinhermawan.OKPlayground;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand Down
1 change: 1 addition & 0 deletions Playground/OKPlayground/ViewModels/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import OllamaKit

@Observable
@MainActor
final class ViewModel {
var ollamaKit = OllamaKit()

Expand Down
8 changes: 4 additions & 4 deletions Sources/OllamaKit/OllamaKit+Chat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ extension OllamaKit {
/// - Returns: An `AsyncThrowingStream<OKChatResponse, Error>` emitting the live stream of chat responses from the Ollama API.
public func chat(data: OKChatRequestData) -> AsyncThrowingStream<OKChatResponse, Error> {
do {
let request = try OKRouter.chat(data: data).asURLRequest()
let request = try OKRouter.chat(data: data).asURLRequest(with: baseURL)

return OKHTTPClient.shared.stream(request: request, with: OKChatResponse.self)
} catch {
return AsyncThrowingStream { continuation in
Expand Down Expand Up @@ -197,8 +197,8 @@ extension OllamaKit {
/// - Returns: An `AnyPublisher<OKChatResponse, Error>` emitting the live stream of chat responses from the Ollama API.
public func chat(data: OKChatRequestData) -> AnyPublisher<OKChatResponse, Error> {
do {
let request = try OKRouter.chat(data: data).asURLRequest()
let request = try OKRouter.chat(data: data).asURLRequest(with: baseURL)

return OKHTTPClient.shared.stream(request: request, with: OKChatResponse.self)
} catch {
return Fail(error: error).eraseToAnyPublisher()
Expand Down
6 changes: 3 additions & 3 deletions Sources/OllamaKit/OllamaKit+CopyModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ extension OllamaKit {
/// - Parameter data: The ``OKCopyModelRequestData`` containing the details needed to copy the model.
/// - Throws: An error if the request to copy the model fails.
public func copyModel(data: OKCopyModelRequestData) async throws -> Void {
let request = try OKRouter.copyModel(data: data).asURLRequest()
let request = try OKRouter.copyModel(data: data).asURLRequest(with: baseURL)

try await OKHTTPClient.shared.send(request: request)
}

Expand All @@ -48,7 +48,7 @@ extension OllamaKit {
/// - Returns: A `AnyPublisher<Void, Error>` that completes when the copy operation is done.
public func copyModel(data: OKCopyModelRequestData) -> AnyPublisher<Void, Error> {
do {
let request = try OKRouter.copyModel(data: data).asURLRequest()
let request = try OKRouter.copyModel(data: data).asURLRequest(with: baseURL)

return OKHTTPClient.shared.send(request: request)
} catch {
Expand Down
6 changes: 3 additions & 3 deletions Sources/OllamaKit/OllamaKit+DeleteModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ extension OllamaKit {
/// - Parameter data: The ``OKDeleteModelRequestData`` containing the details needed to delete the model.
/// - Throws: An error if the request to delete the model fails.
public func deleteModel(data: OKDeleteModelRequestData) async throws -> Void {
let request = try OKRouter.deleteModel(data: data).asURLRequest()
let request = try OKRouter.deleteModel(data: data).asURLRequest(with: baseURL)

try await OKHTTPClient.shared.send(request: request)
}

Expand All @@ -48,7 +48,7 @@ extension OllamaKit {
/// - Returns: A `AnyPublisher<Void, Error>` that completes when the deletion operation is done.
public func deleteModel(data: OKDeleteModelRequestData) -> AnyPublisher<Void, Error> {
do {
let request = try OKRouter.deleteModel(data: data).asURLRequest()
let request = try OKRouter.deleteModel(data: data).asURLRequest(with: baseURL)

return OKHTTPClient.shared.send(request: request)
} catch {
Expand Down
6 changes: 3 additions & 3 deletions Sources/OllamaKit/OllamaKit+Embeddings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ extension OllamaKit {
/// - Returns: An ``OKEmbeddingsResponse`` containing the embeddings from the model.
/// - Throws: An error if the request fails or the response can't be decoded.
public func embeddings(data: OKEmbeddingsRequestData) async throws -> OKEmbeddingsResponse {
let request = try OKRouter.embeddings(data: data).asURLRequest()
let request = try OKRouter.embeddings(data: data).asURLRequest(with: baseURL)

return try await OKHTTPClient.shared.send(request: request, with: OKEmbeddingsResponse.self)
}

Expand All @@ -49,7 +49,7 @@ extension OllamaKit {
/// - Returns: A `AnyPublisher<OKEmbeddingsResponse, Error>` that emits embeddings.
public func embeddings(data: OKEmbeddingsRequestData) -> AnyPublisher<OKEmbeddingsResponse, Error> {
do {
let request = try OKRouter.embeddings(data: data).asURLRequest()
let request = try OKRouter.embeddings(data: data).asURLRequest(with: baseURL)

return OKHTTPClient.shared.send(request: request, with: OKEmbeddingsResponse.self)
} catch {
Expand Down
6 changes: 3 additions & 3 deletions Sources/OllamaKit/OllamaKit+Generate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ extension OllamaKit {
/// - Returns: An `AsyncThrowingStream<OKGenerateResponse, Error>` emitting the live stream of responses from the Ollama API.
public func generate(data: OKGenerateRequestData) -> AsyncThrowingStream<OKGenerateResponse, Error> {
do {
let request = try OKRouter.generate(data: data).asURLRequest()
let request = try OKRouter.generate(data: data).asURLRequest(with: baseURL)

return OKHTTPClient.shared.stream(request: request, with: OKGenerateResponse.self)
} catch {
return AsyncThrowingStream { continuation in
Expand Down Expand Up @@ -63,7 +63,7 @@ extension OllamaKit {
/// - Returns: An `AnyPublisher<OKGenerateResponse, Error>` emitting the live stream of responses from the Ollama API.
public func generate(data: OKGenerateRequestData) -> AnyPublisher<OKGenerateResponse, Error> {
do {
let request = try OKRouter.generate(data: data).asURLRequest()
let request = try OKRouter.generate(data: data).asURLRequest(with: baseURL)

return OKHTTPClient.shared.stream(request: request, with: OKGenerateResponse.self)
} catch {
Expand Down
6 changes: 3 additions & 3 deletions Sources/OllamaKit/OllamaKit+ModelInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ extension OllamaKit {
/// - Returns: An ``OKModelInfoResponse`` containing detailed information about the model.
/// - Throws: An error if the request fails or the response can't be decoded.
public func modelInfo(data: OKModelInfoRequestData) async throws -> OKModelInfoResponse {
let request = try OKRouter.modelInfo(data: data).asURLRequest()
let request = try OKRouter.modelInfo(data: data).asURLRequest(with: baseURL)

return try await OKHTTPClient.shared.send(request: request, with: OKModelInfoResponse.self)
}

Expand All @@ -49,7 +49,7 @@ extension OllamaKit {
/// - Returns: A `AnyPublisher<OKModelInfoResponse, Error>` that emits detailed information about the model.
public func modelInfo(data: OKModelInfoRequestData) -> AnyPublisher<OKModelInfoResponse, Error> {
do {
let request = try OKRouter.modelInfo(data: data).asURLRequest()
let request = try OKRouter.modelInfo(data: data).asURLRequest(with: baseURL)

return OKHTTPClient.shared.send(request: request, with: OKModelInfoResponse.self)
} catch {
Expand Down
6 changes: 3 additions & 3 deletions Sources/OllamaKit/OllamaKit+Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ extension OllamaKit {
/// - Returns: An ``OKModelResponse`` object listing the available models.
/// - Throws: An error if the request fails or the response can't be decoded.
public func models() async throws -> OKModelResponse {
let request = try OKRouter.models.asURLRequest()
let request = try OKRouter.models.asURLRequest(with: baseURL)

return try await OKHTTPClient.shared.send(request: request, with: OKModelResponse.self)
}

Expand All @@ -45,7 +45,7 @@ extension OllamaKit {
/// - Returns: A `AnyPublisher<OKModelResponse, Error>` that emits the list of available models.
public func models() -> AnyPublisher<OKModelResponse, Error> {
do {
let request = try OKRouter.models.asURLRequest()
let request = try OKRouter.models.asURLRequest(with: baseURL)

return OKHTTPClient.shared.send(request: request, with: OKModelResponse.self)
} catch {
Expand Down
4 changes: 2 additions & 2 deletions Sources/OllamaKit/OllamaKit+PullModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension OllamaKit {
/// - Returns: An asynchronous stream that emits ``OKPullModelResponse``.
public func pullModel(data: OKPullModelRequestData) -> AsyncThrowingStream<OKPullModelResponse, Error> {
do {
let request = try OKRouter.pullModel(data: data).asURLRequest()
let request = try OKRouter.pullModel(data: data).asURLRequest(with: baseURL)

return OKHTTPClient.shared.stream(request: request, with: OKPullModelResponse.self)
} catch {
Expand Down Expand Up @@ -67,7 +67,7 @@ extension OllamaKit {
/// - Returns: A combine publisher that emits ``OKPullModelResponse``.
public func pullModel(data: OKPullModelRequestData) -> AnyPublisher<OKPullModelResponse, Error> {
do {
let request = try OKRouter.pullModel(data: data).asURLRequest()
let request = try OKRouter.pullModel(data: data).asURLRequest(with: baseURL)

return OKHTTPClient.shared.stream(request: request, with: OKPullModelResponse.self)
} catch {
Expand Down
4 changes: 2 additions & 2 deletions Sources/OllamaKit/OllamaKit+Reachable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension OllamaKit {
/// - Returns: `true` if the Ollama API is reachable, `false` otherwise.
public func reachable() async -> Bool {
do {
let request = try OKRouter.root.asURLRequest()
let request = try OKRouter.root.asURLRequest(with: baseURL)
try await OKHTTPClient.shared.send(request: request)

return true
Expand All @@ -47,7 +47,7 @@ extension OllamaKit {
/// - Returns: A `AnyPublisher<Bool, Never>` that emits `true` if the API is reachable, `false` otherwise.
public func reachable() -> AnyPublisher<Bool, Never> {
do {
let request = try OKRouter.root.asURLRequest()
let request = try OKRouter.root.asURLRequest(with: baseURL)

return OKHTTPClient.shared.send(request: request)
.map { _ in true }
Expand Down
11 changes: 5 additions & 6 deletions Sources/OllamaKit/OllamaKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
import Foundation

/// Provides a streamlined way to access the Ollama API, encapsulating the complexities of network communication and data processing.
public struct OllamaKit {
public struct OllamaKit: Sendable {
var router: OKRouter.Type
var decoder: JSONDecoder = .default

var baseURL: URL

/// Initializes a new instance of `OllamaKit` with the default base URL for the Ollama API.
///
/// ```swift
/// let ollamaKit = OllamaKit()
/// ```
public init() {
let router = OKRouter.self
router.baseURL = URL(string: "http://localhost:11434")!

self.router = router
self.baseURL = URL(string: "http://localhost:11434")!
}

/// Initializes a new instance of `OllamaKit` with a custom base URL for the Ollama API.
Expand All @@ -34,8 +34,7 @@ public struct OllamaKit {
/// - Parameter baseURL: The base URL to use for API requests.
public init(baseURL: URL) {
let router = OKRouter.self
router.baseURL = baseURL

self.router = router
self.baseURL = baseURL
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

// A structure that encapsulates options for controlling the behavior of content generation in the Ollama API.
public struct OKCompletionOptions: Encodable {
public struct OKCompletionOptions: Encodable, Sendable {
/// Optional integer to enable Mirostat sampling for controlling perplexity.
/// (0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)
/// Mirostat sampling helps regulate the unpredictability of the output,
Expand Down
6 changes: 3 additions & 3 deletions Sources/OllamaKit/RequestData/OKChatRequestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

/// A structure that encapsulates data for chat requests to the Ollama API.
public struct OKChatRequestData {
public struct OKChatRequestData: Sendable {
private let stream: Bool

/// A string representing the model identifier to be used for the chat session.
Expand All @@ -31,7 +31,7 @@ public struct OKChatRequestData {
}

/// A structure that represents a single message in the chat request.
public struct Message: Encodable {
public struct Message: Encodable, Sendable {
/// A ``Role`` value indicating the sender of the message (system, assistant, user).
public let role: Role

Expand All @@ -48,7 +48,7 @@ public struct OKChatRequestData {
}

/// An enumeration that represents the role of the message sender.
public enum Role: String, Encodable {
public enum Role: String, Encodable, Sendable {
/// Indicates the message is from the system.
case system

Expand Down
2 changes: 1 addition & 1 deletion Sources/OllamaKit/RequestData/OKCopyModelRequestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

/// A structure that encapsulates the necessary data to request a model copy operation in the Ollama API.
public struct OKCopyModelRequestData: Encodable {
public struct OKCopyModelRequestData: Encodable, Sendable {
/// A string representing the identifier of the source model to be copied.
public let source: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

/// A structure that encapsulates the necessary data to request a model deletion in the Ollama API.
public struct OKDeleteModelRequestData: Encodable {
public struct OKDeleteModelRequestData: Encodable, Sendable {
/// A string representing the identifier of the model to be deleted.
public let name: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

/// A structure that encapsulates the data required for generating embeddings using the Ollama API.
public struct OKEmbeddingsRequestData: Encodable {
public struct OKEmbeddingsRequestData: Encodable, Sendable {
/// A string representing the identifier of the model.
public let model: String

Expand Down
2 changes: 1 addition & 1 deletion Sources/OllamaKit/RequestData/OKGenerateRequestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

/// A structure that encapsulates the data required for generating responses using the Ollama API.
public struct OKGenerateRequestData {
public struct OKGenerateRequestData: Sendable {
private let stream: Bool

/// A string representing the identifier of the model.
Expand Down
2 changes: 1 addition & 1 deletion Sources/OllamaKit/RequestData/OKModelInfoRequestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

/// A structure that encapsulates the data necessary for requesting information about a specific model from the Ollama API.
public struct OKModelInfoRequestData: Encodable {
public struct OKModelInfoRequestData: Encodable, Sendable {
/// A string representing the identifier of the model for which information is requested.
public let name: String

Expand Down
Loading

0 comments on commit 3af02c9

Please sign in to comment.