Skip to main content

ThinkHub

PyPI Downloads

ThinkHub Logo

ThinkHub is a lightweight Python package designed for small and simple projects or for quickly testing LLM services. It provides a unified interface for interacting with multiple AI providers, making it easy to configure and switch between different services. Built with extensibility in mind, users can effortlessly integrate new providers by creating and registering their own plugins or classes. ThinkHub prioritizes simplicity, flexibility, and user-friendly customization, making it an ideal tool for rapid prototyping and experimentation with AI models.

Key Features

  • Lazy Loading: Only loads the dependencies required for the selected service, reducing memory and installation overhead.
  • Multi-Service Integration: Interact seamlessly with multiple AI services (e.g., chat, transcription, image processing).
  • Plugin System: Register and use custom classes to extend functionality.
  • Dynamic Configuration: Load and manage configurations with environment variable overrides.
  • Error Handling: Robust exception system for identifying and managing provider-related issues.
  • Poetry and pip Support: Flexible dependency and environment management.
  • Python 3.11+: Leverages the latest features of Python for performance and simplicity.

Supported Services

Audio Transcriptions

  • OpenAI: Using the whisper-1 model.
  • Google Speech-to-Text
Provider Completion Streaming Async Completion Async Streaming Async Embedding Async Image Generation Image Input
OpenAI ❌ ❌ ❌ ✅ ❌ ❌ ✅
Google Gemini ❌ ❌ ❌ ✅ ❌ ❌ ✅
Anthropic - Claude.ai ❌ ❌ ❌ ✅ ❌ ❌ ✅

Installation

ThinkHub uses a lazy-loading strategy to optimize memory usage and avoid installing unused dependencies. You can install ThinkHub using either Poetry or pip, as shown below:

1. Install the Base Library

  • Poetry:
    poetry add thinkhub
    
  • pip:
    pip install thinkhub
    

2. Install with Specific Extras

Install only the required dependencies based on the service(s) you plan to use:

  • OpenAI Chat:

    • Poetry:
      poetry add thinkhub --extras openai
      
    • pip:
      pip install thinkhub[openai]
      
  • Google Transcription:

    • Poetry:
      poetry add thinkhub --extras google
      
    • pip:
      pip install thinkhub[google]
      
  • Anthropic Chat:

    • Poetry:
      poetry add thinkhub --extras anthropic
      
    • pip:
      pip install thinkhub[anthropic]
      
  • Gemini Chat:

    • Poetry:
      poetry add thinkhub --extras google-generativeai
      
    • pip:
      pip install thinkhub[google-generativeai]
      
  • Multiple Services (e.g., OpenAI and Anthropic):

    • Poetry:
      poetry add thinkhub --extras openai --extras anthropic
      
    • pip:
      pip install thinkhub[openai,anthropic]
      

3. Install All Services

If you want to install all available services:

  • Poetry:
    poetry add thinkhub --extras all
    
  • pip:
    pip install thinkhub[all]
    

4. Activate the Virtual Environment

  • Poetry:
    poetry shell
    

Usage

Lazy Loading

ThinkHub uses lazy loading to dynamically import the dependencies required for a specific provider. This means that:

  1. Dependencies are only loaded when needed.
  2. Missing dependencies are flagged with clear error messages.

Example: If you attempt to use OpenAI services without the openai extra installed, ThinkHub will raise an error like this:

ImportError: Missing dependencies for provider 'openai': tiktoken. 
Install them using 'poetry install --extras openai' or 'pip install thinkhub[openai]'.

Chat Services

To use a chat service like OpenAI:

from thinkhub.chat import get_chat_service

chat_service = get_chat_service("openai", model="gpt-4o")
async for response in chat_service.stream_chat_response("Hello, ThinkHub!"):
    print(response)

Transcription Services

To use a transcription service like Google:

from thinkhub.transcription import get_transcription_service

transcription_service = get_transcription_service("google")
result = await transcription_service.transcribe("path/to/audio.flac")
print(result)

Image Processing with OpenAI

ThinkHub supports image processing with OpenAI. Here’s an example of how to process multiple images asynchronously:

import asyncio
from thinkhub.chat import get_chat_service

async def process_image_with_openai(image_payloads):
    chat_service = get_chat_service("openai", model="gpt-4")
    async for response in chat_service.stream_chat_response(
        input_data=image_payloads, 
        system_prompt="Analyze these images."
    ):
        print(response)

# Prepare image payloads
image_payloads = [{"image_path": "path/to/image1.jpg"}, {"image_path": "path/to/image2.jpg"}]

# Process images with OpenAI
asyncio.run(process_image_with_openai(image_payloads))

Error Handling

ThinkHub includes robust error handling to simplify debugging and configuration:

  • Dependency Validation: ThinkHub will check for required dependencies dynamically and provide clear installation instructions.

  • Custom Exceptions:

    • ProviderNotFoundError: Raised when a requested provider is not found.
    • ImportError: Raised when dependencies for a provider are missing.

Example:

from thinkhub.exceptions import ProviderNotFoundError

try:
    service = get_chat_service("unsupported_provider")
except ProviderNotFoundError as e:
    print(e)

Development

  1. Run Tests:

    poetry run pytest
    
  2. Code Linting:

    poetry run pre-commit run -a
    
  3. Build the Project:

    poetry build
    

License

This project is licensed under the MIT License..


Acknowledgments

Special thanks to the open-source community for providing the tools and libraries that made this project possible.

Metadata

Release files for thinkhub 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for thinkhub 0.4.2
File Size Uploaded
thinkhub-0.4.2.tar.gz 18.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for thinkhub 0.4.2
File Interpreter ABI Platform
thinkhub-0.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 43.5 kB

Release files / thinkhub-0.4.2.tar.gz

Download URL thinkhub-0.4.2.tar.gz
Size 18.9 kB
Tags Source
SHA-256 checksum
How to use checksums
901bde2c7a3445f0c183a3b93c7aa2ed3c2af67c3ca56c253184269ddd341d39
BLAKE2b-256 checksum
How to use checksums
92966349e97796d43b33fba1b68f3799cf9721e5ef2d5f9f2ddefe9d3c5ccd86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-1021-azure

Release files / thinkhub-0.4.2-py3-none-any.whl

Download URL thinkhub-0.4.2-py3-none-any.whl
Size 24.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f8187c48e3c186ee81a8aaf743f04e4ac96357c3da2760c85ec3a75e2ecb479
BLAKE2b-256 checksum
How to use checksums
c692dc9810b195dce0f1432c67c691a4b67d7834b321a4f558c1b219b91dd398
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-1021-azure

Release history Release notifications | RSS feed

This release

0.4.2 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.9

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.4

2 release files

0.2.2

2 release files

0.1.92

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page