I’m running into a really annoying bug where I ask chatgpt the following
combined_prompt = (
"\nHere are the names and contents of all scripts in the framework: \n" + all_scripts_content +
"\nHere is the original prompt: \n" + original_prompt +
"\nHere is the name of the script that you are modifying: \n" + the_current_script_being_edited +
"Output a SINGLE script based on the following debug output: " + sanitized_debug_output +
"JUST OUTPUT CODE, do not include ```python or similar! It is going to be run directly and will cause an error!"+
"\n IT DOES NOT MATTER IF THE SCRIPT REQUIRES MULTIPLE FILES TO RUN. JUST OUTPUT A SINGLE SCRIPT BASED ON THE NAME OF WHAT YOU ARE MODIFYING!!!"+
"\n The reason why is because this framework goes one script at a time, so it is not necessary to include all scripts at once. Just output the script that you are modifying."
)
No matter how much I try the output clearly contradicts this in multiple ways!
```python
# Import necessary Django and other libraries
import os
import openai
from django.core.wsgi import get_wsgi_application
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView
from django.conf import settings
from django.urls import path
from django.apps import AppConfig
# Environment setup for OpenAI API Key
# SET THIS ENVIRONMENT VARIABLE IN YOUR SERVER SETTINGS, not here in the code.
# Example: export OPENAI_API_KEY='your-api-key'
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
# Django settings
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
settings.configure(
DEBUG=True,
SECRET_KEY='replace-this-with-a-real-secret-key',
ROOT_URLCONF=__name__,
MIDDLEWARE=[
'django.middleware.security.SecurityMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
],
TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
},
],
STATIC_URL='/static/',
)
# Django application setup
class ChatbotAppConfig(AppConfig):
name = 'chatbot_app'
verbose_name = "Chatbot Application"
# Django template view for the chatbot webpage
class ChatbotView(TemplateView):
template_name = "chatbot.html"
# Function to interact with the GPT-3.5-turbo API endpoint
def ask_gpt3(prompt):
response = openai.Completion.create(
engine="gpt-3.5-turbo",
prompt=prompt,
max_tokens=150,
temperature=0.7,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response.choices[0].text.strip()
# Django view to handle POST requests with the user input
@csrf_exempt
def chat_with_gpt3(request):
if request.method == 'POST' and request.POST.get('message'):
user_input = request.POST.get('message')
gpt3_response = ask_gpt3(user_input)
return JsonResponse({'response': gpt3_response})
else:
return JsonResponse({'error': 'Invalid request'}, status=400)
# URL Patterns for the Django app
urlpatterns = [
path('', ChatbotView.as_view(), name='chatbot'),
path('chat/', chat_with_gpt3, name='chat_with_gpt3'),
]
# Standard Django WSGI application setup
application = get_wsgi_application()
# Main entry point
if __name__ == "__main__":
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
And the accompanying HTML (templates/chatbot.html
) for the above Django app:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Chatbot powered by GPT-3.5</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
#chatbox { width: 100%; height: 400px; border: 1px solid #999; overflow: auto; padding: 10px; }
#userInput { width: 100%; padding: 10px; margin-top: 10px; }
.userMsg { color: blue; }
.botMsg { color: green; }
</style>
<script>
// Function to send POST request to backend
function sendMessage(message) {
const xhr = new XMLHttpRequest();
xhr.open("POST", "/chat/", true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function () {
if (xhr.status == 200) {
const resp = JSON.parse(xhr.responseText);
displayMessage(resp.response, "botMsg");
}
};
xhr.send("message=" + encodeURIComponent(message));
}
// Function to display message in chatbox
function displayMessage(message, className) {
const chatbox = document.getElementById("chatbox");
const newMsg = document.createElement("div");
newMsg.innerHTML = message;
newMsg.className = className;
chatbox.appendChild(newMsg);
chatbox.scrollTop = chatbox.scrollHeight; // Auto scroll to the bottom
}
// Function to handle user input
function handleUserInput(e) {
if (e.keyCode === 13) { // Enter key
const userInput = document.getElementById("userInput");
const userMessage = userInput.value;
userInput.value = '';
if(userMessage.trim() !== '') {
displayMessage(userMessage, "userMsg");
sendMessage(userMessage);
}
}
}
</script>
</head>
<body>
<h1>GPT-3.5 powered chatbot</h1>
<div id="chatbox"></div>
<input type="text" id="userInput" placeholder="Ask me something..." onkeydown="handleUserInput(event)">
</body>
</html>
Remember to set your OpenAI API key as an environment variable before running your Django application.
For security reasons, please ensure you generate a new Django secret key for your deployment. Also, consider adding user authentication and production-grade settings for a real-world deployment.
This is really frustrating. I just want it to output CODE. I'm sure many others have encountered this issue but I can't seem to make it stop! I've reformatted the prompt many times over.