### List Available Models Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Retrieve a list of all models currently installed in the Ollama instance. ```swift import OllamaKit let ollamaKit = OllamaKit() Task { do { let response = try await ollamaKit.models() for model in response.models { print("Model: \(model.name)") print(" Size: \(model.size) bytes") print(" Family: \(model.details.family)") print(" Parameters: \(model.details.parameterSize)") print(" Quantization: \(model.details.quantizationLevel)") print(" Modified: \(model.modifiedAt)") } } catch { print("Failed to fetch models: \(error)") } } ``` -------------------------------- ### Get Model Information Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Fetch detailed metadata for a specific model, such as its license and template. ```swift import OllamaKit let ollamaKit = OllamaKit() Task { do { let requestData = OKModelInfoRequestData(name: "llama3.2") let modelInfo = try await ollamaKit.modelInfo(data: requestData) print("License: \(modelInfo.license)") print("Template: \(modelInfo.template)") print("Parameters: \(modelInfo.parameters)") print("Modelfile: \(modelInfo.modelfile)") } catch { print("Failed to get model info: \(error)") } } ``` -------------------------------- ### Add OllamaKit Dependency Source: https://github.com/kevinhermawan/ollamakit/blob/main/README.md Include this entry in the dependencies array of your Package.swift file to install the library. ```swift dependencies: [ .package(url: "https://github.com/kevinhermawan/OllamaKit.git", .upToNextMajor(from: "5.0.0")) ] ``` -------------------------------- ### Delete Model Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Remove a model from the local Ollama installation. ```swift import OllamaKit let ollamaKit = OllamaKit() Task { do { let requestData = OKDeleteModelRequestData(name: "llama3.2-backup") try await ollamaKit.deleteModel(data: requestData) print("Model deleted successfully") } catch { print("Failed to delete model: \(error)") } } ``` -------------------------------- ### Initialize OllamaKit Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Create an instance of OllamaKit using either the default localhost URL or a custom server URL. ```swift import OllamaKit // Initialize with default base URL (http://localhost:11434) let ollamaKit = OllamaKit() // Initialize with custom base URL for remote Ollama server let customURL = URL(string: "https://api.myollama.com")! let remoteOllamaKit = OllamaKit(baseURL: customURL) ``` -------------------------------- ### Configure OllamaKit Completion Options Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Set advanced generation parameters such as temperature, context window, and sampling strategies for fine-tuning model output. Ensure the model name and prompt are correctly specified. ```swift import OllamaKit let ollamaKit = OllamaKit() Task { do { // Create comprehensive completion options let options = OKCompletionOptions( mirostat: 2, // Enable Mirostat 2.0 sampling mirostatEta: 0.1, // Learning rate for Mirostat mirostatTau: 5.0, // Target perplexity numCtx: 4096, // Context window size repeatLastN: 64, // Look-back for repetition check repeatPenalty: 1.1, // Repetition penalty strength temperature: 0.7, // Creativity/randomness seed: 42, // Reproducible outputs stop: "###", // Stop sequence tfsZ: 1.0, // Tail free sampling numPredict: 256, // Max tokens to generate topK: 40, // Top-K sampling topP: 0.9, // Nucleus sampling minP: 0.05 // Minimum probability threshold ) var requestData = OKGenerateRequestData( model: "llama3.2", prompt: "Write a haiku about programming:" ) requestData.options = options for try await response in ollamaKit.generate(data: requestData) { print(response.response, terminator: "") } } catch { print("Generation failed: \(error)") } } ``` -------------------------------- ### Pull Model Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Download a model from the Ollama library, utilizing streaming to track progress. ```swift import OllamaKit let ollamaKit = OllamaKit() Task { do { let requestData = OKPullModelRequestData(model: "llama3.2") for try await response in ollamaKit.pullModel(data: requestData) { print("Status: \(response.status)") if let completed = response.completed, let total = response.total { let progress = Double(completed) / Double(total) * 100 print("Progress: \(String(format: "%.1f", progress))% (\(completed)/\(total) bytes)") } if let digest = response.digest { print("Digest: \(digest)") } } print("Model pull complete!") } catch { print("Failed to pull model: \(error)") } } ``` -------------------------------- ### Generate Text Completions (Streaming) Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Use this to generate text completions from a prompt with streaming responses. Supports optional system messages and generation options like temperature and top-k. ```swift import OllamaKit let ollamaKit = OllamaKit() Task { do { var requestData = OKGenerateRequestData( model: "llama3.2", prompt: "Explain the concept of recursion in programming." ) // Optional: Set system message requestData.system = "You are a helpful programming tutor." // Optional: Configure generation options requestData.options = OKCompletionOptions( temperature: 0.7, numPredict: 500, topK: 40, topP: 0.9 ) var fullResponse = "" for try await response in ollamaKit.generate(data: requestData) { print(response.response, terminator: "") fullResponse += response.response if response.done { print("\n\n--- Generation Complete ---") print("Model: \(response.model)") if let totalDuration = response.totalDuration { print("Total duration: \(totalDuration / 1_000_000)ms") } if let evalCount = response.evalCount { print("Tokens generated: \(evalCount)") } } } } catch { print("Generation failed: \(error)") } } ``` -------------------------------- ### Chat with Tool Calling in Swift Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Integrate LLMs with external APIs by enabling models to call functions and receive structured responses. Ensure the model supports tool calling and define your tools using OKJSONValue. ```swift import OllamaKit let ollamaKit = OllamaKit() Task { do { let weatherTool: OKJSONValue = .object([ "type": .string("function"), "function": .object([ "name": .string("get_current_weather"), "description": .string("Get the current weather for a location"), "parameters": .object([ "type": .string("object"), "properties": .object([ "location": .object([ "type": .string("string"), "description": .string("The city and state, e.g. San Francisco, CA") ]), "format": .object([ "type": .string("string"), "description": .string("Temperature unit: 'celsius' or 'fahrenheit'"), "enum": .array([.string("celsius"), .string("fahrenheit")]) ]) ]), "required": .array([.string("location"), .string("format")]) ]) ]) ]) let messages: [OKChatRequestData.Message] = [ OKChatRequestData.Message( role: .user, content: "What\'s the weather like in Tokyo?" ) ] let chatData = OKChatRequestData( model: "llama3.2", messages: messages, tools: [weatherTool] ) for try await response in ollamaKit.chat(data: chatData) { if let toolCalls = response.message?.toolCalls { for toolCall in toolCalls { if let function = toolCall.function { print("Tool called: \(function.name ?? \"unknown\")") if let arguments = function.arguments, case .object(let argDict) = arguments { if case .string(let location) = argDict["location"] { print(" Location: \(location)") } if case .string(let format) = argDict["format"] { print(" Format: \(format)") } } } } } if let content = response.message?.content, !content.isEmpty { print("Response: \(content)") } } } catch { print("Tool calling failed: \(error)") } } ``` -------------------------------- ### Generate Text from Image (Multimodal) Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Generate text descriptions from images using vision-capable models. Ensure images are base64-encoded before sending. ```swift import OllamaKit import Foundation let ollamaKit = OllamaKit() Task { do { // Load and encode image as base64 let imageURL = URL(fileURLWithPath: "/path/to/image.jpg") let imageData = try Data(contentsOf: imageURL) let base64Image = imageData.base64EncodedString() let requestData = OKGenerateRequestData( model: "llava", prompt: "Describe what you see in this image in detail.", images: [base64Image] ) for try await response in ollamaKit.generate(data: requestData) { print(response.response, terminator: "") } } catch { print("Failed to process image: \(error)") } } ``` -------------------------------- ### Chat Completion with Streaming in Swift Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Initiates a multi-turn conversation with context preservation. Supports system prompts, conversation history, and streaming responses. Configure completion options like temperature and context size as needed. ```swift import OllamaKit let ollamaKit = OllamaKit() Task { do { let messages: [OKChatRequestData.Message] = [ OKChatRequestData.Message( role: .system, content: "You are a friendly AI assistant specializing in Swift programming." ), OKChatRequestData.Message( role: .user, content: "What are the benefits of using Swift's async/await?" ) ] var chatData = OKChatRequestData( model: "llama3.2", messages: messages ) // Optional: Configure completion options chatData.options = OKCompletionOptions( temperature: 0.8, numCtx: 4096, repeatPenalty: 1.1 ) var assistantResponse = "" for try await response in ollamaKit.chat(data: chatData) { if let content = response.message?.content { print(content, terminator: "") assistantResponse += content } if response.done { print("\n\n--- Chat Complete ---") if let evalCount = response.evalCount, let evalDuration = response.evalDuration { let tokensPerSecond = Double(evalCount) / (Double(evalDuration) / 1_000_000_000) print("Speed: \(String(format: "%.1f", tokensPerSecond)) tokens/sec") } } } } catch { print("Chat failed: \(error)") } } ``` -------------------------------- ### Copy Model Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Create a duplicate of an existing model under a new name. ```swift import OllamaKit let ollamaKit = OllamaKit() Task { do { let requestData = OKCopyModelRequestData( source: "llama3.2", destination: "llama3.2-backup" ) try await ollamaKit.copyModel(data: requestData) print("Model copied successfully") } catch { print("Failed to copy model: \(error)") } } ``` -------------------------------- ### Check API Reachability Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Verify connectivity to the Ollama API before executing requests. ```swift import OllamaKit let ollamaKit = OllamaKit() Task { let isReachable = await ollamaKit.reachable() if isReachable { print("Ollama API is available") } else { print("Ollama API is not reachable") } } ``` -------------------------------- ### Chat with Images (Multimodal) in Swift Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Enables visual understanding tasks by sending images within chat messages to vision-capable models. Ensure the image data is correctly encoded to base64. ```swift import OllamaKit import Foundation let ollamaKit = OllamaKit() Task { do { let imageURL = URL(fileURLWithPath: "/path/to/screenshot.png") let imageData = try Data(contentsOf: imageURL) let base64Image = imageData.base64EncodedString() let messages: [OKChatRequestData.Message] = [ OKChatRequestData.Message( role: .user, content: "What UI components do you see in this screenshot?", images: [base64Image] ) ] let chatData = OKChatRequestData( model: "llava", messages: messages ) for try await response in ollamaKit.chat(data: chatData) { if let content = response.message?.content { print(content, terminator: "") } } } catch { print("Chat with image failed: \(error)") } } ``` -------------------------------- ### Generate Structured JSON Responses Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Request structured JSON responses from the model by providing a custom JSON schema. This ensures the output conforms to the defined structure. ```swift import OllamaKit let ollamaKit = OllamaKit() Task { do { let jsonSchema: OKJSONValue = .object([ "type": .string("object"), "properties": .object([ "name": .object([ "type": .string("string") ]), "capital": .object([ "type": .string("string") ]), "population": .object([ "type": .string("integer") ]) ]), "required": .array([.string("name"), .string("capital"), .string("population")]) ]) let requestData = OKGenerateRequestData( model: "llama3.2", prompt: "Provide information about France. Return as JSON.", format: jsonSchema ) var jsonResponse = "" for try await response in ollamaKit.generate(data: requestData) { jsonResponse += response.response } print("JSON Response: \(jsonResponse)") // Output: {"name": "France", "capital": "Paris", "population": 67390000} } catch { print("Failed to generate JSON: \(error)") } } ``` -------------------------------- ### Generate Embeddings in Swift Source: https://context7.com/kevinhermawan/ollamakit/llms.txt Create vector embeddings for text inputs to enable semantic search and similarity comparisons. You can optionally control the model's memory duration using the keepAlive parameter. ```swift import OllamaKit let ollamaKit = OllamaKit() Task { do { var requestData = OKEmbeddingsRequestData( model: "nomic-embed-text", prompt: "The quick brown fox jumps over the lazy dog." ) // Optional: Control model memory duration requestData.keepAlive = "10m" let response = try await ollamaKit.embeddings(data: requestData) if let embedding = response.embedding { print("Embedding dimensions: \(embedding.count)") print("First 5 values: \(embedding.prefix(5))") // Use embeddings for similarity search, clustering, etc. let magnitude = sqrt(embedding.reduce(0) { $0 + $1 * $1 }) print("Vector magnitude: \(magnitude)") } } catch { print("Embedding generation failed: \(error)") } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.