Swift quickstart
In this quickstart, you will learn how to search for and run a model and pipeline.
Create and export an API Key
Create an API key on the Integrations page on Studio. Once generated,
- Add your API key as an environment variable to your system or
- Set the API key as an environment variable in Xcode - this approach keeps your API key separate from your code, which can be beneficial for security and portability.
- MacOS
- Xcode
export TEAM_API_KEY="your_api_key_here"
AiXplainKit.shared.keyManager.TEAM_API_KEY = "<Your Key>"
Install the aiXplain SDK
The Swift Package Manager is a tool for managing the distribution of Swift code.
Adding aiXplainKit
Package to Your Xcode Project
-
Open your Xcode project: Open your existing Xcode project or create a new one.
-
Navigate to the project settings: Click on your project in the Project Navigator to open the project settings.
-
Add the package dependency:
- Select the project in the Project Navigator.
- Click on the
Package Dependencies
tab. - Click the
+
button to add a new package.
-
Enter the package repository URL: In the dialog that appears, enter the URL of the
aiXplainKit
package: https://github.com/aixplain/aiXplainKit -
Choose the package options:
- Select the version rule you want to use (e.g.,
Up to Next Major Version
). - Click
Next
.
- Add the package to your target:
Select the target you want to add the package to, and click
Finish
.
After completing these steps, Xcode will fetch the aiXplainKit
package and add it to your project. You can now import and use the package in your Swift code.
Models
Search
- Text generation
- Image generation
- Speech synthesis
let provider = ModelProvider()
Task {
let query = ModelQuery(functions: ["text-generation"])
let result = try? await provider.list(query)
result?.forEach {
print($0.id, $0.name)
}
}
Let's select Groq Llama 3 70B
, which has ID 6626a3a8c8f1d089790cf5a2
.
let groqLlama3 = try? await ModelProvider().get("6626a3a8c8f1d089790cf5a2")
let provider = ModelProvider()
Task {
let query = ModelQuery(functions: ["text-to-image-generation"])
let result = try? await provider.list(query)
result?.forEach {
print($0.id, $0.name)
}
}
Let's select Stable Diffusion
, which has ID 663bc4f76eb5637aa56d6d31
.
let SDiffusion = try? await ModelProvider().get("663bc4f76eb5637aa56d6d31")
let provider = ModelProvider()
Task {
let query = ModelQuery(functions: ["speech-synthesis"])
let result = try? await provider.list(query)
result?.forEach {
print($0.id, $0.name)
}
}
Let's select Aria
, which has ID 618ba6e8e2e1a9153ca2a3b4
.
let aria = try? await ModelProvider().get("618ba6e8e2e1a9153ca2a3b4")
Run
- Text generation
- Image generation
- Speech synthesis
let response = try await groqLlama3.run("What is the capital of France?")
print(response)
let response = try await SDiffusion.run("A dog in a bathtub wearing a sailor hat.")
print(response)
let response = try await aria.run("Hi! Hope you're having a lovely day!")
print(response)
See our guides on How to search the marketplace and How to call an asset for more information.
Pipelines
To run a pipeline, you must first create one. (The pipeline below is Private
.)
Watch our Design Overview video to learn how to use Design to build your first pipeline (Translation & Speech Synthesis).
Search
Suppose you created a pipeline called German Audio to English Text
, and it has ID 6671d0c3d7b83256062929a3
.
Let's select the pipeline.
let pipeline = try! await PipelineProvider().get("6671d0c3d7b83256062929a3")
Run
let response = try? await pipeline.run(URL("./audio.mp3"))
// print(response?.rawData)
We don't yet have a standard format for Swift pipeline outputs. For now, the best way to see a pipeline output is to run the pipeline and print the response.
See our guides on How to search the marketplace and How to call an asset for more information.