Skip to main content

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: English
  • fr: French
  • es: Spanish
  • de: German
  • it: Italian
  • pt: Portuguese
  • nl: Dutch
  • ja: Japanese
  • ko: Korean
  • zh: Chinese
  • ar: Arabic
  • ru: Russian
  • tr: Turkish
  • hi: 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 the text field with a part of the language model's response.
  • sources: Contains the data field with a list of sources.
  • relevant_questions: Contains the data 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

  1. API Key Security: Never hard-code your API key in your source code. Use environment variables or secure configuration management.

  2. Error Handling: Always implement proper error handling to manage API errors and network issues gracefully.

  3. Rate Limiting: Be aware of any rate limits imposed by the Openperplex API and implement appropriate backoff strategies if necessary.

  4. Streaming Responses: When using search_stream, remember to handle the streaming nature of the response appropriately in your application.

  5. Pro Mode: Use pro_mode=True when you need advanced search features, but be aware that it might be slower.

  6. Date Context: When historical context is important for your query, always specify the date_context parameter.

  7. Localization: Use the location and response_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

openperplex-0.1.4.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

openperplex-0.1.4-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file openperplex-0.1.4.tar.gz.

File metadata

  • Download URL: openperplex-0.1.4.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for openperplex-0.1.4.tar.gz
Algorithm Hash digest
SHA256 877468566d3e9a8d2138787dfc8a17ec14b95e53becd753676f01bb5dded0c4f
MD5 558ddbeceb3b874cc0fcec08395daf9b
BLAKE2b-256 dfa051420b312c6348204e8f4597f110f5c102c957c98e9d30a17f9ec6ecb9c3

See more details on using hashes here.

File details

Details for the file openperplex-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: openperplex-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for openperplex-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8a905c01b1f4cee114daac73b4817726b15eb8b4edf6170e55698c6c0013938d
MD5 f663159d57ddd28b513f01f7025066c8
BLAKE2b-256 e9529ccefde9c02d56129dfaa894a1dadfd5e14cf5b313183524163610bbe106

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page