A unified library for interacting with various AI APIs through a standardized interface.
Project description
SwitchAI
SwitchAI is a lightweight and flexible library that provides a standardized interface for interacting with various AI APIs like OpenAI, Anthropic, Mistral, and more. With SwitchAI, you can easily switch between AI providers or use multiple APIs simultaneously, all with a simple and consistent interface.
Installation
To install SwitchAI, simply use pip:
pip install switchai
For more details, refer to the installation guide.
Getting Started
To use SwitchAI, you will need API keys for the AI providers you intend to interact with. You can set these keys either
as environment variables or pass them as configuration to the SwitchAI client.
Option 1: In Code
from switchai import SwitchAI
client = SwitchAI(provider="openai", model_name="gpt-4", api_key="your_api_key")
Option 2: Environment Variables
Set the API key as an environment variable:
macOS/Linux:
export PROVIDER_API_KEY="your_api_key"
Windows:
set PROVIDER_API_KEY="your_api_key"
Make sure you follow the correct naming conventions for each provider's API key, as outlined in the documentation. This ensures that SwitchAI can automatically detect and use the appropriate key for the chosen provider.
Example Usage
Here are some examples of how you can use SwitchAI to interact with different AI models:
Chat
from switchai import SwitchAI
# Initialize the client with the desired AI model
client = SwitchAI(provider="openai", model_name="gpt-4o")
# Send a message and receive a response
response = client.chat(
messages=[
{"role": "user", "content": "Hello, how are you?"}
]
)
# Print the response
print(response)
Vision
from switchai import SwitchAI
# Initialize the client with the vision model
client = SwitchAI(provider="mistral", model_name="pixtral-large-latest")
# Send an image with a question and receive a response
response = client.chat(
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{"type": "image", "image": "path/to/image/file.jpg"},
],
}
]
)
# Print the response
print(response)
Text Embedding
from switchai import SwitchAI
# Initialize the client with the chosen embedding model
client = SwitchAI(provider="google", model_name="models/text-embedding-004")
# Generate embeddings for a list of text inputs
response = client.embed(
inputs=[
"I am feeling great today!",
"I am feeling sad today."
]
)
# Print the response
print(response)
Speech to Text
from switchai import SwitchAI
# Initialize the client with the desired speech-to-text model
client = SwitchAI(provider="deepgram", model_name="nova-2")
# Transcribe an audio file
response = client.transcribe(
audio_path="path/to/audio/file.wav"
)
# Print the response
print(response)
Image Generation
from switchai import SwitchAI
client = SwitchAI(provider="replicate", model_name="black-forest-labs/flux-schnell")
response = client.generate_image("A beautiful sunset over the mountains.")
image = response.images[0]
image.show()
SuperClients
SuperClients are high-level interfaces that extend the base SwitchAI client to provide additional functionalities.
Browser
Gives a chat model the ability to access websites.
from switchai import SwitchAI, Browser
client = SwitchAI(provider="openai", model_name="gpt-4o")
client = Browser(client)
response = client.chat(
messages=[
{
"role": "user",
"content": "Can you summarize the content of this website: https://example.com?"
},
]
)
print(response)
Classifier
Assigns a label to a text or image input.
from switchai import SwitchAI, Classifier
client = SwitchAI(provider="openai", model_name="gpt-4o-mini")
classifier = Classifier(client, classes=["negative", "positive"])
response = classifier.classify("I am feeling great today!")
print(response) # Output: "positive"
Illustrator
Creates SVG illustrations using simple textual descriptions.
from switchai import SwitchAI, Illustrator
client = SwitchAI(provider="openai", model_name="gpt-4o")
illustrator = Illustrator(client)
illustrator.generate_illustration(
"Design a futuristic logo for my AI app with a sleek, modern aesthetic.",
output_path="logo.svg",
)
ImageRetriever
Retrieves images from a folder of images based on a query.
from switchai import SwitchAI, ImageRetriever
client = SwitchAI(provider="VoyageAI", model_name="voyage-multimodal-3")
image_retriever = ImageRetriever(client, images_folder_path="files/images")
results = image_retriever.retrieve_images("An orange cat in a green field.")
print(results)
Documentation
For full documentation, visit SwitchAI Documentation.
Contributing
Contributions are always welcome! If you'd like to help enhance SwitchAI, feel free to make a contribution.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file switchai-0.5.1.tar.gz.
File metadata
- Download URL: switchai-0.5.1.tar.gz
- Upload date:
- Size: 29.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb9166474dac5929e842a3e8faeb86afa7631a4a79e51543d71371d21aed4544
|
|
| MD5 |
65349036fbd093951cc9bfbc21a74e4f
|
|
| BLAKE2b-256 |
2c6f2b28cb487d599ba26b1cd0f79ad039178942c19a25328f636d7466ec1f52
|
File details
Details for the file switchai-0.5.1-py3-none-any.whl.
File metadata
- Download URL: switchai-0.5.1-py3-none-any.whl
- Upload date:
- Size: 37.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fae43328e7c5ba33f5aa03e0a8409de92b5e2f60d0588bcd05e4d3d07e5d55c2
|
|
| MD5 |
ef5c8e7483453e795ee1d4bc5ea072a6
|
|
| BLAKE2b-256 |
1822b9b79cb73a167af26d1097a34749bf69af6eb60ee660d3862d5523c701fc
|