Skip to main content

Ein Hilfsmodul für die Perplexity API

Project description

README

Overview

The PerplexityAPI class is a Python wrapper for interacting with the Perplexity AI API. It allows users to send chat completion requests to the API and retrieve responses. This class handles the creation of payloads, sending requests, and parsing responses.

Installation

To use the PerplexityAPI class, you need to have the requests library installed. You can install it using pip:

pip install requests

Usage

Initialization

To initialize the PerplexityAPI class, you need to provide your API key:

from perplexity_api import PerplexityAPI

api_key = "your_api_key_here"
api = PerplexityAPI(api_key)

Sending a Request

To send a request to the Perplexity AI API, use the send_request method. You need to specify the model and messages, and you can also provide additional keyword arguments:

model = "your_model_name"
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Tell me a joke."}
]

response = api.send_request(model, messages)

Retrieving the Response

To retrieve the response content from the API, use the get_response method. You can pass the JSON data returned by the send_request method, or it will use the last response stored in the instance:

content = api.get_response(response)
print(content)

Methods

__init__(self, api_key)

Initializes the PerplexityAPI instance with the provided API key.

  • api_key: Your API key for the Perplexity AI API.

create_payload(self, model, messages, **kwargs)

Creates the payload for the API request.

  • model: The model name to use for the request.
  • messages: A list of message dictionaries.
  • **kwargs: Additional keyword arguments to include in the payload.

send_request(self, model, messages, **kwargs)

Sends a request to the Perplexity AI API and returns the JSON response.

  • model: The model name to use for the request.
  • messages: A list of message dictionaries.
  • **kwargs: Additional keyword arguments to include in the payload.

get_response(self, json_data=None)

Retrieves the response content from the JSON data. If no JSON data is provided, it uses the last response stored in the instance.

  • json_data: The JSON data returned by the send_request method (optional).

Example

from perplexity_api import PerplexityAPI

api_key = "your_api_key_here"
api = PerplexityAPI(api_key)

model = "your_model_name"
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Tell me a joke."}
]

response = api.send_request(model, messages)
content = api.get_response(response)
print(content)

This example demonstrates how to initialize the PerplexityAPI class, send a request to the Perplexity AI API, and retrieve the response content.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pplx-0.1.3.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pplx-0.1.3-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file pplx-0.1.3.tar.gz.

File metadata

  • Download URL: pplx-0.1.3.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.6

File hashes

Hashes for pplx-0.1.3.tar.gz
Algorithm Hash digest
SHA256 9d86d64245688384498682b7bb42ea353fa6a1763b8a7cb81817eb613efd7a73
MD5 5007fa0ab96bdbe8d351c79fb152cc52
BLAKE2b-256 4b4809f3f09fcb4792766e0ceb45f2ebf3aee1a08c2a0fb965f81bc1adffbc29

See more details on using hashes here.

File details

Details for the file pplx-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pplx-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.6

File hashes

Hashes for pplx-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 58bd557a4e39930c90da1cd1e68465367550dc41a5f2007c113f93ce52a3350b
MD5 ca94b96e708422d71b193517907746e0
BLAKE2b-256 eedc99805d5200004909e043f301d59b3a17142981fb042d3193015abb5fca3e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page