Table of Contents

Class PerplexityService

Namespace
Mythosia.AI.Services.Perplexity
Assembly
Mythosia.AI.dll
public class PerplexityService : AIService, IAIService, IFunctionRegisterable
Inheritance
PerplexityService
Implements
Inherited Members
Extension Methods

Constructors

PerplexityService(string, HttpClient)

public PerplexityService(string apiKey, HttpClient httpClient)

Parameters

apiKey string
httpClient HttpClient

PerplexityService(string, string, HttpClient)

Creates a PerplexityService with a specific model.

public PerplexityService(string apiKey, string model, HttpClient httpClient)

Parameters

apiKey string
model string
httpClient HttpClient

Properties

Provider

The AI provider for this service

public override string Provider { get; }

Property Value

string

Methods

CreateFunctionMessageRequest()

Creates HTTP request with function definitions

protected override HttpRequestMessage CreateFunctionMessageRequest()

Returns

HttpRequestMessage

CreateMessageRequest()

Creates the HTTP request message for the AI service

protected override HttpRequestMessage CreateMessageRequest()

Returns

HttpRequestMessage

ExtractFunctionCalls(string)

Extracts every function call from one API response.

protected override (string content, FunctionCallBatch functionCalls) ExtractFunctionCalls(string response)

Parameters

response string

Returns

(string content, FunctionCallBatch functionCalls)

ExtractResponseContent(string)

Extracts the response content from the API response

protected override string ExtractResponseContent(string responseContent)

Parameters

responseContent string

Returns

string

GetCompletionAsync(Message)

public override Task<string> GetCompletionAsync(Message message)

Parameters

message Message

Returns

Task<string>

GetCompletionWithImageAsync(string, string)

Perplexity doesn't support multimodal inputs

public override Task<string> GetCompletionWithImageAsync(string prompt, string imagePath)

Parameters

prompt string
imagePath string

Returns

Task<string>

GetCompletionWithSearchAsync(string, string[]?, string)

Gets a completion with web search and citations

public Task<PerplexityService.SonarSearchResponse> GetCompletionWithSearchAsync(string prompt, string[]? domainFilter = null, string recencyFilter = "month")

Parameters

prompt string
domainFilter string[]
recencyFilter string

Returns

Task<PerplexityService.SonarSearchResponse>

GetInputTokenCountAsync()

Gets the token count for the current conversation

public override Task<uint> GetInputTokenCountAsync()

Returns

Task<uint>

GetInputTokenCountAsync(string)

Gets the token count for a specific prompt

public override Task<uint> GetInputTokenCountAsync(string prompt)

Parameters

prompt string

Returns

Task<uint>

GetModelMaxOutputTokens()

Returns the maximum output tokens allowed for the current model. Override in each service to provide model-specific limits.

protected override uint GetModelMaxOutputTokens()

Returns

uint

StreamCompletionAsync(Message, Func<string, Task>)

public override Task StreamCompletionAsync(Message message, Func<string, Task> messageReceivedAsync)

Parameters

message Message
messageReceivedAsync Func<string, Task>

Returns

Task

StreamCoreAsync(Message, StreamOptions, CancellationToken)

Replaces the base pipeline outright for the Sonar-specific implementation; Perplexity has no function calling and so needs no round loop.

Consequence: context-overflow recovery, which lives in that loop, does not run here. An overflow surfaces as an error chunk — flagged context_length_exceeded so the caller can still identify it — but the conversation is not compacted and nothing is re-sent.

protected override IAsyncEnumerable<StreamingContent> StreamCoreAsync(Message message, StreamOptions options, CancellationToken cancellationToken = default)

Parameters

message Message
options StreamOptions
cancellationToken CancellationToken

Returns

IAsyncEnumerable<StreamingContent>

StreamParseJson(string)

Parses streaming JSON data

protected override string StreamParseJson(string jsonData)

Parameters

jsonData string

Returns

string

UseSonarPro()

Switches to Sonar Pro model for enhanced capabilities

public PerplexityService UseSonarPro()

Returns

PerplexityService

UseSonarReasoning()

Switches to Sonar Reasoning Pro model for complex reasoning tasks. The legacy sonar-reasoning model was removed from the Perplexity API on 2025-12-15; sonar-reasoning-pro is its official replacement.

public PerplexityService UseSonarReasoning()

Returns

PerplexityService

WithSearchParameters(bool, string, params string[])

Configures search parameters for the service

public PerplexityService WithSearchParameters(bool returnCitations = true, string recencyFilter = "month", params string[] domainFilter)

Parameters

returnCitations bool
recencyFilter string
domainFilter string[]

Returns

PerplexityService