Can't get openai python package working... please help!

OpenAI “help” is not in the business of diagnosing OS installs or giving programming lessons.

You might find some Mac experts that can help you figure out how to get pip to install things into the current operating environment, or create your own modifiable python install all in user environment, with path set to point to it.

If you want to ask an AI, here’s a chatbot that requires no additional libraries (this example refactored from using requests library by AI)


import os, json, urllib.request, urllib.error

model = "gpt-3.5-turbo-0125"
system = [{"role": "system", "content": f"You are ChatBro, an expert AI assistant powered by {model}"}]
user = [{"role": "user", "name": "intro", "content": "Provide brief introduction."}]
chat = []
params_template = {"model": model, "max_tokens": 666, "top_p":0.9,}
headers = {"Content-Type": "application/json",
           "Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}"}

while not user[0]['content'] == "exit":
    request = {**params_template, **{"messages": system + chat[-10:] + user}}
    try:
        req = urllib.request.Request("https://api.openai.com/v1/chat/completions", \
          headers=headers, data=json.dumps(request).encode())
        response = urllib.request.urlopen(req)
    except urllib.error.HTTPError as e:
        print(f"HTTP error {e.code}: {e.read().decode()}")
        user = [{"role": "user", "content": input("\nPrompt: ")}]
        continue
    except Exception as e:
        print(f"Error: {e}")
        continue
    else:
        response_body = json.loads(response.read().decode())
        reply = response_body['choices'][0]['message']['content']
        print(reply)
        print(response_body['usage'])
    chat += user + [{"role": "assistant", "content": reply}]
    user = [{"role": "user", "content": input("\nPrompt: ")}]

The API key environment variable is needed as described before – or you can just paste it right into the code instead of {os.environ.get('OPENAI_API_KEY')}