|
| 1 | +# Gradient SDK Integration Examples |
| 2 | + |
| 3 | +This directory contains integration examples showing how to use the Gradient SDK with popular Python web frameworks and tools. |
| 4 | + |
| 5 | +## Available Examples |
| 6 | + |
| 7 | +### Web Frameworks |
| 8 | + |
| 9 | +#### [FastAPI Integration](fastapi_integration.py) |
| 10 | +Complete FastAPI application demonstrating: |
| 11 | +- REST API endpoints for chat completions, image generation, and agent interactions |
| 12 | +- Streaming responses with Server-Sent Events |
| 13 | +- Pagination support for large datasets |
| 14 | +- Pydantic models for request/response validation |
| 15 | +- Automatic API documentation generation |
| 16 | + |
| 17 | +**Features:** |
| 18 | +- Chat completion endpoints (`/chat`, `/chat/stream`) |
| 19 | +- Image generation (`/images/generate`) |
| 20 | +- Agent interactions (`/agents`, `/agents/chat`) |
| 21 | +- Model listing with pagination (`/models`) |
| 22 | +- Health checks and statistics |
| 23 | + |
| 24 | +**To run:** |
| 25 | +```bash |
| 26 | +pip install fastapi uvicorn |
| 27 | +export DIGITALOCEAN_ACCESS_TOKEN="your_token" |
| 28 | +export GRADIENT_MODEL_ACCESS_KEY="your_key" |
| 29 | +python examples/fastapi_integration.py |
| 30 | +``` |
| 31 | + |
| 32 | +Visit `http://localhost:8000/docs` for interactive API documentation. |
| 33 | + |
| 34 | +#### [Flask Integration](flask_integration.py) |
| 35 | +Flask application showcasing: |
| 36 | +- RESTful endpoints with proper error handling |
| 37 | +- Streaming responses for real-time chat |
| 38 | +- Caching and rate limiting integration |
| 39 | +- JSON request/response handling |
| 40 | +- Logging and monitoring |
| 41 | + |
| 42 | +**Features:** |
| 43 | +- All core Gradient SDK features |
| 44 | +- Response caching (5-minute TTL) |
| 45 | +- Rate limiting (60 requests/minute) |
| 46 | +- Comprehensive error handling |
| 47 | +- Request logging |
| 48 | + |
| 49 | +**To run:** |
| 50 | +```bash |
| 51 | +pip install flask |
| 52 | +export DIGITALOCEAN_ACCESS_TOKEN="your_token" |
| 53 | +export GRADIENT_MODEL_ACCESS_KEY="your_key" |
| 54 | +python examples/flask_integration.py |
| 55 | +``` |
| 56 | + |
| 57 | +#### [Django Integration](django_integration.py) |
| 58 | +Django views demonstrating: |
| 59 | +- Django-style view functions |
| 60 | +- Integration with Django's URL routing |
| 61 | +- Streaming HTTP responses |
| 62 | +- Django JSON encoder compatibility |
| 63 | +- Settings-based configuration |
| 64 | + |
| 65 | +**Features:** |
| 66 | +- Django view functions for all endpoints |
| 67 | +- URL configuration examples |
| 68 | +- Django settings integration |
| 69 | +- StreamingHttpResponse for real-time features |
| 70 | + |
| 71 | +**To integrate:** |
| 72 | +1. Copy view functions to your Django `views.py` |
| 73 | +2. Add URL patterns to `urls.py` |
| 74 | +3. Configure environment variables in Django settings |
| 75 | +4. Run your Django development server |
| 76 | + |
| 77 | +## Common Features Across Examples |
| 78 | + |
| 79 | +All integration examples demonstrate: |
| 80 | + |
| 81 | +### Core Gradient SDK Features |
| 82 | +- **Chat Completions**: Text generation with various models |
| 83 | +- **Image Generation**: AI-powered image creation |
| 84 | +- **Agent Interactions**: Chat with deployed agents |
| 85 | +- **Model Management**: List and query available models |
| 86 | + |
| 87 | +### Advanced Utilities |
| 88 | +- **Pagination**: Handle large datasets efficiently |
| 89 | +- **Caching**: Reduce API calls with response caching |
| 90 | +- **Rate Limiting**: Prevent API quota exhaustion |
| 91 | +- **Streaming**: Real-time response handling |
| 92 | +- **Error Handling**: Robust error management |
| 93 | + |
| 94 | +### Production Considerations |
| 95 | +- **Environment Variables**: Secure credential management |
| 96 | +- **Logging**: Request/response monitoring |
| 97 | +- **Health Checks**: Service availability monitoring |
| 98 | +- **Statistics**: Usage metrics and insights |
| 99 | + |
| 100 | +## Environment Variables |
| 101 | + |
| 102 | +All examples require these environment variables: |
| 103 | + |
| 104 | +```bash |
| 105 | +# Required |
| 106 | +DIGITALOCEAN_ACCESS_TOKEN="your_digitalocean_token" |
| 107 | +GRADIENT_MODEL_ACCESS_KEY="your_model_access_key" |
| 108 | + |
| 109 | +# Optional |
| 110 | +GRADIENT_AGENT_ACCESS_KEY="your_agent_access_key" |
| 111 | +GRADIENT_AGENT_ENDPOINT="https://your-agent.agents.do-ai.run" |
| 112 | +``` |
| 113 | + |
| 114 | +## API Endpoints |
| 115 | + |
| 116 | +### Common Endpoints Across Frameworks |
| 117 | + |
| 118 | +| Method | Endpoint | Description | |
| 119 | +|--------|----------|-------------| |
| 120 | +| GET | `/health` | Health check | |
| 121 | +| GET | `/models` | List available models | |
| 122 | +| POST | `/chat` | Chat completion | |
| 123 | +| POST | `/chat/stream` | Streaming chat completion | |
| 124 | +| POST | `/images/generate` | Generate images | |
| 125 | +| GET | `/agents` | List agents | |
| 126 | +| POST | `/agents/chat` | Chat with agent | |
| 127 | +| GET | `/stats` | Usage statistics | |
| 128 | +| POST | `/cache/clear` | Clear response cache | |
| 129 | + |
| 130 | +### Request/Response Examples |
| 131 | + |
| 132 | +#### Chat Completion |
| 133 | +```json |
| 134 | +POST /chat |
| 135 | +{ |
| 136 | + "message": "What is the capital of France?", |
| 137 | + "model": "llama3.3-70b-instruct", |
| 138 | + "max_tokens": 100 |
| 139 | +} |
| 140 | + |
| 141 | +Response: |
| 142 | +{ |
| 143 | + "response": "The capital of France is Paris.", |
| 144 | + "model": "llama3.3-70b-instruct", |
| 145 | + "usage": {"prompt_tokens": 10, "completion_tokens": 8} |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +#### Image Generation |
| 150 | +```json |
| 151 | +POST /images/generate |
| 152 | +{ |
| 153 | + "prompt": "A beautiful sunset over mountains" |
| 154 | +} |
| 155 | + |
| 156 | +Response: |
| 157 | +{ |
| 158 | + "images": [...], |
| 159 | + "prompt": "A beautiful sunset over mountains" |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +#### Streaming Chat |
| 164 | +```json |
| 165 | +POST /chat/stream |
| 166 | +{ |
| 167 | + "message": "Tell me a story", |
| 168 | + "model": "llama3.3-70b-instruct" |
| 169 | +} |
| 170 | + |
| 171 | +Response: Server-Sent Events stream |
| 172 | +data: {"content": "Once"} |
| 173 | +data: {"content": " upon"} |
| 174 | +data: {"content": " a"} |
| 175 | +... |
| 176 | +data: [DONE] |
| 177 | +``` |
| 178 | + |
| 179 | +## Best Practices |
| 180 | + |
| 181 | +### Error Handling |
| 182 | +- Always check for required environment variables |
| 183 | +- Implement proper HTTP status codes |
| 184 | +- Log errors for debugging |
| 185 | +- Provide meaningful error messages |
| 186 | + |
| 187 | +### Performance |
| 188 | +- Use caching for frequently accessed data |
| 189 | +- Implement rate limiting to prevent quota exhaustion |
| 190 | +- Consider pagination for large datasets |
| 191 | +- Use streaming for real-time responses |
| 192 | + |
| 193 | +### Security |
| 194 | +- Never commit API keys to version control |
| 195 | +- Use environment variables for credentials |
| 196 | +- Implement proper authentication/authorization |
| 197 | +- Validate input data thoroughly |
| 198 | + |
| 199 | +### Monitoring |
| 200 | +- Add health check endpoints |
| 201 | +- Log API usage and errors |
| 202 | +- Monitor response times |
| 203 | +- Track usage statistics |
| 204 | + |
| 205 | +## Contributing |
| 206 | + |
| 207 | +When adding new integration examples: |
| 208 | + |
| 209 | +1. Follow the existing code structure and patterns |
| 210 | +2. Include comprehensive error handling |
| 211 | +3. Add proper logging and monitoring |
| 212 | +4. Document environment variable requirements |
| 213 | +5. Provide clear setup and usage instructions |
| 214 | +6. Test with real API credentials when possible |
| 215 | + |
| 216 | +## Support |
| 217 | + |
| 218 | +For issues with these examples: |
| 219 | +1. Check that all required environment variables are set |
| 220 | +2. Verify API credentials are valid |
| 221 | +3. Review the Gradient SDK documentation |
| 222 | +4. Check the framework-specific documentation |
| 223 | +5. Open an issue in the Gradient Python repository |
0 commit comments