from boltiotai import openai
import os
from flask import Flask , render_template_string, request
from dotenv import load_dotenv
load_dotenv()
openai_api_key = os.getenv('OPENAI_API_KEY')
def getresult(question):
response = openai.chat.completions.create(
model= "gpt-3.5-turbo",
messages = [{"role":"system", "content":"You are a nice assistant."},
{"role":"user", "content": f"Write one word answer for {question}"}]
)
return response['choices'][0]['message']['content']
app = Flask(__name__)
@app.route('/', methods = ['GET','POST'])
def hello():
return render_template_string('''
<!doctype html>
<html >
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Personal Bot</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script>
async function getresult(question){
question = document.getElementById("question").value;
var output = document.getElementById("output");
output.textContent = "Finding Your answer....";
var response = await fetch ('/generate',
{method : 'POST',
body : new FormData(document.getElementById("form"))
}) ;
var finaloutput = await response.text();
output.textContent = finaloutput;
}
</script>
</head>
<body>
<div class="container">
<form id="form" onsubmit="event.preventDefault(); getresult(question)" ;>
<div class="mb-3">
<label for="question" class="form-label">Question</label>
<input type="text" class="form-control" id="question" name="question" required>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<div class="mb-3">
output:
</div>
<pre id="output"></pre>
</form>
</div>
</body>
</html>
''' )
@app.route('/generate', methods = ['POST'])
def generate():
question = request.form['question']
return getresult(question)
if __name__ == '__main__':
app.run(host='0.0.0.0' ,port = 4499 )
Sir , I was trying to write the code myself according to my understanding after reading the code provided in the course and after completing, I noticed that I have left some lines from the code like from the hello() function, {{output}} in the HTML template, replaced ‘querySelector’ with ‘getElementById’ and many more . But I am still getting correct output. Can you please check this code and explain why? Were those removed things just extras ?