Class: Boxcars::Perplexityai
- Includes:
- OpenAICompatibleChatHelpers, UnifiedObservability
- Defined in:
- lib/boxcars/engine/perplexityai.rb
Overview
An engine that uses PerplexityAI’s API.
Constant Summary collapse
- DEFAULT_PARAMS =
{ model: "llama-3-sonar-large-32k-online", temperature: 0.1 }.freeze
- DEFAULT_NAME =
"PerplexityAI engine"- DEFAULT_DESCRIPTION =
"useful for when you need to use Perplexity AI to answer questions. " \ "You should ask targeted questions"
Instance Attribute Summary collapse
-
#perplexity_params ⇒ Object
readonly
Returns the value of attribute perplexity_params.
Attributes inherited from Engine
Instance Method Summary collapse
-
#client(prompt:, inputs: {}, perplexity_api_key: nil, **kwargs) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#default_params ⇒ Object
rubocop:enable Metrics/MethodLength.
-
#initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, batch_size: 20, **kwargs) ⇒ Perplexityai
constructor
A new instance of Perplexityai.
Methods inherited from Engine
#add_usage_detail!, #aggregate_generate_usage!, #aggregate_token_usage_details!, #append_generate_choices!, #capabilities, #extract_answer, #generate, #generate_one, #generation_info, #get_num_tokens, #normalize_generate_response, #process_generate_prompt!, #process_generate_response!, #run, #supports?, #usage_nested_token_value, #usage_token_value, #validate_response!
Constructor Details
#initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, batch_size: 20, **kwargs) ⇒ Perplexityai
Returns a new instance of Perplexityai.
21 22 23 24 25 26 27 |
# File 'lib/boxcars/engine/perplexityai.rb', line 21 def initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, batch_size: 20, **kwargs) raise ArgumentError, "unknown keyword: :prompts" if kwargs.key?(:prompts) user_id = kwargs.delete(:user_id) @perplexity_params = DEFAULT_PARAMS.merge(kwargs) super(description:, name:, batch_size:, user_id:) end |
Instance Attribute Details
#perplexity_params ⇒ Object (readonly)
Returns the value of attribute perplexity_params.
11 12 13 |
# File 'lib/boxcars/engine/perplexityai.rb', line 11 def perplexity_params @perplexity_params end |
Instance Method Details
#client(prompt:, inputs: {}, perplexity_api_key: nil, **kwargs) ⇒ Object
rubocop:disable Metrics/MethodLength
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/boxcars/engine/perplexityai.rb', line 30 def client(prompt:, inputs: {}, perplexity_api_key: nil, **kwargs) start_time = Time.now response_data = { response_obj: nil, parsed_json: nil, success: false, error: nil, status_code: nil } current_params = @perplexity_params.merge(kwargs) api_request_params = nil current_prompt_object = prompt begin Boxcars::OptionalDependency.require!("faraday", feature: "Boxcars::Perplexityai") api_key = perplexity_api_key || Boxcars.configuration.perplexity_api_key(**current_params.slice(:perplexity_api_key)) raise Boxcars::ConfigurationError, "Perplexity API key not set" if api_key.nil? || api_key.strip.empty? conn = Faraday.new(url: "https://api.perplexity.ai") do |faraday| faraday.request :json faraday.response :json faraday.response :raise_error faraday.adapter Faraday.default_adapter end = current_prompt_object.(inputs)[:messages] supported_params = filter_supported_params(current_params) api_request_params = { model: supported_params[:model], messages: }.merge(supported_params.except(:model, :messages, :perplexity_api_key)) (api_request_params[:messages]) if Boxcars.configuration.log_prompts && api_request_params[:messages] response = conn.post('/chat/completions') do |req| req.headers['Authorization'] = "Bearer #{api_key}" req.body = api_request_params end parsed_json = normalize_generate_response(response.body) response_data[:response_obj] = response response_data[:parsed_json] = parsed_json response_data[:status_code] = response.status if response.success? && parsed_json["choices"] response_data[:success] = true else response_data[:success] = false err_details = parsed_json["error"] if parsed_json.is_a?(Hash) msg = if err_details "#{err_details['type']}: #{err_details['message']}" else "Unknown Perplexity API Error (status: #{response.status})" end response_data[:error] = StandardError.new(msg) end rescue StandardError => e handle_openai_compatible_standard_error(e, response_data) if defined?(Faraday::Error) && e.is_a?(Faraday::Error) response_data[:status_code] = e.response_status if e.respond_to?(:response_status) if e.respond_to?(:response) && e.response.is_a?(Hash) normalized_error_response = normalize_generate_response(e.response) response_data[:response_obj] = e.response response_data[:status_code] ||= normalized_error_response["status"] error_body = normalized_error_response["body"] response_data[:parsed_json] = error_body if error_body.is_a?(Hash) end end ensure duration_ms = ((Time.now - start_time) * 1000).round request_context = { prompt: current_prompt_object, inputs:, user_id:, conversation_for_api: api_request_params&.dig(:messages) } track_ai_generation( duration_ms:, current_params:, request_context:, response_data:, provider: :perplexity_ai ) end perplexity_handle_call_outcome(response_data:) end |
#default_params ⇒ Object
rubocop:enable Metrics/MethodLength
113 114 115 |
# File 'lib/boxcars/engine/perplexityai.rb', line 113 def default_params @perplexity_params end |