A Python client for the Openperplex API
Project description
Openperplex
Openperplex is a powerful Python library that provides an interface to interact with an AI-powered search and question-answering system. It offers both streaming and non-streaming search capabilities, making it versatile for various use cases.
Features
- Supports multiple AI providers (OpenAI and Groq)
- Streaming and non-streaming search options
- Simple and advanced search interfaces
- Error handling with custom exceptions
- Date context and location-based search
- Professional mode for advanced queries
Installation
To install Openperplex, use pip:
pip install openperplex
Usage
Initialization
To use Openperplex, you need to initialize it with your API key and provider information:
from openperplex import Openperplex
client = Openperplex(
api_key="your_api_key",
provider="openai", # or "groq"
provider_key="your_provider_key"
)
Streaming Search
Openperplex offers two streaming search methods:
search_stream
: Advanced streaming search with additional parameters.search_simple_stream
: Simple streaming search with minimal parameters.
Advanced Streaming Search
query = "What are the latest developments in AI?"
for result in client.search_stream(query=query, date_context="2024-08-11", location="us", pro_mode=True):
if result["type"] == "llm":
print("AI response:", result["text"])
elif result["type"] == "sources":
print("Sources:", result["data"])
elif result["type"] == "relevant":
print("Relevant questions :", result["data"])
elif result["type"] == "finished":
print("Search completed")
Simple Streaming Search
query = "Tell me about quantum computing"
for result in client.search_simple_stream(query):
if result["type"] == "llm":
print("AI response:", result["text"])
# Handle other result types as needed
Non-Streaming Search
Openperplex also provides non-streaming search methods:
search
: Advanced search with additional parameters.search_simple
: Simple search with minimal parameters.
Advanced Search
query = "What are the economic impacts of climate change?"
results = client.search(query, date_context="2024-08-11", location="us", pro_mode=True)
print(results)
Simple Search
query = "Explain the theory of relativity"
result = client.search_simple(query)
print(result)
Error Handling
Openperplex uses custom exceptions to handle errors. You can catch these exceptions to manage error scenarios:
from openperplex import Openperplex, OpenperplexError
try:
client = Openperplex(api_key="invalid_key", provider="openai", provider_key="invalid_key")
result = client.search_simple("Test query")
except OpenperplexError as e:
print(f"An error occurred: {e.status_code} - {e.detail}")
Advanced Usage
Date Context
You can provide a date context to get results relevant to a specific time:
results = client.search("Current global events", date_context="2024-08-11")
Location-Based Search
Specify a location to get region-specific results:
results = client.search("Local news", location="uk")
Professional Mode
Enable professional mode for more detailed and technical responses:
results = client.search("Advanced AI algorithms", pro_mode=True)
Closing the Client
The client automatically closes the underlying HTTP client when the Openperplex instance is deleted. However, you can explicitly close it if needed:
client.__del__()
Conclusion
Openperplex provides a flexible and powerful interface for AI-powered search and question-answering. Whether you need real-time streaming results or simple query responses, Openperplex has you covered. Experiment with different search methods and parameters to find the best fit for your use case.
For more information and updates, please refer to the official documentation or contact the library maintainers.
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.1.tar.gz
.
File metadata
- Download URL: openperplex-0.1.1.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bcde69f367d2385fa9d67719e8c0bc0b7e3ab56d4378b9dcf2eb077e2124673 |
|
MD5 | d8c53b9706301f3090452259b413fb5c |
|
BLAKE2b-256 | 1654148f40f944f934688a805dca1f9a5114b817c28b210d8e4e9440dbb4b9bc |
File details
Details for the file openperplex-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: openperplex-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.5 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 | 51a0fc4ba40c74efe06284661870baab7ca6519de92c8716d9c4547bdaa89f8e |
|
MD5 | d9720094c11a44c2127578f638387f37 |
|
BLAKE2b-256 | 806167e7fb3a2201a0883db4ff0a6f3d1dc7c443195ae0666fdaa4ea8e29c057 |