I have been trying to provide context to my chatbot, and did this using the ‘messages’ parameter, but I kept getting no response from the chatbot after introducing that to my code. I see the following error come up:
openai.error.InvalidRequestError: Unrecognized request argument supplied: messages
I made sure that my OpenAI key is valid and my API version are up to date, and I feel this has to be an issue with my code, but I cannot figure out how to get the bot to understand/respond using this context provided through the messages parameter. I’m afraid it may be an issue with implementation in Django because I can provide context easily in Jupyter when I don’t need to write a separate file for the chatbot (see code below), a separate file calling the chatbot function: views.py, and a separate file implementing the chatbot using JavaScript in index.html. There must be a communication issue, but i have been losing my mind trying to find it for the past few days and would greatly appreciate help!
chat_bot.py
import openai
from django.conf import settings
def get_chat_response(prompt):
openai.api_key = settings.CHATGPT_KEY
messages=[
{"role": "system",
"content": "You are an STI health assistant, an automated personal sexual health counselor. "
},
{ "role": "user",
"content": prompt # User's message
}
]
response = openai.Completion.create(
engine='text-davinci-003',
messages=messages,
max_tokens=50,
temperature=0.5,
n = 1
)
print(response.choices)
return response.choices[0].text.strip()
views.py (just the part with chat function)
def chat_gpt(request):
response = get_chat_response(request.GET.get('prompt'))
# print(response)
return HttpResponse(json.dumps(response), content_type='application/json')
index.html (JS where chatbot is implemented)
<script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>
<script>
document.addEventListener("DOMContentLoaded", function() {
var sendBtn = document.getElementById("send-btn");
var userInput = document.getElementById("user-input");
var chatBox = document.getElementById("chat-box");
sendBtn.addEventListener("click", function() {
var message = userInput.value.trim();
if (message !== "") {
appendMessage(message, "sent");
userInput.value = "";
scrollToBottom();
// TODO: Handle user message and generate bot response
$.ajax({
url: "http://127.0.0.1:8000/chat_gpt/?prompt=" + message,
type: 'GET',
dataType: 'json', // added data type
success: function(res) {
console.log(res);
// alert(res);
appendMessage(res, "received");
}
});
}
});
function appendMessage(message, type) {
var messageElement = document.createElement("div");
messageElement.classList.add("message", type);
messageElement.innerText = message;
chatBox.appendChild(messageElement);
}
function scrollToBottom() {
chatBox.scrollTop = chatBox.scrollHeight;
}
});
</script>