-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to pull model from library (#39)
* add pullModel method to download model from ollama library * add details to `OKModelResponse`
- Loading branch information
1 parent
f947524
commit 4620016
Showing
5 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// OllamaKit+PullModel.swift | ||
// | ||
// | ||
// Created by Lukas Pistrol on 25.11.24. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
|
||
extension OllamaKit { | ||
/// Establishes an asynchronous stream for pulling a model from the ollama library. | ||
/// | ||
/// This method will periodically yield ``OKPullModelResponse`` structures as the model is being pulled. | ||
/// Depending on the size of the model and the speed of the internet connection, this process may take a while. | ||
/// The stream will complete once the model has been fully pulled. | ||
/// | ||
/// ```swift | ||
/// let ollamaKit = OllamaKit() | ||
/// let reqData = OKPullModelRequestData(model: "llama3.2") | ||
/// | ||
/// for try await response in ollamaKit.pullModel(data: reqData) { | ||
/// print(response.status) | ||
/// if let progress = response.completed, let total = response.total { | ||
/// print("Progress: \(progress)/\(total) bytes") | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// - Parameter data: The ``OKPullModelRequestData`` used to initiate the chat streaming from the ollama library. | ||
/// - Returns: An asynchronous stream that emits ``OKPullModelResponse``. | ||
public func pullModel(data: OKPullModelRequestData) -> AsyncThrowingStream<OKPullModelResponse, Error> { | ||
do { | ||
let request = try OKRouter.pullModel(data: data).asURLRequest() | ||
|
||
return OKHTTPClient.shared.stream(request: request, with: OKPullModelResponse.self) | ||
} catch { | ||
return AsyncThrowingStream { continuation in | ||
continuation.finish(throwing: error) | ||
} | ||
} | ||
} | ||
|
||
/// Establishes a Combine publisher for pulling a model from the ollama library. | ||
/// | ||
/// This method will periodically yield ``OKPullModelResponse`` structures as the model is being pulled. | ||
/// Depending on the size of the model and the speed of the internet connection, this process may take a while. | ||
/// The stream will complete once the model has been fully pulled. | ||
/// | ||
/// ```swift | ||
/// let ollamaKit = OllamaKit() | ||
/// let reqData = OKPullModelRequestData(model: "llama3.2") | ||
/// | ||
/// ollamaKit.pullModel(data: reqData) | ||
/// .sink { completion in | ||
/// // Handle completion | ||
/// } receiveValue: { response in | ||
/// print(response.status) | ||
/// if let progress = response.completed, let total = response.total { | ||
/// print("Progress: \(progress)/\(total) bytes") | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// - Parameter data: The ``OKPullModelRequestData`` used to initiate the chat streaming from the ollama library. | ||
/// - Returns: A combine publisher that emits ``OKPullModelResponse``. | ||
public func pullModel(data: OKPullModelRequestData) -> AnyPublisher<OKPullModelResponse, Error> { | ||
do { | ||
let request = try OKRouter.pullModel(data: data).asURLRequest() | ||
|
||
return OKHTTPClient.shared.stream(request: request, with: OKPullModelResponse.self) | ||
} catch { | ||
return Fail(error: error).eraseToAnyPublisher() | ||
} | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
Sources/OllamaKit/RequestData/OKPullModelRequestData.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// OKPullModelRequestData.swift | ||
// | ||
// | ||
// Created by Lukas Pistrol on 25.11.24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A structure that encapsulates the data necessary for pulling a new model from the ollama library using the Ollama API. | ||
public struct OKPullModelRequestData: Encodable { | ||
private let stream: Bool | ||
|
||
/// A string representing the identifier of the model for which information is requested. | ||
public let model: String | ||
|
||
public init(model: String) { | ||
self.stream = true | ||
self.model = model | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// OKPullModelResponse.swift | ||
// | ||
// | ||
// Created by Lukas Pistrol on 25.11.24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// The response model for pulling a new model from the ollama library. | ||
public struct OKPullModelResponse: Decodable { | ||
/// The current status. | ||
public let status: String | ||
|
||
/// The digest hash of the current file. | ||
public let digest: String? | ||
|
||
/// The size of the current file. | ||
public let total: Int? | ||
|
||
/// The number of bytes that have been completed. | ||
public let completed: Int? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters