Skip to main content

Django GPT is a library for integrating powerful text generators like GPT-3 and GPT-4 into your Django application. It allows you to automatically generate text based on instructions, which can be useful for creating property descriptions or other textual data.

Installation

You can install Django GPT using pip:

pip install django-ckeditor-5
pip install django-gpt

Usage Example

from django.db import models
from django_gpt.models import DjangoGptField

class PropertyModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=255)

    # GPT
    gpt_instruction = models.TextField(
        default='', blank=True, help_text='GPT instruction')

    description_html = DjangoGptField(
        type="html",
        field_instruction_name="gpt_instruction",
        gpt_role="You're a real estate agent and you're writing a description for a property.",
        gpt_content_length=300,  # Default maximum generated text length.
        allowed_tags=['p', 'br', 'b', 'strong', 'i', 'em', 'u',
                      'ul', 'ol', 'li', 'a', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
        default="",
        blank=True,
    )

    description_text = DjangoGptField(
        type="textarea",
        field_instruction_name="gpt_instruction",
        gpt_role="You're a real estate agent and you're writing a description for a property.",
        gpt_content_length=300,  # Default maximum generated text length.
        default="",
        blank=True,
    )

How It Works

The Django GPT library allows you to create Django model fields that automatically generate text based on instructions. In the example above, we have two fields: description_html and description_text, which use GPT to generate property description text.

  1. gpt_instruction: This field is intended for instructions that you provide to GPT to specify what to generate.

  2. description_html and description_text: These fields will be automatically filled with generated text based on the instructions provided in the gpt_instruction field.

  3. field_instruction_name: This field determines where to take the instruction for text generation. If you change the field_instruction_name value, automatic regeneration of text will occur for fields that reference this instruction. GPT also takes into account the previous value that was in fields of type DjangoGptField. If you clear the value and change the instruction, text generation will be based solely on the new instruction. If there are no changes in the instruction, text regeneration will not occur. This allows you to preserve previous versions of generated text if needed for comparison.

  4. gpt_content_length: This parameter defines the maximum length of generated text. In this example, the maximum length of the text is set to 300 characters. You can customize this parameter according to your needs.

Configuration

To configure the Django GPT library, you can use the following parameters in your Django application’s settings.py file:

# Version of GPT to use (3 or 4).
DJANGO_GPT_VERSION = 3

# ISO language code (e.g., "en"). If not specified, it uses the value from LANGUAGE_CODE, and if that's not set, it defaults to "en".
DJANGO_GPT_LANGUAGE = "en"

# API key for GPT.
CHATGPT_API_KEY = "your-api-key-here"
# Please make sure to replace `"your-api-key-here"` with your actual GPT API key in the settings before using the library.

To use the rich text editor for HTML fields like description_html, consider integrating a package like django-ckeditor-5. Here’s an example of how to configure the editor in your Django settings:

INSTALLED_APPS = [
    ...
    "django_ckeditor_5",
    ...
]

CKEDITOR_5_CONFIGS = {
    'default': {
        'toolbar': ['heading', '|', 'outdent', 'indent', '|', 'bold', 'italic', 'link', 'underline', 'strikethrough',
                    'code', 'subscript', 'superscript', 'highlight', '|', 'codeBlock', 'sourceEditing', 'insertImage',
                    'bulletedList', 'numberedList', 'todoList', '|', 'blockQuote', 'imageUpload', '|',
                    'fontSize', 'fontFamily', 'fontColor', 'fontBackgroundColor', 'mediaEmbed', 'removeFormat',
                    'insertTable',],
        'image': {
            'toolbar': ['imageTextAlternative', '|', 'imageStyle:alignLeft',
                        'imageStyle:alignRight', 'imageStyle:alignCenter', 'imageStyle:side', '|'],
            'styles': [
                'full',
                'side',
                'alignLeft',
                'alignRight',
                'alignCenter',
            ]

        },
        'heading': {
            'options': [
                {'model': 'paragraph', 'title': 'Paragraph',
                    'class': 'ck-heading_paragraph'},
                {'model': 'heading1', 'view': 'h1', 'title': 'Heading 1',
                    'class': 'ck-heading_heading1'},
                {'model': 'heading2', 'view': 'h2', 'title': 'Heading 2',
                    'class': 'ck-heading_heading2'},
                {'model': 'heading3', 'view': 'h3',
                    'title': 'Heading 3', 'class': 'ck-heading_heading3'},
                 ...
            ]
        }
    },
}

Release files for django-gpt 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-gpt 1.0.1
File Size Uploaded
django-gpt-1.0.1.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-gpt 1.0.1
File Interpreter ABI Platform
django_gpt-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.6 kB

Release files / django-gpt-1.0.1.tar.gz

Download URL django-gpt-1.0.1.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
77379c77c79cf80d638640578eda0a89dd16a8da42a122d7647eb7f64ad8f32d
BLAKE2b-256 checksum
How to use checksums
22b76c69d7893444c904b3377bf2e16b292e36c47c0e1c70c680ba4e3047be46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / django_gpt-1.0.1-py3-none-any.whl

Download URL django_gpt-1.0.1-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
190775fb0651109b0fe400ba3996a61b0dccff571b615438815a2afde8a83d6b
BLAKE2b-256 checksum
How to use checksums
873f916b979cb27775e37ebc4ff77c77e16e93236e79b74b4129f780e93f5058
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page