Skip to main content

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,

  1. Add your API key as an environment variable to your system or
  2. 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.
export TEAM_API_KEY="your_api_key_here"

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

  1. Open your Xcode project: Open your existing Xcode project or create a new one.

  2. Navigate to the project settings: Click on your project in the Project Navigator to open the project settings.

  3. 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.
  4. Enter the package repository URL: In the dialog that appears, enter the URL of the aiXplainKit package: https://github.com/aixplain/aiXplainKit

  5. Choose the package options:

  • Select the version rule you want to use (e.g., Up to Next Major Version).
  • Click Next.
  1. 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

let provider = ModelProvider()
Task {
let query = ModelQuery(functions: ["text-generation"])
let result = try? await provider.list(query)

result?.forEach {
print($0.id, $0.name)
}
}
Show output

Let's select Groq Llama 3 70B, which has ID 6626a3a8c8f1d089790cf5a2.

let groqLlama3 = try? await ModelProvider().get("6626a3a8c8f1d089790cf5a2")

Run

let response = try await groqLlama3.run("What is the capital of France?")
print(response)
Show output
note

See our guides on How to search the marketplace and How to call an asset for more information.

Pipelines

info

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)
info

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.

Agents

Agents Template

import aiXplainKit


let agentTools:[CreateAgentTool] = []

# Agent
let agent:Agent = AgentProvider.create(
name="<agent_name>",
description="<agent_Description>",
llmId="<model_id_of_the_llm_to_power_the_agent>",
tools=[
.model(model,description)
.asset(id,description),
]
)

You can empower Agents by equipping them with models and agents or any other asset at aiXplain as agent tools. They can use these tools when responding to requests.

Model Tools

To use models as tools for agents, you can do this by using the CreateAgentTool enum. This enum allows you to specify different types of agent tools, including models. Specify the exact model you want by its Model ID. For this example, let’s specify:

  • The AWS English speech synthesis - Amy model, which has ID 618ba6e5e2e1a9153ca2a3a5.
let model = ModelProvider().get("618ba6e5e2e1a9153ca2a3a5")
let tools:CreateAgentTool = .model(model:model,description:"Speech Systhesis")

Other Assets

You can use the asset enum to transform any aiXplain asset into a usable tool. This includes pipelines and models. For this example, let’s specify:

  • The My pipeline pipeline, which has ID 618ba6e5e2e1a9153ca2a3a6.
let tools:CreateAgentTool = .asset(id:"618ba6e5e2e1a9153ca2a3a6",description:"Speech Systhesis")

Create and deploy an Agent

Use the AgentProvider class to create and deploy an agent. Specify the following parameters:

  • a unique name for your agent,
  • a brief description stating the agent's purpose,
  • [optional] the llm_id of the language model you want to power the agent (default is OpenAI's GPT-4o Mini).
  • [optonal] the tools you want your agent to use (default is []),

In this example we want a Agent that can search on Google and Wikipedia. So we are going to use the followin tools in aiXplain

  • Google Search(id: 65c51c556eb563350f6e1bb1)
  • Wikipedia (id: 6633fd59821ee31dd914e232)
import aiXplainKit

let searchAgent = AgentProvider.create(
name="Text Analysis Agent",
description="An agent that analyses text.",
llmId="6646261c6eb563165658bbb1" # GPT-4o
tools=[
.asset(id: "65c51c556eb563350f6e1bb1", description: "Allows the agent to perform web searches to find up-to-date information on any topic. Use this tool to access the most recent and relevant online content."),
.asset(id: "6633fd59821ee31dd914e232", description: "Allows the agent to perform web searches to find up-to-date information on Wikipedia."),
]
)

print(searchAgent.debugDescription)

You can use any LLMs on the aiXplain marketplace to power your agent. Use the code below to see our available LLMs.

from aixplain.factories import ModelFactory

let modelList = ModelProvider().list(ModelQuery(functions: ["text-generation"]))

# Print the sorted models
for model in modelList:
print(model.name, model.id)

Run Agents

The aiXplainKit SDK allows you to run agents and team agents and gives you the option to use short-term memory (history). Agent and Team Agent inputs can be URLs, file paths, or direct text. The examples below use only direct text. Use the run method to call an agent.

let agent = AgentProvider().get("<AGENTID>")
let agentResponse = agent.run(
"""
Analyse the following text.

Bilbo Baggins leaves the One Ring to his heir, Frodo, after his birthday celebration.
Seventeen years later, Gandalf confirms that the Ring belongs to the Dark Lord Sauron
and advises Frodo to leave the Shire. Frodo embarks on a journey with Sam, Merry, and
Pippin, pursued by Black Riders. They encounter various dangers, including Old Man
Willow and a barrow-wight, but are saved by Tom Bombadil. In Bree, they meet Strider,
who helps guide them to Rivendell. Along the way, Frodo is wounded by the Black Riders
but is saved by Strider and Glorfindel. At Rivendell, the Council of Elrond determines
that the Ring must be destroyed in Mordor. Frodo volunteers for the task, and a
fellowship forms to aid him: Sam, Merry, Pippin, Gandalf, Aragorn, Boromir, Legolas,
and Gimli. After an attempt to cross the Misty Mountains fails, they travel through
the Mines of Moria, where Gandalf falls in battle with a Balrog. The remaining members
reach Lothlórien, where Galadriel tests them and offers gifts. As they continue their
journey, Boromir tries to seize the Ring, leading Frodo to decide to go to Mordor alone.
Sam follows him, and together they set off for Mordor.
"""
)

print(agentResponse)

Short-Term Memory

Every agent response includes a data.sessionID value, which corresponds to a history. To continue using a history, specify the sessionID from the first query as an input parameter for all subsequent queries.

let response  = agent.run(
"Analyse the following text: The quick brown fox jumped over the lazy dog. Answer in text and audio."
)

print(response)
let sessionID = response.data.sessionID
print("Session id: \(session_id)")
let newResponse = = agent.run(
"What makes this text interesting?",
session_id=sessionID,
)

print(newResponse)