How to Install and Use the AISuite Library by Andrew Ng
Simplify Complex Processes and Accelerate Results with AISuite
In today's world of Artificial Intelligence (AI), efficiency in developing models and pipelines is crucial to ensure projects are completed quickly and with maximum accuracy.
AISuite is a Python library developed by Andrew Ng and collaborators, designed to streamline the process of building AI applications by simplifying the complexities of working with various large language models (LLMs).
With AISuite, integrating multiple AI providers becomes simple and efficient, removing the complexity of configuring individual APIs for each service.
A single string change is all it takes to switch between OpenAI, Anthropic, Azure, Google, HuggingFace, Ollama, and many others.
In this article, we’ll explore how to install and use AISuite, highlighting its key features and functionalities.
What is AISuite?
AISuite is an open-source Python library (available on GitHub) developed by Andrew Ng and his team. It simplifies the integration of multiple AI providers in a seamless and efficient way.
The library offers a user-friendly interface that allows easy switching between various LLMs, following the OpenAI interface format. This ensures consistent interactions across different providers.
With AISuite, you can effortlessly compare and evaluate models from providers like OpenAI, Anthropic, and Meta’s Llama across tasks such as text generation, analysis, and building interactive systems.
Installation
First, install the library using pip:
pip install aisuite[all]
This command will install the necessary packages for all supported LLM providers.
Next, set up your provider keys for authentication. On Linux, you can add them via the terminal:
export OPENAI_API_KEY='Your OpenAI Key'
export ANTHROPIC_API_KEY='Your Anthropic Key'
On Windows, add the keys within your Python application:
import os
os.environ['OPENAI_API_KEY'] = 'Your OpenAI Key'
os.environ['ANTHROPIC_API_KEY'] = 'Your Anthropic Key'
Simple Python Application
Now, let’s create a basic Python application to test AISuite. Create a main.py
file in your workspace and add the following code:
import aisuite as ai
client = ai.Client()
messages = [
{"role": "system", "content": "Respond in poetry form."},
{"role": "user", "content": "Tell me a joke."},
]
response = client.chat.completions.create(model="openai:gpt-4o", messages=messages, temperature=0.75)
print(response.choices[0].message.content)
You can modify the prompt with different instructions. In this example, we use OpenAI’s GPT-4o model with the key set earlier.
Run the Python file with the following command:
python main.py
Sample output:
“A penguin went to the market,
To buy fish, its favorite treat.
At checkout, the clerk asked:
‘Do you have a bag or will you carry it with your beak?’”
Not the funniest joke, but at least it followed the instructions!
Running a Local Model with Ollama
If you’re not familiar with Ollama, check out my guide explaining how it works.
To install Ollama on Linux, run:
curl -fsSL https://ollama.com/install.sh | sh
On Windows, download the installer from Ollama’s website.
Run the following command to execute a local model:
ollama run llama3.2
Ollama will download and run the Llama3.2 model, a version with 3B parameters, on your local machine.
Updating the Python Application
Let’s update our Python script to interact with multiple models:
import aisuite as ai
client = ai.Client()
models = ["openai:gpt-4o", "ollama:llama3.2"]
messages = [
{"role": "system", "content": "Respond in poetry form."},
{"role": "user", "content": "Tell me a joke."},
]
for model in models:
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=0.75
)
print(f'\n\n---Model: {model}---\n')
print(response.choices[0].message.content)
This code allows us to interact with different AI models (GPT-4 and Llama 3.2) and generate responses based on the provided context. It will make two requests, one for each model, and print the responses.
Run the script:
python main.py
Sample output:
---Model: openai:gpt-4o---
In a field of flowers so bright,
A bee began its flight.
I asked, “What’s your quest tonight?”
She buzzed, “Just seeking delight!”
---Model: ollama:llama3.2---
In a world so full of cheer,
A joke brings smiles ear to ear.
A man paid with movie coins one day,
The waiter laughed and joined the play.
Conclusion
AISuite makes it easier than ever to integrate multiple AI providers, ideal for those looking to optimize their time and fully harness the power of language models.
Whether you’re tackling simple or complex tasks, this tool provides the flexibility to switch between powerful platforms with just one line of code, fostering innovation in your projects.
If you haven’t tried AISuite yet, I encourage you to dive in and follow this practical guide.
Let me know how your experience goes.
See you next time! 💗