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}
try:
# Make a request to the GPT API
gpt_response = requests.post(gpt_api_endpoint, 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’:
app.run(debug=True)
127.0.0.1 - - [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
127.0.0.1 - - [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
/v1/chat/completions
thank you very much for the answers