I know this is out there but i don't see a resolution for API errors

Ok, ive seen this all over. But never see a clear resolution. Here is the code im using.

import openai

Set the API key

openai.api_key = OPENAI_API_KEY

Test an OpenAI API call

def summarize_text(text):
response = openai.ChatCompletion.create(
model=“gpt-4”,
messages=[
{“role”: “system”, “content”: “Summarize the following text.”},
{“role”: “user”, “content”: text},
],
)
return response[“choices”][0][“message”][“content”]

Test summarization

try:
summary = summarize_text(“This is a test text for summarization.”)
print(“Summary:”, summary)
except Exception as e:
print(“Error with OpenAI API:”, e)

And here is the error i keep getting no matter what platform im on or changes i make from the suggestions.

Access token generated successfully:
Error with OpenAI API:

You tried to access openai.ChatCompletion, 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.

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: v1.0.0 Migration Guide · openai/openai-python · Discussion #742 · GitHub

I am using GPT 4, and version 1.57 so i have the most up to date items. Please help, im new and have no clue and im on week 2 of trying to figure this out.

You tried to use these old methods that you obtained (somewhere?)

A click on the sidebar “API Reference” → Chat, can provide you an example of usage. Usage example code that can inform your function, also needing style fixin’s for quality performance and an OpenAI-recommended model.

from openai import OpenAI

def summarize_text(text: str) -> str:
    """
    Summarizes the given text using the OpenAI API.

    Args:
        text (str): The input text to summarize.

    Returns:
        str: The summarized text.
    """
    client = OpenAI()  # Instantiate the OpenAI client, using env variable

    # Make a chat completion call to the OpenAI API
    response = client.chat.completions.create(
        model="gpt-4o",  # Use the multimodal version of GPT-4
        messages=[
            {"role": "system", "content": "You produce shorter summaries of input documents"},
            {"role": "user", "content": "Summarize the following text: \n\n" + text},
        ],
        max_completion_tokens=1000,  # constrain the max output length
    )

    # Return the summary content directly
    return response.choices[0].message.content


# Test summarization function
try:
    test_text = "(Imagine a tardigrade article to summarize here)"
    summary = summarize_text(test_text)
    print("Summary:", summary)
except Exception as e:
    print("Error with OpenAI API:", e)
1 Like