Skip to main content

A library for interacting with various AI models

Project description

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

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

modular-ai-0.1.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

modular_ai-0.1.2-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file modular-ai-0.1.2.tar.gz.

File metadata

  • Download URL: modular-ai-0.1.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.12

File hashes

Hashes for modular-ai-0.1.2.tar.gz
Algorithm Hash digest
SHA256 3693a42ba38dc050fe709298951202ba5806b32fa72ed552e48c938aa3b6a0c2
MD5 82fb154bea126b2bae2a7b601da478d2
BLAKE2b-256 28cbe8fcdc63fa0dfc388af00e5b6be63f354eaebf8a13df8a81258bbe1bc286

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modular_ai-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.12

File hashes

Hashes for modular_ai-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a1a70f465cb928ed55e93120409d358a4ba73eea2e604078e99f6993078f2d60
MD5 6c59e0e01531f7167721b01d66dbdc01
BLAKE2b-256 8d57bbb9e897776e85507adf6f83ee5284be28c29d698d5f70885375e45be73c

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