How to Fix Python Code Accessing openai.Completion that is No Longer Supported in the Updated API?

This topic was automatically generated from Slack. You can find the original thread here.

Hey, looking for some assistance with some Python code that will talk to ChatGPT. I’m getting this error even though I believe I’ve changed the Python code to call into the API correctly. APIRemovedInV1
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. 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

Hi ! We don’t use a specific version for the openai package in Python steps. Pipedream downloads the latest version when you deploy your workflow. If you need to use an older version, try pinning the package version.
Let me know if that helps

I haven’t got round to actually deploying - I was testing the code from within the flow builder.

here’s the Python code that I’ve tweaked with the help of ChatGPT :slightly_smiling_face:

import os
import openai

def handler(pd: “pipedream”):
openai.api_key = os.environ.get(“openai_api_key”)
user_request = pd.steps[“trigger”][“event”][“text”]

_# Use the updated method from the OpenAI 1.0.0 interface_
response = openai.Completion.create(
    model="text-davinci-004",  _# Replace with your preferred model_
    prompt=user_request,
    temperature=0.7,
    max_tokens=256,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
)

_# Parsing the response based on the new API structure_
**if** response.choices:
    response_to_user = response.choices[0].text.strip()
    **print**(f'Answer: {response_to_user}')
    **return** {'response_to_user': response_to_user}
**else**:
    **return** {'response_to_user': 'No response generated'}

I would recommend using the latest version since OpenAI recommends migrating.
But if you want to use an older version, add this to the top of your code:
# pipedream add-package openai==0.28

I definitely want to use the latest version… how do I go about instantiating that?

do I have the ability to issue for example the command:

pip install --upgrade openai

(new to Pipedream!)

From the migration docs:

from openai import OpenAI

def handler(pd: "pipedream"):
  client = OpenAI(api_key=your_api_key)

No need to run that command! Pipedream will automatically use the latest version

Have you connected your OpenAI account with Pipedream?

Yep

actually… yes I did the other day but had other problems, hence starting again

Have you tried our built-in actions for OpenAI?

i did… (hence why I’ve reverted to Python). I was sending a message to OpenAI fine and getting a response but then it was getting stuck looping and chewed up the credits so had to abandon it. I found a web article that was actually trying to do the same thing but in Python.

Let me revisit this again using your built-in actions because I wasn’t using the “complete” action - that may have been my issue. Because call is async, using “complete” I think behaves differently…

I’ll try again later.

thanks

Thanks for sharing all this! I’m trying to understand if there is any improvements we need to make on our side — what’s your core use-case? Are you using the Create Completion action or the Chat (recommend this one) action?