openai.ChatCompletion issue

Seems I’m another person affected by the OpenAI migration issue. Can you please help me?

The full error:

OpenAI API error: 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

My app.py:

from flask import Flask, render_template, request, jsonify
import openai  # Importing openai directly
import configparser

# Read configurations from config.ini
config = configparser.ConfigParser()
config.read('config.ini')

# Check if the required OpenAI API key is present in the configuration
if 'OpenAI' not in config or 'api_key' not in config['OpenAI']:
    raise ValueError("OpenAI API key not found in config.ini")

# Initialize the OpenAI client
openai.api_key = config['OpenAI']['api_key']

app = Flask(__name__)
app.static_folder = 'static'

@app.route('/')
def home():
    # Render the chat.html template when the root URL is accessed
    return render_template('chat.html')

@app.route('/send', methods=['POST'])
def send_message():
    # Get the user's message from the request
    user_message = request.json.get('message', '')

    # Placeholder character info 
    character_info = {
        "name": "Unknown Character",
        "appearance": {
            "body_type": "Unknown",
            "complexion": "Unknown"
        },
        "bio": {
            "House_of_Ashes": "Unknown",
            "accident": "Unknown"
        },
        "traits": "Unknown"
    }

    # Update the prompt to include character details
    prompt = config['Prompt']['prompt'].format(
        name=character_info['name'],
        traits=character_info['traits'],
        appearance=character_info['appearance'],
        bio=character_info['bio'],
        conversation_summary=''
    )

    # Construct the conversation format for the OpenAI API
    conversation = [
        {"role": "system", "content": f"You are {character_info['name']}, and you are talking to {user_message}."},
        {"role": "user", "content": user_message}
    ]

    try:
        # Debug: print the conversation being sent to OpenAI API
        print("Sending conversation to OpenAI API:", conversation)

        # Call the OpenAI API to process the user's message and get a response
        response = openai.ChatCompletion.create(
            model=config['LanguageModel']['model'],
            messages=conversation
        )

        # Debug: print the response received from OpenAI API
        print("Received response from OpenAI API:", response)

        bot_response = response['choices'][0]['message']['content']
    except Exception as e:
        # Handle API errors
        print("Error:", str(e))  # Debug: print the error
        return jsonify(response="OpenAI API error: " + str(e))

    # Update the conversation summary with the latest interaction
    prompt = prompt.replace('{conversation_summary}', f'Player: {user_message}nCharacter: {bot_response}n')

    # Return the bot's response as JSON
    return jsonify(response=bot_response)

if __name__ == '__main__':
    app.run(debug=True)

ChatGPT doesn’t help, I’ve tried running the solutions listed in the link from the error and that didn’t work too.

New contributor

Akhanta Nightshade is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Please read the official Python OpenAI package docs

The minimal usage example is like this:

import os
from openai import OpenAI

client = OpenAI(
    # This is the default and can be omitted
    api_key=os.environ.get("OPENAI_API_KEY"),
)

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

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật