openai.ChatCompletion out of date help migrate client.chat.completions

37 views Asked by At

Error message

ChatGPT API Artificial Intelligence at Your Fingertips Type your question here Submit

Alittlebean:

What are phasers?

ChatGPT:

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

You can run openai migrate to automatically upgrade your codebase to use the 1.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. pip install openai==0.28

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

Usage

The full API of this library can be found in api.md.

import os
from openai import OpenAI

client = OpenAI(
    # This is the default and can be omitted
    api_key=os.environ.get("OPENAI_API_KEY"), # put the ChatGPT Assistant API key here but do not post it here in public
)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model="gpt-3.5-turbo",
)

https://github.com/openai/openai-python

ChatGPT-API-Flask-Website https://github.com/redemptionwxy/ChatGPT-API-Flask-Website

main.py that I need help with

from flask import Flask, request, render_template, redirect
import openai
import os
# from openai import OpenAI

openai.api_key = 'MY_API_KEY_HERE' # put the ChatGPT Assistant API key here but do not post it here in public

# can be empty
# client = OpenAI(
    # This is the default and can be omitted
    # api_key=os.environ.get("OPENAI_API_KEY"), # put the ChatGPT Assistant API key here but do not post it here in public
#)

server = Flask(__name__)

def send_gpt(prompt):
    try:
        response = openai.ChatCompletion.create(
        # response = client.chat.completions.create(
        model='gpt-3.5-turbo',
        messages=[{"role": "user", "content": prompt}]
        )
        return response["choices"][0]['message']['content']
    except Exception as e:
        return e


@server.route('/', methods=['GET', 'POST'])
def get_request_json():
    if request.method == 'POST':
        if len(request.form['question']) < 1:
            return render_template(
                'chat3.5.html', question="NULL", res="Question can't be empty!")
        question = request.form['question']
        print("======================================")
        print("Receive the question:", question)
        res = send_gpt(question)
        print("Q:\n", question)
        print("A:\n", res)

        return render_template('chat3.5.html', question=question, res=str(res))
    return render_template('chat3.5.html', question=0)

if __name__ == '__main__':
    server.run(debug=True, host='0.0.0.0', port=5000)
0

There are 0 answers