|
1 | | -# aisuite |
| 1 | +# aisuite |
2 | 2 |
|
3 | 3 | [](https://pypi.org/project/aisuite/) |
4 | 4 | [](https://github.com/psf/black) |
5 | 5 |
|
6 | | -Simple, unified interface to multiple Generative AI providers. |
| 6 | +`aisuite` is a lightweight Python library that provides a **unified API for working with multiple Generative AI providers**. |
| 7 | +It offers a consistent interface for models from *OpenAI, Anthropic, Google, Hugging Face, AWS, Cohere, Mistral, Ollama*, and others—abstracting away SDK differences, authentication details, and parameter variations. |
| 8 | +Its design is modeled after OpenAI’s API style, making it instantly familiar and easy to adopt. |
7 | 9 |
|
8 | | -`aisuite` makes it easy for developers to interact with multiple Gen-AI services through a standardized interface. Using an interface similar to OpenAI's, `aisuite` supports **chat completions** and **audio transcription**, making it easy to work with the most popular AI providers and compare results. It is a thin wrapper around python client libraries, and allows creators to seamlessly swap out and test different providers without changing their code. |
| 10 | +`aisuite` lets developers build and **run LLM-based or agentic applications across providers** with minimal setup. |
| 11 | +While it’s not a full-blown agents framework, it includes simple abstractions for creating standalone, lightweight agents. |
| 12 | +It’s designed for low learning curve — so you can focus on building AI systems, not integrating APIs. |
9 | 13 |
|
10 | | -All of the top providers are supported. |
11 | | -Sample list of supported providers include - Anthropic, AWS, Azure, Cerebras, Cohere, Google, Groq, HuggingFace, Ollama, Mistral, OpenAI, Sambanova, Watsonx and others. |
| 14 | +--- |
12 | 15 |
|
13 | | -To maximize stability, `aisuite` uses either the HTTP endpoint or the SDK for making calls to the provider. |
| 16 | +## Key Features |
| 17 | + |
| 18 | +`aisuite` is designed to eliminate the complexity of working with multiple LLM providers while keeping your code simple and portable. Whether you're building a chatbot, an agentic application, or experimenting with different models, `aisuite` provides the abstractions you need without getting in your way. |
| 19 | + |
| 20 | +* **Unified API for multiple model providers** – Write your code once and run it with any supported provider. Switch between OpenAI, Anthropic, Google, and others with a single parameter change. |
| 21 | +* **Easy agentic app or agent creation** – Build multi-turn agentic applications using a single parameter `max_turns`. No need to manually manage tool execution loops. |
| 22 | +* **Pass Tool calls easily** – Pass real Python functions instead of JSON specs; aisuite handles schema generation and execution automatically. |
| 23 | +* **MCP tools** – Connect to MCP-based tools without writing boilerplate; aisuite handles connection, schema and execution seamlessly. |
| 24 | +* **Modular and extensible provider architecture** – Add support for new providers with minimal code. The plugin-style architecture makes extensions straightforward. |
| 25 | + |
| 26 | +--- |
14 | 27 |
|
15 | 28 | ## Installation |
16 | 29 |
|
17 | 30 | You can install just the base `aisuite` package, or install a provider's package along with `aisuite`. |
18 | 31 |
|
19 | | -This installs just the base package without installing any provider's SDK. |
| 32 | +Install just the base package without any provider SDKs: |
20 | 33 |
|
21 | 34 | ```shell |
22 | 35 | pip install aisuite |
23 | 36 | ``` |
24 | 37 |
|
25 | | -This installs aisuite along with anthropic's library. |
| 38 | +Install aisuite with a specific provider (e.g., Anthropic): |
26 | 39 |
|
27 | 40 | ```shell |
28 | 41 | pip install 'aisuite[anthropic]' |
29 | 42 | ``` |
30 | 43 |
|
31 | | -This installs all the provider-specific libraries |
| 44 | +Install aisuite with all provider libraries: |
32 | 45 |
|
33 | 46 | ```shell |
34 | 47 | pip install 'aisuite[all]' |
35 | 48 | ``` |
36 | 49 |
|
37 | | -## Set up |
| 50 | +## Setup |
38 | 51 |
|
39 | 52 | To get started, you will need API Keys for the providers you intend to use. You'll need to |
40 | 53 | install the provider-specific library either separately or when installing aisuite. |
@@ -76,52 +89,44 @@ for model in models: |
76 | 89 |
|
77 | 90 | Note that the model name in the create() call uses the format - `<provider>:<model-name>`. |
78 | 91 | `aisuite` will call the appropriate provider with the right parameters based on the provider value. |
79 | | -For a list of provider values, you can look at the directory - `aisuite/providers/`. The list of supported providers are of the format - `<provider>_provider.py` in that directory. We welcome providers adding support to this library by adding an implementation file in this directory. Please see section below for how to contribute. |
| 92 | +For a list of provider values, you can look at the directory - `aisuite/providers/`. The list of supported providers are of the format - `<provider>_provider.py` in that directory. We welcome providers to add support to this library by adding an implementation file in this directory. Please see section below for how to contribute. |
80 | 93 |
|
81 | 94 | For more examples, check out the `examples` directory where you will find several notebooks that you can run to experiment with the interface. |
82 | 95 |
|
83 | | -## Adding support for a provider |
84 | | - |
85 | | -We have made easy for a provider or volunteer to add support for a new platform. |
86 | | - |
87 | | -### Naming Convention for Provider Modules |
88 | | - |
89 | | -We follow a convention-based approach for loading providers, which relies on strict naming conventions for both the module name and the class name. The format is based on the model identifier in the form `provider:model`. |
90 | | - |
91 | | -- The provider's module file must be named in the format `<provider>_provider.py`. |
92 | | -- The class inside this module must follow the format: the provider name with the first letter capitalized, followed by the suffix `Provider`. |
93 | | - |
94 | | -#### Examples |
| 96 | +--- |
95 | 97 |
|
96 | | -- **Hugging Face**: |
97 | | - The provider class should be defined as: |
| 98 | +## Chat Completions |
98 | 99 |
|
99 | | - ```python |
100 | | - class HuggingfaceProvider(BaseProvider) |
101 | | - ``` |
| 100 | +The chat API provides a high-level abstraction for model interactions. It supports all core parameters (`temperature`, `max_tokens`, `tools`, etc.) in a provider-agnostic way. |
102 | 101 |
|
103 | | - in providers/huggingface_provider.py. |
104 | | - |
105 | | -- **OpenAI**: |
106 | | - The provider class should be defined as: |
107 | | - |
108 | | - ```python |
109 | | - class OpenaiProvider(BaseProvider) |
110 | | - ``` |
| 102 | +```python |
| 103 | +response = client.chat.completions.create( |
| 104 | + model="google:gemini-pro", |
| 105 | + messages=[{"role": "user", "content": "Summarize this paragraph."}], |
| 106 | +) |
| 107 | +print(response.choices[0].message.content) |
| 108 | +``` |
111 | 109 |
|
112 | | - in providers/openai_provider.py |
| 110 | +`aisuite` standardizes request and response structures so you can focus on logic rather than SDK differences. |
113 | 111 |
|
114 | | -This convention simplifies the addition of new providers and ensures consistency across provider implementations. |
| 112 | +--- |
115 | 113 |
|
116 | | -## Tool Calling |
| 114 | +## Tool Calling & Agentic apps |
117 | 115 |
|
118 | 116 | `aisuite` provides a simple abstraction for tool/function calling that works across supported providers. This is in addition to the regular abstraction of passing JSON spec of the tool to the `tools` parameter. The tool calling abstraction makes it easy to use tools with different LLMs without changing your code. |
119 | 117 |
|
120 | 118 | There are two ways to use tools with `aisuite`: |
121 | 119 |
|
122 | 120 | ### 1. Manual Tool Handling |
123 | 121 |
|
124 | | -This is the default behavior when `max_turns` is not specified. |
| 122 | +This is the default behavior when `max_turns` is not specified. In this mode, you have full control over the tool execution flow. You pass tools using the standard OpenAI JSON schema format, and `aisuite` returns the LLM's tool call requests in the response. You're then responsible for executing the tools, processing results, and sending them back to the model in subsequent requests. |
| 123 | + |
| 124 | +This approach is useful when you need: |
| 125 | +- Fine-grained control over tool execution logic |
| 126 | +- Custom error handling or validation before executing tools |
| 127 | +- The ability to selectively execute or skip certain tool calls |
| 128 | +- Integration with existing tool execution pipelines |
| 129 | + |
125 | 130 | You can pass tools in the OpenAI tool format: |
126 | 131 |
|
127 | 132 | ```python |
@@ -200,156 +205,108 @@ When `max_turns` is specified, `aisuite` will: |
200 | 205 | 3. Send the tool results back to the LLM |
201 | 206 | 4. Repeat until the conversation is complete or max_turns is reached |
202 | 207 |
|
203 | | -In addition to `response.choices[0].message`, there is an additional field `response.choices[0].intermediate_messages`: which contains the list of all messages including tool interactions used. This can be used to continue the conversation with the model. |
| 208 | +In addition to `response.choices[0].message`, there is an additional field `response.choices[0].intermediate_messages` which contains the list of all messages including tool interactions used. This can be used to continue the conversation with the model. |
204 | 209 | For more detailed examples of tool calling, check out the `examples/tool_calling_abstraction.ipynb` notebook. |
205 | 210 |
|
206 | | -## Audio Transcription |
| 211 | +### Model Context Protocol (MCP) Integration |
207 | 212 |
|
208 | | -> **Note:** Audio transcription support is currently under development. The API and features described below are subject to change. |
| 213 | +`aisuite` natively supports **MCP**, a standard protocol that allows LLMs to securely call external tools and access data. You can connect to MCP servers—such as a filesystem or database—and expose their tools directly to your model. |
| 214 | +Read more about MCP here - https://modelcontextprotocol.io/docs/getting-started/intro |
209 | 215 |
|
210 | | -`aisuite` provides audio transcription (speech-to-text) with the same unified interface pattern used for chat completions. Transcribe audio files across multiple providers with consistent code. |
| 216 | +Install aisuite with MCP support: |
211 | 217 |
|
212 | | -### Basic Usage |
213 | | - |
214 | | -```python |
215 | | -import aisuite as ai |
216 | | -client = ai.Client() |
| 218 | +```shell |
| 219 | +pip install 'aisuite[mcp]' |
| 220 | +``` |
217 | 221 |
|
218 | | -# Transcribe an audio file |
219 | | -result = client.audio.transcriptions.create( |
220 | | - model="openai:whisper-1", |
221 | | - file="meeting.mp3" |
222 | | -) |
223 | | -print(result.text) |
| 222 | +You'll also need an MCP server. For example, to use the filesystem server: |
224 | 223 |
|
225 | | -# Switch providers without changing your code |
226 | | -result = client.audio.transcriptions.create( |
227 | | - model="deepgram:nova-2", |
228 | | - file="meeting.mp3" |
229 | | -) |
230 | | -print(result.text) |
| 224 | +```shell |
| 225 | +npm install -g @modelcontextprotocol/server-filesystem |
231 | 226 | ``` |
232 | 227 |
|
233 | | -### Common Parameters |
| 228 | +There are two ways to use MCP tools with aisuite: |
234 | 229 |
|
235 | | -Use OpenAI-style parameters that work across all providers: |
| 230 | +#### Option 1: Config Dict Format (Recommended for Simple Use Cases) |
236 | 231 |
|
237 | 232 | ```python |
238 | | -result = client.audio.transcriptions.create( |
239 | | - model="openai:whisper-1", |
240 | | - file="interview.mp3", |
241 | | - language="en", # Specify audio language |
242 | | - prompt="Technical discussion about AI", # Context hints |
243 | | - temperature=0.2 # Sampling temperature (where supported) |
244 | | -) |
245 | | -``` |
| 233 | +import aisuite as ai |
246 | 234 |
|
247 | | -These parameters are automatically mapped to each provider's native format. |
| 235 | +client = ai.Client() |
| 236 | +response = client.chat.completions.create( |
| 237 | + model="openai:gpt-4o", |
| 238 | + messages=[{"role": "user", "content": "List the files in the current directory"}], |
| 239 | + tools=[{ |
| 240 | + "type": "mcp", |
| 241 | + "name": "filesystem", |
| 242 | + "command": "npx", |
| 243 | + "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/directory"] |
| 244 | + }], |
| 245 | + max_turns=3 |
| 246 | +) |
248 | 247 |
|
249 | | -### Provider-Specific Features |
| 248 | +print(response.choices[0].message.content) |
| 249 | +``` |
250 | 250 |
|
251 | | -Each provider offers unique capabilities you can access directly: |
| 251 | +#### Option 2: Explicit MCPClient (Recommended for Advanced Use Cases) |
252 | 252 |
|
253 | | -**OpenAI Whisper:** |
254 | 253 | ```python |
255 | | -result = client.audio.transcriptions.create( |
256 | | - model="openai:whisper-1", |
257 | | - file="speech.mp3", |
258 | | - response_format="verbose_json", # Get detailed metadata |
259 | | - timestamp_granularities=["word"] # Word-level timestamps |
260 | | -) |
261 | | -``` |
| 254 | +import aisuite as ai |
| 255 | +from aisuite.mcp import MCPClient |
262 | 256 |
|
263 | | -**Deepgram:** |
264 | | -```python |
265 | | -result = client.audio.transcriptions.create( |
266 | | - model="deepgram:nova-2", |
267 | | - file="meeting.mp3", |
268 | | - punctuate=True, # Auto-add punctuation |
269 | | - diarize=True, # Identify speakers |
270 | | - sentiment=True, # Sentiment analysis |
271 | | - summarize=True # Auto-summarization |
| 257 | +# Create MCP client once, reuse across requests |
| 258 | +mcp = MCPClient( |
| 259 | + command="npx", |
| 260 | + args=["-y", "@modelcontextprotocol/server-filesystem", "/path/to/directory"] |
272 | 261 | ) |
273 | | -``` |
274 | 262 |
|
275 | | -**Google Speech-to-Text:** |
276 | | -```python |
277 | | -result = client.audio.transcriptions.create( |
278 | | - model="google:default", |
279 | | - file="call.mp3", |
280 | | - enable_automatic_punctuation=True, |
281 | | - enable_speaker_diarization=True, |
282 | | - diarization_speaker_count=2 |
| 263 | +# Use with aisuite |
| 264 | +client = ai.Client() |
| 265 | +response = client.chat.completions.create( |
| 266 | + model="openai:gpt-4o", |
| 267 | + messages=[{"role": "user", "content": "List the files"}], |
| 268 | + tools=mcp.get_callable_tools(), |
| 269 | + max_turns=3 |
283 | 270 | ) |
284 | | -``` |
285 | 271 |
|
286 | | -**Hugging Face:** |
287 | | -```python |
288 | | -result = client.audio.transcriptions.create( |
289 | | - model="huggingface:openai/whisper-large-v3", |
290 | | - file="presentation.mp3", |
291 | | - return_timestamps="word" # Word-level timestamps |
292 | | -) |
| 272 | +print(response.choices[0].message.content) |
| 273 | +mcp.close() # Clean up |
293 | 274 | ``` |
294 | 275 |
|
295 | | -### Streaming Transcription |
| 276 | +For detailed usage (security filters, tool prefixing, and `MCPClient` management), see [docs/mcp-tools.md](docs/mcp-tools.md). |
| 277 | +For detailed examples, see `examples/mcp_tools_example.ipynb`. |
296 | 278 |
|
297 | | -For real-time or large audio files, use streaming: |
| 279 | +--- |
298 | 280 |
|
299 | | -```python |
300 | | -async def transcribe_stream(): |
301 | | - stream = client.audio.transcriptions.create_stream_output( |
302 | | - model="deepgram:nova-2", |
303 | | - file="long_recording.mp3" |
304 | | - ) |
305 | | - |
306 | | - async for chunk in stream: |
307 | | - print(chunk.text, end="", flush=True) |
308 | | - if chunk.is_final: |
309 | | - print() # New line for final results |
| 281 | +## Extending aisuite: Adding a Provider |
310 | 282 |
|
311 | | -# Run the async function |
312 | | -import asyncio |
313 | | -asyncio.run(transcribe_stream()) |
314 | | -``` |
| 283 | +New providers can be added by implementing a lightweight adapter. The system uses a naming convention for discovery: |
315 | 284 |
|
316 | | -### Supported Providers |
| 285 | +| Element | Convention | |
| 286 | +| --------------- | ---------------------------------- | |
| 287 | +| **Module file** | `<provider>_provider.py` | |
| 288 | +| **Class name** | `<Provider>Provider` (capitalized) | |
317 | 289 |
|
318 | | -- **OpenAI**: `whisper-1` |
319 | | -- **Deepgram**: `nova-2`, `nova`, `enhanced`, `base` |
320 | | -- **Google**: `default`, `latest_long`, `latest_short` |
321 | | -- **Hugging Face**: `openai/whisper-large-v3`, `openai/whisper-tiny`, `facebook/wav2vec2-base-960h`, `facebook/wav2vec2-large-xlsr-53` |
| 290 | +Example: |
322 | 291 |
|
323 | | -### Installation |
| 292 | +```python |
| 293 | +# providers/openai_provider.py |
| 294 | +class OpenaiProvider(BaseProvider): |
| 295 | + ... |
| 296 | +``` |
324 | 297 |
|
325 | | -Install transcription providers: |
| 298 | +This convention ensures consistency and enables automatic loading of new integrations. |
326 | 299 |
|
327 | | -```shell |
328 | | -# Install with specific provider |
329 | | -pip install 'aisuite[openai]' # For OpenAI Whisper |
330 | | -pip install 'aisuite[deepgram]' # For Deepgram |
331 | | -pip install 'aisuite[google]' # For Google Speech-to-Text |
332 | | -pip install 'aisuite[huggingface]' # For Hugging Face models |
| 300 | +--- |
333 | 301 |
|
334 | | -# Install all providers |
335 | | -pip install 'aisuite[all]' |
336 | | -``` |
| 302 | +## Contributing |
337 | 303 |
|
338 | | -Set API keys: |
| 304 | +Contributions are welcome. Please review the [Contributing Guide](https://github.com/andrewyng/aisuite/blob/main/CONTRIBUTING.md) and join our [Discord](https://discord.gg/T6Nvn8ExSb) for discussions. |
339 | 305 |
|
340 | | -```shell |
341 | | -export OPENAI_API_KEY="your-openai-api-key" |
342 | | -export DEEPGRAM_API_KEY="your-deepgram-api-key" |
343 | | -export GOOGLE_APPLICATION_CREDENTIALS="path/to/credentials.json" |
344 | | -export HF_TOKEN="your-huggingface-token" |
345 | | -``` |
346 | | - |
347 | | -For more examples and advanced usage, check out `examples/asr_example.ipynb`. |
| 306 | +--- |
348 | 307 |
|
349 | 308 | ## License |
350 | 309 |
|
351 | | -aisuite is released under the MIT License. You are free to use, modify, and distribute the code for both commercial and non-commercial purposes. |
352 | | - |
353 | | -## Contributing |
| 310 | +Released under the **MIT License** — free for commercial and non-commercial use. |
354 | 311 |
|
355 | | -If you would like to contribute, please read our [Contributing Guide](https://github.com/andrewyng/aisuite/blob/main/CONTRIBUTING.md) and join our [Discord](https://discord.gg/T6Nvn8ExSb) server! |
| 312 | +--- |
0 commit comments