How is the code working after making little changes in it?

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 ?

Hi @narayanprasad

The omitted or modified parts in your code were not critical for its core functionality:

  • {{output}}: This was probably intended for server-side rendering of initial content, which you handle dynamically in JavaScript.
  • querySelector vs. getElementById: Since you are selecting by ID, getElementById works just as well.
  • Direct HTML in render_template_string: This is acceptable for small templates.

Overall, your code is functional and follows good practices for the most part. If you find it working correctly, it means the omitted parts were either redundant or served a purpose that you’ve effectively covered in a different way.

If you still face any issue, please feel free to get back to us.

Ok sir but is my code fine or needs any enhancements ?

Hi @narayanprasad

Your code is good to go. You can play around with the frontend to improve the UI , add animation, spinners after clicking generate button and so on.

Happy learning!

What can I do after exhausting all free tokens ?

Hi @narayanprasad

If your credits run out completely, you’ll need to buy more separately. However, before that, I suggest taking advantage of the free credits offered by Bolt IoT. These credits are limited but they have no expiry date. We’ve allocated enough credits to cover your training and internship. If you exhaust them, just inform us, and we’ll explore ways to assist you.

Happy learning!

Hi @narayanprasad

Are you experiencing any issues with your credits being exhausted? If so, please let us know, and we will take the necessary actions to assist you.

No sir I have enough credits right now but I am asking what if it get exhausted.

Hi @narayanprasad

If your credits get exhausted, please inform us, and we will take the necessary actions as soon as possible.