Error calling GPT API: 400 Client Error: Bad Request for url

Hey everyone!
I am trying to build a chatbot for my team and i am trying to use gpt API to be as a an NLP, basically, i want users to ask questions in a text box and then using js, their input will be structured as prompts to gpt api and the gpt api will know which SQL query to preform based on the information i gave him, then he should give back an answer to the user.
It is my first time working with the gpt api and i am a bit lost, i succeded to build the HTML,js code but i tried to use the chat gpt to build the flask app and i am struggling.
This is my code here and i will provide the error i get after it:
@app.route(‘/get_response’, methods=[‘POST’])

def get_response():
user_input = request.json.get(‘user_input’)

if not user_input:
    return jsonify({'error': 'Missing user input'}), 400

# Call GPT API to get the GPT response based on user input
gpt_api_endpoint = '()'
gpt_api_key = 'MY_API_KEY'
headers = {'Authorization': f'Bearer {gpt_api_key}'}
gpt_payload = {'prompt': user_input, 'max_tokens': 500}

    # Make a request to the GPT API
    gpt_response =, headers=headers, json=gpt_payload)
    gpt_response.raise_for_status()  # Raise an HTTPError for bad responses (4xx and 5xx)
    gpt_answer = gpt_response.json()['choices'][0]['text']
except requests.RequestException as e:
    app.logger.error(f'Error calling GPT API: {e}')
    return jsonify({'error': 'Error calling GPT API'}), 500

# Choose the appropriate query based on GPT response
query_type = "get_the_right_format"

# Get database details based on the query type
database_details = DATABASE_40_DETAILS if query_type in SERVER_40_QUERIES else DATABASE_41_DETAILS

# Get the SQL query
sql_query = SERVER_40_QUERIES[query_type]["sql_query"]

# Perform a database query based on the selected query
database_answer = execute_sql_query(sql_query, database_details)

return jsonify({'bot_response': str(database_answer), 'gpt_response': gpt_answer})

if name == ‘main’: - - [18/Nov/2023 19:32:19] “OPTIONS /get_response HTTP/1.1” 200 -
[2023-11-18 19:32:19,787] ERROR in app: Error calling GPT API: 400 Client Error: Bad Request for url:/v1/chat/completions - - [18/Nov/2023 19:32:19] “POST /get_response HTTP/1.1” 500 -

I didn’t find any explanations regarding using the right endpoint and would really use some help here and now i am using the open a URL and after him
thank you very much for the answers

Calling the chat completions endpoint where you send messages and get an AI answer is not how you interact with an “assistant”, nor is there an API product called “GPT” or “GPT API”. Nor do you send a “prompt” to an unspecified chat completions model.

The learning resources are to start with “chat” in the API reference (found on the forum’s sidebar of links) and start by following the programming examples of interacting with a model and getting a response.

Then understand that you should not be writing HTML client javascript to interact with the API, unless you like having your API key stolen and account emptied by someone.

ChatGPT does not have sufficient training to know how to code for the OpenAI API.

Thank you very much!
I still have much to learn, I’ve hidden my API key using GitIgnore and will use node.js instead of python for the prosses