Skip to content

Commit

Permalink
Merge pull request #71 from knmlprz/setup-django-ninja
Browse files Browse the repository at this point in the history
instalation of django-ninja, model implementation and CRUD
  • Loading branch information
TheJimmyNowak authored Jan 13, 2024
2 parents 167f9c9 + 40d77d5 commit d4c9b6c
Show file tree
Hide file tree
Showing 17 changed files with 339 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
.envrc
models/**
db/
static/
.idea/

**/__pycache__/
1 change: 0 additions & 1 deletion api/.gitignore

This file was deleted.

6 changes: 4 additions & 2 deletions api/api/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pathlib import Path
import os
import environ

env = environ.Env()
Expand Down Expand Up @@ -28,6 +29,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"documents",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -67,7 +69,7 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": env("POSTGRES_NAME"),
"NAME": env("POSTGRES_DB"),
"USER": env("POSTGRES_USER"),
"PASSWORD": env("POSTGRES_PASSWORD"),
"HOST": env("POSTGRES_HOST"),
Expand Down Expand Up @@ -111,7 +113,7 @@
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = "static/"

STATIC_ROOT = os.path.join(BASE_DIR, "static/")
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

Expand Down
7 changes: 5 additions & 2 deletions api/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.urls import path, include
from django.conf.urls.static import static

urlpatterns = [
path("admin/", admin.site.urls),
]
path("documents/", include("documents.urls")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Empty file added api/documents/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions api/documents/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin
from .models import Document, Chunk

admin.site.register(Chunk)
admin.site.register(Document)
6 changes: 6 additions & 0 deletions api/documents/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class DocumentsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'documents'
36 changes: 36 additions & 0 deletions api/documents/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 4.2.7 on 2024-01-12 20:31

from django.db import migrations, models
import django.db.models.deletion
import pgvector.django


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Chunk',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.CharField(max_length=100)),
('embedding', pgvector.django.VectorField(dimensions=10)),
('chunk_idx', models.IntegerField()),
('start_char', models.IntegerField()),
('end_char', models.IntegerField()),
],
),
migrations.CreateModel(
name='Document',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.TextField()),
('embedding', pgvector.django.VectorField(dimensions=10)),
('chunks', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='documents.chunk')),
],
),
]
Empty file.
16 changes: 16 additions & 0 deletions api/documents/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.db import models
from pgvector.django import VectorField


class Chunk(models.Model):
text = models.CharField(max_length=100)
embedding = VectorField(dimensions=10)
chunk_idx = models.IntegerField()
start_char = models.IntegerField()
end_char = models.IntegerField()


class Document(models.Model):
text = models.TextField()
embedding = VectorField(dimensions=10)
chunks = models.ForeignKey(Chunk, on_delete=models.CASCADE, null=True, blank=True)
3 changes: 3 additions & 0 deletions api/documents/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
1 change: 1 addition & 0 deletions api/documents/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
urlpatterns = []
238 changes: 237 additions & 1 deletion api/poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ uvicorn = "^0.24.0.post1"
channels = "^4.0.0"
psycopg2-binary = "^2.9.9"
django-environ = "^0.11.2"
django-ninja = "^1.0.1"
pgvector = "^0.2.4"


[build-system]
Expand Down
5 changes: 4 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ services:

db:
profiles: ["dev","prod"]
image: postgres:16
build:
context: ./postgres
dockerfile: postgres.Dockerfile
volumes:
- ./db:/var/lib/postgresql/data
- ./postgres/vector_extension.sql:/docker-entrypoint-initdb.d/0-vector_extension.sql
env_file:
- .env

Expand Down
14 changes: 14 additions & 0 deletions postgres/postgres.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM postgres:16

RUN apt-get update && apt-get install -y \
build-essential \
git \
postgresql-server-dev-all \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /tmp
RUN git clone https://github.com/pgvector/pgvector.git

WORKDIR /tmp/pgvector
RUN make
RUN make install
2 changes: 2 additions & 0 deletions postgres/vector_extension.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Create the 'vector' extension within the database that is set in the docker-compose.yml
CREATE EXTENSION IF NOT EXISTS vector;

0 comments on commit d4c9b6c

Please sign in to comment.