A Python client for the Openperplex API
Project description
Openperplex Python Library Documentation
The Openperplex Python library provides an interface to interact with the Openperplex API, allowing you to perform various search and web-related operations.
Installation
To install the Openperplex library, use pip:
pip install --upgrade openperplex
Initialization
To use the Openperplex library, you need to initialize it with your API key:
from openperplex import Openperplex
api_key = "your_openperplex_api_key_here"
client = Openperplex(api_key)
Available Methods
1. search
Perform a non-streaming search query.
result = client.search(
query="What are the latest developments in AI?",
date_context="2024-08-25", # if empty, the current date of the api server is used
location="us", # default is "us"
pro_mode=False, # default is False
response_language="en" # default is "auto"
)
print(result["llm_response"])
print(result["images"])
print("Sources:", result["sources"])
print("Relevant Questions:", result["relevant_questions"])
2. search_simple
Perform a simplified non-streaming search query.
answer = client.search_simple(
query="Who won the FIFA World Cup in 2022?",
location="fr",
date_context="2024-08-25 7:00 AM",
pro_mode=False,
response_language="fr",
answer_type="text"
)
print(answer["llm_response"])
3. search_stream
Perform a streaming search query.
for chunk in client.search_stream(
query="Explain quantum computing",
date_context="2024-08-25 7:00 AM",
location="de",
pro_mode=False,
response_language="de",
answer_type="markdown"
):
if chunk["type"] == "llm":
print(chunk["text"], end="", flush=True)
elif chunk["type"] == "sources":
print("Sources:", chunk["data"])
elif chunk["type"] == "relevant_questions":
print("Relevant Questions:", chunk["data"])
Example with pro_mode enabled:
for chunk in client.search_stream(
query="Explain quantum computing",
date_context="2024-08-25 7:00 AM",
location="us",
pro_mode=True,
response_language="auto",
answer_type="html"
):
if chunk["type"] == "llm":
print(chunk["text"], end="", flush=True)
4. get_website_text
Retrieve the text content of a website.
result = client.get_website_text("https://www.example.com")
print(result)
5. get_website_markdown
Get the markdown representation of a website.
result = client.get_website_markdown("https://www.example.com")
print(result)
6. get_website_screenshot
Get a screenshot of a website.
result = client.get_website_screenshot("https://www.example.com")
print(f"Screenshot available at: {result['url']}")
7. query_from_url
Perform a query based on the content of 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",
answer_type="text" # default is "text" if not specified
)
print(response)
Parameters
query
The search query or question.
date_context
Optional date for context (format: "YYYY-MM-DD" or "YYYY-MM-DD HH:MM AM/PM"). If empty, the current date of the API server is used.
location
Country code for search context. Default is "us". See the list of supported locations below.
pro_mode
Boolean to enable or disable pro mode. Default is False.
response_language
Language code for the response. Default is "auto" (auto-detect). See the list of supported languages below.
answer_type
Type of answer format. Options are "text" (default), "markdown", or "html".
Supported Locations
The location
parameter accepts the following country codes:
๐บ๐ธ us (United States), ๐จ๐ฆ ca (Canada), ๐ฌ๐ง uk (United Kingdom), ๐ฒ๐ฝ mx (Mexico), ๐ช๐ธ es (Spain), ๐ฉ๐ช de (Germany), ๐ซ๐ท fr (France), ๐ต๐น pt (Portugal), ๐ณ๐ฑ nl (Netherlands), ๐น๐ท tr (Turkey), ๐ฎ๐น it (Italy), ๐ต๐ฑ pl (Poland), ๐ท๐บ ru (Russia), ๐ฟ๐ฆ za (South Africa), ๐ฆ๐ช ae (United Arab Emirates), ๐ธ๐ฆ sa (Saudi Arabia), ๐ฆ๐ท ar (Argentina), ๐ง๐ท br (Brazil), ๐ฆ๐บ au (Australia), ๐จ๐ณ cn (China), ๐ฐ๐ท kr (Korea), ๐ฏ๐ต jp (Japan), ๐ฎ๐ณ in (India), ๐ต๐ธ ps (Palestine), ๐ฐ๐ผ kw (Kuwait), ๐ด๐ฒ om (Oman), ๐ถ๐ฆ qa (Qatar), ๐ฎ๐ฑ il (Israel), ๐ฒ๐ฆ ma (Morocco), ๐ช๐ฌ eg (Egypt), ๐ฎ๐ท ir (Iran), ๐ฑ๐พ ly (Libya), ๐พ๐ช ye (Yemen), ๐ฎ๐ฉ id (Indonesia), ๐ต๐ฐ pk (Pakistan), ๐ง๐ฉ bd (Bangladesh), ๐ฒ๐พ my (Malaysia), ๐ต๐ญ ph (Philippines), ๐น๐ญ th (Thailand), ๐ป๐ณ vn (Vietnam)
Supported Languages
The response_language
parameter accepts the following language codes:
auto
: Auto-detect the user question language (default)en
: Englishfr
: Frenches
: Spanishde
: Germanit
: Italianpt
: Portuguesenl
: Dutchja
: Japaneseko
: Koreanzh
: Chinesear
: Arabicru
: Russiantr
: Turkishhi
: Hindi
Response Structure
search and search_simple methods
llm_response
: The main response from the language model.images
: (if available) A list of relevant images.sources
: A list of sources used to generate the response.relevant_questions
: A list of related questions that might be of interest.
search_stream method
The streaming response is divided into chunks, each with a type
field:
llm
: Contains thetext
field with a part of the language model's response.sources
: Contains thedata
field with a list of sources.relevant_questions
: Contains thedata
field with a list of related questions.
get_website_screenshot method
url
: The URL where the screenshot can be accessed.
Error Handling
The library raises OpenperplexError
exceptions for API errors. Always wrap your API calls in try-except blocks:
from openperplex import Openperplex, OpenperplexError
try:
result = client.search("AI advancements")
print(result["llm_response"])
except OpenperplexError as e:
print(f"An error occurred: {e}")
Remember to handle potential network errors and other exceptions as needed in your application.
Best Practices
-
API Key Security: Never hard-code your API key in your source code. Use environment variables or secure configuration management.
-
Error Handling: Always implement proper error handling to manage API errors and network issues gracefully.
-
Rate Limiting: Be aware of any rate limits imposed by the Openperplex API and implement appropriate backoff strategies if necessary.
-
Streaming Responses: When using
search_stream
, remember to handle the streaming nature of the response appropriately in your application. -
Pro Mode: Use
pro_mode=True
when you need advanced search features, but be aware that it might be slower. -
Date Context: When historical context is important for your query, always specify the
date_context
parameter. -
Localization: Use the
location
andresponse_language
parameters to get more relevant and localized results.
Conclusion
The Openperplex Python library provides a powerful interface to access advanced search and web analysis capabilities. By leveraging its various methods and parameters, you can create sophisticated applications that can understand and process web content in multiple languages and contexts.
For any issues, feature requests, or further questions, please refer to the official Openperplex documentation or contact their support team.
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.5.tar.gz
.
File metadata
- Download URL: openperplex-0.1.5.tar.gz
- Upload date:
- Size: 9.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 | 0ae5402e9555ebbf18331efeefd399f1f38eebca105f20958cb1a3d99b5006bd |
|
MD5 | 0cd67dbfcf97073555cab2f228f9e6d3 |
|
BLAKE2b-256 | a9dfa7c1c28254526d17c53f58f1b6efebe6ae1727c756026bd16b34e1ae8f87 |
File details
Details for the file openperplex-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: openperplex-0.1.5-py3-none-any.whl
- Upload date:
- Size: 6.2 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 | 3465f371cc7acba1b1efa9e86259c0f475ea592964a8322416355ee0cefd7aab |
|
MD5 | d31de85d7a3550e29a329e482ba31311 |
|
BLAKE2b-256 | 03e8aaa96b0eb4c7e08b1353e6498c8fff079295a388cb8993dc076f51dbeb7b |