A custom Python SDK for interacting with AI models, compatible with OpenAI's API format
Project description
AI SDK Python
A comprehensive Python SDK for interacting with AI models, compatible with OpenAI's API format and supporting multiple providers.
Features
- 🤖 Multiple Provider Support: OpenAI, LiteLLM, and VLLM providers
- 🔄 Streaming Support: Real-time streaming responses with event handling
- 🛡️ Error Handling: Comprehensive error handling with custom exceptions
- 🔧 Flexible Configuration: Environment variables, config files, and programmatic setup
- 📦 Easy Integration: Simple, intuitive API design
- 🎯 Type Safety: Full type hints support
Installation
pip install nexgen-sdk
For development:
pip install nexgen-sdk[dev]
Quick Start
Basic Usage
from ai_sdk import AISDKClient
# Initialize client
client = AISDKClient(
api_key="your-api-key",
provider="openai" # or "vllm", "litellm"
)
# Create chat completion
response = client.chat().create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": "Hello, world!"}
]
)
print(response['choices'][0]['message']['content'])
Streaming
# Stream responses in real-time
response = client.chat().create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True
)
for chunk in response:
if chunk['choices'][0]['delta'].get('content'):
print(chunk['choices'][0]['delta']['content'], end='', flush=True)
Provider-Specific Examples
VLLM Provider with New Format
client = AISDKClient(provider="vllm", api_key="your-key")
# New format supports complex user content objects
response = client.chat().create(
system_prompt="You are a helpful AI assistant",
messages=[
{
"role": "user",
"content": {
"query": "What is Python?",
"detail_level": "intermediate",
"include_examples": True
}
}
],
model="model_name"
)
Configuration
Environment Variables
export AI_SDK_API_KEY="your-api-key"
export AI_SDK_BASE_URL="https://api.openai.com/v1"
export AI_SDK_TIMEOUT="30.0"
Builder Pattern
client = (AISDKClient.builder()
.with_provider("vllm")
.with_api_key("your-key")
.with_base_url("https://your-vllm-endpoint")
.with_timeout(60.0)
.build())
Providers
OpenAI Provider
- Base URL:
https://api.openai.com/v1 - Models: GPT-3.5, GPT-4, and all OpenAI models
- Features: Chat, completions, embeddings
VLLM Provider
- Custom Endpoints: Supports any VLLM deployment
- New Format: Enhanced message format with complex user content
- Streaming: Optimized real-time streaming
LiteLLM Provider
- Unified Interface: Access 100+ models through LiteLLM
- Model Support: OpenAI, Anthropic, Cohere, and more
- Fallback: Automatic fallback between providers
API Reference
Client
AISDKClient()- Main client classchat()- Access chat completionscompletions()- Access text completionsembeddings()- Access embeddings
Chat Completions
create()- Create chat completion or stream- Parameters:
messages,model,temperature,stream, etc.
Error Handling
from ai_sdk.exceptions import (
AISDKException,
APIException,
AuthenticationException,
RateLimitException
)
try:
response = client.chat().create(...)
except AuthenticationException:
print("Invalid API key")
except RateLimitException:
print("Rate limit exceeded")
except AISDKException as e:
print(f"SDK Error: {e}")
Development
Setup
git clone https://github.com/Dhruv1969Karnwal/ai-sdk-python
cd ai-sdk-python
pip install -e ".[dev]"
Running Tests
pytest
Code Formatting
black ai_sdk
flake8 ai_sdk
mypy ai_sdk
Examples
See the examples/ directory for comprehensive usage examples:
new_format_example.py- VLLM new format examplesdebug_streaming.py- Streaming debug examplestest_clean_streaming.py- Clean streaming tests
Requirements
- Python 3.7+
- httpx>=0.23.0
- typing_extensions>=3.7.4 (Python < 3.8)
Optional Dependencies
- litellm>=1.0.0 (for LiteLLM provider)
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
- 📖 Documentation
- 🐛 Issues
- 💬 Discussions
Contributing
Contributions are welcome! Please see our Contributing Guide for details.
Changelog
See CHANGELOG.md for version history.
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 nexgen_sdk-0.1.1.tar.gz.
File metadata
- Download URL: nexgen_sdk-0.1.1.tar.gz
- Upload date:
- Size: 25.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0742cfa74a085ab8db3da5a35f0c562a770778b94bd7dcea6b201af4bda322b5
|
|
| MD5 |
689598cdcef83cb76cbba43c69554ba9
|
|
| BLAKE2b-256 |
b22cca3aadd5c2d3ca0c1054945ae0081e6033e007206440b44056301b7f5455
|
File details
Details for the file nexgen_sdk-0.1.1-py3-none-any.whl.
File metadata
- Download URL: nexgen_sdk-0.1.1-py3-none-any.whl
- Upload date:
- Size: 29.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72f76ce5beba411d414091857fb234e1e179d83f8d13c43fb2fd7936617e201b
|
|
| MD5 |
598f251326baeb9bd1792c03882342f9
|
|
| BLAKE2b-256 |
1221781d9370dabbd7f45c2a7afc2143810adb85ae7a250b7c0b6532cb08e1ac
|