A lightweight Python backend that simplifies interaction with OpenAI's powerful language models for developers and users.
This repository contains the source code for the AI Response Wrapper, built as an MVP. This lightweight Python backend simplifies interaction with OpenAI's powerful language models for developers and users by reducing the complexity of API calls and response parsing, enabling efficient integration of AI-powered capabilities within applications and workflows.
Feature | Description |
---|---|
Request Handling | The wrapper receives user requests in the form of prompts and parameters, validates their syntax and format, and translates them into correctly structured API calls for OpenAI. |
API Communication | The wrapper securely communicates with OpenAI's API, sending validated requests and receiving responses. |
Response Processing | The wrapper receives raw responses from the OpenAI API, performs error handling, and formats the responses into a usable format. |
Error Handling | The wrapper implements a robust error handling system to gracefully handle potential issues during API communication or response processing. |
User Authentication | The wrapper implements a secure user authentication system using JWTs to control access to sensitive resources and manage user configurations. |
Database Integration | The wrapper stores user data and API logs securely in a PostgreSQL database for persistence, auditing, and analysis. |
The application follows a microservice architecture:
- Backend: Python server built with FastAPI handles requests and communication with OpenAI.
- Database: PostgreSQL database stores user information, API logs, and other relevant data.
- OpenAI: OpenAI's powerful language models are accessed through their API.
- Python 3.9+
- FastAPI
- Uvicorn
- SQLAlchemy
- Psycopg2-binary
- Pydantic
- OpenAI
- Requests
- JSON
- Argparse
- Logging
- PyJWT
- Python-dotenv
└── app
├── main.py
├── routers
│ ├── prompts
│ │ ├── __init__.py
│ │ ├── models.py
│ │ └── routes.py
│ └── responses
│ ├── __init__.py
│ ├── models.py
│ ├── routes.py
│ └── services.py
├── database
│ ├── __init__.py
│ ├── models.py
│ └── database.py
└── utils
├── __init__.py
├── auth.py
├── error_handler.py
├── logger.py
└── data_validation.py
- Prerequisites:
- Python 3.9+
- pip
- PostgreSQL
- OpenAI API key (sign up for an account and get your key from https://platform.openai.com/)
- Clone the repository:
git clone https://github.com/coslynx/AI-Response-Wrapper-MVP.git
- Navigate to the project directory:
cd AI-Response-Wrapper-MVP
- Install dependencies:
pip install -r requirements.txt
- Set up environment variables:
- Create a
.env
file in the root directory. - Add the following environment variables:
DATABASE_URL=postgresql://user:password@host:port/database_name OPENAI_API_KEY=your_openai_api_key SECRET_KEY=your_secret_key_here
- Create a
- Create the database:
python -m app.database.database create_all
- Start the application:
The application will be accessible at
python -m app.main
http://localhost:8000
.
- Access the API: Send requests to the server using tools like
curl
or a programming language like Python. - Authentication: Generate a JWT token using the
/auth/login
endpoint and include it in theAuthorization
header for subsequent requests. - Send requests: Use the
/prompts
and/responses
endpoints for prompt handling and response generation.
- Build the application:
- (If necessary for your hosting provider)
- Choose a hosting provider:
- Consider platforms like Heroku, AWS, or other cloud providers.
- Configure your hosting provider:
- Follow the specific instructions for your chosen hosting provider.
- Set up environment variables (including your database connection string and OpenAI API key).
- Deploy your application:
- Deploy the application to your chosen hosting provider.
Authentication:
- The API requires an OpenAI API key and user authentication for security.
- Set up the
OPENAI_API_KEY
andSECRET_KEY
environment variables before making API calls. - Generate a JWT token using the
/auth/login
endpoint. - Include the token in the
Authorization
header of all subsequent requests.
Endpoints:
-
/auth/login
- Method: POST
- Parameters:
username
: (string)password
: (string)
- Response:
- A JSON object containing the generated JWT token.
-
/prompts
- Method: POST
- Parameters:
text
: (string) Prompt text.model
: (string) OpenAI model name.parameters
: (optional, JSON) Model-specific parameters.
- Response:
- A JSON object containing the newly created prompt information.
-
/responses
- Method: POST
- Parameters:
prompt_id
: (int) The ID of the prompt.model
: (string) OpenAI model name.parameters
: (optional, JSON) Model-specific parameters.
- Response:
- A JSON object containing the generated response.
Example API Call:
curl -X POST http://localhost:8000/auth/login \
-H "Content-Type: application/json" \
-d '{"username": "your_username", "password": "your_password"}'
curl -X POST http://localhost:8000/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_jwt_token" \
-d '{"prompt_id": 1, "model": "text-davinci-003", "parameters": {"temperature": 0.7}}'
Example API Response:
{
"id": 1,
"text": "This is a test prompt.",
"model": "text-davinci-003",
"parameters": "{}",
"user_id": 1,
"responses": []
}
{
"id": 1,
"text": "This is the response to the test prompt.",
"model": "text-davinci-003",
"parameters": "{\"temperature\": 0.7}",
"generation_time": "2024-03-28T12:34:56.789Z",
"prompt_id": 1
}
Contributions are welcome! Please open an issue or submit a pull request with any suggestions, bug fixes, or improvements.
This project is licensed under the MIT License.
This MVP was entirely generated using artificial intelligence through CosLynx.com.
No human was directly involved in the coding process of the repository: AI-Response-Wrapper-MVP
### 📞 Contact
For any questions or concerns regarding this AI-generated MVP, please contact CosLynx at:
- Website: [CosLynx.com](https://coslynx.com)
- Twitter: [@CosLynxAI](https://x.com/CosLynxAI)
<p align="center">
<h1 align="center">🌐 CosLynx.com</h1>
</p>
<p align="center">
<em>Create Your Custom MVP in Minutes With CosLynxAI!</em>
</p>
<div class="badges" align="center">
<img src="https://img.shields.io/badge/Developers-Drix10,_Kais_Radwan-red" alt="">
<img src="https://img.shields.io/badge/Website-CosLynx.com-blue" alt="">
<img src="https://img.shields.io/badge/Backed_by-Google,_Microsoft_&_Amazon_for_Startups-red" alt="">
<img src="https://img.shields.io/badge/Finalist-Backdrop_Build_v4,_v6-black" alt="">
</div>