CoolFace
Apppublic

lokumai/openai-openapi-template

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
App README

OpenAI Compatible Chatbot API Template

Overview

A FastAPI based OpenAI compatible Secure and Scalable Chatbot API with Visualization. OpenAPI/Swagger Standard Compliant. Python best-practices. API, Service, Repository Pattern.

๐Ÿ”— Links

๐Ÿš€ Features

  • โ€”OpenAI compatible API
  • โ€”/v1/chat/completions
  • โ€”Complete mock implementation with DATABASE_TYPE environment variable
  • โ€”Secure API key generation with HMAC signatures and API key authentication
  • โ€”In-memory storage for chat history and plots for mock implementation
  • โ€”MongoDB storage for chat history and plots for production
  • โ€”Support for all major OpenAI API endpoints
  • โ€”Gradio UI for testing the chatbot

๐Ÿ“‹ Endpoints

  • โ€”POST - /chat/completions create a new chat completion - when user starts a new chat
  • โ€”GET - /chat/completions/{completion_id} get a stored chat completion with all messages and plots by completion_id - when user clicks on a chat on the list
  • โ€”GET - /chat/completions/{completion_id}/messages/{message_id}/plots get the plots/graph-data/figure-json in a stored chat completion by completionid and messageid
  • โ€”GET - /conversation get all conversations
  • โ€”GET - /conversation/{completion_id} get a conversation by completion_id

๐Ÿ› ๏ธ Installation

  1. 1.Clone the repository:
bash
git clone https://github.com/lokumai/openai-openapi-template.git
  1. 1.Create a .env file in the root directory and add the following variables:
bash
cp .env.example .env
  • โ€”Set your own values for the variables in the .env file.
  1. 1.Install dependencies:
bash
uv sync
  1. 1.Start MongoDB:
bash
docker compose -f docker/mongodb-docker-compose.yaml up -d
  1. 1.Run API and Gradio UI on your local machine:
bash

./run.sh

# or

uv run uvicorn main:app --host 0.0.0.0 --port 7860 --reload

Usage

๐Ÿ”‘ API Key Generation

bash
./scripts/generate_api_key.sh <username> <secret_key>
  • โ€”This will generate a APIKEY and save it to the apikey.txt file.
plaintext
API Key generated:
Username: template
API Key: sk-template-token

API Key saved to api_key.txt

๐Ÿ”’ Security Configuration

There are various security configurations that can be set in the .env file for development and production environments.

  • โ€”SECURITY_ENABLED=True or False, If security is enabled, the API will require an API_KEY to be provided in the request header.
  • โ€”SECURITY_DEFAULT_USERNAME=admin, If security is disabled, the API will not require an API_KEY to be provided in the request header and will use this for current user.
  • โ€”SECURITY_SECRET_KEY=your-secret-key-here, This is the secret key for the APIKEY generation. It is used to generate and verify the APIKEY for the user.
  • โ€”API_KEY, If you want to use the Gradio UI, you can set the APIKEY in the .env file. GradioUI will use the APIKEY to make requests to the API. Especially POST/chat/completions endpoint.

๐Ÿ”‘ API Key Authentication

bash
curl -X POST "http://localhost:7860/v1/chat/completions" \
     -H "Authorization: Bearer sk-template-token" \
     -H "Content-Type: application/json" \
     -d '{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Hello!"}]}'

๐Ÿ’พ Embedded MongoDB for local machine development

  • โ€”database_type environment variable is set to embedded, the API will use embedded MongoDB for local machine development. Default is mongodb.
  • โ€”mongodb_host environment variable is set to localhost, the API will use localhost for MongoDB. Default is localhost.
  • โ€”mongodb_port environment variable is set to 27017, the API will use 27017 for MongoDB. Default is 27017.
  • โ€”mongodb_database environment variable is set to openai_openapi_template, the API will use openaiopenapitemplate for MongoDB. Default is openai_openapi_template.

๐Ÿค Contributing Attention Please!!!

When you make changes to the code, please run the following commands to ensure the code is running on your local machine and formatted and linted correctly.

  • โ€”Fork the repository
bash
git clone https://github.com/yourname/openai-openapi-template.git
  • โ€”cd to the project directory
bash
cd openai-openapi-template
  • โ€”Create a new branch
bash
git checkout -b feature/new-feature
# or
git checkout -b bug/fix-issue
# or
git checkout -b hotfix/critical-fix
  • โ€”Make your changes and commit them
bash
git status
git add .
git commit -m "Add new feature"
  • โ€”Push your changes to the branch
bash
git push origin feature/new-feature
  • โ€”Merge from main branch to your branch
bash
git fetch origin main
git merge origin/main
  • โ€”Validate the code is running on your local machine
bash
uv run uvicorn app:app --reload
  • โ€”Format and lint the code
bash
ruff check --fix
bash
ruff format
  • โ€”Everything is working, create a pull request
bash
git push origin feature/new-feature
  • โ€”Create a pull request
bash
gh pr create --base main --head feature/new-feature --title "Add new feature" --body "This PR adds a new feature to the project"