Skip to content

Commit

Permalink
Enhance documentation for response models and helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusziade committed Oct 27, 2024
1 parent 6b72099 commit 557ae78
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 74 deletions.
26 changes: 25 additions & 1 deletion Sources/Swollama/Models/ChatResponse.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
import Foundation

/// Response from a chat completion request
/// A response from a chat completion request.
/// - Note: This struct includes both the chat message and various performance metrics.
public struct ChatResponse: Codable, Sendable {
/// The identifier of the model used to generate the response.
public let model: String

/// The timestamp when the response was generated.
public let createdAt: Date

/// The generated chat message containing the response content.
public let message: ChatMessage

/// Indicates whether the response generation is complete.
public let done: Bool

/// The total time taken to generate the response, in microseconds.
public let totalDuration: UInt64?

/// The time taken to load the model, in microseconds.
public let loadDuration: UInt64?

/// The number of prompt evaluations performed.
public let promptEvalCount: Int?

/// The time spent evaluating prompts, in microseconds.
public let promptEvalDuration: UInt64?

/// The total number of evaluations performed.
public let evalCount: Int?

/// The total time spent on evaluations, in microseconds.
public let evalDuration: UInt64?

/// Mapping between property names and JSON keys.
private enum CodingKeys: String, CodingKey {
case model
case createdAt = "created_at"
Expand All @@ -26,6 +47,9 @@ public struct ChatResponse: Codable, Sendable {
case evalDuration = "eval_duration"
}

/// Creates a new chat response by decoding from the given decoder.
/// - Parameter decoder: The decoder to read data from.
/// - Throws: `DecodingError` if the data is corrupted or any required keys are missing.
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

Expand Down
30 changes: 29 additions & 1 deletion Sources/Swollama/Models/GenerateResponse.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
import Foundation

// Update GenerateResponse.swift
/// A response from a text generation request.
/// - Note: This struct includes both the generated text and various performance metrics.
public struct GenerateResponse: Codable, Sendable {
/// The identifier of the model used for generation.
public let model: String

/// The timestamp when the response was generated.
public let createdAt: Date

/// The generated text response.
public let response: String

/// Indicates whether the generation is complete.
public let done: Bool

/// The reason why generation was completed, if applicable.
public let doneReason: String?

/// Context tokens used during generation.
public let context: [Int]?

/// The total time taken for generation, in microseconds.
public let totalDuration: UInt64?

/// The time taken to load the model, in microseconds.
public let loadDuration: UInt64?

/// The number of prompt evaluations performed.
public let promptEvalCount: Int?

/// The time spent evaluating prompts, in microseconds.
public let promptEvalDuration: UInt64?

/// The total number of evaluations performed.
public let evalCount: Int?

/// The total time spent on evaluations, in microseconds.
public let evalDuration: UInt64?

/// Mapping between property names and JSON keys.
private enum CodingKeys: String, CodingKey {
case model
case createdAt = "created_at"
Expand All @@ -30,6 +55,9 @@ public struct GenerateResponse: Codable, Sendable {
case evalDuration = "eval_duration"
}

/// Creates a new generate response by decoding from the given decoder.
/// - Parameter decoder: The decoder to read data from.
/// - Throws: `DecodingError` if the data is corrupted or any required keys are missing.
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

Expand Down
10 changes: 10 additions & 0 deletions Sources/Swollama/Models/ModelFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ import Foundation

/// Represents the supported model formats in Ollama.
public enum ModelFormat: String, Codable {
/// GGUF (GGML Universal Format) model format
case gguf

/// SafeTensors model format, designed for safe and efficient serialization
case safetensors

/// PyTorch model format
case pytorch

/// Used when the model format is not recognized
case unknown

/// Creates a new ModelFormat from a decoder.
/// - Parameter decoder: The decoder to read data from.
/// - Throws: `DecodingError` if decoding fails.
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let value = try container.decode(String.self)
Expand Down
16 changes: 16 additions & 0 deletions Sources/Swollama/Models/QuantizationLevel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@ import Foundation

/// Represents the quantization level of a model.
public enum QuantizationLevel: String, Codable {
/// 4-bit quantization, version 0
case Q4_0 = "Q4_0"

/// 4-bit quantization, version 1
case Q4_1 = "Q4_1"

/// 5-bit quantization, version 0
case Q5_0 = "Q5_0"

/// 5-bit quantization, version 1
case Q5_1 = "Q5_1"

/// 8-bit quantization, version 0
case Q8_0 = "Q8_0"

/// 8-bit quantization, version 1
case Q8_1 = "Q8_1"

/// Used when the quantization level is not recognized
case unknown

/// Creates a new QuantizationLevel from a decoder.
/// - Parameter decoder: The decoder to read data from.
/// - Throws: `DecodingError` if decoding fails.
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let value = try container.decode(String.self)
Expand Down
17 changes: 15 additions & 2 deletions Sources/SwollamaCLI/Tools/ModelFormatter.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import Foundation
import Swollama

/// Protocol defining the interface for formatting model information.
protocol ModelFormatter {
/// Formats a model entry into a human-readable string.
/// - Parameter model: The model entry to format.
/// - Returns: A formatted string representation of the model.
func format(_ model: ModelListEntry) -> String
}

/// Default implementation of ModelFormatter that provides a standardized format.
struct DefaultModelFormatter: ModelFormatter {
/// Date formatter configured for displaying model modification times.
private let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .short
return formatter
}()


/// Formats a model entry into a multi-line string containing key model information.
/// - Parameter model: The model entry to format.
/// - Returns: A formatted string with model details including size, family, parameters, and modification date.
func format(_ model: ModelListEntry) -> String {
"""
- \(model.name)
Expand All @@ -26,12 +35,16 @@ struct DefaultModelFormatter: ModelFormatter {
}
}

/// Utility struct for formatting file sizes into human-readable strings.
struct FileSize {
/// Formats a byte count into a human-readable string with appropriate units.
/// - Parameter bytes: The number of bytes to format.
/// - Returns: A formatted string with the appropriate size unit (GB, MB, KB, or bytes).
static func format(bytes: Int) -> String {
let gigabyte = 1024 * 1024 * 1024
let megabyte = 1024 * 1024
let kilobyte = 1024

if bytes >= gigabyte {
return String(format: "%.2f GB", Double(bytes) / Double(gigabyte))
} else if bytes >= megabyte {
Expand Down
Loading

0 comments on commit 557ae78

Please sign in to comment.