To use the aisuite package in your Python project, follow these steps:
1. Installation:
You can install aisuite using pip. Choose one of the following options:
-
Base installation: This installs only the core aisuite package:
pip install aisuite
-
Installation with provider-specific libraries: This installs aisuite along with the necessary library for a specific provider (e.g., Anthropic):
pip install 'aisuite[anthropic]'
-
Installation with all provider libraries: This installs aisuite along with libraries for all supported providers:
pip install 'aisuite[all]'
2. Set up API Keys:
You'll need API keys for the Generative AI providers you want to use (e.g., OpenAI, Anthropic, Google). You can set these as environment variables or pass them as configuration to the aisuite.Client constructor.
3. Code Example:
Here's an example demonstrating how to use aisuite to generate chat completion responses from GPT-4o and Claude 3.5 Sonnet:
import aisuite as ai
client = ai.Client()
models = ["openai:gpt-4o", "anthropic:claude-3-5-sonnet-20240620"]
messages = [
{"role": "system", "content": "Respond in Pirate English."},
{"role": "user", "content": "Tell me a joke."},
]
for model in models:
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=0.75
)
print(response.choices[0].message.content)
In this example:
ai.Client() creates an aisuite client.
models specifies a list of models to use, using the format <provider>:<model-name>.
messages defines the chat messages.
client.chat.completions.create() generates the chat completion.
4. Model Naming Convention:
The model parameter in the create() call uses the format <provider>:<model-name>. The aisuite library uses this to route the request to the correct provider with the appropriate parameters. You can find the supported provider values by inspecting the files in the aisuite/providers/ directory. The provider files are named as <provider>_provider.py.
5. Provider Configurations:
You can pass provider configurations to the Client constructor, such as API keys. For example:
client = ai.Client(provider_configs={
"openai": {"api_key": "your_openai_api_key"},
"aws-bedrock": {
"aws_access_key": "your_aws_access_key",
"aws_secret_key": "your_aws_secret_key",
"aws_region": "us-west-2"
}
})
6. More Examples:
Refer to the examples directory in the repository for more detailed usage examples. The chat-ui example demonstrates a Streamlit-based chat UI that uses aisuite.
7. Chat UI Example
The examples/chat-ui/chat.py file shows how to build a simple chat UI using Streamlit and aisuite. To run it, you would:
- Install Streamlit:
pip install streamlit
- Create a
config.yaml file (an example is provided) to configure the LLMs you want to use. This file specifies the provider and model names. Also, create a .env file in the same directory to set the necessary API keys as environment variables.
- Run the app:
streamlit run chat.py
The config.yaml file would look something like this:
- name: "OpenAI GPT-4o"
provider: "openai"
model: "gpt-4o"
- name: "Anthropic [Redacted Person] 3.5 Sonnet"
provider: "anthropic"
model: "claude-3-5-sonnet-20240620"
- name: "Azure/OpenAI GPT-4o"
provider: "azure"
model: "gpt-4o"
- name: "Huggingface/Mistral 7B"
provider: "huggingface"
model: "mistralai/Mistral-7B-Instruct"
8. Contributing
If you want to contribute to the project, refer to the Contributing Guide and join the Discord server. The README.md also details the naming conventions to follow when adding support for a new provider.