如何在Google Cloud Platform中正确设置API密钥?

huangapple go评论140阅读模式
英文:

How do I correctly set up an API key in Google Cloud Platform?

问题

我正在尝试在GCP中部署一个需要OpenAI API密钥的项目。我将API密钥设置为环境变量,方法如下:

  1. export OPENAI_API_KEY='sh-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

并且我可以在Python中访问它,如下所示:

如何在Google Cloud Platform中正确设置API密钥?

然而,当我构建项目的Docker镜像并尝试运行它时,它会出现以下错误:

  1. $ docker run app
  2. [nltk_data] Downloading package punkt to /root/nltk_data...
  3. [nltk_data] Unzipping tokenizers/punkt.zip.
  4. Traceback (most recent call last):
  5. File "/app/app.py", line 9, in <module>
  6. os.environ['OPENAI_API_KEY'] = os.getenv("OPENAI_API_KEY")
  7. File "/usr/local/lib/python3.9/os.py", line 684, in __setitem__
  8. value = self.encodevalue(value)
  9. File "/usr/local/lib/python3.9/os.py", line 756, in encode
  10. raise TypeError("str expected, not %s" % type(value).__name__)
  11. TypeError: str expected, not NoneType

所以,这里有什么问题?

如果我执行以下操作,就不会出现任何错误:

  1. $ python
  2. Python 3.9.2 (default, Feb 28 2021, 17:03:44)
  3. [GCC 10.2.1 20210110] on linux
  4. Type "help", "copyright", "credits", or "license" for more information.
  5. >>> import os
  6. >>> os.environ['OPENAI_API_KEY'] = os.getenv("OPENAI_API_KEY")
  7. >>>

所以,我不明白为什么在运行Docker镜像时会出现这个错误。

英文:

I am trying to deploy a project in GCP that needs OpenAI API key. I set the API key as an environment variable from the cloud shell terminal as follows:

  1. export OPENAI_API_KEY=&#39;sh-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#39;

And I can access it using Python as you can see here:

如何在Google Cloud Platform中正确设置API密钥?

However, after I build a docker image of the project and try to run it, it gives the following error:

  1. $ docker run app
  2. [nltk_data] Downloading package punkt to /root/nltk_data...
  3. [nltk_data] Unzipping tokenizers/punkt.zip.
  4. Traceback (most recent call last):
  5. File &quot;/app/app.py&quot;, line 9, in &lt;module&gt;
  6. os.environ[&#39;OPENAI_API_KEY&#39;] = os.getenv(&quot;OPENAI_API_KEY&quot;)
  7. File &quot;/usr/local/lib/python3.9/os.py&quot;, line 684, in __setitem__
  8. value = self.encodevalue(value)
  9. File &quot;/usr/local/lib/python3.9/os.py&quot;, line 756, in encode
  10. raise TypeError(&quot;str expected, not %s&quot; % type(value).__name__)
  11. TypeError: str expected, not NoneType

So, what's wrong here?

If I do the following, I don't get any error:

  1. $ python
  2. Python 3.9.2 (default, Feb 28 2021, 17:03:44)
  3. [GCC 10.2.1 20210110] on linux
  4. Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
  5. &gt;&gt;&gt; import os
  6. &gt;&gt;&gt; os.environ[&#39;OPENAI_API_KEY&#39;] = os.getenv(&quot;OPENAI_API_KEY&quot;)
  7. &gt;&gt;&gt;

So I don't understand why it is giving this error when running the docker image.

答案1

得分: 2

Docker无法访问主机环境变量。您需要通过docker run调用传递它们,使用-e|--env参数。您可以尝试:

  1. docker run -e OPENAI_API_KEY='sh-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' app

或者

  1. docker run -e OPENAI_API_KEY="$OPENAI_API_KEY" app
英文:

Docker does not have access to the host environment variables. You need to pass them into the docker run call via -e|--env. Could you try:

  1. docker run -e OPENAI_API_KEY=&#39;sh-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#39; app

or

  1. docker run -e OPENAI_API_KEY=&quot;$OPENAI_API_KEY&quot; app

huangapple
  • 本文由 发表于 2023年6月19日 13:38:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定