Perplexity 🔍

AI-powered search with real-time web access and citations.

🌐

What you can do: Web-grounded search, real-time information retrieval, research with citations, and reasoning models for complex queries.

Setup

Add your Perplexity API key in the ProtectMyAPI Dashboard.


let perplexity = ProtectMyAPI.perplexityService()
 
let response = try await perplexity.createChatCompletion(
    request: PerplexityChatRequest(
        model: .sonarLarge,
        messages: [
            .user("What are the latest features in iOS 18?")
        ]
    )
)
 
// Get the answer
print(response.choices.first?.message.content ?? "")
 
// Get citations (sources)
for citation in response.citations ?? [] {
    print("Source: \(citation)")
}

With Recency Filter

Get only recent information:

let response = try await perplexity.createChatCompletion(
    request: PerplexityChatRequest(
        model: .sonarPro,
        messages: [
            .user("Latest news about AI developments")
        ],
        searchRecencyFilter: .day // Only last 24 hours
    )
)

Domain Filtering

Search only specific domains:

let response = try await perplexity.createChatCompletion(
    request: PerplexityChatRequest(
        model: .sonarLarge,
        messages: [
            .user("SwiftUI best practices")
        ],
        searchDomainFilter: ["developer.apple.com", "swift.org"]
    )
)

Streaming

for try await chunk in perplexity.createChatCompletionStream(
    request: PerplexityChatRequest(
        model: .sonarLarge,
        messages: [.user("Explain quantum computing")]
    )
) {
    print(chunk.choices.first?.delta?.content ?? "", terminator: "")
}

For quick searches with summarized results:

let results = try await perplexity.search(
    queries: ["Best restaurants in San Francisco", "Michelin star"],
    options: PerplexitySearchOptions(
        model: .sonarPro,
        recencyFilter: .month
    )
)
 
print("Answer: \(results.answer ?? "")")
for result in results.results {
    print("- \(result.title): \(result.url)")
}

Reasoning Models

For complex questions requiring deep analysis:

let response = try await perplexity.createChatCompletion(
    request: PerplexityChatRequest(
        model: .sonarReasoningPro, // Best reasoning model
        messages: [
            .user("Compare the economic policies of the G7 nations and their impact on inflation in 2024")
        ]
    )
)

Available Models

ModelDescriptionBest For
llama-3.1-sonar-small-128k-onlineFast, smallQuick searches
llama-3.1-sonar-large-128k-onlineBalancedMost use cases
llama-3.1-sonar-huge-128k-onlineMost capableComplex research

Reasoning Models

ModelDescriptionBest For
sonar-reasoningStandard reasoningAnalysis
sonar-reasoning-proAdvanced reasoningComplex problems
ModelDescriptionBest For
llama-3.1-sonar-small-128k-chatFast, no searchConversation
llama-3.1-sonar-large-128k-chatBetter, no searchAnalysis

Recency Filters

FilterDescription
.hourLast hour only
.dayLast 24 hours
.weekLast 7 days
.monthLast 30 days
.yearLast year

Pricing Note

Perplexity charges per request. Online models cost more than chat models due to web search. Check their pricing page for current rates.