Skip to content

AI-powered client for OpenAI API to build NLP applications... Created at https://coslynx.com

Notifications You must be signed in to change notification settings

coslynx/OpenAI-API-Python-Client

Repository files navigation

OpenAI-API-Python-Client

A Python backend API wrapper that simplifies integration of OpenAI's powerful NLP capabilities.

Developed with the software and tools below.

Programming Language Web Framework Database API Integration
git-last-commit GitHub commit activity GitHub top language

πŸ“‘ Table of Contents

  • πŸ“ Overview
  • πŸ“¦ Features
  • πŸ“‚ Structure
  • πŸ’» Installation
  • πŸ—οΈ Usage
  • 🌐 Hosting
  • πŸ“„ License
  • πŸ‘ Authors

πŸ“ Overview

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.

πŸ“¦ Features

Feature Description
βš™οΈ Architecture Utilizes a microservices architecture, with the API wrapper running as a standalone service. This provides flexibility and allows for independent scaling of components.
πŸ“„ 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.
πŸ”— Dependencies Leverages various libraries such as fastapi, uvicorn, pydantic, openai, sqlalchemy, psycopg2-binary, alembic, pyjwt, requests, logging, and prometheus_client for essential functionalities.
🧩 Modularity The codebase is organized into modules for user management, API interaction, data validation, database interactions, and utility functions, promoting code reusability and maintainability.
πŸ§ͺ 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.
⚑️ Performance Employs optimization techniques such as caching API responses, optimizing database queries, and asynchronous processing to ensure efficient operation.
πŸ” Security Implements security measures to protect user data and API keys, including secure storage of API keys, data encryption, and rate limiting.
πŸ”€ Version Control Uses Git for version control and employs a Gitflow branching model for a structured and collaborative development process.
πŸ”Œ Integrations Integrates with popular cloud platforms like AWS or Azure for hosting the database and server infrastructure.
πŸ“Ά Scalability Designed for scalability, leveraging cloud-based solutions for automatic scaling and resource management.

πŸ“‚ Structure

openai-api-client/
β”œβ”€β”€ api
β”‚   β”œβ”€β”€ routes
β”‚   β”‚   β”œβ”€β”€ user.py
β”‚   β”‚   └── openai.py
β”‚   └── schemas
β”‚       β”œβ”€β”€ user.py
β”‚       └── openai.py
β”œβ”€β”€ dependencies
β”‚   β”œβ”€β”€ auth.py
β”‚   β”œβ”€β”€ database.py
β”‚   β”œβ”€β”€ openai.py
β”‚   └── utils.py
β”œβ”€β”€ models
β”‚   β”œβ”€β”€ base.py
β”‚   β”œβ”€β”€ user.py
β”‚   └── api_usage.py
β”œβ”€β”€ services
β”‚   β”œβ”€β”€ user.py
β”‚   └── openai.py
β”œβ”€β”€ startup.sh
β”œβ”€β”€ commands.json
β”œβ”€β”€ tests
β”‚   β”œβ”€β”€ conftest.py
β”‚   β”œβ”€β”€ unit
β”‚   β”‚   β”œβ”€β”€ test_openai.py
β”‚   β”‚   └── test_user.py
β”‚   └── integration
β”‚       β”œβ”€β”€ test_openai_routes.py
β”‚       └── test_user_routes.py
β”œβ”€β”€ migrations
β”‚   └── versions
β”‚       └── ...
β”‚           └── ...
β”‚               └── alembic_version.py
β”œβ”€β”€ README.md
β”œβ”€β”€ .env.example
β”œβ”€β”€ .env
β”œβ”€β”€ gunicorn.conf.py
β”œβ”€β”€ Procfile
β”œβ”€β”€ .gitignore
└── .flake8

πŸ’» Installation

πŸ”§ Prerequisites

  • Python 3.9 or higher
  • PostgreSQL 14+
  • pip (Python package manager)
  • alembic (Database migration tool)

πŸš€ Setup Instructions

  1. Clone the repository:

    git clone https://github.com/coslynx/OpenAI-API-Python-Client.git
    cd OpenAI-API-Python-Client
  2. Install dependencies:

    pip install -r requirements.txt
  3. Set up the database:

    • Create a PostgreSQL database and user if you don't already have one.
    • Update the DATABASE_URL in your .env file with your database connection string.
    • Run database migrations:
    alembic upgrade head
  4. Configure environment variables:

    • Create a .env file based on the .env.example file.
    • Replace placeholder values with your actual API keys and database credentials.

πŸ—οΈ Usage

πŸƒβ€β™‚οΈ Running the MVP

  1. Start the application server:

    uvicorn main:app --host 0.0.0.0 --port 8000

βš™οΈ Configuration

  • .env file: Contains environment variables like OPENAI_API_KEY, DATABASE_URL, and SECRET_KEY.
  • gunicorn.conf.py: Configures the gunicorn web server for deployment.

πŸ“š Examples

User Registration:

curl -X POST http://localhost:8000/api/v1/users/register \
     -H "Content-Type: application/json" \
     -d '{"username": "your_username", "email": "your_email@example.com", "password": "your_password"}'

User Login:

curl -X POST http://localhost:8000/api/v1/users/login \
     -H "Content-Type: application/json" \
     -d '{"email": "your_email@example.com", "password": "your_password"}'

Text Completion:

curl -X POST http://localhost:8000/api/v1/openai/complete \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer your_access_token" \
     -d '{"text": "The quick brown fox jumps over the", "model": "text-davinci-003", "temperature": 0.7, "max_tokens": 256}'

🌐 Hosting

πŸš€ Deployment Instructions

Deploying to Heroku:

  1. Install the Heroku CLI:

    pip install -g heroku
  2. Log in to Heroku:

    heroku login
  3. Create a new Heroku app:

    heroku create openai-api-python-client-production
  4. Set up environment variables:

    heroku config:set OPENAI_API_KEY=your_openai_api_key
    heroku config:set DATABASE_URL=your_database_url
    heroku config:set SECRET_KEY=your_secret_key
  5. Deploy the code:

    git push heroku main
  6. Run database migrations:

    heroku run alembic upgrade head

πŸ”‘ Environment Variables

  • OPENAI_API_KEY: Your OpenAI API key.
  • DATABASE_URL: Your PostgreSQL database connection string.
  • SECRET_KEY: A secret key for JWT authentication.

πŸ“œ API Documentation

πŸ” Endpoints

  • POST /api/v1/users/register: Register a new user.

    • Request Body:

      {
        "username": "your_username",
        "email": "your_email@example.com",
        "password": "your_password"
      }
    • Response Body:

      {
        "id": 1,
        "username": "your_username",
        "email": "your_email@example.com",
        "api_key": "your_api_key"
      }
  • POST /api/v1/users/login: Login an existing user and obtain an access token.

    • Request Body:

      {
        "email": "your_email@example.com",
        "password": "your_password"
      }
    • Response Body:

      {
        "access_token": "your_access_token",
        "token_type": "bearer"
      }
  • GET /api/v1/users/me: Get the current user's information.

    • Authorization: Bearer your_access_token

    • Response Body:

      {
        "id": 1,
        "username": "your_username",
        "email": "your_email@example.com",
        "api_key": "your_api_key"
      }
  • POST /api/v1/openai/complete: Complete a given text using OpenAI's text completion API.

    • Authorization: Bearer your_access_token

    • Request Body:

      {
        "text": "The quick brown fox jumps over the",
        "model": "text-davinci-003",
        "temperature": 0.7,
        "max_tokens": 256
      }
    • Response Body:

      {
        "response": "lazy dog."
      }
  • POST /api/v1/openai/translate: Translate a given text using OpenAI's translation API.

    • Authorization: Bearer your_access_token

    • Request Body:

      {
        "text": "Hello world",
        "source_language": "en",
        "target_language": "fr"
      }
    • Response Body:

      {
        "response": "Bonjour le monde"
      }
  • POST /api/v1/openai/summarize: Summarize a given text using OpenAI's summarization API.

    • Authorization: Bearer your_access_token

    • Request Body:

      {
        "text": "The quick brown fox jumps over the lazy dog.",
        "model": "text-davinci-003"
      }
    • Response Body:

      {
        "response": "A brown fox jumps over a lazy dog."
      }

πŸ”’ Authentication

  • Register a new user or login to receive a JWT access token.
  • Include the access token in the Authorization header for all protected routes using the format: Authorization: Bearer your_access_token

πŸ“œ License & Attribution

πŸ“„ License

This Minimum Viable Product (MVP) is licensed under the GNU AGPLv3 license.

πŸ€– AI-Generated MVP

This MVP was entirely generated using artificial intelligence through CosLynx.com.

No human was directly involved in the coding process of the repository: OpenAI-API-Python-Client

πŸ“ž Contact

For any questions or concerns regarding this AI-generated MVP, please contact CosLynx at:

🌐 CosLynx.com

Create Your Custom MVP in Minutes With CosLynxAI!