Skip to main content

Modular AI Library

A Python library that allows you to import and use various AI models easily. This library supports multiple AI model providers, making it straightforward to switch between different models without writing initialisation code each time.

Features

  • Easy Integration: Quickly integrate with multiple AI providers.
  • Flexibility: Easily switch between different AI models.
  • Convenience: Avoid the hassle of writing repetitive initialisation code.

Installation

First, install the library using pip:

pip install modular-ai

Setup

Environment Variables

This library requires a .env file for storing API keys and URLs for the different AI providers (You don't need to have all of them). Create a .env file in the root of your project and add the respective API keys and URLs like so:

OPENAI_API_KEY=your_openai_api_key
OPENAI_API_URL=openai_api_url

GEMINI_API_KEY=your_gemini_api_key
GEMINI_API_URL=gemini_api_url

ANTHROPIC_API_KEY=your_anthropic_api_key
ANTHROPIC_API_URL=anthropic_api_url

COHERE_API_KEY=your_cohere_api_key
COHERE_API_URL=cohere_api_url

GOOGLE_API_KEY=your_google_api_key
GOOGLE_API_URL=google_api_url

Make sure to replace the placeholder values with your actual API keys and URLs.

Usage

Importing the Library

Start by importing the necessary classes from the library:

from ailib.models import Models

Fetching and Registering Models

Fetch and register models from each provider. This step initialises the models and makes them available for use.

# Fetch and register models from each provider
Models.fetch_and_register_models("openai", "openai")
Models.fetch_and_register_models("gemini", "gemini")
Models.fetch_and_register_models("anthropic", "anthropic")
Models.fetch_and_register_models("cohere", "cohere")
Models.fetch_and_register_models("google", "google")

Generating Text Using a Registered Model

Once the models are registered, you can use them to generate text. Here's an example using OpenAI's GPT-3 model:

# Get the OpenAI model
model = Models.get_model("openai", "gpt-4o")

# Generating text without token limit
response = model.generate_text("Hello, world!")
print(response)

# Generate text with token limit
response = model.generate_text("Hello, world!", max_tokens=128000)
print(response)

Listing Available Models

You can list all registered models for each provider:

# List all registered models
all_models = Models.list_models()
print(all_models)

Full Example

Here's a complete example demonstrating how to set up and use the library:

from ailib.models import Models

# Fetch and register models from each provider
Models.fetch_and_register_models("openai", "openai")
Models.fetch_and_register_models("gemini", "gemini")
Models.fetch_and_register_models("anthropic", "anthropic")
Models.fetch_and_register_models("cohere", "cohere")
Models.fetch_and_register_models("google", "google")

# Generate text using a registered model
model = Models.get_model("openai", "gpt-4o")
response = model.generate_text("Hello, world!")
print(response)

# List all registered models
all_models = Models.list_models()
print(all_models)

Running Tests

To ensure everything is working correctly, you can run the provided test suite. This will verify the initialisation, model listing, and text generation for different APIs. Make sure you have your .env file properly set up with actual API keys to run these tests.

python -m unittest discover -s tests

Metadata

Release files for modular-ai 0.1.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 modular-ai 0.1.2
File Size Uploaded Provenance Actions
modular-ai-0.1.2.tar.gz 4.5 kB Without provenance Details for modular-ai-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for modular-ai 0.1.2
File Interpreter ABI Platform Actions
modular_ai-0.1.2-py3-none-any.whl Python 3 none any Details for modular_ai-0.1.2-py3-none-any.whl

Total release size: 10.0 kB

Release files / modular-ai-0.1.2.tar.gz

Download URL modular-ai-0.1.2.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3693a42ba38dc050fe709298951202ba5806b32fa72ed552e48c938aa3b6a0c2
BLAKE2b-256 checksum
How to use checksums
28cbe8fcdc63fa0dfc388af00e5b6be63f354eaebf8a13df8a81258bbe1bc286
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Release files / modular_ai-0.1.2-py3-none-any.whl

Download URL modular_ai-0.1.2-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a1a70f465cb928ed55e93120409d358a4ba73eea2e604078e99f6993078f2d60
BLAKE2b-256 checksum
How to use checksums
8d57bbb9e897776e85507adf6f83ee5284be28c29d698d5f70885375e45be73c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Security for modular-ai 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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