Yadja – YAML to Django

Today I want to introduce a quick-and-dirty prototype of a project I’ve been thinking about since I the advance of LLMs.

The idea is simple: we take a YAML file with loosely defined models, and turn this into a full-fledged Django application using LLMs, in this case the excellent LLM from OpenAI which has been making headlines: ChatGPT.

I want to create the following files when scaffolding a project:

  1. Apps for each model
  2. Serializers so I can easily create APIs using DRF
  3. Admin definitions

The project is written in Python, just a few LOCs and has only OpenAI as a dependency. You’ll also need an OpenAI API key and have access to GPT4. If you don’t have access to GPT4, you can change the model type to 3.5-turbo but it’s more prone to errors.

Getting Started

The repository is live on GitHub: https://github.com/briandeheus/yadja

First, clone the project on Github [email protected]:briandeheus/yadja.git. Enter the directory and create a new virtual environment and activate it. Make sure you also install openai via either pip or conda. This is the only dependency.

Here’s an example YAML file that I used to generate the scaffolding for a simple to-do project:

- name: project
  fields:
    - title
    - actor (user)
- name: task
  fields:
    - project
    - name
    - done
    - actor (user)
    - created_on
    - updated_on
    - finished_on
- name: comment
  fields:
    - body
    - actor (user)
    - task

First, make sure we create a django project.

django-admin startproject todo

And then you simply call main.py.

python main.py \
  --definition=./examples/todo.yaml \
  --output=/home/brian/code/todo \
  --project_name=todo

At this point we’ll begin creating the proper models, admin files, and serializers, and appconfigs. After a few minutes your todo directory should look something like this. The script will let you know when it’s done.

The settings file should be automatically receiving the newly generated apps. This last step is the most unstable, so please double-check the settings file.

Now, in your Django project directory run ./manage.py makemigrations to make the migrations.

(.venv) ➜  todo ./manage.py makemigrations
Migrations for 'projects':
  projects/migrations/0001_initial.py
    - Create model Project
Migrations for 'tasks':
  tasks/migrations/0001_initial.py
    - Create model Task
Migrations for 'comments':
  comments/migrations/0001_initial.py
    - Create model Comment

and run ./manage.py migrate to run the migrations. Next up create a new user via ./manage.py createsuperuser. With your superuser created, run the development server via ./manage.py runserver and login via the admin panel on http://localhost:8000/admin.

Clicking into the Tasks model, we can see it did a pretty good job at setting up autocomplete fields.

class TaskAdmin(admin.ModelAdmin):
    list_display = ('project', 'name', 'done', 'actor', 'created_on', 'updated_on', 'finished_on')
    list_filter = ('done',)
    search_fields = ('name',)
    autocomplete_fields = ('project', 'actor')

There’s still a lot of things we can improve, such as automatically generating the APIs as well. However, for sake of a proof of concept I’m happy with the result we have so far in just one hour of coding.

Discuss on Hackernews => https://news.ycombinator.com/item?id=36058395

Discuss on Reddit => https://www.reddit.com/r/programming/comments/13qn1c9/yadja_yaml_to_django_using_chatgpt/