You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
% 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 readin 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_clientif 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.organizationif 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# azureif 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_endpointif 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
The text was updated successfully, but these errors were encountered:
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
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.
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:
FWIW, running
openai migrate
does work but I don't know enough about the lib to put this into a PR yet.Expected behavior
The Prompt Engineering: A Practical Example – Real Python tutorial should work with
openai>=1.0.0
.Additional context
The text was updated successfully, but these errors were encountered: