【问题标题】:How to store API keys in environment variable? and call the same in google colab如何将 API 密钥存储在环境变量中?并在 google colab 中调用相同的
【发布时间】:2021-09-27 05:05:08
【问题描述】:

我不确定如何使用我的 GPT-3 API 密钥/环境变量制作“.json”文件,但我想在 Google Colab 中使用它来自动生成代码。

有人可以告诉我怎么做吗?

我想使用下面的代码从 .json 文件中获取 API 密钥。

with open('GPT_SECRET_KEY.json') as f:
    data = json.load(f)
openai.api_key = data["API_KEY"]

【问题讨论】:

  • 只需创建一个包含{"API_KEY": "1234"} 内容的文件,然后像您显示的那样加载它。有什么问题?
  • 除非您可能会破坏您的私钥并在您的系统中形成一个大的安全整体
  • 不要这样做,您最终会暴露您的私有 API 密钥。我不使用colab,所以无法给出具体答案,寻找一种将“秘密”存储在您的谷歌帐户中的方法,然后您应该可以使用谷歌的客户端库来检索该密钥,有点像Github秘密
  • @AlexeyLarionov Larionov,是的,我明白了

标签: python environment-variables gpt-3


【解决方案1】:

要从 json 文件中读取,请执行以下操作:

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"]

你的 json 文件的结构应该是这样的。

{
    "API_KEY": "xxxxthis_is_the_key_you_are_targetingxxxx", 
    "API_KEY1": "xxxxxxxxxxxxxxxxxxxxxxx",
    "API_KEY2": "xxxxxxxxxxxxxxxxxxxxxxx"
}
如果我的回答不够清楚,

Here is a link 可以回答类似的问题。

【讨论】:

  • 为什么要重复问题中的代码 sn-p?这是如何解决的?
猜你喜欢
  • 2021-06-12
  • 2019-04-17
  • 1970-01-01
  • 1970-01-01
  • 2020-12-06
  • 1970-01-01
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多