openai / openai-quickstart-python

Python example app from the OpenAI API quickstart tutorial
https://platform.openai.com/docs/quickstart?context=python
MIT License
1.69k stars 1.31k forks source link

Can't find API key #49

Closed ghrasko closed 7 months ago

ghrasko commented 1 year ago

Not a real coding issue. I am testing on Windows 10. I added the API key to the (renamed) .env file, but it still can't find it. If I add it to the environment by the "set" command in the (elevated) command prompt and then run "flask run", it works fine, but this was clearly not the original intention. Probably I lack some basic information, but can anybody help?

kranfnus commented 1 year ago

I am still not able to setup my venv. How did you do it?

ghrasko commented 1 year ago

Hi,

I created a config file called .env and added the API key to that:

OPENAI_API_KEY=<mykey>

I invoke it the following way:

import os
import openai
from dotenv import load_dotenv

def init_gpt():
    load_dotenv()
    openai.api_key = os.getenv("OPENAI_API_KEY")

I call the API this way:

def call_gpt(myprompt):

    response = openai.Completion.create(
        model = "text-davinci-003",
        prompt = myprompt,
        temperature = 0.6,
        max_tokens = 150,
        top_p = 1,
        frequency_penalty = 0,
        presence_penalty = 0.6,
        stop = [<my stop seq1>, <my stop seq2>]

    return response

This works for me.

kranfnus commented 1 year ago

My problem came from the fact that I was running a too recent version of Python. One needs to run a Python V3.9.x !

ghrasko commented 1 year ago

My problem came from the fact that I was running a too recent version of Python. One needs to run a Python V3.9.x !

Well, I use the most recent Python version with the above code.

logankilpatrick commented 7 months ago

We cover this in our getting started guide: https://platform.openai.com/docs/quickstart?context=python