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

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

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

问题

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

export OPENAI_API_KEY='sh-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

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

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

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

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

所以,这里有什么问题?

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

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

所以,我不明白为什么在运行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:

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:

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

So, what's wrong here?

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

$ python
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; import os
&gt;&gt;&gt; os.environ[&#39;OPENAI_API_KEY&#39;] = os.getenv(&quot;OPENAI_API_KEY&quot;)
&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参数。您可以尝试:

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

或者

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:

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

or

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:

确定