【问题标题】:AttributeError: module 'openai' has no attribute 'Embedding'AttributeError: 模块 \'openai\' 没有属性 \'Embedding\'
【发布时间】:2023-02-26 18:32:06
【问题描述】:

根据OpenAi的文档和网上找的大量演示,下面的代码在Python中运行应该没有问题:

import openai
response = openai.Embedding.create(
  input="porcine pals say",
  model="text-embedding-ada-002"
)

但是,当我在本地 Jupyter 实例上运行此代码时,我收到以下错误:

AttributeError                            Traceback (most recent call last)
>! <ipython-input-209-e3e908b35b81> in <module>
1 import openai
2 response = openai.Embedding.create(
3   input="porcine pals say",
4   model="text-embedding-ada-002"
5 )

AttributeError: module 'openai' has no attribute 'Embedding'

这仅适用于嵌入,因为其他引擎(如 Completion)在我的本地机器上运行良好。

我将我的openai库升级到最新版本,但错误依然存在。我还向 ChatGPT 寻求帮助,但它的回应似乎只不过是使用 Completion(而非 Embedding)的变通方法。这没有用。

我的问题是其他人是否遇到过同样的问题?如果是这样,您是如何解决的?我目前没有从 OpenAI 的新“text-embedding-ada-oo2”模型中检索嵌入的解决方法。因此,即使有我可以使用的解决方法,那也很好。

【问题讨论】:

  • 您真的安装了最新版本吗,它是您的 Jupyter notebook 中使用的版本吗?请检查并确认两者。
  • 请链接“文档”。

标签: python embedding openai-api


【解决方案1】:

我测试了你的代码:

  • Python 3.11.1 和 OpenAI Python 库 0.26.3
  • Python 3.11.1 和 OpenAI Python 库 0.26.5(最新版本)

在这两种情况下,我都运行了test.py,OpenAI API 返回了嵌入:

[-0.02801201120018959, 0.022157097235322, -0.011196479201316833, 0.005577428266406059, 0.012320289388298988, 0.007221521344035864, 0.00034121860517188907, -0.020603187382221222, -0.011182605288922787, -0.011349095962941647, -0.007270080968737602, 0.03884775936603546, -0.016232814639806747, 7.668747275602072e-05, -0.018938282504677773, 0.040873393416404724, 0.01576109230518341, 0.032798610627651215, 0.0067047071643173695, -0.03257662057876587, -0.01071088295429945, -0.002186920726671815, 0.018535930663347244, -0.0074435085989534855, -0.0016180785605683923, -0.009108412079513073, 0.023946870118379593, -0.03690537437796593, -0.024030115455389023, -0.007582250516861677, 0.015539104118943214, -0.02534816414117813, -0.008275960572063923, -0.015261620283126831, -0.019853981211781502, 0.0053346301428973675, 0.0011671670945361257, -0.02440471760928631, 0.05225023627281189, -0.010988366790115833, -0.004113700240850449 , 0.020686432719230652, ...]

测试.py

import openai

response = openai.Embedding.create(
  input = 'Create embedding for this text',
  model = 'text-embedding-ada-002'
)

content = response['data'][0]['embedding']

print(content)

可能是什么问题呢?

您可能使用的 Python 版本太旧,OpenAI 无法正常工作。

第 1 步:升级 Python

this

第 2 步:升级 OpenAI Python 库

pip install -U openai

【讨论】:

    猜你喜欢
    • 2023-02-26
    • 2018-04-14
    • 2019-02-18
    • 1970-01-01
    • 2020-01-01
    • 2019-07-20
    • 2021-11-05
    • 2021-11-04
    相关资源
    最近更新 更多