Skip to main content

A library for interacting with various AI models

Project description

AI Model 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 ai_model_library

Setup

1. Environment Variables

This library requires a .env file for storing API keys and URLs for the different AI providers. 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", "text-davinci-003")

# Generate text
response = model.generate_text("Hello, world!", max_tokens=5)
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", "text-davinci-003")
response = model.generate_text("Hello, world!", max_tokens=5)
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

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

modular_ai-0.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modular-ai-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 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.0.tar.gz
Algorithm Hash digest
SHA256 dd9fa28a80e9c94e4e190de1e31e6e0f6ee5f68b3c514e2b084de703c0a646fe
MD5 2758a5d547bfc3c867d9426604de9d67
BLAKE2b-256 2cf442fe55d1a145d37d53dd0e1584ba77014af21dccfd4dfda7c11d7fcf28d2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modular_ai-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7aea7e18e1682f30a281c7fdc1a243e3088b9778ddd19258e1dc20bdc34b551b
MD5 45cdca3ca8bee37c2d18ea609f89101b
BLAKE2b-256 5f5f83b5538073b9cda897a201d6dbf351fbe6fa37d929a0a009f8a5f67a5563

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