Openai migrate problem

someone help please, i can’t modify my python code normally for new openai version, can someone help and modify my code for 3.5-turbo version?:

import os
import sys
from openai import OpenAI
from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

# Your existing API key setup code
client = OpenAI(
  api_key=os.environ['OPENAI_API_KEY'],  # this is also the default, it can be omitted
)
@app.route('/welcome')
def index():
    return render_template('login-signup.html')

@app.route('/main-chat')
def main_chat():
    return render_template('main-chat.html')

@app.route('/ask', methods=['POST'])
def ask():
    messages = request.json['messages']
    
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages
    )
    
    # Assuming the API returns a 'choices' field with a list, where you want to get the first item
    message_from_ai = response.choices[0].message['content']
    
    # Now, return this message in a JSON response
    return jsonify({'response': message_from_ai})


    # Process the `chat_completion` response and extract the message
    # Here I'm assuming the response has a 'choices' field with a text property



@app.route('/get-firebase-config', methods=['GET'])
def get_firebase_config():
    # Ensure there's a 'firebase' environment variable
    if 'firebase' not in os.environ:
      sys.stderr.write("...")# Or return a more specific error message

    firebase_config = {
        "apiKey": os.environ['firebase'],
        # Add the rest of your static Firebase config here
        "authDomain": "padawanai.firebaseapp.com",
        "projectId": "padawanai",
        "storageBucket": "padawanai.appspot.com",
        "messagingSenderId": "265704826546",
        "appId": "1:265704826546:web:881ac592399d94aca0cbab",
        "measurementId": "G-11HPQE36PF"
    }
    return jsonify(firebase_config)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)

Hello @shkrtv!

You may have forgotten to create the homepage. You can change the code to:

import os
import sys
from openai import OpenAI
from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

# Your existing API key setup code
client = OpenAI(
  api_key=os.environ['OPENAI_API_KEY'],  # this is also the default, it can be omitted
)
@app.route('/')
def homepage():
  return "your page content here"
@app.route('/welcome')
def index():
    return render_template('login-signup.html')

@app.route('/main-chat')
def main_chat():
    return render_template('main-chat.html')

@app.route('/ask', methods=['POST'])
def ask():
    messages = request.json['messages']
    
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages
    )
    
    # Assuming the API returns a 'choices' field with a list, where you want to get the first item
    message_from_ai = response.choices[0].message['content']
    
    # Now, return this message in a JSON response
    return jsonify({'response': message_from_ai})


    # Process the `chat_completion` response and extract the message
    # Here I'm assuming the response has a 'choices' field with a text property



@app.route('/get-firebase-config', methods=['GET'])
def get_firebase_config():
    # Ensure there's a 'firebase' environment variable
    if 'firebase' not in os.environ:
      sys.stderr.write("...")# Or return a more specific error message

    firebase_config = {
        "apiKey": os.environ['firebase'],
        # Add the rest of your static Firebase config here
        "authDomain": "padawanai.firebaseapp.com",
        "projectId": "padawanai",
        "storageBucket": "padawanai.appspot.com",
        "messagingSenderId": "265704826546",
        "appId": "1:265704826546:web:881ac592399d94aca0cbab",
        "measurementId": "G-11HPQE36PF"
    }
    return jsonify(firebase_config)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)