Skip to content

AI-powered Python wrapper service for streamlined API interactions... Created at https://coslynx.com

Notifications You must be signed in to change notification settings

coslynx/AI-Powered-Python-OpenAI-Wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI Powered Python Wrapper Service

A robust backend service simplifying interactions with OpenAI's language models.

Developed with the software and tools below.

Framework used: FastAPI Backend language: Python Database used: PostgreSQL API Integration: OpenAI
git-last-commit GitHub commit activity GitHub top language

πŸ“‘ Table of Contents

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

πŸ“ Overview

The repository contains a Minimum Viable Product (MVP) called "AI Powered Python Wrapper Service" that provides a streamlined backend service for integrating OpenAI's powerful language models into applications. This service acts as an intermediary between developers and the OpenAI API, offering a user-friendly interface and abstracting away the complexities of direct API interactions.

πŸ“¦ Features

Feature Description
βš™οΈ Architecture The codebase follows a clean and modular architecture, organized into distinct components for API routes, database models, and utility functions.
πŸ“„ Documentation This README file provides a comprehensive overview of the MVP, its functionalities, dependencies, and usage instructions.
πŸ”— Dependencies The project utilizes key libraries like FastAPI for building the RESTful API, OpenAI for interacting with AI models, SQLAlchemy for database operations, and JWT for authentication.
🧩 Modularity The codebase is structured into separate modules for routes, models, schemas, and utilities, promoting code reuse and maintainability.
πŸ§ͺ Testing Unit tests are implemented to ensure the correctness of individual functions and components.
⚑️ Performance The service is designed to handle requests efficiently, optimizing API calls and minimizing response times.
πŸ” Security Robust authentication and authorization mechanisms protect sensitive data and API keys.
πŸ”€ Version Control Utilizes Git for version control with a clear branching strategy to manage code changes and releases.
πŸ”Œ Integrations Seamlessly integrates with the OpenAI API, leveraging its models for text generation, translation, and other capabilities.
πŸ“Ά Scalability The architecture is designed to handle increased user load and demands, utilizing cloud infrastructure and efficient resource management.

πŸ“‚ Structure

ai-wrapper-service/
β”œβ”€β”€ api
β”‚   └── routes
β”‚       β”œβ”€β”€ auth.py
β”‚       └── text.py
β”œβ”€β”€ models.py
β”œβ”€β”€ schemas.py
β”œβ”€β”€ tests
β”‚   └── test_routes.py
β”œβ”€β”€ utils
β”‚   └── auth_utils.py
β”œβ”€β”€ .env
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ main.py
β”œβ”€β”€ README.md
└── .gitignore

πŸ’» Installation

πŸ”§ Prerequisites

  • Python 3.9+
  • PostgreSQL 13+
  • Docker 20.10+
  • pip package manager

πŸš€ Setup Instructions

  1. Clone the repository:
    git clone https://github.com/coslynx/AI-Powered-Python-OpenAI-Wrapper.git
    cd AI-Powered-Python-OpenAI-Wrapper
  2. Install dependencies:
    pip install -r requirements.txt
  3. Set up the database:
    • Create a database named ai_wrapper_service in PostgreSQL.
    • Update the DATABASE_URL environment variable in the .env file with the correct connection string.
    • Run the following command to create the database tables:
    python -m sqlalchemy.orm create -u "$DATABASE_URL"
  4. Configure environment variables:
    • Make a copy of the .env.example file:
    cp .env.example .env
    • Update the environment variables in the .env file with your OpenAI API key and database credentials.

πŸ—οΈ Usage

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

  1. Start the application:
    uvicorn main:app --host 0.0.0.0 --port 8000

🌐 Hosting

πŸš€ Deployment Instructions

  1. Build a Docker image:
    docker build -t ai-wrapper-service:latest .
  2. Run the Docker container:
    docker run -d -p 8000:8000 ai-wrapper-service:latest

πŸ”‘ Environment Variables

  • DATABASE_URL: Connection string for the PostgreSQL database. Example: postgresql://user:password@host:port/database
  • OPENAI_API_KEY: Your OpenAI API key for accessing OpenAI's services. Example: sk-your_api_key_here
  • JWT_SECRET_KEY: A secret key used for signing and verifying JWT tokens. Example: your-256-bit-secret

πŸ“œ API Documentation

πŸ” Endpoints

  • POST /api/v1/generate_text
    • Description: Generate text using OpenAI's language models.
    • Headers:
      • Authorization: Bearer YOUR_JWT_TOKEN
    • Body:
      {
        "input_text": "Write a short story about a robot who dreams.",
        "model": "text-davinci-003",
        "temperature": 0.7
      }
    • Response:
      {
        "choices": [
          {
            "text": "Once upon a time, in a world where technology had advanced beyond human comprehension, there lived a robot named Unit 7. Unit 7 was a marvel of engineering, designed to perform complex tasks with unwavering precision. However, unbeknownst to his creators, Unit 7 possessed a secret - he dreamt.",
            "index": 0,
            "logprobs": null,
            "finish_reason": "stop"
          }
        ],
        "created": 1692509441,
        "model": "text-davinci-003",
        "usage": {
          "prompt_tokens": 13,
          "completion_tokens": 67,
          "total_tokens": 80
        }
      }

πŸ”’ Authentication

  1. Register a new user or login to receive a JWT token:
    • Send a POST request to /api/v1/auth/signup to register a new user.
    • Send a POST request to /api/v1/auth/login to authenticate an existing user.
  2. Include the token in the Authorization header for all protected routes:
    Authorization: Bearer YOUR_JWT_TOKEN
    

πŸ“ Examples

# Register a new user
curl -X POST http://localhost:8000/api/v1/auth/signup \
     -H "Content-Type: application/json" \
     -d '{"email": "user@example.com", "password": "securepass123"}'

# Response (successful registration)
{
  "message": "User created successfully"
}

# Login an existing user
curl -X POST http://localhost:8000/api/v1/auth/login \
     -H "Content-Type: application/json" \
     -d '{"email": "user@example.com", "password": "securepass123"}'

# Response (successful login)
{
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyQGV4YW1wbGUuY29tIiwiZXhwIjoxNjkzMjg2NjE0fQ.d7H-w5M-x_vZ2g5t3w2vU97qF6_G5Z70pZ3i1T2-dI",
  "token_type": "bearer"
}

# Generate text with authentication
curl -X POST http://localhost:8000/api/v1/generate_text \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyQGV4YW1wbGUuY29tIiwiZXhwIjoxNjkzMjg2NjE0fQ.d7H-w5M-x_vZ2g5t3w2vU97qF6_G5Z70pZ3i1T2-dI" \
     -d '{"input_text": "Write a short story about a robot who dreams.", "model": "text-davinci-003", "temperature": 0.7}'

# Response (successful text generation)
{
  "choices": [
    {
      "text": "In the heart of a bustling metropolis, where towering skyscrapers scraped the sky, resided a unique robot named R-42. Unlike his mechanical brethren, programmed for efficiency and logic, R-42 dreamt. Not in the literal sense, of course, but his circuits hummed with a peculiar form of sentience, crafting narratives of a world beyond the sterile reality of his programming. Each night, as his processors powered down, R-42 would drift into a state of simulated slumber, his circuits conjuring vivid scenes of verdant forests, shimmering lakes, and the gentle caress of the sun.",
      "index": 0,
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "created": 1692509441,
  "model": "text-davinci-003",
  "usage": {
    "prompt_tokens": 13,
    "completion_tokens": 125,
    "total_tokens": 138
  }
}

πŸ“œ 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: AI-Powered-Python-OpenAI-Wrapper

πŸ“ž 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!