只有 Python 3.8 – Azure ML 内核才能在笔记本中找到 OpenAI 模块。

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

Why only Python 3.8 - Azure ML kernel can find openai module in notebook

问题

Azure ML Studio中,我创建了一个notebook,安装了一些包并尝试运行了以下代码:

!pip install -r requirements.txt

以上代码成功运行。

! pip show openai

pip show openai的结果如下:

Name: openai
Version: 0.25.0
Summary: Python client library for the OpenAI API
Home-page: https://github.com/openai/openai-python
Author: OpenAI
Author-email: support@openai.com
License: None
Location: /anaconda/envs/azureml_py38/lib/python3.8/site-packages
Requires: typing-extensions, pandas, requests, openpyxl, pandas-stubs, tqdm, numpy
Required-by:

代码:

import openai
import re
import requests
import sys
from num2words import num2words
import os
import pandas as pd
import numpy as np
from openai.embeddings_utils import get_embedding, cosine_similarity
from transformers import GPT2TokenizerFast

API_KEY = "somekey"
RESOURCE_ENDPOINT = "https://someendpoint/"
openai.api_type = "azure"
openai.api_key = API_KEY
openai.api_base = RESOURCE_ENDPOINT
openai.api_version = "2022-12-01"

url = openai.api_base + "/openai/deployments?api-version=2022-12-01"

r = requests.get(url, headers={"api-key": API_KEY})

print(r.text)

我发现只有Python 3.8 Azure ML可以找到openai模块。当我选择其他内核时,会出现错误moduel openai not found。为什么会这样?

选择其他内核时出现的错误信息如下:

ModuleNotFoundError: No module named 'openai'

只有 Python 3.8 – Azure ML 内核才能在笔记本中找到 OpenAI 模块。

英文:

In Azure ML Studio, I created a notebook, installed some packages and tried to run a code

!pip install -r requirements.txt

above worked

! pip show openai

Result of pip show openai

Name: openai
Version: 0.25.0
Summary: Python client library for the OpenAI API
Home-page: https://github.com/openai/openai-python
Author: OpenAI
Author-email: support@openai.com
License: None
Location: /anaconda/envs/azureml_py38/lib/python3.8/site-packages
Requires: typing-extensions, pandas, requests, openpyxl, pandas-stubs, tqdm, numpy
Required-by: 

Code

import openai
import re
import requests
import sys
from num2words import num2words
import os
import pandas as pd
import numpy as np
from openai.embeddings_utils import get_embedding, cosine_similarity
from transformers import GPT2TokenizerFast

#API_KEY = os.getenv("AZURE_OPENAI_API_KEY") 
API_KEY = "somekey"
#RESOURCE_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT") 
RESOURCE_ENDPOINT = "https://someendpoint/"
openai.api_type = "azure"
openai.api_key = API_KEY
openai.api_base = RESOURCE_ENDPOINT
openai.api_version = "2022-12-01"

url = openai.api_base + "/openai/deployments?api-version=2022-12-01"

r = requests.get(url, headers={"api-key": API_KEY})

print(r.text)

I found that only Python 3.8 Azure ML could find openai module. When I selected some other kernel then I got error moduel openai not found. Why?

Error when other kernel is selected

ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 import openai
      2 import re
      3 import requests

ModuleNotFoundError: No module named 'openai'

只有 Python 3.8 – Azure ML 内核才能在笔记本中找到 OpenAI 模块。

答案1

得分: 2

只要确保在pip命令中使用正确的前缀。
在我的情况下,安装openai时我运行的是'%pip'而不是'!pip'。

使用'%pip'时,软件包将安装在与您的笔记本相同的环境中,而不是在shell中。

英文:

Just make sure to use the right prefix for the pip command.
In my case I ran '%pip' and not '!pip' when installing openai.

With '%pip' the packages are installed in the same environment as you notebook and not in the shell

答案2

得分: 0

我在我的环境中尝试过,并获得了以下结果:

起初,在我的环境中我遇到了相同的错误。

错误:

ModuleNotFound Error
Input In [9], in <cell line: 1>()
----> 1 import openai
      2 import re
      3 import requests
ModuleNotFound Error:No module named openai '

当您在 Azure ML Studio 中切换到不同的内核时,导致找不到 openai 模块 的原因是因为该模块安装在与笔记本内核关联的 Python 环境中。Azure ML Studio 中的每个内核都有自己独立的 Python 环境,这意味着您在一个内核中安装的包将无法在另一个内核中使用。

您需要直接使用以下命令进行安装。当我尝试相同的代码和包时,它成功执行了:

!pip install openai==0.27.4
!pip install pandas==2.0.0
!pip install num2words==0.5.12

代码:

import openai
import re
import requests
import sys
from num2words import num2words
import os
import pandas as pd
import numpy as np
from openai.embeddings_utils import get_embedding, cosine_similarity
from transformers import GPT2TokenizerFast

API_KEY = "somekey"
RESOURCE_ENDPOINT = "https://someendpoint/"
openai.api_type = "azure"
openai.api_key = API_KEY
openai.api_base = RESOURCE_ENDPOINT
openai.api_version = "2022-12-01"

url = openai.api_base + "/openai/deployments?api-version=2022-12-01"

r = requests.get(url, headers={"api-key": API_KEY})

print(r.text)

输出:

{
  "data": [
    {
      "scale_settings": {
        "scale_type": "standard"
      },
      "model": "gpt-35-turbo",
      "owner": "organization-owner",
      "id": "deploymentname1",
      "status": "succeeded",
      "created_at": 16807xxx,
      "updated_at": 16807xxx,
      "object": "deployment"
    }
  ],
  "object": "list"
}

参考链接:
在 Azure 机器学习中运行 Jupyter 笔记本 - Azure 机器学习 | Microsoft Learn

英文:

I tried in my environment and got the below results:

Initailly, I got the same error in my environment.

Error:

   ModuleNotFound Error
    Input In [9], in &lt;cell line: 1&gt;()
    ----&gt; 1 import openai
    2 import re
    3 import requests
    Traceback (most recent call last)
    ModuleNotFound Error:No module named openai &#39;

只有 Python 3.8 – Azure ML 内核才能在笔记本中找到 OpenAI 模块。

The reason why the openai module is not found when you switch to a different kernel in Azure ML Studio is because the module is installed in the Python environment associated with the notebook's kernel. Each kernel in Azure ML Studio has its own isolated Python environment, which means that the packages you install in one kernel will not be available in another kernel.

You need directly install by commands.when I tried with same code and packages it executed successfully:

!pip install openai==0.27.4
!pip install pandas==2.0.0
!pip install num2words==0.5.12

Code:

import openai
import re
import requests
import sys
from num2words import num2words
import os
import pandas as pd
import numpy as np
from openai.embeddings_utils import get_embedding, cosine_similarity
from transformers import GPT2TokenizerFast

#API_KEY = os.getenv(&quot;AZURE_OPENAI_API_KEY&quot;) 
API_KEY = &quot;somekey&quot;
#RESOURCE_ENDPOINT = os.getenv(&quot;AZURE_OPENAI_ENDPOINT&quot;) 
RESOURCE_ENDPOINT = &quot;https://someendpoint/&quot;
openai.api_type = &quot;azure&quot;
openai.api_key = API_KEY
openai.api_base = RESOURCE_ENDPOINT
openai.api_version = &quot;2022-12-01&quot;

url = openai.api_base + &quot;/openai/deployments?api-version=2022-12-01&quot;

r = requests.get(url, headers={&quot;api-key&quot;: API_KEY})

print(r.text)

Output:

{
  &quot;data&quot;: [
    {
      &quot;scale_settings&quot;: {
        &quot;scale_type&quot;: &quot;standard&quot;
      },
      &quot;model&quot;: &quot;gpt-35-turbo&quot;,
      &quot;owner&quot;: &quot;organization-owner&quot;,
      &quot;id&quot;: &quot;deploymentname1&quot;,
      &quot;status&quot;: &quot;succeeded&quot;,
      &quot;created_at&quot;: 16807xxx,
      &quot;updated_at&quot;: 16807xxx,
      &quot;object&quot;: &quot;deployment&quot;
    }
  ],
  &quot;object&quot;: &quot;list&quot;
}

只有 Python 3.8 – Azure ML 内核才能在笔记本中找到 OpenAI 模块。

Reference:
Run Jupyter notebooks in your workspace - Azure Machine Learning | Microsoft Learn

huangapple
  • 本文由 发表于 2023年4月11日 06:05:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981077.html
匿名

发表评论

匿名网友

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

确定