Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt Engineering uses old version of openai lib causing an excpetion #480

Closed
etoews opened this issue Dec 18, 2023 · 4 comments
Closed

Comments

@etoews
Copy link

etoews commented Dec 18, 2023

Describe the bug
The Prompt Engineering: A Practical Example – Real Python tutorial uses an old version of openai lib causing an openai.lib._old_api.APIRemovedInV1 exception.

To Reproduce
Steps to reproduce the behavior:

% git clone git@github.com:realpython/materials.git

% cd materials/prompt-engineering

% python -m venv venv

% source venv/bin/activate
(venv) % python -m pip install openai
Collecting openai
  Downloading openai-1.5.0-py3-none-any.whl (223 kB)
...

% python app.py chats.txt
Traceback (most recent call last):
  File "/Users/etoews/dev/temp/prompt-engineering/app.py", line 99, in <module>
    main(parse_args())
  File "/Users/etoews/dev/temp/prompt-engineering/app.py", line 54, in main
    print(get_completion(file_content, settings))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/etoews/dev/temp/prompt-engineering/app.py", line 59, in get_completion
    response = openai.Completion.create(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/etoews/dev/temp/prompt-engineering/venv/lib/python3.11/site-packages/openai/lib/_old_api.py", line 39, in __call__
    raise APIRemovedInV1(symbol=self._symbol)
openai.lib._old_api.APIRemovedInV1:

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

FWIW, running openai migrate does work but I don't know enough about the lib to put this into a PR yet.

% venv/bin/openai migrate
Retrieving Grit CLI metadata from https://api.keygen.sh/v1/accounts/custodian-dev/artifacts/marzano-macos-arm64
./app.py
     import tomllib
     from pathlib import Path

    -import openai
    +from openai import OpenAI
    +
    +client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

     # Authenticate
    -openai.api_key = os.getenv("OPENAI_API_KEY")


     class Settings(dict):

     def get_completion(content: str, settings: Settings) -> str:
         """Send a request to the /completions endpoint."""
    -    response = openai.Completion.create(
    -        model=settings.model,
    -        prompt=assemble_prompt(content, settings),
    -        max_tokens=settings.max_tokens,
    -        temperature=settings.temperature,
    -    )
    +    response = client.completions.create(model=settings.model,
    +    prompt=assemble_prompt(content, settings),
    +    max_tokens=settings.max_tokens,
    +    temperature=settings.temperature)
         return response["choices"][0]["text"]


     def get_chat_completion(content: str, settings: Settings) -> str:
         """Send a request to the /chat/completions endpoint."""
    -    response = openai.ChatCompletion.create(
    -        model=settings.model,
    -        messages=assemble_chat_messages(content, settings),
    -        temperature=settings.temperature,
    -    )
    +    response = client.chat.completions.create(model=settings.model,
    +    messages=assemble_chat_messages(content, settings),
    +    temperature=settings.temperature)
         return response["choices"][0]["message"]["content"]




Log in ./venv/lib/python3.11/site-packages/openai/cli/_cli.py: TODO: The 'openai.http_client' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(http_client=http_client)'

  - Range: 183:5 - 183:37
  - Source:
  openai.http_client = http_client
  - Syntax tree:
  (assignment left: (attribute object: (identifier) attribute: (identifier)) right: (identifier))
./venv/lib/python3.11/site-packages/openai/cli/_cli.py
             proxies=proxies or None,
             http2=can_use_http2(),
         )
    -    openai.http_client = http_client
    +    # TODO: The 'openai.http_client' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(http_client=http_client)'
    +    # openai.http_client = http_client

         if args.organization:
    -        openai.organization = args.organization
    +        # TODO: The 'openai.organization' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(organization=args.organization)'
    +        # openai.organization = args.organization

         if args.api_key:
    -        openai.api_key = args.api_key

         if args.api_base:
    -        openai.base_url = args.api_base
    +        # TODO: The 'openai.base_url' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(base_url=args.api_base)'
    +        # openai.base_url = args.api_base

         # azure
         if args.api_type is not None:
    -        openai.api_type = args.api_type

         if args.azure_endpoint is not None:
    -        openai.azure_endpoint = args.azure_endpoint
    +        # TODO: The 'openai.azure_endpoint' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(azure_endpoint=args.azure_endpoint)'
    +        # openai.azure_endpoint = args.azure_endpoint

         if args.api_version is not None:
    -        openai.api_version = args.api_version

         if args.azure_ad_token is not None:
    -        openai.azure_ad_token = args.azure_ad_token
    +        # TODO: The 'openai.azure_ad_token' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(azure_ad_token=args.azure_ad_token)'
    +        # openai.azure_ad_token = args.azure_ad_token

         try:
             if args.args_model:


Processed 1167 files and found 2 matches

Expected behavior
The Prompt Engineering: A Practical Example – Real Python tutorial should work with openai>=1.0.0.

Additional context

% python --version
Python 3.11.3
@etoews etoews changed the title Prompt Engineering uses old version of openai lib Prompt Engineering uses old version of openai lib causing an excpetion Dec 18, 2023
@etoews
Copy link
Author

etoews commented Dec 18, 2023

Or change this part of the tutorial

image

To this

% python -m pip install -r requirements.txt
Collecting openai==0.27.8
  Downloading openai-0.27.8-py3-none-any.whl (73 kB)
...

@dbader
Copy link
Member

dbader commented Dec 19, 2023

Thanks -- we're looking into it! :)

@dbader
Copy link
Member

dbader commented Dec 19, 2023

Quick update, we've added instructions for pinning the openai module to the article. We've also slated it for a bigger refresh in the new year to take into account the new models and updated OpenAI API endpoints. I'm closing this issue for now. Thanks again for the report.

@dbader dbader closed this as completed Dec 19, 2023
@etoews
Copy link
Author

etoews commented Dec 19, 2023

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants