Class: PplxApi::Client
- Inherits:
-
Object
- Object
- PplxApi::Client
- Defined in:
- lib/pplx_api/client.rb
Constant Summary collapse
- API_ENDPOINT =
"https://api.perplexity.ai/chat/completions".freeze
Instance Method Summary collapse
- #generate_response(model: 'mistral-7b-instruct', messages:, max_tokens: nil, temperature: nil, top_p: nil, top_k: nil, stream: false, presence_penalty: nil, frequency_penalty: nil) ⇒ Object
-
#initialize(api_key: nil) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(api_key: nil) ⇒ Client
Returns a new instance of Client.
5 6 7 8 |
# File 'lib/pplx_api/client.rb', line 5 def initialize(api_key: nil) @uri = URI(API_ENDPOINT) @api_key = api_key || PplxApi.config.api_key end |
Instance Method Details
#generate_response(model: 'mistral-7b-instruct', messages:, max_tokens: nil, temperature: nil, top_p: nil, top_k: nil, stream: false, presence_penalty: nil, frequency_penalty: nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/pplx_api/client.rb', line 10 def generate_response(model: 'mistral-7b-instruct', messages:, max_tokens: nil, temperature: nil, top_p: nil, top_k: nil, stream: false, presence_penalty: nil, frequency_penalty: nil) headers = { "Content-Type" => "application/json", "Authorization" => "Bearer #{@api_key}" } body = { model: model, messages: , max_tokens: max_tokens, temperature: temperature, top_p: top_p, top_k: top_k, stream: stream, presence_penalty: presence_penalty, frequency_penalty: frequency_penalty }.compact request = Net::HTTP::Post.new(@uri, 'Content-Type' => 'application/json') request.body = body.to_json response = Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: true) do |http| http.request(request) end case response when Net::HTTPSuccess JSON.parse(response.body) when Net::HTTPUnprocessableEntity raise "Validation Error" else raise "Unknown Error: #{response.code}" end end |