This library is primarily designed for Conversational AI integration in Swift applications. Please use an alternative dependency for other features, such as speech synthesis.
First, create a session configuration and set up the necessary callbacks:
Copy
Ask AI
// Configure the sessionlet config = ElevenLabsSDK.SessionConfig(agentId: "your-agent-id")// Set up callbacksvar callbacks = ElevenLabsSDK.Callbacks()callbacks.onConnect = { conversationId in print("Connected with ID: \(conversationId)")}callbacks.onDisconnect = { print("Disconnected")}callbacks.onMessage = { message, role in print("\(role.rawValue): \(message)")}callbacks.onError = { error, info in print("Error: \(error), Info: \(String(describing: info))")}callbacks.onStatusChange = { status in print("Status changed to: \(status.rawValue)")}callbacks.onModeChange = { mode in print("Mode changed to: \(mode.rawValue)")}callbacks.onVolumeUpdate = { volume in print("Volume updated: \(volume)")}
Client Tools allow you to register custom functions that can be called by your AI agent during conversations. This enables your agent to perform actions in your application.
The ElevenLabs SDK currently uses a default input sample rate of 16,000 Hz. However, the output sample rate is configurable based on the agent’s settings. Ensure that the output sample rate aligns with your specific application’s audio requirements for smooth interaction.
The SDK does not currently support ulaw format for audio encoding. For compatibility, consider using alternative formats.
Here’s an example SwiftUI view implementing the conversation interface:
Copy
Ask AI
struct ConversationalAIView: View { @State private var conversation: ElevenLabsSDK.Conversation? @State private var mode: ElevenLabsSDK.Mode = .listening @State private var status: ElevenLabsSDK.Status = .disconnected @State private var audioLevel: Float = 0.0 private func startConversation() { Task { do { let config = ElevenLabsSDK.SessionConfig(agentId: "your-agent-id") var callbacks = ElevenLabsSDK.Callbacks() callbacks.onConnect = { conversationId in status = .connected } callbacks.onDisconnect = { status = .disconnected } callbacks.onModeChange = { newMode in DispatchQueue.main.async { mode = newMode } } callbacks.onVolumeUpdate = { newVolume in DispatchQueue.main.async { audioLevel = newVolume } } conversation = try await ElevenLabsSDK.Conversation.startSession( config: config, callbacks: callbacks ) } catch { print("Failed to start conversation: \(error)") } } } var body: some View { VStack { // Your UI implementation Button(action: startConversation) { Text(status == .connected ? "End Call" : "Start Call") } } }}
This SDK is currently experimental and under active development. While it’s
stable enough for testing and development, it’s not recommended for production
use yet.