Skip to main content

Integrate powerful text generators like GPT-3 and GPT-4 into your Django application for automated text generation based on instructions.

Project description

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'},
                 ...
            ]
        }
    },
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-gpt-1.0.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_gpt-1.0.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file django-gpt-1.0.1.tar.gz.

File metadata

  • Download URL: django-gpt-1.0.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for django-gpt-1.0.1.tar.gz
Algorithm Hash digest
SHA256 77379c77c79cf80d638640578eda0a89dd16a8da42a122d7647eb7f64ad8f32d
MD5 8707f534b74f2c0db73c3005b88e590a
BLAKE2b-256 22b76c69d7893444c904b3377bf2e16b292e36c47c0e1c70c680ba4e3047be46

See more details on using hashes here.

File details

Details for the file django_gpt-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_gpt-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for django_gpt-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 190775fb0651109b0fe400ba3996a61b0dccff571b615438815a2afde8a83d6b
MD5 b53e811feeb532b46d8aa6820349b0d1
BLAKE2b-256 873f916b979cb27775e37ebc4ff77c77e16e93236e79b74b4129f780e93f5058

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page