cevheri's picture
docs: update readme
8f52d3f
|
raw
history blame
6.88 kB
metadata
title: OpenAI compatible Chatbot API
emoji: 🀯
colorFrom: pink
colorTo: yellow
sdk: docker
pinned: false
short_description: OpenAI compatible Chatbot API

OpenAI Compatible Chatbot API Template

A FastAPI based OpenAI compatible Chatbot API with Visualization.

πŸ”— Links

πŸš€ Features

  • OpenAI compatible API

    • /v1/chat/completions
    • /v1/chat/completions/{completion_id}
    • /v1/chat/completions/{completion_id}/messages
    • /v1/chat/completions/{completion_id}/messages/{message_id}/plots
    • /v1/conversation
    • /v1/conversation/{completion_id}
  • Complete mock implementation with USE_MOCK_API 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 : http://127.0.0.1:7860/ui

πŸ“‹ Endpoints

  • GET - /chat/completions list stored chat completions - for first load the chatbot
  • POST - /chat/completions create a new chat completion - when user starts a new chat
  • GET - /chat/completions/{completion_id} get a stored chat completion by completion_id - when user clicks on a chat on the list
  • POST - /chat/completions/{completion_id} modify a stored chat completion by completion_id - NOT IMPLEMENTED YET
  • DELETE - /chat/completions/{completion_id} delete a stored chat completion by completion_id - NOT IMPLEMENTED YET
  • GET - /chat/completions/{completion_id}/messages get the messages in a stored chat completion 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 completion_id and message_id

πŸ› οΈ Installation

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

./run.sh

# or

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

Usage

πŸ”‘ API Key Generation

./scripts/generate_api_key.sh <username> <secret_key>
  • This will generate a API_KEY and save it to the api_key.txt file.
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 API_KEY generation. It is used to generate and verify the API_KEY for the user.
  • API_KEY, If you want to use the Gradio UI, you can set the API_KEY in the .env file. GradioUI will use the API_KEY to make requests to the API. Especially POST/chat/completions endpoint.

πŸ”‘ API Key Authentication

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 openai_openapi_template 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
git clone https://github.com/yourname/openai-openapi-template.git
  • cd to the project directory
cd openai-openapi-template
  • Create a new branch
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
git status
git add .
git commit -m "Add new feature"
  • Push your changes to the branch
git push origin feature/new-feature
  • Merge from main branch to your branch
git fetch origin main
git merge origin/main
  • Validate the code is running on your local machine
uv run uvicorn app:app --reload
  • Format and lint the code
ruff check --fix
ruff format
  • Everything is working, create a pull request
git push origin feature/new-feature
  • Create a pull request
gh pr create --base main --head feature/new-feature --title "Add new feature" --body "This PR adds a new feature to the project"

Open Issue

Mock Implementation:

  • Implement Mock response for all endpoints
  • Implement API-Key Authentication and validation in all endpoints

Production Implementation:

  • POST chat/completions - create a new chat completion
  • GET chat/completions - list stored chat completions
  • GET chat/completions/{completion_id} - get a stored chat completion by id
  • GET chat/completions/{completion_id}/messages - get the messages in a stored chat completion by id
  • GET chat/completions/{completion_id}/messages/{message_id}/plots - get the plots/graph-data/figure-json in a stored chat completion by id and message id
  • GET conversation - get all conversations
  • GET conversation/{completion_id} - get a conversation by completion_id