A Python client for the Openperplex API
Project description
Openperplex
Openperplex is a Python library that provides a simple interface to interact with the Openperplex API. It allows users to perform searches and retrieve information using various endpoints, with support for both streaming and non-streaming responses.
Installation
You can install Openperplex using pip:
pip install openperplex
Requirements
- Python 3.6+
- httpx
- typing
Usage
Initialization
To use the Openperplex library, you need to initialize the Openperplex
class with your API key, provider, and provider key:
from openperplex import Openperplex
client = Openperplex(
api_key="your_api_key",
provider="openai", # or "groq"
provider_key="your_provider_key"
)
Methods
1. Search Stream
The search_stream
method allows you to perform a search and receive results as a stream of events.
for event in client.search_stream(
query="Your search query",
date_context="optional_date_context",
location="us",
pro_mode=False
):
print(event)
2. Search Simple Stream
The search_simple_stream
method provides a simplified streaming search.
for event in client.search_simple_stream("Your search query"):
print(event)
3. Search
The search
method performs a search and returns the complete results as a list.
results = client.search(
query="Your search query",
date_context="optional_date_context",
location="us", // can be any country code
pro_mode=False
)
print(results)
4. Search Simple
The search_simple
method provides a simplified search that returns results as a string.
result = client.search_simple("Your search query")
print(result)
Error Handling
The library uses custom OpenperplexError
exceptions to handle errors. You can catch these exceptions to handle errors in your code:
from openperplex import Openperplex, OpenperplexError
client = Openperplex(api_key="your_api_key", provider="openai", provider_key="your_provider_key")
try:
results = client.search("Your query")
except OpenperplexError as e:
print(f"An error occurred: {e.status_code} - {e.detail}")
Streaming Responses
For methods that return a stream (search_stream
and search_simple_stream
), the library yields dictionaries with different types of events:
{"type": "llm", "text": "..."}
: LLM-generated text{"type": "sources", ...}
: Source information{"type": "relevant", ...}
: Relevant questions{"type": "finished", ...}
: Indicates the end of the stream
You can process these events as needed in your application.
Configuration Options
api_key
(required): Your Openperplex API keyprovider
(required): The provider to use ("openai" or "groq")provider_key
(required): Your provider API keybase_url
(optional): The base URL for the Openperplex API (defaults to the provided URL)
Notes
- The library automatically closes the HTTP client when the
Openperplex
instance is deleted. - Make sure to handle rate limiting and other API-specific requirements as per the Openperplex API documentation.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
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
Built Distribution
File details
Details for the file openperplex-0.1.0.tar.gz
.
File metadata
- Download URL: openperplex-0.1.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dddf1eca2c1449f860ccc60cbf2b70aa76a716b5b5a0d69c19b7e2d85eb7d81a |
|
MD5 | 36286fe288875b8c1b588e1c9fa049d4 |
|
BLAKE2b-256 | 83a1c6a56aeb4fa9339e9a23b26208e68f13c0aed4168943849dd90db69b75b0 |
File details
Details for the file openperplex-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: openperplex-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8baca155cd062a39988ff95ae9166ac62e8eec804bdb96946cc943085b2ad10 |
|
MD5 | d974551006185661e787266b855debb9 |
|
BLAKE2b-256 | d12dde39ef3cfe41e48878316d5dc7df163d4284e969a21b53b0b6907ba739d7 |