Based on the information provided in the extracts, it seems that the OpenAI API has indeed been updated and the way you're trying to use the openai.completions.create method is no longer supported.
In the new version of the API, you need to create an instance of the OpenAI class and then use the chat.completions.create method on that instance. Here's how you can modify your code:
from openai import OpenAI
def get_api_response(prompt: str) -> str | None:
text: str | None = None
choices = None
try:
client = OpenAI(api_key='your-api-key')
response = client.chat.completions.create(
model="gpt-3.5-turbo-instruct",
messages=[
{"role": "system", "content": "You are a helpful research assistant."},
{"role": "user", "content": prompt}
],
max_tokens=200,
temperature=1.2,
top_p=1,
frequency_penalty=0,
presence_penalty=0.7,
stop=['Human:', 'AI:']
)
print(response)
choices = response.choices[0]
text = choices.message.content
except Exception as e:
print('ERROR:', e)
return text
Please replace 'your-api-key' with your actual API key.
Also, note that the response object is no longer a dictionary, but an instance of the ChatCompletion class. Therefore, you can't use the get method on it. Instead, you can access the choices attribute directly, and each choice is an object with a message attribute, which in turn has a content attribute.
This information is based on the extracts from the OpenAI Python API library Github Issues (https://github.com/openai/openai-python/issues).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: