-1

I'm building a chatbot withing Django using chat GPT's API and I'm following this tutorial https://www.youtube.com/watch?v=qrZGfBBlXpk. Anyway I don't see any difference in my code yet I'm running into a peculiar problem when I request from my html mile during the message sending process it returns null from the ''chatbot''

def chatbot(request):
    if request.method == 'POST':
        message = request.POST.get('message')
        response = message
        return JsonResponse({'message': message, 'response': response})
    return render(request, 'chatbot.html')

whats even weirder is when I console log the message in the JS

messageForm.addEventListener('submit', (e) => {
        e.preventDefault();

        const message = messageInput.value.trim()
        console.log('Message:', message)

It shows up in the log? but somewhere along the line it returns null? I noticed when I used the davinci model and the chat would respond but only with nonsense which to me implied it wasnt actually getting MY message it was getting null and returning nonsense

this Is the rest of the JS

const messageItem = document.createElement('li');
    messageItem.classList.add('message', 'sent');
    messageItem.innerHTML = `<div class="message-sender">
                                <div class="message-sender"
                                    <b>You<b>
                                </div>
                                <div class="message-content">
                                    ${message}
                                </div>
                            </div>`;
    messageList.appendChild(messageItem);
    messageInput.value = '';

    const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;

    fetch('', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urleencoded',
        'X-CSRFToken': csrftoken
        },
        body: new URLSearchParams({
            'csrfmiddlewaretoken': csrftoken,
            'message': message
        })
    })

      .then(response => response.json())
      .then(data => {
        const response = data.response;
        const messageItem = document.createElement('li');
        messageItem.classList.add('message', 'recieved');
        messageItem.innerHTML = `
        <div class="message-text">
            <div class="message-sender"
             <b>AI Helper<b>
            </div>
            <div class="message-content">
                ${response}
            </div>
        </div>
          `;
          messageList.appendChild(messageItem);
      });
    });
    ```
userr554
  • 57
  • 5
  • 2
    Probably the message = request.POST.get('message') is the problem here. Can you log that? Also - don't code with ChatGPT. It's a tool! It will never output you a good program, if you don't know what to do – SickerDude43 Jul 23 '23 at 10:42
  • SickerDude43, I'm using its API (to integrate with an aplication) not it itself. And yes I printed it and it seems to be returning none, But why? – userr554 Jul 23 '23 at 17:36

1 Answers1

0

I feel so stupid, This isnt even really an answer I should be posting, it was 7am and I had been up all night when I posted this but yea it was a typo here

headers: { 'Content-Type': 'application/x-www-form-urleencoded',

should be headers: { 'Content-Type': 'application/x-www-form-urlencoded',

userr554
  • 57
  • 5