|
| 1 | +<div class="hero-icon" align="center"> |
| 2 | + <img src="https://raw.githubusercontent.com/PKief/vscode-material-icon-theme/ec559a9f6bfd399b82bb44393651661b08aaf7ba/icons/folder-markdown-open.svg" width="100" /> |
| 3 | +</div> |
| 4 | + |
| 5 | +<h1 align="center"> |
| 6 | +OpenAI-API-Python-Client |
| 7 | +</h1> |
| 8 | +<h4 align="center">A Python backend API wrapper that simplifies integration of OpenAI's powerful NLP capabilities.</h4> |
| 9 | +<h4 align="center">Developed with the software and tools below.</h4> |
| 10 | +<div class="badges" align="center"> |
| 11 | + <img src="https://img.shields.io/badge/Language-Python-blue" alt="Programming Language"> |
| 12 | + <img src="https://img.shields.io/badge/Framework-FastAPI-red" alt="Web Framework"> |
| 13 | + <img src="https://img.shields.io/badge/Database-PostgreSQL-blue" alt="Database"> |
| 14 | + <img src="https://img.shields.io/badge/API-OpenAI-black" alt="API Integration"> |
| 15 | +</div> |
| 16 | +<div class="badges" align="center"> |
| 17 | + <img src="https://img.shields.io/github/last-commit/coslynx/OpenAI-API-Python-Client?style=flat-square&color=5D6D7E" alt="git-last-commit" /> |
| 18 | + <img src="https://img.shields.io/github/commit-activity/m/coslynx/OpenAI-API-Python-Client?style=flat-square&color=5D6D7E" alt="GitHub commit activity" /> |
| 19 | + <img src="https://img.shields.io/github/languages/top/coslynx/OpenAI-API-Python-Client?style=flat-square&color=5D6D7E" alt="GitHub top language" /> |
| 20 | +</div> |
| 21 | + |
| 22 | +## 📑 Table of Contents |
| 23 | +- 📍 Overview |
| 24 | +- 📦 Features |
| 25 | +- 📂 Structure |
| 26 | +- 💻 Installation |
| 27 | +- 🏗️ Usage |
| 28 | +- 🌐 Hosting |
| 29 | +- 📄 License |
| 30 | +- 👏 Authors |
| 31 | + |
| 32 | +## 📍 Overview |
| 33 | + |
| 34 | +This repository provides a Minimum Viable Product (MVP) called "OpenAI-API-Python-Client". It offers a user-friendly Python backend API wrapper that simplifies the integration of OpenAI's powerful NLP capabilities into various projects. This MVP differentiates itself by focusing on simplicity and efficiency, making it ideal for developers of all skill levels. |
| 35 | + |
| 36 | +## 📦 Features |
| 37 | + |
| 38 | +| | Feature | Description | |
| 39 | +|----|--------------------|--------------------------------------------------------------------------------------------------------------------| |
| 40 | +| ⚙️ | **Architecture** | Utilizes a microservices architecture, with the API wrapper running as a standalone service. This provides flexibility and allows for independent scaling of components. | |
| 41 | +| 📄 | **Documentation** | Provides detailed documentation, including API usage instructions, code examples, and tutorials. This streamlines onboarding and enables users to quickly learn and leverage the API. | |
| 42 | +| 🔗 | **Dependencies** | Leverages various libraries such as `fastapi`, `uvicorn`, `pydantic`, `openai`, `sqlalchemy`, `psycopg2-binary`, `alembic`, `pyjwt`, `requests`, `logging`, and `prometheus_client` for essential functionalities. | |
| 43 | +| 🧩 | **Modularity** | The codebase is organized into modules for user management, API interaction, data validation, database interactions, and utility functions, promoting code reusability and maintainability. | |
| 44 | +| 🧪 | **Testing** | Includes a comprehensive testing framework, including unit tests, integration tests, and end-to-end tests. This ensures the quality, stability, and reliability of the codebase. | |
| 45 | +| ⚡️ | **Performance** | Employs optimization techniques such as caching API responses, optimizing database queries, and asynchronous processing to ensure efficient operation. | |
| 46 | +| 🔐 | **Security** | Implements security measures to protect user data and API keys, including secure storage of API keys, data encryption, and rate limiting. | |
| 47 | +| 🔀 | **Version Control**| Uses Git for version control and employs a Gitflow branching model for a structured and collaborative development process. | |
| 48 | +| 🔌 | **Integrations** | Integrates with popular cloud platforms like AWS or Azure for hosting the database and server infrastructure. | |
| 49 | +| 📶 | **Scalability** | Designed for scalability, leveraging cloud-based solutions for automatic scaling and resource management. | |
| 50 | + |
| 51 | +## 📂 Structure |
| 52 | + |
| 53 | +```text |
| 54 | +openai-api-client/ |
| 55 | +├── api |
| 56 | +│ ├── routes |
| 57 | +│ │ ├── user.py |
| 58 | +│ │ └── openai.py |
| 59 | +│ └── schemas |
| 60 | +│ ├── user.py |
| 61 | +│ └── openai.py |
| 62 | +├── dependencies |
| 63 | +│ ├── auth.py |
| 64 | +│ ├── database.py |
| 65 | +│ ├── openai.py |
| 66 | +│ └── utils.py |
| 67 | +├── models |
| 68 | +│ ├── base.py |
| 69 | +│ ├── user.py |
| 70 | +│ └── api_usage.py |
| 71 | +├── services |
| 72 | +│ ├── user.py |
| 73 | +│ └── openai.py |
| 74 | +├── startup.sh |
| 75 | +├── commands.json |
| 76 | +├── tests |
| 77 | +│ ├── conftest.py |
| 78 | +│ ├── unit |
| 79 | +│ │ ├── test_openai.py |
| 80 | +│ │ └── test_user.py |
| 81 | +│ └── integration |
| 82 | +│ ├── test_openai_routes.py |
| 83 | +│ └── test_user_routes.py |
| 84 | +├── migrations |
| 85 | +│ └── versions |
| 86 | +│ └── ... |
| 87 | +│ └── ... |
| 88 | +│ └── alembic_version.py |
| 89 | +├── README.md |
| 90 | +├── .env.example |
| 91 | +├── .env |
| 92 | +├── gunicorn.conf.py |
| 93 | +├── Procfile |
| 94 | +├── .gitignore |
| 95 | +└── .flake8 |
| 96 | +``` |
| 97 | + |
| 98 | +## 💻 Installation |
| 99 | + |
| 100 | +### 🔧 Prerequisites |
| 101 | + |
| 102 | +- Python 3.9 or higher |
| 103 | +- PostgreSQL 14+ |
| 104 | +- `pip` (Python package manager) |
| 105 | +- `alembic` (Database migration tool) |
| 106 | + |
| 107 | +### 🚀 Setup Instructions |
| 108 | + |
| 109 | +1. **Clone the repository:** |
| 110 | + |
| 111 | + ```bash |
| 112 | + git clone https://github.com/coslynx/OpenAI-API-Python-Client.git |
| 113 | + cd OpenAI-API-Python-Client |
| 114 | + ``` |
| 115 | + |
| 116 | +2. **Install dependencies:** |
| 117 | + |
| 118 | + ```bash |
| 119 | + pip install -r requirements.txt |
| 120 | + ``` |
| 121 | + |
| 122 | +3. **Set up the database:** |
| 123 | + |
| 124 | + - Create a PostgreSQL database and user if you don't already have one. |
| 125 | + - Update the `DATABASE_URL` in your `.env` file with your database connection string. |
| 126 | + - Run database migrations: |
| 127 | + |
| 128 | + ```bash |
| 129 | + alembic upgrade head |
| 130 | + ``` |
| 131 | + |
| 132 | +4. **Configure environment variables:** |
| 133 | + |
| 134 | + - Create a `.env` file based on the `.env.example` file. |
| 135 | + - Replace placeholder values with your actual API keys and database credentials. |
| 136 | + |
| 137 | +## 🏗️ Usage |
| 138 | + |
| 139 | +### 🏃♂️ Running the MVP |
| 140 | + |
| 141 | +1. **Start the application server:** |
| 142 | + |
| 143 | + ```bash |
| 144 | + uvicorn main:app --host 0.0.0.0 --port 8000 |
| 145 | + ``` |
| 146 | + |
| 147 | +### ⚙️ Configuration |
| 148 | + |
| 149 | +- **`.env` file:** Contains environment variables like `OPENAI_API_KEY`, `DATABASE_URL`, and `SECRET_KEY`. |
| 150 | +- **`gunicorn.conf.py`:** Configures the `gunicorn` web server for deployment. |
| 151 | + |
| 152 | +### 📚 Examples |
| 153 | + |
| 154 | +**User Registration:** |
| 155 | + |
| 156 | +```bash |
| 157 | +curl -X POST http://localhost:8000/api/v1/users/register \ |
| 158 | + -H "Content-Type: application/json" \ |
| 159 | + -d '{"username": "your_username", "email": "your_email@example.com", "password": "your_password"}' |
| 160 | +``` |
| 161 | + |
| 162 | +**User Login:** |
| 163 | + |
| 164 | +```bash |
| 165 | +curl -X POST http://localhost:8000/api/v1/users/login \ |
| 166 | + -H "Content-Type: application/json" \ |
| 167 | + -d '{"email": "your_email@example.com", "password": "your_password"}' |
| 168 | +``` |
| 169 | + |
| 170 | +**Text Completion:** |
| 171 | + |
| 172 | +```bash |
| 173 | +curl -X POST http://localhost:8000/api/v1/openai/complete \ |
| 174 | + -H "Content-Type: application/json" \ |
| 175 | + -H "Authorization: Bearer your_access_token" \ |
| 176 | + -d '{"text": "The quick brown fox jumps over the", "model": "text-davinci-003", "temperature": 0.7, "max_tokens": 256}' |
| 177 | +``` |
| 178 | + |
| 179 | +## 🌐 Hosting |
| 180 | + |
| 181 | +### 🚀 Deployment Instructions |
| 182 | + |
| 183 | +**Deploying to Heroku:** |
| 184 | + |
| 185 | +1. **Install the Heroku CLI:** |
| 186 | + |
| 187 | + ```bash |
| 188 | + pip install -g heroku |
| 189 | + ``` |
| 190 | + |
| 191 | +2. **Log in to Heroku:** |
| 192 | + |
| 193 | + ```bash |
| 194 | + heroku login |
| 195 | + ``` |
| 196 | + |
| 197 | +3. **Create a new Heroku app:** |
| 198 | + |
| 199 | + ```bash |
| 200 | + heroku create openai-api-python-client-production |
| 201 | + ``` |
| 202 | + |
| 203 | +4. **Set up environment variables:** |
| 204 | + |
| 205 | + ```bash |
| 206 | + heroku config:set OPENAI_API_KEY=your_openai_api_key |
| 207 | + heroku config:set DATABASE_URL=your_database_url |
| 208 | + heroku config:set SECRET_KEY=your_secret_key |
| 209 | + ``` |
| 210 | + |
| 211 | +5. **Deploy the code:** |
| 212 | + |
| 213 | + ```bash |
| 214 | + git push heroku main |
| 215 | + ``` |
| 216 | + |
| 217 | +6. **Run database migrations:** |
| 218 | + |
| 219 | + ```bash |
| 220 | + heroku run alembic upgrade head |
| 221 | + ``` |
| 222 | + |
| 223 | +### 🔑 Environment Variables |
| 224 | + |
| 225 | +- `OPENAI_API_KEY`: Your OpenAI API key. |
| 226 | +- `DATABASE_URL`: Your PostgreSQL database connection string. |
| 227 | +- `SECRET_KEY`: A secret key for JWT authentication. |
| 228 | + |
| 229 | +## 📜 API Documentation |
| 230 | + |
| 231 | +### 🔍 Endpoints |
| 232 | + |
| 233 | +- **POST `/api/v1/users/register`:** Register a new user. |
| 234 | + - **Request Body:** |
| 235 | + |
| 236 | + ```json |
| 237 | + { |
| 238 | + "username": "your_username", |
| 239 | + "email": "your_email@example.com", |
| 240 | + "password": "your_password" |
| 241 | + } |
| 242 | + ``` |
| 243 | + |
| 244 | + - **Response Body:** |
| 245 | + |
| 246 | + ```json |
| 247 | + { |
| 248 | + "id": 1, |
| 249 | + "username": "your_username", |
| 250 | + "email": "your_email@example.com", |
| 251 | + "api_key": "your_api_key" |
| 252 | + } |
| 253 | + ``` |
| 254 | + |
| 255 | +- **POST `/api/v1/users/login`:** Login an existing user and obtain an access token. |
| 256 | + - **Request Body:** |
| 257 | + |
| 258 | + ```json |
| 259 | + { |
| 260 | + "email": "your_email@example.com", |
| 261 | + "password": "your_password" |
| 262 | + } |
| 263 | + ``` |
| 264 | + |
| 265 | + - **Response Body:** |
| 266 | + |
| 267 | + ```json |
| 268 | + { |
| 269 | + "access_token": "your_access_token", |
| 270 | + "token_type": "bearer" |
| 271 | + } |
| 272 | + ``` |
| 273 | + |
| 274 | +- **GET `/api/v1/users/me`:** Get the current user's information. |
| 275 | + - **Authorization:** Bearer your_access_token |
| 276 | + - **Response Body:** |
| 277 | +
|
| 278 | + ```json |
| 279 | + { |
| 280 | + "id": 1, |
| 281 | + "username": "your_username", |
| 282 | + "email": "your_email@example.com", |
| 283 | + "api_key": "your_api_key" |
| 284 | + } |
| 285 | + ``` |
| 286 | +
|
| 287 | +- **POST `/api/v1/openai/complete`:** Complete a given text using OpenAI's text completion API. |
| 288 | + - **Authorization:** Bearer your_access_token |
| 289 | + - **Request Body:** |
| 290 | + |
| 291 | + ```json |
| 292 | + { |
| 293 | + "text": "The quick brown fox jumps over the", |
| 294 | + "model": "text-davinci-003", |
| 295 | + "temperature": 0.7, |
| 296 | + "max_tokens": 256 |
| 297 | + } |
| 298 | + ``` |
| 299 | + |
| 300 | + - **Response Body:** |
| 301 | + |
| 302 | + ```json |
| 303 | + { |
| 304 | + "response": "lazy dog." |
| 305 | + } |
| 306 | + ``` |
| 307 | + |
| 308 | +- **POST `/api/v1/openai/translate`:** Translate a given text using OpenAI's translation API. |
| 309 | + - **Authorization:** Bearer your_access_token |
| 310 | + - **Request Body:** |
| 311 | +
|
| 312 | + ```json |
| 313 | + { |
| 314 | + "text": "Hello world", |
| 315 | + "source_language": "en", |
| 316 | + "target_language": "fr" |
| 317 | + } |
| 318 | + ``` |
| 319 | +
|
| 320 | + - **Response Body:** |
| 321 | +
|
| 322 | + ```json |
| 323 | + { |
| 324 | + "response": "Bonjour le monde" |
| 325 | + } |
| 326 | + ``` |
| 327 | +
|
| 328 | +- **POST `/api/v1/openai/summarize`:** Summarize a given text using OpenAI's summarization API. |
| 329 | + - **Authorization:** Bearer your_access_token |
| 330 | + - **Request Body:** |
| 331 | + |
| 332 | + ```json |
| 333 | + { |
| 334 | + "text": "The quick brown fox jumps over the lazy dog.", |
| 335 | + "model": "text-davinci-003" |
| 336 | + } |
| 337 | + ``` |
| 338 | + |
| 339 | + - **Response Body:** |
| 340 | + |
| 341 | + ```json |
| 342 | + { |
| 343 | + "response": "A brown fox jumps over a lazy dog." |
| 344 | + } |
| 345 | + ``` |
| 346 | + |
| 347 | +### 🔒 Authentication |
| 348 | + |
| 349 | +- Register a new user or login to receive a JWT access token. |
| 350 | +- Include the access token in the `Authorization` header for all protected routes using the format: `Authorization: Bearer your_access_token` |
| 351 | + |
| 352 | +## 📜 License & Attribution |
| 353 | + |
| 354 | +### 📄 License |
| 355 | + |
| 356 | +This Minimum Viable Product (MVP) is licensed under the [GNU AGPLv3](https://choosealicense.com/licenses/agpl-3.0/) license. |
| 357 | + |
| 358 | +### 🤖 AI-Generated MVP |
| 359 | + |
| 360 | +This MVP was entirely generated using artificial intelligence through [CosLynx.com](https://coslynx.com). |
| 361 | + |
| 362 | +No human was directly involved in the coding process of the repository: OpenAI-API-Python-Client |
| 363 | + |
| 364 | +### 📞 Contact |
| 365 | + |
| 366 | +For any questions or concerns regarding this AI-generated MVP, please contact CosLynx at: |
| 367 | +- Website: [CosLynx.com](https://coslynx.com) |
| 368 | +- Twitter: [@CosLynxAI](https://x.com/CosLynxAI) |
| 369 | + |
| 370 | +<p align="center"> |
| 371 | + <h1 align="center">🌐 CosLynx.com</h1> |
| 372 | +</p> |
| 373 | +<p align="center"> |
| 374 | + <em>Create Your Custom MVP in Minutes With CosLynxAI!</em> |
| 375 | +</p> |
| 376 | +<div class="badges" align="center"> |
| 377 | + <img src="https://img.shields.io/badge/Developers-Drix10,_Kais_Radwan-red" alt=""> |
| 378 | + <img src="https://img.shields.io/badge/Website-CosLynx.com-blue" alt=""> |
| 379 | + <img src="https://img.shields.io/badge/Backed_by-Google,_Microsoft_&_Amazon_for_Startups-red" alt=""> |
| 380 | + <img src="https://img.shields.io/badge/Finalist-Backdrop_Build_v4,_v6-black" alt=""> |
| 381 | +</div> |
0 commit comments