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:
git clone https://github.com/harveygw/ailib
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file modular-ai-0.1.1.tar.gz
.
File metadata
- Download URL: modular-ai-0.1.1.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | a074331af2137ab746085d285fbcb82e332e34a0b055c8964fd75ca47fb99cef |
|
MD5 | 03e9a213b734be3178c867817f5d6500 |
|
BLAKE2b-256 | 6b92a5f88bb7b6c6a415af3da877cd6db26c644c8aa016f804df2cbe193d3923 |
File details
Details for the file modular_ai-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: modular_ai-0.1.1-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | bef93d85467104563c02a5f032bb5be98aec935d9b9bb497647f195efaa39fe5 |
|
MD5 | 819a775d6ee2eadf891fc624c70dda40 |
|
BLAKE2b-256 | 4c2bf29ecf281406e5ed8e217386fdfbfb3febbb557aae77c192c6d9074af11e |