Integrating ChatGPT into Django app for AI-powered content creation

Systems like ChatGPT and other large language models are a helpful tool for AI-powered content creation. Examples are creation of articles, opinions, news, reviews or product description. In this article I cover how ChatGPT and similar AI models can be integrated into a Django app for AI writing and editing.

Example: Automated creation of online shop product descriptions

Let's assume that you run an online shop with certain products. It might be the case that you have only a very limited product description that is not engaging much with your customers and possible not very helpful from a SEO perspective. You might start with a simple product description like:

Foldable camping chair, grey, Aluminium

When you feed this into a large language model and ask for a improved product description you might end with:

Introducing our versatile and durable Camping Chair in a sleek shade of grey. Crafted with premium quality aluminium, this chair is built to withstand any outdoor adventure, making it an essential companion for your camping trips, picnics, or beach outings. Its lightweight and foldable design allows for easy transportation and storage, making it ideal for those who value convenience. The sturdy construction and ergonomic design ensure optimal comfort and support, providing you with a cozy seating experience wherever you go. So whether you are enjoying a bonfire by the lake or simply unwinding in your backyard, our Camping Chair in grey aluminium will elevate your outdoor leisure experience to new heights.

A ChatGPT integration into a django app could help you to automate this process and scale on a large number without the need to manually interact with a chat bot.

In the following sections I will illustrate how a simple ChatGPT-Django integration can be created on a more technical level. If you have a use case for such integration and need help with development, please contact us.

Adding the openai python bindings

To interact with OpenAI models a python package called "openai" is offered that you can simply install and add to your Django project. For pip users you would simply run:

pip install openai

Instead of using low level HTTP calls you can then use predefined python classes and methods to simply interact with the language model. How this interaction could look like will be shown in a moment.

Handling the OpenAI key

For API access, openai uses a secret API key. You can create such keys in your openai account. Such key should not be hard-coded. You could inject the API key via Django settings from an environment variable like:


Finding a suitable prompt and adding it to the django app

Large language model model (LLM) chatbots operate in a way that they accept natural language prompts as input. This input describes a task that should be performed by the model. In the above mentioned use case I simply use the following prompt:

Create a product description based on the following product details: //TEXT-INPUT//. Do not invent any additional product parameters or properties.

Prompt engineering is the process of finding suitable prompts to be feed into the AI system and various approaches can be used to find and improve the prompt.

Depending on your use case you could either hard-code the prompt in your Django app, make it configurable via settings or store it in the database via Django model for more flexibility. For this example, I store it in the settings to make it configurable across installations:

OPENAI_PROMPT = "Create a product description based on the following product details: //TEXT-INPUT//. Do not invent any additional product parameters or properties."

This prompt defined in the settings will then be used in your Django app.

Adding the AI-powered field to the Django model

For the automated creation of product description or similar text, I won't create any frontend like a django view or template but rather interact with it through the Django admin. I will simply create a model with two fields:
1. The somehow limited product text, called "text_input"
2. The field for the AI generated response which is the full product description

The django model then looks like:

class Product(models.Model):
    text_input = models.TextField()
    generated_text = models.TextField(blank=True)

In order to actually call the OpenAI API, the model save method is overwriteen:

    def save(self, *args, **kwargs):
        if not self.generated_text:
            client = OpenAI(api_key=settings.OPENAI_API_KEY)
            completion =
                    {"role": "system",
                     "content": "You are a writer creating product descriptions for an online shop."},
                    {"role": "user", "content": settings.OPENAI_PROMPT.replace("//TEXT-INPUT//", self.text_input)}
            self.generated_text = completion.choices[0].message.content
        super().save(*args, **kwargs)

Within the save method, an OpenAI instance is created using the API key from the settings. Next a chat completion is created which makes use of the previously defined prompt. The output from the LLM model is then assigned to the generated_text field and the whole model instance is saved.

The model is added to the django admin via:

class ProductAdmin(admin.ModelAdmin):
    readonly_fields = ("generated_text",), ProductAdmin)

Using the ChatGPT integration via django admin

In this simply prototype I avoid creating a specific Django frontend and rather use the existing admin functionality to interact with Django model instances. After creating a django superuser, I can access the app and navigate to the admin:

Once the shortened product description is provided and the new product is saved, it automatically calls the openai API and adds the AI-generated product description to the django object:

Now we got AI-powered content directly added into our Django app which could be used for various purposes like showing it in an online shop. A very similar approach could be used for news articles, blog articles or other pieces of information.

Improving the ChatGPT django integration

Obviously there are many more possibilities for a django chatgpt integration.

Instead of simply using the django admin you could also create a suitable frontend for uses to interact with the ChatGPT. This would include the creation of a django view and template.

Another option would be to add upload functionality for a csv file with information that is processed by the django app. For every item in the list, such as a product for instance, the ChatGPT API is called and new column is added to the file with the AI-model response. This would allow for a processing of a large number of items without much manual work.

In case you have a use case for a django app, especially for AI integration, please do not hesitate to contact us.