NexaSwift is a Swift wrapper for the llama.cpp library. This repository provides a Swifty API, allowing Swift developers to easily integrate and use llama.cpp
models in their projects.
NOTE: Currently, we support text inference capabilities.
To add NexaSwift to your Swift project, add the following dependency in your Package.swift
file:
.package(url: "https://github.com/NexaAI/nexa-sdk.git", .branch("main"))
Create a configuration and initialize NexaSwift with the path to your model file:
let configuration = NexaSwift.Configuration(
maxNewToken: 128,
stopTokens: []
)
let modelPath = "path/to/your/model"
let nexaSwift = try NexaSwift.NexaTextInference(modelPath: modelPath, modelConfiguration: configuration)
var messages:[ChatCompletionRequestMessage] = []
let userMessage = ChatCompletionRequestMessage.user(
ChatCompletionRequestUserMessage(content: .text("user input"))
)
messages.append(userMessage)
For non-streaming mode, simply call the start method with your prompt. This will return the complete response once it’s available.
let response = try await nexaSwift.createChatCompletion(for: messages)
print(response.choices[0].message.content ?? "")
In streaming mode, you can process the response in real-time as it’s generated:
for try await response in await nexaSwift.createChatCompletionStream(for: messages) {
print(response.choices[0].delta.content ?? "")
}
if let response = try? await nexaSwift.createCompletion(for: prompt) {
print(response.choices[0].text))
}
for try await response in await nexaSwift.createCompletionStream(for: prompt) {
print(response.choices[0].text)
}
Open the swift test project folder in Xcode and run the project.
NexaSwift supports all models compatible with llama.cpp. You can download models from the Nexa AI ModelHub.