Getting Started with FreeToken Swift

This is a quick guide to get you up and running with the FreeToken Swift SDK. Follow these steps to integrate the SDK and perform your first AI run!

Installation

Add FreeToken to your project using Swift Package Manager:

  1. Open your project in Xcode
  2. Go to File > Add Package Dependencies...
  3. Enter the repository URL in the top right "Search" box: https://github.com/FreeTokenAI/FreeTokenSwift Make sure you are logged into your GitHub account in Xcode if you encounter any issues
  4. Enter the version you want to use (e.g., 1.0.0 or main for the latest)
  5. Click Add Package
  6. Select the target you want to add the package to and click Add Package. The target is usually the name of your app.

Quick Start

Creating your App & Agent in the FreeToken web console

  1. Go to the FreeToken Console
  2. Sign up for a new account or log in if you already have one
  3. Go to the Apps navigation item and click Create New App.
  4. Fill in the required details and create your app
  5. In the Manage App Tokens section, create a new App Token. Save this token as you'll need it to configure the SDK
  6. Create a new Agent in the Agents section by selecting Create Agent
  7. Fill in the required details and create your Agent

You are now ready to configure your app!

1. Configure the Client

import FreeToken

// Configure with your API token
let client = FreeToken.shared.configure(
    appToken: "your-api-token-here"
)

2. Register Your Device

Before using AI features, register your device with a scope:

await client.registerDeviceSession(scope: "my-app-v1") {
    print("Device registered successfully!")
    // Device is ready for AI operations
} error: { error in
    print("Failed to register device: \(error.localizedDescription)")
}

3. Download AI Model

If you want to use local AI processing:

await client.downloadAIModel { state in
    switch state {
    case .downloaded:
        print("AI model ready for local processing")
    case .aiNotSupported:
        print("Device doesn't support local AI - will use cloud")
    case .cloudOnly:
        print("Model is cloud-only and cannot be downloaded")
    }
} error: { error in
    print("Failed to download model: \(error.localizedDescription)")
}

4. Create Your First Message Thread

// Create a message thread for conversation
await client.createMessageThread { messageThread in
    print("Created message thread: \(messageThread.id)")
    
    // Add a user message
    let userMessage = Message(role: .user, content: "Hello! Can you help me understand Swift closures?")
    
    // Add message to the thread
    await client.addMessageToThread(id: messageThread.id, message: userMessage) { message in
        print("Added message: \(message.content)")
    } error: { error in
        print("Failed to add message: \(error.localizedDescription)")
    }
} error: { error in
    print("Failed to create thread: \(error.localizedDescription)")
}

5. Run the Message Thread

// Run the thread through AI
await client.runMessageThread(id: messageThreadID) { responseMessage in
    print("AI Response: \(responseMessage.content)")
} error: { error in
    print("AI processing failed: \(error.localizedDescription)")
} chatStatusStream: { token, status in 
    // Returns enum representing the internal state of the AI model and streaming response (if available)
    print("Chat status: \(status) (token: \(token))")
}