Debug School

rakesh kumar
rakesh kumar

Posted on

InvalidRequestError at chatgpt This is a chat model and not supported in the completions endpoint using django.

yesterday i integrate open ai api using django and i got the error
when i prompt the data in input field i face this error

Image description

my code is in views.py

 openai.api_key = settings.OPENAI_API_KEY
            response = openai.Completion.create(
                engine='gpt-3.5-turbo',
                prompt=user_input,
                max_tokens=50,
                temperature=0
            )
Enter fullscreen mode Exit fullscreen mode

Error

InvalidRequestError at /chatgpt/
This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?
Request Method: POST
Request URL:    http://localhost:8000/chatgpt/
Django Version: 4.1.9
Exception Type: InvalidRequestError
Exception Value:    
This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?
Enter fullscreen mode Exit fullscreen mode

Solution

replace gpt-3.5-turbo to davinci
and my improved version of coding

 openai.api_key = settings.OPENAI_API_KEY
            response = openai.Completion.create(
                engine='davinci',
                prompt=user_input,
                max_tokens=50,
                temperature=0
            )
Enter fullscreen mode Exit fullscreen mode

Full Coding

@csrf_exempt
def chat_with_gpt(request):
    if request.method == 'POST':
        form = ChatForm(request.POST)
        if form.is_valid():
            user_input = form.cleaned_data['message']

            # Make a request to ChatGPT using the OpenAI library
            openai.api_key = settings.OPENAI_API_KEY
            response = openai.Completion.create(
                engine='davinci',
                prompt=user_input,
                max_tokens=50,
                temperature=0
            )

            if response.choices:
                print('data 200 choices')
                generated_message = response.choices[0].text.strip()
                print(generated_message)
            else:
                print('data else choices')
                generated_message = 'Failed to generate response'


            # Save the user input and generated response to the database
            chat_message = ChatMessage(message=user_input, generated_message=generated_message)
            chat_message.save()

            return render(request, 'chatgpt.html', {'form': form, 'generated_message': generated_message})
    else:
        form = ChatForm()

    return render(request, 'chatgpt.html', {'form': form})
Enter fullscreen mode Exit fullscreen mode

template.html

<html>
<head>
    <title>ChatGPT</title>
</head>
<body>
    <h1>ChatGPT using model</h1>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Send">
    </form>
    {% if generated_message %}
    <h2>Generated Message:</h2>
    <p>{{ generated_message }}</p>

    {% else %}
    <h1>Generated Message will display after data is saved into database:</h1>
    {% endif %}
</body>
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Top comments (0)