Skip to content

Chatbot with Llama 3.1 and Ollama Framework and DRF

Notifications You must be signed in to change notification settings

TRextabat/chatbot

Repository files navigation

chatbot

Chatbot with Llama 3.1 and Ollama Framework and DRF

Setup

  1. Clone the repository:
    git clone https://github.com/TRextabat/chatbot.git
    cd chatbot

Installing Dependencies

Make sure you have Docker and Docker Compose installed on your machine.

you should setup Nvidia Containers Toolkit for using GPU.

Example .env File

Create a .env file in the root of your project with the following content:

export DEBUG=1
export SECRET_KEY=your-secret-key
export DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
export SQL_ENGINE=django.db.backends.postgresql
export POSTGRES_USER=postgres
export POSTGRES_PASSWORD=chat123
export POSTGRES_HOST=postgres
export POSTGRES_PORT=5432
export MAX_FILE_UPLOAD_SIZE=20971520 # 20MB
export LLAMA_MODEL=llama-3.1-7b
export LLAMA_URL=http://llama:11434

Using Docker Compose

  1. Build and start the Docker containers:

    docker-compose up --build -d
  2. Apply database migrations:

    docker-compose exec api python manage.py migrate
  3. Create a superuser:

    docker-compose exec api python manage.py createsuperuser
  4. The application will be available at http://localhost:8000.

Data Modles

  • ChatSession: Represents a chat session with metadata and associated PDF documents.
  • ChatMassage: Represents individual messages within a chat session.
  • PDFDocument: Represents uploaded PDF documents.

Services

  • LlamaService: Handles the initialization, indexing, and querying of documents using the Llama-index library and the Llama model.

How It Works

  1. Upload PDF Documents: User can upload pdf to coudl use it in any session they want in a chat session or multipel chatsessions.
  2. Start a Chat Session: Users can start a new chat session by using multiple PDFs and providing metadata such as a session name. The PDF documents woudl be parsed, and their text content is indexed for efficient retrieval.
  3. Interact with the Chatbot: Users can send messages to the chatbot, which processes the messages using the Llama model and responds with relevant information extracted from the indexed documents.
  4. Activate/Deactivate Sessions: Chat sessions can be activated or deactivated, allowing for multiple active sessions at a time.

Using the Chatbot

  1. you could see swagger documentation at http://localhost:8000/swagger/

swagger

Note

check ports 8000 and 11434 and 5432 are free. before ruuning the project. for unix systems:

sudo lsof -i :port_number

when you will create your first chat session it will take a little bit long because the app would make modles in backend

Testing the RESTful APIs with curl

Upload a PDF Document

curl -X POST http://localhost:8000/pdf/ \
    -H "Content-Type: multipart/form-data" \
    -F "title=Test PDF" \
    -F "file=@/path/to/your/test.pdf"

Get PDF Documents

curl -X GET http://localhost:8000/pdf/ \
    -H "Content-Type: application/json"

Start a Chat Session

curl -X POST http://localhost:8000/chat/ \
    -H "Content-Type: application/json" \
    -d '{
        "title": "Chat Session Title",
        "description": "Chat Session Description",
        "pdf_documents": [uuid, uuid, uuid]
    }'

Get chat sessions

curl -X GET "http://localhost:8000/chat/?datetime=2023-01-01T00:00:00Z&number=10&history=true" \
    -H "Content-Type: application/json"

Activate ChatSession

curl -X PATCH http://localhost:8000/chat/activate/<uuid:session_id>/ \
    -H "Content-Type: application/json"

Deactivate ChatSession

curl -X PATCH http://localhost:8000/chat/deactivate/<uuid:session_id>/ \
    -H "Content-Type: application/json"

Interact with caht session

curl -X POST http://localhost:8000/chat/interact/<uuid:session_id>/ \
    -H "Content-Type: application/json" \
    -d '{
        "message_text": "Hello, chatbot!"
    }'

About

Chatbot with Llama 3.1 and Ollama Framework and DRF

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published