Banner Image Banner Image


Get Insights And Updates On Tech Industry Trends

Home | Blog
How to Build a Web Assistant Using Django and OpenAI GPT-3.5 API in Python

How to Build a Web Assistant Using Django and OpenAI GPT-3.5 API in Python

March 24, 2023

Want to build a web assistant Using Django and OpenAI GPT-3.5 API in Python? 

Want to add AI magic to your coding? 

Yes, we are talking about integrating OpenAI GPT-3.5 API – the powerhouse of large language models into Python scripts. Start leveraging ChatGPT development services and build a powerful web assistant now! 

Artificial Intelligence is gaining immense popularity and bringing automation at an increasing rate. Looking towards the growing advancement, web applications are integrating with various machine learning models for adding sophistication to the app. Gradually, it will be able to handle every complex task with the utmost ease. 

For instance, an intelligent web app can offer 360-degree services – from taking customer orders online to attending and responding to their queries, suggesting solutions, and giving feedback at the helm. You can seek professional assistance to build a web application with ChatGPT. 


Building Web Assistant Using Django and OpenAI GPT-3.5 API

If you have ever thought of building an intelligent web application for your enterprise, this blog is for you. Here, you will learn how to build a web assistant using Django and Open AI GPT- 3.5 API in Python. 

Django is a high-level and powerful web framework that helps you to develop complex, data-driven websites with pragmatic designs. It enables the rapid development of secure and maintainable websites.

OpenAI GPT-3.5 API is a machine-learning platform that understands as well as generates natural language or code and solves complex problems with greater accuracy. It lets you train and deploy AI models for streamlining automation in the business. 

This web assistant will resolve your queries pertaining to sports, education, digital media, entertainment, technology, and more. If you want to start building a web assistant using these powerful frameworks, start by setting up a development environment. Further, create a Django application.

Once it is done, you will have to integrate the Django framework with the OpenAI GPT-3.5 API. Test the performance and bring it into action. Follow this step-by-step tutorial on building AI Chatbot with DialoGPT. 


How to integrate AI and use ChatGPT in a Python script?

Want to start using the ChatGPT language model in the Python script? Start using the OpenAI Python library (currently v0.27.0) -:

Firstly, you have to sign up for OpenAI API access here and get the API key. You can install it using pip, the Python package manager, with the following command:

pip install openai


Set Up Your OpenAI API Key

After signing up for the OpenAI API access, you need to set up your OpenAI API key. Create an environment variable named OPENAI_API_KEY and put your API key as its value to configure your API key.

In case, if you do not have one, you have to follow these instructions mentioned in the OpenAI API documentation 23.

Now, add the next line to your Python code to import the OpenAI API client: import open

Initialize the OpenAI API client by adding the following lines to your Python code:

This specifies which GPT model you are using as there are various other models available. Every model has their own unique set of capabilities and performance characteristics. 

Furthermore, write the openai.Completion.create() function for creating text using the ChatGPT language model. 

 Have a look here at how you can generate a response to a given prompt. Note that there is an initial “system” prompt, followed by the user’s question:

response = openai.ChatCompletion.create(



     {“role”: “system”, “content”: “You are a helpful assistant.”},

     {“role”: “user”, “content”: “Hello, ChatGPT!”},



message = response.choices[0][‘message’]

print(“{}: {}”.format(message[‘role’], message[‘content’]))


assistant: Hello! How can I assist you today?

Awesome, right? Let’s take a look at another example of how to integrate ChatGPT into Python, this time using more parameters.

How to use ChatGPT API parameters

In the below example, more parameters are added to openai.ChatCompletion.create() to generate a response. Here’s what each means:


  • The engine parameter specifies which language model to use (“text-davinci-002” is the most powerful GPT-3 model at the time of writing)
  • The prompt parameter is the text prompt to generate a response to
  • The max_tokens parameter sets the maximum number of tokens (words) that the model should generate
  • The temperature parameter controls the level of randomness in the generated text
  • The stop parameter can be used to specify one or more strings that should be used to indicate the end of the generated text
  • If you want to generate multiple responses, you can set n to the number of responses you want returned

The strip() method removes any leading and trailing spaces from the text.

response = openai.ChatCompletion.create(




     {“role”: “system”, “content”: “You are a helpful assistant with exciting, interesting things to say.”},

     {“role”: “user”, “content”: “Hello, how are you?”},



message = response.choices[0][‘message’]

print(“{}: {}”.format(message[‘role’], message[‘content’]))

The generated text is returned in the choices field of the response, which is a list of objects that contain the “role” (assistant or user) and “content” (the generated text). In this example, we only requested one response so we just got the first item in the choices list and printed the role, and generated text.


assistant: Hello! I am just a computer programmer, but I am functioning properly and ready to help. How can I assist you today?

It’s important to note that although ChatGPT is magical, it does not have human-level intelligence. Responses shown to your users should always be properly vetted and tested before being used in a production context. Don’t expect ChatGPT to understand the physical world, use logic, be good at math, or check facts.


Handling errors from the OpenAI GPT-3.5 API

It’s a best practice to monitor exceptions that occur when interacting with any external API. For example, the API might be temporarily unavailable, or the expected parameters or response format may have changed and you might need to update your code, and your code should be the thing to tell you about this. Here’s how to do it with Rollbar:

import rollbar

rollbar.init(‘your_rollbar_access_token’, ‘testenv’)


def ask_chatgpt(question):

response = openai.ChatCompletion.create(




         {“role”: “system”, “content”: “You are a helpful assistant with exciting, interesting things to say.”},

         {“role”: “user”, “content”: question},



message = response.choices[0][‘message’]

return message[‘content’]



print(ask_chatgpt(“Hello, how are you?”))

except Exception as e:

# monitor exception using Rollbar


print(“Error asking ChatGPT”, e)

To get your Rollbar access token, sign up for free and follow the instructions for Python.


Here we have discussed and shown the step-by-step process of building a web Assistant by using OpenAI GPT and Django. Building a chatbot is a quite fun and rewarding project for the business. It not only eases your tasks but helps you in maintaining and strengthening customer relationships at the helm. 

We started with installing the OpenAI Python package, followed by setting up a Django project and application for building a seamless user interface by leveraging Django’s template system. Under expert guidance, we have covered ways to test the web assistant and troubleshoot any issue for error-free deployment. 

Now that you have built a web assistant, you can test and try different features and settings for improving the performance at the helm. 

And, if you are looking for an industry-specific expert for building a web assistant or chatbot for your website, you can call us now!

Techcronus is an experienced and People’s Trusted Chatbot App Development Company

build custom-informed GPT-3-based chatbots 


By TechcronusAdmin

Techcronus is a worldwide supplier of Enterprise Business Solutions and Custom Software Development Services, with locations in the USA, Australia, and India. It has accomplished the successful delivery of over 800 projects to start-ups, SMBs, and well-known brands, such as Starbucks, Unilever, and IKEA. The firm's areas of expertise include Microsoft Dynamics 365 ERP/CRM solutions, Web Development, Business Applications Management (.NET and DevOps), Mobile Development (Native, Hybrid, Blockchain), Staff Augmentation, Product Development & Support, and UI Design and UX.