Skip to content
/ ChatAI Public

A virtual assistant based on OpenAI's Assistant API

License

Notifications You must be signed in to change notification settings

dpasca/ChatAI

Repository files navigation

ChatAI

This is a chatbot based on OpenAI Completion API, implementing agents and function-calling for fact-checking.

It runs as a web app with Flask. It's been tested on DigitalOcean App platform and in a Droplet (server).

This application is mainly developed by Davide Pasca.
See commits for other contributors.

Features

  • AI chatbot based on OpenAI and Completion API
  • RAG based on LLamaindex and Chroma DB
  • Real-time fact-checking using agents
  • Sense of time and location using prompt injection
  • Web search using function-calling and Brave/DuckDuckGo
  • Code syntax highlighting and LaTeX rendering

Notes about Fact-Checking

The main assistant is currently not aware of the fact-checking.

This is a design choice to keep things simple in terms of user experience. Feedback from one AI to another is definitely important, but I'd rather implement (in the future) this kind of back-and-forth self-correction in a thread away from the user.

Fact-checking in web app runs in background, it takes some time to complete, but does not hang the interface.

Quick start

Environment variables

The web app requires the following environment variables. Set them in the .env file for local development and in the app settings for production:

Variable Description Required
OPENAI_API_KEY The API key for OpenAI (get it from the OpenAI dashboard) Yes
CHATAI_FLASK_SECRET_KEY A random string used by Flask to encrypt cookies
e.g. python -c "import os; print(os.urandom(24).hex())"
Yes
BRAVE_API_KEY The API key for Brave (https://brave.com/search/api/)
If there is no Brave API key, the search will default to DuckDuckGo (unreliable)
Optional
DO_SPACES_ACCESS_KEY The access key for DigitalOcean Spaces Optional
DO_SPACES_SECRET_KEY The secret key for DigitalOcean Spaces Optional
DO_STORAGE_CONTAINER The name of the container in DigitalOcean Spaces
e.g. myai_spaces
Optional
DO_STORAGE_SERVER The URL of the DigitalOcean Spaces server
e.g. https://myai.sfo.digitaloceanspaces.com
Optional

See below on how to set the Digital Ocean storage variables (optional).

Spaces and containers (optional)

Create a DigitalOcean Space and a container for the app.

The storage container name and server URL need to be placed in the environment variables, both in the .env file for local development and in the app settings for production.

DO_SPACES_ACCESS_KEY, DO_SPACES_SECRET_KEY and DO_STORAGE_CONTAINER are from menu API -> Spaces Keys -> Generate New Key

Local development

It's suggested to use a virtual environment for Python.

  1. Install Flask
  2. Install the dependencies with pip install -r requirements.txt
  3. Create a .env file in the root directory with the following variables:
OPENAI_API_KEY=********
CHATAI_FLASK_SECRET_KEY=********
BRAVE_API_KEY=********
DO_SPACES_ACCESS_KEY=********
DO_SPACES_SECRET_KEY=********
DO_STORAGE_CONTAINER=********
DO_STORAGE_SERVER=https://********

Production

This is an example for DigitalOcean. Change the steps as needed if you'll be using a different provider, or your own server.

  1. Created a dedicated Project in DigitalOcean, if required.
  2. Create an App under the Project.
  3. The first step of the app creation will ask where to get the code from. You should select this GitHub repository.
  4. Set the required environment variables in the app (to update them later, go to Manage -> Apps -> Your app name -> Settings -> App-Level Environment Variables).
  5. Go to the app's Overview page, click on the app name link to go to Component Settings.
    • Source Directory: blank (default)
    • Run Command: cd app_web && python app.py

Usage

Local development

Run with either of the following commands:

  • (cd app_web && python app.py)
  • (cd app_web && flask --debug run --host=0.0.0.0 --port=8080)

The app will be available locally at http://127.0.0.1:8080.

Production

The app will be available globally at https://yourappname.ondigitalocean.app.

Knowledge files

This app implements a RAG system via LLamaIndex. See the directory knowledge for scripts on how to build this.

Optimization

A size optimization script is provided for PDF files for RAG.

This script assumes that images are not relevant.

  1. Install GhostScript (i.e. brew install ghostscript)
  2. Put your knowledge files (currently PDF format) into knowledge/src directory.
  3. cd knowledge
  4. ./optimize_pdfs.sh src _output
  5. Optimized files will be in knowledge/_output