Documentation
¶
Overview ¶
Package perplexity implements a client for the Perplexity API.
It is described at https://docs.perplexity.ai/api-reference
Index ¶
- func ProcessStream(chunks iter.Seq[ChatStreamChunkResponse]) (iter.Seq[genai.Reply], func() (genai.Usage, [][]genai.Logprob, error))
- func Scoreboard() scoreboard.Score
- type ChatRequest
- type ChatResponse
- type ChatStreamChunkResponse
- type Client
- func (c *Client) GenStream(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (iter.Seq[genai.Reply], func() (genai.Result, error))
- func (c *Client) GenStreamRaw(ctx context.Context, in *ChatRequest) (iter.Seq[ChatStreamChunkResponse], func() error)
- func (c *Client) GenSync(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (genai.Result, error)
- func (c *Client) GenSyncRaw(ctx context.Context, in *ChatRequest, out *ChatResponse) error
- func (c *Client) HTTPClient() *http.Client
- func (c *Client) ModelID() string
- func (c *Client) Name() string
- func (c *Client) OutputModalities() genai.Modalities
- func (c *Client) Scoreboard() scoreboard.Score
- type Content
- type Contents
- type ErrorResponse
- type FinishReason
- type GenOption
- type Images
- type Message
- type SearchResult
- type Usage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ChatRequest ¶
type ChatRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
SearchMode string `json:"search_mode,omitzero"` // "web", "academic"
ReasoningEffort string `json:"reasoning_effort,omitzero"` // "low", "medium", "high" (model: sonar-deep-research)
MaxTokens int64 `json:"max_tokens,omitzero"`
Temperature float64 `json:"temperature,omitzero"` // [0, 2.0]
TopP float64 `json:"top_p,omitzero"` // [0, 1.0]
SearchDomainFilter []string `json:"search_domain_filter,omitzero"` // Max 10 items. Prefix "-" to exclude.
ReturnImages bool `json:"return_images,omitzero"`
ReturnRelatedQuestions bool `json:"return_related_questions,omitzero"`
SearchRecencyFilter string `json:"search_recency_filter,omitzero"` // "month", "week", "day", "hour"
SearchAfterDateFilter string `json:"search_after_date_filter,omitzero"` // RFC3339 date
SearchBeforeDateFilter string `json:"search_before_date_filter,omitzero"` // RFC3339 date
LastUpdatedAfterFilter string `json:"last_updated_after_filter,omitzero"` // RFC3339 date
LastUpdatedBeforeFilter string `json:"last_updated_before_filter,omitzero"` // RFC3339 date
TopK int64 `json:"top_k,omitzero"` // [0, 2048]
Stream bool `json:"stream"` //
PresencePenalty float64 `json:"presence_penalty,omitzero"` // [0, 2.0]
FrequencyPenalty float64 `json:"frequency_penalty,omitzero"` // [0, 2.0]
// Only available in higher tiers, see
// https://docs.perplexity.ai/guides/usage-tiers and
// https://docs.perplexity.ai/guides/structured-outputs
ResponseFormat struct {
Type string `json:"type,omitzero"` // "json_schema", "regex"
JSONSchema struct {
Schema *jsonschema.Schema `json:"schema,omitzero"`
} `json:"json_schema,omitzero"`
Regex struct {
Regex string `json:"regex,omitzero"`
} `json:"regex,omitzero"`
} `json:"response_format,omitzero"`
WebSearchOptions struct {
SearchContextSize string `json:"search_context_size,omitzero"` // "low", "medium", "high"
UserLocation struct {
Latitude float64 `json:"latitude,omitzero"` // e.g. 37.7749
Longitude float64 `json:"longitude,omitzero"` // e.g. -122.4194
CountryCode string `json:"country_code,omitzero"` // e.g. "US", "CA", "FR"
} `json:"user_location,omitzero"`
} `json:"web_search_options,omitzero"`
// These are "documented" at https://docs.perplexity.ai/guides/search-control-guide#curl-2
DisableSearch bool `json:"disable_search,omitzero"`
EnableSearchClassifier bool `json:"enable_search_classifier,omitzero"`
}
ChatRequest is documented at https://docs.perplexity.ai/api-reference/chat-completions-post
func (*ChatRequest) Init ¶
Init initializes the provider specific completion request with the generic completion request.
func (*ChatRequest) SetStream ¶
func (c *ChatRequest) SetStream(stream bool)
SetStream sets the streaming mode.
type ChatResponse ¶
type ChatResponse struct {
ID string `json:"id"` // UUID
Model string `json:"model"`
Object string `json:"object"` // "chat.completion"
Created base.Time `json:"created"`
Citations []string `json:"citations"` // Same URLs from SearchResults in the same order.
Images []Images `json:"images"` // The images do not seem to have a direct relation with the citations.
Choices []struct {
Index int64 `json:"index"`
FinishReason FinishReason `json:"finish_reason"`
Message Message `json:"message"`
Delta struct {
Content string `json:"content"`
Role string `json:"role"`
} `json:"delta"`
} `json:"choices"`
RelatedQuestions []string `json:"related_questions"` // Questions related to the query
SearchResults []SearchResult `json:"search_results"`
Usage Usage `json:"usage"`
}
ChatResponse is the provider-specific chat completion response.
type ChatStreamChunkResponse ¶
type ChatStreamChunkResponse = ChatResponse
ChatStreamChunkResponse is the provider-specific streaming chat chunk.
type Client ¶
type Client struct {
base.NotImplemented
// contains filtered or unexported fields
}
Client implements genai.Provider.
func New ¶
New creates a new client to talk to the Perplexity platform API.
If ProviderOptionAPIKey is not provided, it tries to load it from the PERPLEXITY_API_KEY environment variable. If none is found, it will still return a client coupled with an base.ErrAPIKeyRequired error. Get your API key at https://www.perplexity.ai/settings/api
To use multiple models, create multiple clients. Models are listed at https://docs.perplexity.ai/guides/model-cards
func (*Client) GenStream ¶
func (c *Client) GenStream(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (iter.Seq[genai.Reply], func() (genai.Result, error))
GenStream implements genai.Provider.
func (*Client) GenStreamRaw ¶
func (c *Client) GenStreamRaw(ctx context.Context, in *ChatRequest) (iter.Seq[ChatStreamChunkResponse], func() error)
GenStreamRaw provides access to the raw API.
func (*Client) GenSync ¶
func (c *Client) GenSync(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (genai.Result, error)
GenSync implements genai.Provider.
func (*Client) GenSyncRaw ¶
func (c *Client) GenSyncRaw(ctx context.Context, in *ChatRequest, out *ChatResponse) error
GenSyncRaw provides access to the raw API.
func (*Client) HTTPClient ¶
HTTPClient returns the HTTP client to fetch results (e.g. videos) generated by the provider.
func (*Client) OutputModalities ¶
func (c *Client) OutputModalities() genai.Modalities
OutputModalities implements genai.Provider.
It returns the output modalities, i.e. what kind of output the model will generate (text, audio, image, video, etc).
func (*Client) Scoreboard ¶
func (c *Client) Scoreboard() scoreboard.Score
Scoreboard implements genai.Provider.
type Content ¶
type Content struct {
Type string `json:"type"` // "text", "image_url"
Text string `json:"text,omitzero"`
ImageURL struct {
URL string `json:"url,omitzero"`
} `json:"image_url,omitzero"`
}
Content is a provider-specific content block.
type Contents ¶
type Contents []Content
Contents is a collection of content blocks.
func (*Contents) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type ErrorResponse ¶
type ErrorResponse struct {
Detail string `json:"detail"`
ErrorVal struct {
Message string `json:"message"`
Type string `json:"type"`
Code int `json:"code"`
} `json:"error"`
}
ErrorResponse is the provider-specific error response.
func (*ErrorResponse) Error ¶
func (er *ErrorResponse) Error() string
func (*ErrorResponse) IsAPIError ¶
func (er *ErrorResponse) IsAPIError() bool
IsAPIError implements base.ErrorResponseI.
type FinishReason ¶
type FinishReason string
FinishReason is a provider-specific finish reason.
const ( FinishStop FinishReason = "stop" FinishLength FinishReason = "length" )
Finish reason values.
func (FinishReason) ToFinishReason ¶
func (f FinishReason) ToFinishReason() genai.FinishReason
ToFinishReason converts to a genai.FinishReason.
type GenOption ¶ added in v0.2.0
type GenOption struct {
// DisableRelatedQuestions disabled related questions, to save on tokens and latency.
DisableRelatedQuestions bool
}
GenOption defines Perplexity specific options.
type Images ¶
type Images struct {
Height int64 `json:"height"` // in pixels
ImageURL string `json:"image_url"` // URL to the image
OriginURL string `json:"origin_url"` // URL to the page that contains the image
Width int64 `json:"width"` // in pixels
Title string `json:"title,omitzero"`
}
Images is a provider-specific images container.
type Message ¶
type Message struct {
Role string `json:"role"` // "system", "assistant", "user"
Content Contents `json:"content,omitzero"`
}
Message is documented at https://docs.perplexity.ai/api-reference/chat-completions
type SearchResult ¶
type SearchResult struct {
Date string `json:"date"` // RFC3339 date, or null
Title string `json:"title"`
URL string `json:"url"` // URL to the search result
LastUpdated string `json:"last_updated"` // YYYY-MM-DD
Snippet string `json:"snippet"` // TODO: Add!
Source string `json:"source,omitzero"`
}
SearchResult is a provider-specific search result.
type Usage ¶
type Usage struct {
PromptTokens int64 `json:"prompt_tokens"`
CompletionTokens int64 `json:"completion_tokens"`
TotalTokens int64 `json:"total_tokens"`
SearchContextSize string `json:"search_context_size"` // "low"
ReasoningTokens int64 `json:"reasoning_tokens"`
CitationTokens int64 `json:"citation_tokens"`
NumSearchQueries int64 `json:"num_search_queries"`
Cost struct {
RequestCost float64 `json:"request_cost"`
InputTokensCost float64 `json:"input_tokens_cost"`
OutputTokensCost float64 `json:"output_tokens_cost"`
ReasoningTokensCost float64 `json:"reasoning_tokens_cost"`
CitationTokensCost float64 `json:"citation_tokens_cost"`
SearchQueriesCost float64 `json:"search_queries_cost"`
TotalCost float64 `json:"total_cost"`
} `json:"cost"`
}
Usage is the provider-specific token usage.