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
apiKeystringhttpClientHttpClient
PerplexityService(string, string, HttpClient)
public PerplexityService(string apiKey, string model, HttpClient httpClient)
Parameters
apiKeystringmodelstringhttpClientHttpClient
Properties
AgentOptions
public PerplexityAgentOptions AgentOptions { get; set; }
Property Value
LastResponseId
public string? LastResponseId { get; }
Property Value
Provider
The AI provider for this service
public override string Provider { get; }
Property Value
Methods
ApplyCapabilityRequestProfile(AIRequestProfile)
Applies only execution-local profile settings needed to describe capabilities.
protected override void ApplyCapabilityRequestProfile(AIRequestProfile profile)
Parameters
profileAIRequestProfile
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
profileAIRequestProfile
Returns
CaptureProviderRequestOptions(Message)
Captures and consumes provider-specific per-request options before execution starts.
protected override object? CaptureProviderRequestOptions(Message message)
Parameters
messageMessage
Returns
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
settingsIDictionary<string, object>
CloneProviderRequestOptions(object?)
Creates fresh provider execution state when a prepared request is executed again.
protected override object? CloneProviderRequestOptions(object? options)
Parameters
optionsobject
Returns
CreateFunctionMessageRequest()
Creates HTTP request with function definitions
protected override HttpRequestMessage CreateFunctionMessageRequest()
Returns
CreateMessageRequest()
Creates the HTTP request message for the AI service
protected override HttpRequestMessage CreateMessageRequest()
Returns
ExtractFunctionCalls(string)
Extracts every function call from one API response.
protected override (string content, FunctionCallBatch functionCalls) ExtractFunctionCalls(string response)
Parameters
responsestring
Returns
ExtractResponseContent(string)
Extracts the response content from the API response
protected override string ExtractResponseContent(string responseContent)
Parameters
responseContentstring
Returns
GetAgentResponseAsync(string, CancellationToken)
Retrieves a stored response, including responses created without background mode.
public Task<PerplexityAgentResponse> GetAgentResponseAsync(string responseId, CancellationToken cancellationToken = default)
Parameters
responseIdstringcancellationTokenCancellationToken
Returns
GetCompletionAsync(Message)
public override Task<string> GetCompletionAsync(Message message)
Parameters
messageMessage
Returns
GetConversationCompactionBlockReason()
Returns a reason when protocol state requires retaining the original conversation prefix.
protected override string? GetConversationCompactionBlockReason()
Returns
GetInputTokenCountAsync()
Gets the token count for the current conversation
public override Task<uint> GetInputTokenCountAsync()
Returns
GetInputTokenCountAsync(string)
Returns a local approximation; authoritative usage comes from the Agent response.
public override Task<uint> GetInputTokenCountAsync(string prompt)
Parameters
promptstring
Returns
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
GetRequestMessageOverrideTargetId()
Identifies the user input replaced by request context while tool rounds append messages.
protected override string? GetRequestMessageOverrideTargetId()
Returns
GetResponseFileContentAsync(string, string, CancellationToken)
public Task<byte[]> GetResponseFileContentAsync(string responseId, string fileId, CancellationToken cancellationToken = default)
Parameters
responseIdstringfileIdstringcancellationTokenCancellationToken
Returns
GetResponseFilesAsync(string, CancellationToken)
public Task<IReadOnlyList<PerplexityResponseFile>> GetResponseFilesAsync(string responseId, CancellationToken cancellationToken = default)
Parameters
responseIdstringcancellationTokenCancellationToken
Returns
GetRunRequestedModel()
Resolves the explicit model ID sent for the captured run request.
protected override string? GetRunRequestedModel()
Returns
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
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
responseIdstring
Returns
StartBackgroundAsync(Message, AIRequestContext?, CancellationToken)
public Task<PerplexityBackgroundRun> StartBackgroundAsync(Message message, AIRequestContext? context = null, CancellationToken cancellationToken = default)
Parameters
messageMessagecontextAIRequestContextcancellationTokenCancellationToken
Returns
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
promptstringcontextAIRequestContextcancellationTokenCancellationToken
Returns
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
Returns
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
messageMessageoptionsStreamOptionscancellationTokenCancellationToken
Returns
StreamParseJson(string)
Parses streaming JSON data
protected override string StreamParseJson(string jsonData)
Parameters
jsonDatastring
Returns
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
optionsStreamOptionsuseFunctionsboolpolicyFunctionCallingPolicycancellationTokenCancellationToken
Returns
UsePreset(PerplexityPreset)
public PerplexityService UsePreset(PerplexityPreset preset)
Parameters
presetPerplexityPreset
Returns
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
ValidateRequestFeatures(AIRequestFeatures)
Reject unsupported features before any network request or history mutation.
protected override void ValidateRequestFeatures(AIRequestFeatures features)
Parameters
featuresAIRequestFeatures
WithPerplexityOptions(PerplexityAgentOptions)
public PerplexityService WithPerplexityOptions(PerplexityAgentOptions options)
Parameters
optionsPerplexityAgentOptions