This repository hosts the BaseMind.AI Swift SDK. The SDK is a gRPC client library for connecting with the BaseMind.AI platform.
It supports iOS >= 13 and MacOS >= 12.
Add the sdk in your Package.swift
file dependencies:
dependencies: [
.package(url: "https://github.com/basemind-ai/sdk-ios.git", from: "1.0.0"),
]
Then add the dependency to a target:
targets: [
.target(
name: "MyApp",
dependencies: ["BaseMindClient"]
),
]
Before using the client you have to initialize it. The init function requires an apiKey
that you can create using the BaseMind platform (visit https://basemind.ai):
import BaseMindClient
let client = BaseMindClient(apiKey: "<MyApiKey>")
Once the client is initialized, you can use it to interact with the AI model(s) you configured in the BaseMind dashboard.
You can request an LLM prompt using the requestPrompt
method, which expects a dictionary of string key/value pairs - correlating with any template variables defined in the dashboard (if any):
import BaseMindClient
let client = BaseMindClient(apiKey: "<MyApiKey>")
func handlePromptRequest(userInput: String) async throws -> String {
let templateVariables = ["userInput": userInput]
let response = try client.requestPrompt(templateVariables)
return response.content
}
You can also stream a prompt response using the requestStream
method:
import BaseMindClient
let client = BaseMindClient(apiKey: "<MyApiKey>")
func handlePromptStream(userInput: String) async throws -> [String] {
let templateVariables = ["userInput": userInput]
let stream = try client.requestStream(templateVariables)
var chunks: [String] = []
for try await response in stream {
chunks.append(response.content)
}
return chunks
}
Similarly to the requestPrompt
method, requestStream
expects a dictionary of strings (if any template variables are defined in the dashboard).
It returns a data container that is both an AsyncSequence and an AsyncIterator. You can therefore loop and iterate through the results as fits your use case.
All errors thrown by the client are instances of BaseMindError
. Errors are thrown in the following cases:
- The api key is empty (
BaseMindError.missingToken
). - A server side or connectivity error occurred (
BaseMindError.serverError
) - A required template variable was not provided in the dictionary of the request (
BaseMindError.invalidArgument
). - The task containing streaming is cancelled (
BaseMindError.cancelled
).
You can pass an options struct to the client:
import BaseMindClient
import OSLog
let options = ClientOptions(
debug: true,
host: "127.0.0.1",
logger: Logger(subsystem: "my-sub-system", category: "client"),
port: 443,
promptConfigId: "c5f5d1fd-d25d-4ba2-b103-8c85f48a679d"
)
let client = BaseMindClient(apiKey: "<MyApiKey>", options: options)
debug
: if set to true (default false) the client will log debug messages.host
: the host of the BaseMind Gateway server to use.logger
: an OSLog.Logger instance. If provided it will override the default logger used.port
: the server port.promptConfigId
: the ID of the prompt config to use. If given this value will be used by the server. If not provided, the default prompt configuration will be used.
Note: you can have multiple client instances with different promptConfigId
values set. This allows you to use multiple model configurations within a single application.
Repository Structure:
root # repository root, holding all tooling configurations
├─── .github # GitHub CI/CD and other configurations
├─── .swiftpm # Swift Package Manager Workspace
├─── proto/gateway # Git submodule that includes the protobuf schema
├─── Tests # Tests
├─── Sources/BaseMindGateway # Protobuf and gRPC stubs generated from the proto file
└─── Sources/BaseMindClient # The SDK source code
- Clone to repository to your local machine including the submodules.
git clone --recurse-submodules https://github.com/basemind-ai/sdk-ios.git
-
Install TaskFile and the following prerequisites:
- Python >= 3.11
- Swift >= 5.9
- XCode (optional but recommended)
-
If you're using MacOS - execute the setup task with:
task setup
This will setup pre-commit and the protobuf dependencies.
If you're using a linux or windows machine, you will need to setup the components manually.
To lint the project, execute the lint command:
task lint
To update the dependencies, execute the update-dependencies command:
task update
This will update the swift dependencies and the the pre-commit hooks.
swift test
task generate
This command will generate new protobuf and grpc stubs, replacing those in Sources/BaseMindGateway
.
The SDK is open source.
Pull requests are welcome - as long as they address an issue or substantially improve the SDK or the test app.
Note: Tests are mandatory for the SDK - untested code will not be merged.