0

I'm not sure how to make a ".json" file with my GPT-3 API key/environment variable, but I'd like to utilize it in Google Colab for automatic code generation.

Could someone please show me how to do this?

I want to get the API key from the.json file using the code below.

with open('GPT_SECRET_KEY.json') as f:
    data = json.load(f)
openai.api_key = data["API_KEY"]
kumaran T
  • 27
  • 2
  • 8
  • Just make a file with `{"API_KEY": "1234"}` content, load it similarly to what you've shown. What's the problem? – Alexey S. Larionov Jul 19 '21 at 14:31
  • 1
    Except that you can potentially compromise your private keys and make a big security whole in your system – Alexey S. Larionov Jul 19 '21 at 14:32
  • Don't do this, you will end up exposing your private API key. I don't use colab, so can't give a specific answer, look for a way to store "secrets" in your google account, then you should be able to use google's client library to retrieve that key, kinda like Github secrets – suvayu Jul 19 '21 at 14:37
  • @AlexeyLarionov Larionov, yes, I understood – kumaran T Jul 19 '21 at 14:40

1 Answers1

0

To read from a json file you do the following:

import json

my_key = ''
with open('GPT_SECRET_KEY.json', 'r') as file_to_read:
    json_data = json.load(file_to_read)
    my_key = json_data["API_KEY"]

The structure of your json file should look something like this.

{
    "API_KEY": "xxxxthis_is_the_key_you_are_targetingxxxx", 
    "API_KEY1": "xxxxxxxxxxxxxxxxxxxxxxx",
    "API_KEY2": "xxxxxxxxxxxxxxxxxxxxxxx"
}

Here is a link to a similar question if my answer was not clear enough.

gfdb
  • 311
  • 2
  • 9