Migration error when trying to use Vision API

I’m trying to analyse images using the GPT-4 Vision API but I’m running into the error:

“You tried to access openai.Completion, but this is no longer supported in openai>=1.0.0 - see the README at GitHub - openai/openai-python: The official Python library for the OpenAI API for the API.”

I tried to migrate the code but that didn’t seem to help:

" Processed 1 files and found 0 matches"

I also tried re-installing the OpenAI module but to no avail.

For context, here’s the snippet of code I’m trying to run:

Function to analyze image frames

def analyze_frames(frames):
try:
# Crafting the prompt for image analysis
prompt_messages = “Analyze these sequential frames from a video and describe each image. Then, synthesize these descriptions to provide an overall understanding of the video’s concept and storyline.”

    for frame in frames:
        # Send each frame for analysis
        response = openai.Completion.create(
            model="gpt-4-vision-preview",  # Adjust the model as per your requirement
            prompt=prompt_messages,
            max_tokens=1000,
            attachments=[{"data": frame, "type": "image/jpeg"}]  # Attach the frame
        )
        print(response.choices[0].text)

        # Print the token usage if available
        if 'usage' in response:
            print("Token usage for this request:", response['usage']['total_tokens'])

except Exception as e:
    print("Error in analyzing frames:", e)

Please advise :pray:

The API has changed completely for v1.0.0 and above:
check the quickstart tutorial,
https://platform.openai.com/docs/quickstart?context=python
You need to rewrite openai.Completion.create()

1 Like