Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

docs: improves the documentation #6

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
59 changes: 59 additions & 0 deletions Sources/OllamaKit/RequestData/Completion/OKCompletionOptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// OKCompletionData.swift
//
//
// Created by Kevin Hermawan on 02/01/24.
//

import Foundation

/// A structure that encapsulates options for controlling the behavior of content generation in the Ollama API.
public struct OKCompletionOptions: Encodable {
/// Optional integer to enable Mirostat sampling for controlling perplexity. (0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)
public var mirostat: Int?

/// Optional float influencing the adjustment speed of the Mirostat algorithm. (Lower = slower adjustment)
public var mirostatEta: Float?

/// Optional float controlling the balance between coherence and diversity. (Lower = more focused text)
public var mirostatTau: Float?

/// Optional integer setting the size of the context window for token generation.
public var numCtx: Int?

/// Optional integer for the number of GQA groups in the transformer layer, specific to some models.
public var numGqa: Int?

/// Optional integer indicating the number of layers to send to the GPU(s).
public var numGpu: Int?

/// Optional integer for the number of threads used in computation, recommended to match physical CPU cores.
public var numThread: Int?

/// Optional integer setting how far back the model checks to prevent repetition.
public var repeatLastN: Int?

/// Optional float setting the penalty strength for repetitions.
public var repeatPenalty: Float?

/// Optional float to control the model's creativity (higher = more creative).
public var temperature: Float?

/// Optional integer for setting a random number seed for generation consistency.
public var seed: Int?

/// Optional string defining stop sequences for the model to cease generation.
public var stop: String?

/// Optional float for tail free sampling, reducing impact of less probable tokens.
public var tfsZ: Float?

/// Optional integer for the maximum number of tokens to predict.
public var numPredict: Int?

/// Optional integer to limit nonsense generation and control answer diversity.
public var topK: Int?

/// Optional float working with top-k to balance text diversity and focus.
public var topP: Float?
}
78 changes: 37 additions & 41 deletions Sources/OllamaKit/RequestData/OKChatRequestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,52 @@

import Foundation

/// A structure representing the data required to generate responses from the Ollama API.
///
/// It includes the model name, prompt, and other optional parameters that tailor the generation process, such as format and context.
/// A structure that encapsulates data for chat requests to the Ollama API.
public struct OKChatRequestData: Encodable {
private let stream: Bool

/// A string representing the model identifier to be used for the chat session.
public let model: String
public let messages: [ChatMessage]
public var format: Format?
public var options: Options?
public var template: String?

public init(model: String, messages: [ChatMessage]) {
/// An array of ``Message`` instances representing the content to be sent to the Ollama API.
public let messages: [Message]

/// Optional ``OKCompletionOptions`` providing additional configuration for the chat request.
public var options: OKCompletionOptions?

public init(model: String, messages: [Message]) {
self.stream = true
self.model = model
self.messages = messages
}
}

public struct ChatMessage: Encodable {
public var role: String
public var content: String
public let images: [String]

public init(role: String, content: String, images: [String] = []) {
self.role = role
self.content = content
self.images = images
/// A structure that represents a single message in the chat request.
public struct Message: Encodable {
/// A ``Role`` value indicating the sender of the message (system, assistant, user).
public let role: Role

/// A string containing the message's content.
public let content: String

/// An optional array of base64-encoded images.
public let images: [String]

public init(role: Role, content: String, images: [String] = []) {
self.role = role
self.content = content
self.images = images
}

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

/// Indicates the message is from the assistant.
case assistant

/// Indicates the message is from the user.
case user
}
}
}

public enum Format: String, Encodable {
case json
}

public struct Options: Encodable {
public var mirostat: Int?
public var mirostatEta: Double?
public var mirostatTau: Double?
public var numCtx: Int?
public var numGqa: Int?
public var numGpu: Int?
public var numThread: Int?
public var repeatLastN: Int?
public var repeatPenalty: Int?
public var temperature: Double?
public var seed: Int?
public var stop: String?
public var tfsZ: Double?
public var numPredict: Int?
public var topK: Int?
public var topP: Double?
}
7 changes: 4 additions & 3 deletions Sources/OllamaKit/RequestData/OKCopyModelRequestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

import Foundation

/// A structure representing the request data for copying a model via the Ollama API.
///
/// This structure holds the information necessary to duplicate a model, including the source model's name and the desired destination name.
/// A structure that encapsulates the necessary data to request a model copy operation in the Ollama API.
public struct OKCopyModelRequestData: Encodable {
/// A string representing the identifier of the source model to be copied.
public let source: String

/// A string indicating the identifier for the destination or the new copy of the model.
public let destination: String

public init(source: String, destination: String) {
Expand Down
5 changes: 2 additions & 3 deletions Sources/OllamaKit/RequestData/OKDeleteModelRequestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import Foundation

/// A structure representing the request data for deleting a model through the Ollama API.
///
/// This structure encapsulates the name of the model to be deleted, providing a straightforward way to specify which model should be removed.
/// A structure that encapsulates the necessary data to request a model deletion in the Ollama API.
public struct OKDeleteModelRequestData: Encodable {
/// A string representing the identifier of the model to be deleted.
public let name: String

public init(name: String) {
Expand Down
28 changes: 18 additions & 10 deletions Sources/OllamaKit/RequestData/OKGenerateRequestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,32 @@

import Foundation

/// A structure representing the data required to generate responses from the Ollama API.
///
/// It includes the model name, prompt, and other optional parameters that tailor the generation process, such as format and context.
/// A structure that encapsulates the data required for generating responses using the Ollama API.
public struct OKGenerateRequestData: Encodable {
private let stream: Bool


/// A string representing the identifier of the model.
public let model: String

/// A string containing the initial input or prompt.
public let prompt: String
public var format: Format?

/// /// An optional array of base64-encoded images.
public let images: [String]

/// An optional string specifying the system message.
public var system: String?
public var template: String?
public var options: Options?
public var context: [Int]?
public var raw: Bool?

public init(model: String, prompt: String) {
/// An optional array of doubles representing contextual information.
public var context: [Double]?

/// Optional ``OKCompletionOptions`` providing additional configuration for the generation request.
public var options: OKCompletionOptions?

public init(model: String, prompt: String, images: [String] = []) {
self.stream = true
self.model = model
self.prompt = prompt
self.images = images
}
}
5 changes: 2 additions & 3 deletions Sources/OllamaKit/RequestData/OKModelInfoRequestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import Foundation

/// A structure representing the request data for fetching model information from the Ollama API.
///
/// This structure is used to specify the name of the model for which detailed information is requested.
/// A structure that encapsulates the data necessary for requesting information about a specific model from the Ollama API.
public struct OKModelInfoRequestData: Encodable {
/// A string representing the identifier of the model for which information is requested.
public let name: String

public init(name: String) {
Expand Down
21 changes: 21 additions & 0 deletions Sources/OllamaKit/Responses/Completion/OKCompletionResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// OKCompletionResponse.swift
//
//
// Created by Kevin Hermawan on 02/01/24.
//

import Foundation

protocol OKCompletionResponse: Decodable {
var model: String { get }
var createdAt: Date { get }
var done: Bool { get }

var totalDuration: Int? { get }
var loadDuration: Int? { get }
var promptEvalCount: Int? { get }
var promptEvalDuration: Int? { get }
var evalCount: Int? { get }
var evalDuration: Int? { get }
}
43 changes: 38 additions & 5 deletions Sources/OllamaKit/Responses/OKChatResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,56 @@

import Foundation

/// A structure representing the response from a chat request to the Ollama API.
///
/// Contains details of the generation process, including the model used, response content, and various performance metrics.
public struct OKChatResponse: Decodable {
/// A structure that represents the response to a chat request from the Ollama API.
public struct OKChatResponse: OKCompletionResponse, Decodable {
/// A string representing the identifier of the model that processed the request.
public let model: String

/// A `Date` indicating when the response was created.
public let createdAt: Date

/// An optional `Message` instance representing the content of the response.
public let message: Message?

/// A boolean indicating whether the chat session is complete.
public let done: Bool

/// An optional integer representing the total duration of processing the request.
public let totalDuration: Int?

/// An optional integer indicating the duration of loading the model.
public let loadDuration: Int?

/// An optional integer specifying the number of evaluations performed on the prompt.
public let promptEvalCount: Int?

/// An optional integer indicating the duration of prompt evaluations.
public let promptEvalDuration: Int?

/// An optional integer representing the total number of evaluations performed.
public let evalCount: Int?

/// An optional integer indicating the duration of all evaluations.
public let evalDuration: Int?

/// A structure that represents a single response message.
public struct Message: Decodable {
public var role: String
/// A ``Role`` value indicating the sender of the message (system, assistant, user).
public var role: Role

/// A string containing the message's content.
public var content: String

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

/// Indicates the message is from the assistant.
case assistant

/// Indicates the message is from the user.
case user
}
}
}
29 changes: 24 additions & 5 deletions Sources/OllamaKit/Responses/OKGenerateResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,38 @@

import Foundation

/// A structure representing the response from a generate request to the Ollama API.
///
/// Contains details of the generation process, including the model used, response content, and various performance metrics.
public struct OKGenerateResponse: Decodable {
/// A structure that represents the response to a content generation request from the Ollama API.
public struct OKGenerateResponse: OKCompletionResponse, Decodable {
/// A string representing the identifier of the model used for generation.
public let model: String

/// A `Date` indicating when the response was generated.
public let createdAt: Date

/// A string containing the generated content.
public let response: String
public let done: Bool

/// An optional array of integers representing contextual information used in the generation.
public let context: [Int]?

/// A boolean indicating whether the generation process is complete.
public let done: Bool

/// An optional integer representing the total duration of processing the request.
public let totalDuration: Int?

/// An optional integer indicating the duration of loading the model.
public let loadDuration: Int?

/// An optional integer specifying the number of evaluations performed on the prompt.
public let promptEvalCount: Int?

/// An optional integer indicating the duration of prompt evaluations.
public let promptEvalDuration: Int?

/// An optional integer representing the total number of evaluations performed.
public let evalCount: Int?

/// An optional integer indicating the duration of all evaluations.
public let evalDuration: Int?
}
11 changes: 8 additions & 3 deletions Sources/OllamaKit/Responses/OKModelInfoResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@

import Foundation

/// A structure encapsulating detailed information about a specific model from the Ollama API.
///
/// Includes the model's license, template, modelfile, and operational parameters.
/// A structure that represents the response containing information about a specific model from the Ollama API.
public struct OKModelInfoResponse: Decodable {
/// A string detailing the licensing information for the model.
public let license: String

/// A string representing the template used by the model.
public let template: String

/// A string containing the path or identifier of the model file.
public let modelfile: String

/// A string detailing the parameters or settings of the model.
public let parameters: String
}
Loading