Skip to content

Perplexity

The Perplexity provider offers access to Sonar API - a language model that uniquely combines real-time web search with natural language processing. Each response is grounded in current web data and includes detailed citations, making it ideal for research, fact-checking, and obtaining up-to-date information.

API keys can be obtained from the Perplexity Platform.

The Perplexity provider is available in the PerplexityProvider module. Add it to your Swift package:

Package.swift
dependencies: [
.package(url: "https://github.com/teunlao/swift-ai-sdk", from: "0.2.1")
],
targets: [
.target(
name: "YourTarget",
dependencies: [
.product(name: "SwiftAISDK", package: "swift-ai-sdk"),
.product(name: "PerplexityProvider", package: "swift-ai-sdk")
]
)
]

You can import the default provider instance perplexity from PerplexityProvider:

import PerplexityProvider
// Use the global perplexity instance
let model = perplexity("sonar-pro")

For custom configuration, you can use createPerplexityProvider to create a provider instance with your settings:

import PerplexityProvider
let customPerplexity = createPerplexityProvider(
settings: PerplexityProviderSettings(
apiKey: ProcessInfo.processInfo.environment["PERPLEXITY_API_KEY"] ?? ""
)
)

You can use the following optional settings to customize the Perplexity provider instance:

  • baseURL String

    Use a different URL prefix for API calls. The default prefix is https://api.perplexity.ai.

  • apiKey String

    API key that is being sent using the Authorization header. It defaults to the PERPLEXITY_API_KEY environment variable.

  • headers [String: String]

    Custom headers to include in the requests.

You can create Perplexity models using a provider instance:

import SwiftAISDK
import PerplexityProvider
let result = try await generateText(
model: perplexity("sonar-pro"),
prompt: "What are the latest developments in quantum computing?"
)
let text = result.text

Websites that have been used to generate the response are included in the sources property of the result:

import SwiftAISDK
import PerplexityProvider
let result = try await generateText(
model: perplexity("sonar-pro"),
prompt: "What are the latest developments in quantum computing?"
)
print(result.sources)

The Perplexity provider includes additional metadata in the response through providerMetadata. Additional configuration options are available through providerOptions.

import SwiftAISDK
import PerplexityProvider
let result = try await generateText(
model: perplexity("sonar-pro"),
prompt: "What are the latest developments in quantum computing?",
providerOptions: PerplexityLanguageModelOptions(
returnImages: true // Enable image responses (Tier-2 Perplexity users only)
)
)
print(result.providerMetadata)
// Example output:
// PerplexityProviderMetadata(
// usage: PerplexityUsage(citationTokens: 5286, numSearchQueries: 1),
// images: [
// PerplexityImage(
// imageUrl: "https://example.com/image1.jpg",
// originUrl: "https://elsewhere.com/page1",
// height: 1280,
// width: 720
// )
// ]
// )

The metadata includes:

  • usage: Object containing citationTokens and numSearchQueries metrics
  • images: Array of image URLs when returnImages is enabled (Tier-2 users only)

You can enable image responses by setting returnImages: true in the provider options. This feature is only available to Perplexity Tier-2 users and above.

ModelImage InputObject GenerationTool UsageTool Streaming
sonar-deep-research
sonar-reasoning-pro
sonar-reasoning
sonar-pro
sonar