PerplexiPy - A robust Perplexity AI API client
Project description
% perplexipy(3) Version 1.1.2 | Perplexity AI high level API documentation
Name
PerplexiPy - Perplexity AI high level library
Synopsis
client = PerplexityClient() \
print(client.query('What is the meaning of 42?') \
for result in client.queryStreamable('List of all US presidents'): \
print(result)
Description
PerplexiPy is a high-level, convenience library for interacting with the Perplexity API from any Python 3.9+ application. The library aims to simplify interactions with Perplexity models by encapsulating all the implementation details of the lower level OpenAI API. All interaction between the code and this library occurs in the form of string and native Python objects, not OpenAPI / Swagger mapped objects.
PerplexiPy implements a combination of the Perplexity AI and the OpenAI outputs. API semantics follow the "literate programming" workflow, and attempt to make the resulting code as simple to follow as possible.
Perplexity AI is an AI-powered search engine that uses natural language processing and machine learning to provide accurate and comprehensive answers to end-user queries. It can be argued that it outperforms OpenAI's offerings in accuracy and responsiveness. PerplexiPy enables the creation of Python programs and tools that leverage the power of Perplexity AI.
Documentation
These documents encompass all information about PerplexiPy:
- This README file, hosted in the perplexipy GitHub repository project on GitHub and exported to the PyPI project page
- A
man
page autogenerated from this README file,perplexipy.3
- The complete PerplexiPy API reference on GitHub Pages - https://cime-software.github.io/perplexipy/perplexipy.html
- The PerplexiPy-tutorial.ipynb tutorial notebook
The man
page can be generated from the source distribution using this command:
make manpage
The output will reside in: ./manpages/perplexipy.3
. The man
page isn't
included in the Python package wheel because there's no accepted standard
installation that's cross-compatible among all supported operating systems and
distributions.
Installation
pip install perplexipy
Package information: https://pypi.org/project/perplexipy
PerplexiPy requires an API key issued by Perplexity. You may set PERPLEXITY_API_KEY
as an environment variable or use a .env
file.
API key
Access to the Perplexity API requires a paid subscription and an API key.
Use of .env
is recommended for storing the private key. There is an automatic
constant PERPLEXITY_API_KEY
that gets initialized to the value of a .env
key
of the same name via the dotenv
API. Otherwise, PERPLEXITY_API_KEY
may
be handled like any other secret by the implementing team. This module provides
no other tools or services for handling the API key.
This key is used only during PerplexityClient
instantiation:
import os
key = os.environ['PERPLEXITY_API_KEY']
client = PerplexityClient(key = key)
print(client.query('Brief answer: greet the world in Swedish.'))
Usage
See the PerplexiPy-tutorial.ipynb notebook for a richer example of the API's capabilities.
In general:
client = PerplexityClient()
result = client.query('Show me how to declare a list in Python')
# result is a string
results = client.queryBatch('Show me different ways of declaring a Python list')
for result in results:
print(result)
# results is a tuple of one or more results
results = client.queryStreamable('Tell me why lists are important in programming')
for result in results:
print(result)
# results is a long stream of data, served over time.
models = client.models # lists all models supported by Perplexity AI
print('Model names:')
for model in models.keys():
print(' - %s' % model)
try:
client.model = 'bogus-LLM-7b'
except PerplexityClientError as e:
print(e)
client.model = models.keys()[0] # OK
Async usage
Not supported at this time.
Interactive usage
PerplexiPy ships with the Codex Playground, an interactive REPL console. To run it:
# In a virtualenv:
pip install -U perplexipy
export PERPLEXITY_API_KEY="your-key-goes-here"
codex repl
Full description of Codex Playground and other use cases like streaming API and CLI argument passing in the Codex README.
License
The PerplexiPy package, documentation and examples are licensed under the BSD-3 open source license.
See also
- codex - a PerplexiPy command line code explainer
- API documentation: https://cime-software.github.io/perplexipy
- Perplexity AI
- m0toko: a Sopel bot plug-in that uses PerplexiPy for providing AI-enhanced chatbot functionality: https://github.com/pr3d4t0r/m0toko
- PyPI: openai
Caveats
The code should work with Python 3.7 or later, but it was only tested with Python 3.9.16 and later. Download the package and install it from source if support for an earlier Python version is required.
Bugs
Feature requests and bug reports:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for PerplexiPy-1.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8957776630dec4b6ab8cc21b31a2bac6fdc26b15ad4bffc7f1b6d6507229a003 |
|
MD5 | 78d7c1de79bee6860f06da42cf0a3bd8 |
|
BLAKE2b-256 | 29c2d8fb5aafad16bbae5f754356e5e5c37020800616ffc79b2a52d77797132e |