Class: Perplexity::API

Inherits:
Object
  • Object
show all
Defined in:
lib/perplexity.rb

Overview

Public: Perplexity AI API client

Constant Summary collapse

DEFAULT_URI_BASE =
"https://api.perplexity.ai"
DEFAULT_REQUEST_TIMEOUT =
120
MODELS =
%w[
  sonar-small-chat
  sonar-small-online
  sonar-medium-chat
  sonar-medium-online
  codellama-70b-instruct
  mistral-7b-instruct
  mixtral-8x7b-instruct
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil) ⇒ API

Returns a new instance of API.



26
27
28
29
# File 'lib/perplexity.rb', line 26

def initialize(api_key: nil)
  @api_key = api_key
  configure!
end

Instance Attribute Details

#api_key=(value) ⇒ Object (writeonly)

Sets the attribute api_key

Parameters:

  • value

    the value to set the attribute api_key to.



12
13
14
# File 'lib/perplexity.rb', line 12

def api_key=(value)
  @api_key = value
end

Instance Method Details

#clientObject



42
43
44
# File 'lib/perplexity.rb', line 42

def client
  @client ||= OpenAI::Client.new
end

#configure!Object

Raises:



31
32
33
34
35
36
37
38
39
40
# File 'lib/perplexity.rb', line 31

def configure!
  raise ConfigurationError, "API key is required" unless @api_key

  OpenAI.configure do |config|
    config.access_token = @api_key
    config.uri_base = DEFAULT_URI_BASE
    config.api_version = "" # NOTE: Perplexity API URLs are not versioned
    config.extra_headers = { "Content-Type" => "application/json" }
  end
end

#modelsObject



46
47
48
# File 'lib/perplexity.rb', line 46

def models
  MODELS
end