Skip to content

name 'openai' is not defined in openai 1.2.0 #1151

Closed
@ejgutierrez74

Description

@ejgutierrez74

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

This code should work as documented in usage https://github.com/openai/openai-python

Also i have doubt about using client with or without api key as parameter...in courses of OpenAi form DeepLearning, they show client without api key as parameter, and they prefer to use an environment variable saved in a .env file.:

client = OpenAI(
    # This is the default and can be omitted
    api_key=os.environ.get("OPENAI_API_KEY"),
)

or

client = OpenAI()

To Reproduce

1 - Execute the snippet below from DeepLearning official short course. Reading the usage of your main github file as linked above, it should work.

2 - Gives error : name 'openai' is not defined in openai 1.2.0

Code snippets

from openai import OpenAI
import os


from dotenv import load_dotenv, find_dotenv

_ = load_dotenv(find_dotenv()) # read local .env file

openai.api_key  = os.getenv('OPENAI_API_KEY')

client = OpenAI()

def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=0
    )
    return response.choices[0].message.content



def get_completion_from_messages(messages, 
                                 model="gpt-3.5-turbo", 
                                 temperature=0, 
                                 max_tokens=500):
    response = client.chat.completions.create(model=model,
    messages=messages,
    temperature=temperature, 
    max_tokens=max_tokens)
    return response.choices[0].message.content

messages =  [  
{'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'},    
{'role':'user', 'content':'tell me a joke'},   
{'role':'assistant', 'content':'Why did the chicken cross the road'},   
{'role':'user', 'content':'I don\'t know'}  ]

response = get_completion_from_messages(messages)
print(response)


### OS

Ubuntu 22.04

### Python version

Python 3.11

### Library version

openai v1.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions