Skip to main content

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:

  1. search_stream: Advanced streaming search with additional parameters.
  2. 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:

  1. search: Advanced search with additional parameters.
  2. 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

openperplex-0.1.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

openperplex-0.1.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: openperplex-0.1.2.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

Hashes for openperplex-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5f60c4f796d4de1715431208e17045d5d31dff77c0cb145d01ae34f3d07e792f
MD5 e938241dbc492c546250eef30be4ab3c
BLAKE2b-256 e32aeaacecec5cbaf429c45171415f4211f0a978311fdeb53fccea995f4b0a20

See more details on using hashes here.

File details

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

File metadata

  • Download URL: openperplex-0.1.2-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

Hashes for openperplex-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b8245a6761bbe7ed7625a2de6a62b006adfb69f115a39ee94134395963329b10
MD5 65bc0bbe8ef98a65422670b4c52d699d
BLAKE2b-256 04468d570e03b8f42ac672a70317557d6092e1c636845f5e6085ca931f751430

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