Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a table on the database to store configuration #122

Merged
merged 14 commits into from
Oct 24, 2024
Merged
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,13 @@ help:
@python -c "${PRINT_HELP_PYSCRIPT}" < $(MAKEFILE_LIST)

api:
fastapi dev labs/api/main.py
fastapi dev labs/api/main.py

runserver:
poetry run python manage.py runserver

asgi_api:
poetry run uvicorn asgi_app:app --reload --port 8000

load_config:
python manage.py loaddata config.json
jpb18 marked this conversation as resolved.
Show resolved Hide resolved
28 changes: 28 additions & 0 deletions asgi_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import django
from fastapi import FastAPI
from django.core.asgi import get_asgi_application
from labs.api import codemonkey_endpoints, github_endpoints

# Set the DJANGO_SETTINGS_MODULE environment variable to point to your Django project's settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

# Initialize Django settings
django.setup()

# Load Django ASGI application
django_app = get_asgi_application()

# Create FastAPI app
fastapi_app = FastAPI()

# include codemonkey and github into fastapi
fastapi_app.include_router(codemonkey_endpoints.router)
fastapi_app.include_router(github_endpoints.router)

# ASGI application to route to Django or FastAPI
async def app(scope, receive, send):
if scope["type"] == "http" and scope["path"].startswith("/admin"):
await django_app(scope, receive, send)
else:
await fastapi_app(scope, receive, send)
File renamed without changes.
7 changes: 7 additions & 0 deletions config/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from .models import Config

@admin.register(Config)
class ConfigAdmin(admin.ModelAdmin):
list_display = ('llm_model', 'config_key', 'config_value', 'config_type', 'created_at', 'updated_at')
search_fields = (['config_key', 'llm_model'])
16 changes: 16 additions & 0 deletions config/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for config project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

application = get_asgi_application()
File renamed without changes.
50 changes: 50 additions & 0 deletions config/fixtures/config.json
jpb18 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"model": "config.config",
"pk": 1,
"fields": {
"llm_model": "gpt-3.5-turbo",
"config_key": "api_key",
"config_value": "your_api_key_here",
"config_type": "string",
"created_at": "2024-10-20T12:00:00Z",
"updated_at": "2024-10-20T12:00:00Z"
}
},
{
"model": "config.config",
"pk": 2,
"fields": {
"llm_model": "gpt-3.5-turbo",
"config_key": "endpoint",
"config_value": "model_endpoint_here",
"config_type": "string",
"created_at": "2024-10-20T12:00:00Z",
"updated_at": "2024-10-20T12:00:00Z"
}
},
{
"model": "config.config",
"pk": 3,
"fields": {
"llm_model": "gpt-3.5-turbo",
"config_key": "prompt",
"config_value": "prompt goes here",
"config_type": "string",
"created_at": "2024-10-20T12:00:00Z",
"updated_at": "2024-10-20T12:00:00Z"
}
},
{
"model": "config.config",
"pk": 4,
"fields": {
"llm_model": "gpt-3.5-turbo",
"config_key": "response_format",
"config_value": "response_format goes here",
"config_type": "string",
"created_at": "2024-10-20T12:00:00Z",
"updated_at": "2024-10-20T12:00:00Z"
}
}
]
File renamed without changes.
42 changes: 42 additions & 0 deletions config/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 5.1.2 on 2024-10-18 14:13

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Config",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("llm_model", models.TextField(blank=True, null=True)),
("config_key", models.TextField(unique=True)),
("config_value", models.TextField(blank=True, null=True)),
("config_type", models.TextField(blank=True, null=True)),
("created_at", models.DateTimeField(default=django.utils.timezone.now)),
("updated_at", models.DateTimeField(auto_now=True)),
],
options={
"indexes": [
models.Index(
fields=["config_key", "llm_model"],
name="config_conf_config__4d9363_idx",
)
],
},
),
]
Empty file added config/migrations/__init__.py
Empty file.
1 change: 1 addition & 0 deletions config/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .config import Config as Config
18 changes: 18 additions & 0 deletions config/models/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.db import models
from django.utils import timezone

class Config(models.Model):
llm_model = models.TextField(blank=True, null=True)
config_key = models.TextField(unique=True)
config_value = models.TextField(blank=True, null=True)
config_type = models.TextField(blank=True, null=True)
jpb18 marked this conversation as resolved.
Show resolved Hide resolved
created_at = models.DateTimeField(default=timezone.now)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
return f"{self.config_key}: {self.config_value}"

class Meta:
indexes = [
models.Index(fields=['config_key', 'llm_model'])
]
127 changes: 127 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""
Django settings for config project.

Generated by 'django-admin startproject' using Django 5.1.2.

For more information on this file, see
https://docs.djangoproject.com/en/5.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.1/ref/settings/
"""
import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-hxak7+phh51^p%vuxxk5%28b4tz^%e1m=6@q*tmf1eq-0$32_3"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
jpb18 marked this conversation as resolved.
Show resolved Hide resolved

ALLOWED_HOSTS = []
jpb18 marked this conversation as resolved.
Show resolved Hide resolved


# Application definition

INSTALLED_APPS = [
"config",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "config.urls"

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]

WSGI_APPLICATION = "config.wsgi.application"


# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.environ.get("DATABASE_NAME"),
"USER": os.environ.get("DATABASE_USER"),
"PASSWORD": os.environ.get("DATABASE_PASS"),
"HOST": os.environ.get("DATABASE_HOST"),
"PORT": os.environ.get("DATABASE_PORT"),
jpb18 marked this conversation as resolved.
Show resolved Hide resolved
}
}


# Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]


# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
22 changes: 22 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
URL configuration for config project.

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/5.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
path("admin/", admin.site.urls),
]
16 changes: 16 additions & 0 deletions config/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for config project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

application = get_wsgi_application()
6 changes: 4 additions & 2 deletions labs/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from kombu import Queue
from redbeat import RedBeatSchedulerEntry, schedulers
import redis
from labs.config import settings
from config import configuration_variables as settings
import logging


Expand All @@ -14,7 +14,9 @@
LOW_PRIORITY_QUEUE_NAME = f"{CELERY_QUEUE_PREFIX}-low"
HIGH_PRIORITY_QUEUE_NAME = f"{CELERY_QUEUE_PREFIX}-high"

redis_client = redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=0, decode_responses=True)
redis_client = redis.StrictRedis(
host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=0, decode_responses=True
)


app = Celery(
Expand Down
2 changes: 1 addition & 1 deletion labs/database/connect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from labs.config import settings
from config import configuration_variables as settings
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
import logging
Expand Down
2 changes: 1 addition & 1 deletion labs/database/vectorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import logging

from labs.config import settings
from config import configuration_variables as settings
from labs.database.embeddings import reembed_code


Expand Down
2 changes: 1 addition & 1 deletion labs/github/github.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base64
import requests
from dataclasses import dataclass
from labs.config import settings
from config import configuration_variables as settings
import git
import os
import logging
Expand Down
Loading
Loading