Class: Durable::Llm::Providers::Perplexity

Inherits:
Base
  • Object
show all
Defined in:
lib/durable/llm/providers/perplexity.rb

Overview

The Perplexity provider class for interacting with Perplexity’s API.

This class provides methods for text completion, embedding generation, streaming responses, and model listing using Perplexity’s language models. It handles authentication, HTTP communication, error handling, and response normalization to provide a consistent interface for Perplexity’s API services.

Defined Under Namespace

Classes: PerplexityChoice, PerplexityEmbeddingResponse, PerplexityMessage, PerplexityResponse, PerplexityStreamChoice, PerplexityStreamDelta, PerplexityStreamResponse

Constant Summary collapse

BASE_URL =
'https://api.perplexity.ai'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

models, options, #stream?

Constructor Details

#initialize(api_key: nil) ⇒ Perplexity

Returns a new instance of Perplexity.



37
38
39
40
41
42
43
44
# File 'lib/durable/llm/providers/perplexity.rb', line 37

def initialize(api_key: nil)
  super
  @conn = Faraday.new(url: BASE_URL) do |faraday|
    faraday.request :json
    faraday.response :json
    faraday.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



35
36
37
# File 'lib/durable/llm/providers/perplexity.rb', line 35

def api_key
  @api_key
end

Class Method Details

.stream?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/durable/llm/providers/perplexity.rb', line 72

def self.stream?
  true
end

Instance Method Details

#completion(options) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/durable/llm/providers/perplexity.rb', line 46

def completion(options)
  response = @conn.post('chat/completions') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.body = options
  end

  handle_response(response)
end

#default_api_keyObject



27
28
29
30
31
32
33
# File 'lib/durable/llm/providers/perplexity.rb', line 27

def default_api_key
  begin
    Durable::Llm.configuration.perplexity&.api_key
  rescue NoMethodError
    nil
  end || ENV['PERPLEXITY_API_KEY']
end

#embedding(model:, input:, **options) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/durable/llm/providers/perplexity.rb', line 55

def embedding(model:, input:, **options)
  response = @conn.post('embeddings') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.body = { model: model, input: input, **options }
  end

  handle_response(response, PerplexityEmbeddingResponse)
end

#modelsObject



64
65
66
67
68
69
70
# File 'lib/durable/llm/providers/perplexity.rb', line 64

def models
  response = @conn.get('models') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
  end

  handle_response(response).data.map { |model| model['id'] }
end

#stream(options) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/durable/llm/providers/perplexity.rb', line 76

def stream(options)
  options[:stream] = true

  response = @conn.post('chat/completions') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.headers['Accept'] = 'text/event-stream'

    options['temperature'] = options['temperature'].to_f if options['temperature']

    req.body = options

    user_proc = proc do |chunk, _size, _total|
      yield PerplexityStreamResponse.new(chunk)
    end

    req.options.on_data = to_json_stream(user_proc: user_proc)
  end

  handle_response(response)
end