openai/openai-quickstart-python

Can't find API key

ghrasko opened this issue · 5 comments

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?

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

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.

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 !

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.