Error in running "Custom Recipe Generator" Code in OpenAI

When I paste and run the code given in Module 2 of “How to write Code for Open AI” This is what the error is shown -
 python3
File “/home/runner/WhitesmokeHotBootstrapping/”, line 21
return response[‘choices’][0][‘message’][‘content’]
IndentationError: unindent does not match any outer indentation level
exit status 1

This is the code I used:

Kindly help me with this issue

Python is an indentation-sensitive language. It means the Python interpreter cares about how many spaces have you provided for indentation for each line and considers it as a respective block.

(Indentation means the number of spaces before the first word in a line. Usually, the indentation is 4 spaces or 1 tab.)

The error here can be possibly corrected by re-checking the indentation at each block and line.

1 Like

Okay Thank you!
but It seems I got another error because,
this is the code I used

and this is the response I’m getting
 python3
 Milk Eggs Bread
bash: Milk: command not found

Please let me know what am I doing wrong…

I assume you have got an API key from OpenAI ChatGPT, and replaced “OPENAI_API_KEY” with the key in line 5:

openai.api_key = os.environ['OPENAI_API_KEY']

and then add this code below at line 22:

user_prompt = input("Enter Components: \n")
tutorial_out = generate_tutorial(user_prompt)

@prashant0410 ,
In your code, you have defined a function named generate_tutorial but you are nowhere accessing the function. The function you have written is just defined, the code that I have added in the previous post takes the input of components and invokes(calls) the function with the components entered by you as parameters.
Which in turn invoke(call) the OpenAI’s API, get the result, and print it.

Hi @prashant0410,

Try this code out

# This code imports the necessary modules and libraries for a Flask web application that will render templates using OpenAI and accept user input through HTML forms.

import openai

import os

from flask import Flask, render_template_string, request

# Sets the API key for the OpenAI API using the value of the 'OPENAI_API_KEY' environment variable.

openai.api_key = os.environ['secret_key']

# This code uses the OpenAI API to generate a tutorial for making a recipe using a list of components provided as an argument.3

def generate_tutorial(components):

 response = openai.ChatCompletion.create(



   "role": "system",

   "content": "You are a helpful assistant"

  }, {




   f"Suggest a recipe using the items listed as available. Make sure you have a nice name for this recipe listed at the start. Also, include a funny version of the name of the recipe on the following line. Then share the recipe in a step-by-step manner. In the end, write a fun fact about the recipe or any of the items used in the recipe. Here are the items available: {components}, Haldi, Chilly Powder, Tomato Ketchup, Water, Garam Masala, Oil"


 return response['choices'][0]['message']['content']

# Create a Flask web application object named app and define a route for the root URL that responds to GET and POST requests

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])

# This code defines a function that generates a tutorial based on user input obtained through a POST request.

def hello():

 output = ""

 if request.method == 'POST':

  components = request.form['components']

  output = generate_tutorial(components)

# This is a HTML template for a Custom Recipe Generator web page. It includes a form for users to input a list of ingredients/items they have, and two JavaScript functions for generating a recipe based on the input and copying the output to the clipboard. The template uses the Bootstrap CSS framework for styling.

 return render_template_string('''

 <!DOCTYPE html>



  <title>Infinite Project Generator</title>

    <link href="" rel="stylesheet">


  async function generateTutorial() {

   const components = document.querySelector('#components').value;

   const output = document.querySelector('#output');

   output.textContent = 'Cooking a recipe for you...';

   const response = await fetch('/generate', {

    method: 'POST',

    body: new FormData(document.querySelector('#tutorial-form'))


   const newOutput = await response.text();

   output.textContent = newOutput;


  function copyToClipboard() {

   const output = document.querySelector('#output');

   const textarea = document.createElement('textarea');

   textarea.value = output.textContent;




   alert('Copied to clipboard');





  <div class="container">

   <h1 class="my-4">Custom Recipe Tutorial Generator</h1>

   <form id="tutorial-form" onsubmit="event.preventDefault(); generateTutorial();" class="mb-3">

    <div class="mb-3">

     <label for="components" class="form-label">Ingredients / Items:</label>

     <input type="text" class="form-control" id="components" name="components" placeholder="Enter the list of Ingredients or items you have e.g. Bread, jam, potato etc. " required>


    <button type="submit" class="btn btn-primary">Share with me a tutorial</button>


   <div class="card">

    <div class="card-header d-flex justify-content-between align-items-center">


     <button class="btn btn-secondary btn-sm" onclick="copyToClipboard()">Copy</button>


    <div class="card-body">

     <pre id="output" class="mb-0" style="white-space: pre-wrap;">{{ output }}</pre>








# This code defines a route for the URL "/generate" that only accepts POST requests.

@app.route('/generate', methods=['POST'])

# This code defines a function 'generate' that takes a POST request containing a 'components' field and returns the result of calling the 'generate_tutorial' function with the provided components as input.

def generate():

 components = request.form['components']

 return generate_tutorial(components)

# This code snippet starts the Flask application if the script is being run directly as the main program, running on the IP address '' and port number 8080.

if __name__ == '__main__':'', port=8080)

Also, when attempting to add elements for a recipe, try using all lowercase letters with spaces in between them. It should function as expected.

Please inform me if you encounter any further difficulties.

Hey @rohit.ts ,

Is all the webpage HTML template and app part required for just basic functioning of the code from the CLI?.. or is that part of a project/assignment for a course?.. Just curious

Got it sir, Thank you! The code is working now

Hello sir,
I tried the code you mentioned, It was not working first but then after few reloads it worked but now, IT shows some error again
This is showing in Webview and

File “/home/runner/HoarseUnlinedPublishing/venv/lib/python3.10/site-packages/openai/”, line 619, in _interpret_response
File "/home/runner/HoarseUnlinedPublishing/venv/lib/python3.10/site-
packages/openai/", line 679, in _interpret_response_line
raise self.handle_error_response(
openai.error.AuthenticationError: You didn’t provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you’re accessing the API from your browser and are prompted for a username and password. You can obtain an API key from OpenAI Platform - - [30/Sep/2023 10:23:33] “POST /generate HTTP/1.1” 500 -
This is showing in Console sir

Hi @gunavardhanvyas,

Apologies for the delay in getting back to you.
The necessity for a webpage HTML template and app components depends on the specific project or assignment requirements and the purpose of the code.

In a basic command-line interface (CLI) application, you typically don’t need webpage HTML templates or app components as the primary functionality is executed through the command line. However, if the project requires a user interface or integration with a web application, then HTML templates and app components become essential to create the interface and ensure the code interacts effectively with the user or the web environment.

For CLI-focused projects or assignments, the main focus is on the functionality, logic, and implementation of the intended features within the console. On the other hand, projects that involve web or app development will necessitate HTML templates and related components to ensure the code can be accessed and used through a user interface.

It’s essential to thoroughly review the project or assignment requirements to determine if HTML templates and app components are necessary for the basic functioning or if they are part of a larger project involving a user interface or web application.

Hi @prashant0410,

Apologies for the delay in getting back to you.
In the code which I have given you will have to replace the secret_key with your API Key you take from the Open API Website.
The error message you’re encountering is from the OpenAI API and specifically states that you haven’t provided an API key for authentication.
To resolve this issue, you need to make sure you include your API key in the request when calling the OpenAI API.

# Sets the API key for the OpenAI API using the value of the 'OPENAI_API_KEY' environment variable.

openai.api_key = os.environ['secret_key']

This error is because an API key has not been provided by you in your latest code. Go to ‘Secrets’ in your repl and under OPENAI_API_KEY, write the value as your own API key, which you have copied from your openAI account previously.

1 Like