A Python client for the Openperplex API
Project description
Openperplex
Openperplex is a powerful Python library for interacting with the Openperplex API, providing easy access to advanced search and content retrieval functionalities. This library is in Alpha stage and is subject to change.
Table of Contents
Installation
Install Openperplex using pip:
pip install --upgrade openperplex
Getting Started
To use Openperplex, you'll need an API key. If you don't have one, create an account on the Openperplex API website and get your API key from your account. The Api is free to use for 500 requests per month.
Usage
Initialization
First, import the Openperplex class and create an instance:
from openperplex import Openperplex
api_key = "your_api_key_here"
client = Openperplex(api_key)
Search
Perform a full search with sources, citations, and relevant questions:
result = client.search(
query="What are the latest developments in AI?",
date_context="2023",
location="us",
pro_mode=True,
response_language="en"
)
print(result["llm_response"])
print("Sources:", result["sources"])
print("Relevant Questions:", result["relevant_questions"])
Simple Search
Perform a simple search that returns only the answer:
answer = client.search_simple(
query="Who won the FIFA World Cup in 2022?",
location="fr",
date_context="2023",
pro_mode=False,
response_language="fr"
)
print(answer)
Streaming Search
Get search results in a stream, useful for displaying real-time updates:
for chunk in client.search_stream(
query="Explain quantum computing",
date_context="2023",
location="de",
pro_mode=True,
response_language="de"
):
if chunk["type"] == "llm":
print(chunk["text"], end="", flush=True)
elif chunk["type"] == "sources":
print("\nSources:", chunk["data"])
elif chunk["type"] == "relevant_questions":
print("\nRelevant Questions:", chunk["data"])
For a simpler streaming search:
for chunk in client.search_simple_stream(
query="What are the benefits of meditation?",
location="es",
date_context="2023",
pro_mode=False,
response_language="es"
):
if chunk["type"] == "llm":
print(chunk["text"], end="", flush=True)
Website Content Retrieval
Retrieve text content from a website:
text_content = client.get_website_text("https://www.example.com")
print(text_content)
Get a screenshot of a website:
screenshot_url = client.get_website_screenshot("https://www.example.com")
print(f"Screenshot available at: {screenshot_url}")
Retrieve markdown content from a website:
markdown_content = client.get_website_markdown("https://www.example.com")
print(markdown_content)
URL-based Querying
Query content from a specific URL:
response = client.query_from_url(
url="https://www.example.com/article",
query="What is the main topic of this article?",
response_language="it"
)
print(response)
Parameter Specifications
Response Language
The response_language
parameter allows you to specify the language of the API's response. Available options are:
"en"
: English"es"
: Spanish"it"
: Italian"fr"
: French"de"
: German (Allemand)"auto"
: Automatically detect the language of the user's question
Example:
result = client.search("Qual è la capitale dell'Italia?", response_language="it")
Location
The location
parameter helps provide more relevant results based on the country where the search is performed. Some available options include:
"us"
: United States"fr"
: France"es"
: Spain"mx"
: Mexico"ma"
: Morocco"ca"
: Canada
Example:
result = client.search("Meilleurs restaurants près de chez moi", location="fr", response_language="fr")
Using the appropriate location
parameter can significantly improve the relevance of search results for location-specific queries.
Error Handling
Openperplex uses custom exceptions for error handling. Always wrap your API calls in try-except blocks:
from openperplex import Openperplex, OpenperplexError
client = Openperplex("your_api_key_here")
try:
result = client.search("What is the meaning of life?")
print(result)
except OpenperplexError as e:
print(f"An error occurred: {e.status_code} - {e.detail}")
except Exception as e:
print(f"An unexpected error occurred: {str(e)}")
Contributing
We welcome contributions! Please contact me on Twitter
License
Openperplex is released under the MIT 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.3.tar.gz
.
File metadata
- Download URL: openperplex-0.1.3.tar.gz
- Upload date:
- Size: 7.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 | 99a7c4fa3b2735c95174f2322d30afb09e54fef5dc2fe002b1caae84af47e5bd |
|
MD5 | c0505062ecf0742c504ebe6ef82596e8 |
|
BLAKE2b-256 | 4f0a4a78aeab85b3130842267f90a9664d0ccad8a431a2ecd0ceb0d6365d2ed1 |
File details
Details for the file openperplex-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: openperplex-0.1.3-py3-none-any.whl
- Upload date:
- Size: 5.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 | 6a729581a986154c57e8070e99d2d8ab95e685f0ee0a69001c841b9deb4f9d2b |
|
MD5 | a266916e60ed017cf410035af744db40 |
|
BLAKE2b-256 | a26527d3093d97adad7db751a2653c6aa608e393a1611a6c6e13b15bc0698db0 |