Table of Contents

Class PerplexityService

Namespace
Mythosia.AI.Services.Perplexity
Assembly
Mythosia.AI.dll

Perplexity Agent API with hosted search, local function calling and streaming.

public class PerplexityService : AIService, IAIService, IAIRunService, IAIRequestFeatureService, IFunctionRegisterable, IAIProcessingInfoService
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)

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

Parameters

apiKey string
model string
httpClient HttpClient

Properties

AgentOptions

public PerplexityAgentOptions AgentOptions { get; set; }

Property Value

PerplexityAgentOptions

LastResponseId

public string? LastResponseId { get; }

Property Value

string

Provider

The AI provider for this service

public override string Provider { get; }

Property Value

string

Methods

ApplyCapabilityRequestProfile(AIRequestProfile)

Applies only execution-local profile settings needed to describe capabilities.

protected override void ApplyCapabilityRequestProfile(AIRequestProfile profile)

Parameters

profile AIRequestProfile

Remarks

Common builder overrides are already captured. Custom providers may override this hook with SetExecutionSetting calls for native mode flags. Do not prepare execution, reserve token budgets, invoke callbacks, validate requests, or modify service/caller-owned state. Execution profile hooks are intentionally not called during inspection.

ApplyProviderSpecificRequestProfile(AIRequestProfile)

protected override Action ApplyProviderSpecificRequestProfile(AIRequestProfile profile)

Parameters

profile AIRequestProfile

Returns

Action

CaptureProviderRequestOptions(Message)

Captures and consumes provider-specific per-request options before execution starts.

protected override object? CaptureProviderRequestOptions(Message message)

Parameters

message Message

Returns

object

Remarks

Framework delegation and tool rounds reuse the returned snapshot; new public calls capture independently. Internal summary and rewrite work do not call this hook and do not inherit these options.

CaptureRequestSettings(IDictionary<string, object?>)

Captures provider and common defaults. Overrides copy native mutable option values.

protected override void CaptureRequestSettings(IDictionary<string, object?> settings)

Parameters

settings IDictionary<string, object>

CloneProviderRequestOptions(object?)

Creates fresh provider execution state when a prepared request is executed again.

protected override object? CloneProviderRequestOptions(object? options)

Parameters

options object

Returns

object

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

GetAgentResponseAsync(string, CancellationToken)

Retrieves a stored response, including responses created without background mode.

public Task<PerplexityAgentResponse> GetAgentResponseAsync(string responseId, CancellationToken cancellationToken = default)

Parameters

responseId string
cancellationToken CancellationToken

Returns

Task<PerplexityAgentResponse>

GetCompletionAsync(Message)

public override Task<string> GetCompletionAsync(Message message)

Parameters

message Message

Returns

Task<string>

GetConversationCompactionBlockReason()

Returns a reason when protocol state requires retaining the original conversation prefix.

protected override string? GetConversationCompactionBlockReason()

Returns

string

GetInputTokenCountAsync()

Gets the token count for the current conversation

public override Task<uint> GetInputTokenCountAsync()

Returns

Task<uint>

GetInputTokenCountAsync(string)

Returns a local approximation; authoritative usage comes from the Agent response.

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

GetRequestMessageOverrideTargetId()

Identifies the user input replaced by request context while tool rounds append messages.

protected override string? GetRequestMessageOverrideTargetId()

Returns

string

GetResponseFileContentAsync(string, string, CancellationToken)

public Task<byte[]> GetResponseFileContentAsync(string responseId, string fileId, CancellationToken cancellationToken = default)

Parameters

responseId string
fileId string
cancellationToken CancellationToken

Returns

Task<byte[]>

GetResponseFilesAsync(string, CancellationToken)

public Task<IReadOnlyList<PerplexityResponseFile>> GetResponseFilesAsync(string responseId, CancellationToken cancellationToken = default)

Parameters

responseId string
cancellationToken CancellationToken

Returns

Task<IReadOnlyList<PerplexityResponseFile>>

GetRunRequestedModel()

Resolves the explicit model ID sent for the captured run request.

protected override string? GetRunRequestedModel()

Returns

string

Remarks

Override when provider options replace the configured model or translate it to a wire ID. Return null when the request delegates model selection without a single model ID. Called within the captured request settings and provider-feature scope.

ResolveRequestCapabilities()

Resolves adapter support using RequestSetting and CurrentRequestFeatures without side effects.

protected override AIModelCapabilities ResolveRequestCapabilities()

Returns

AIModelCapabilities

Remarks

Custom providers may override this hook. The default preserves compatibility by returning Unknown. Overrides must not send requests, consume options, change history or invoke user callbacks.

ResumeBackgroundRun(string)

Reattaches to a saved response ID. No network request is made until the handle is used.

public PerplexityBackgroundRun ResumeBackgroundRun(string responseId)

Parameters

responseId string

Returns

PerplexityBackgroundRun

StartBackgroundAsync(Message, AIRequestContext?, CancellationToken)

public Task<PerplexityBackgroundRun> StartBackgroundAsync(Message message, AIRequestContext? context = null, CancellationToken cancellationToken = default)

Parameters

message Message
context AIRequestContext
cancellationToken CancellationToken

Returns

Task<PerplexityBackgroundRun>

StartBackgroundAsync(string, AIRequestContext?, CancellationToken)

Submits durable server work. The returned handle can poll, reconnect, cancel and retrieve files.

public Task<PerplexityBackgroundRun> StartBackgroundAsync(string prompt, AIRequestContext? context = null, CancellationToken cancellationToken = default)

Parameters

prompt string
context AIRequestContext
cancellationToken CancellationToken

Returns

Task<PerplexityBackgroundRun>

Remarks

Captures the conversation without appending a turn. Client functions require StartRunAsync; background work supports hosted tools and MCP. Jobs are created with streaming enabled so the handle can reconnect. Caller cancellation stops submission, not a known server job.

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)

Core streaming loop. Override this method to replace the full streaming pipeline (round loop, StatelessMode, summary policy). Most providers should override StreamRoundAsync(StreamOptions, bool, FunctionCallingPolicy, CancellationToken) instead.

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

StreamRoundAsync(StreamOptions, bool, FunctionCallingPolicy, CancellationToken)

Executes a single streaming round: sends an HTTP request, reads the SSE stream, yields chunks, and handles function execution if detected. Yield a FunctionResult to signal the template to continue to the next round. Pending native asynchronous calls also keep the round loop active until their results have been delivered and the model finishes.

protected override IAsyncEnumerable<StreamingContent> StreamRoundAsync(StreamOptions options, bool useFunctions, FunctionCallingPolicy policy, CancellationToken cancellationToken)

Parameters

options StreamOptions
useFunctions bool
policy FunctionCallingPolicy
cancellationToken CancellationToken

Returns

IAsyncEnumerable<StreamingContent>

UsePreset(PerplexityPreset)

public PerplexityService UsePreset(PerplexityPreset preset)

Parameters

preset PerplexityPreset

Returns

PerplexityService

ValidateProviderRequestOptions(object?, Message)

Validates captured native options against the completed request before history or HTTP changes.

protected override void ValidateProviderRequestOptions(object? options, Message message)

Parameters

options object
message Message

ValidateRequestFeatures(AIRequestFeatures)

Reject unsupported features before any network request or history mutation.

protected override void ValidateRequestFeatures(AIRequestFeatures features)

Parameters

features AIRequestFeatures

WithPerplexityOptions(PerplexityAgentOptions)

public PerplexityService WithPerplexityOptions(PerplexityAgentOptions options)

Parameters

options PerplexityAgentOptions

Returns

PerplexityService