gpt4 book ai didi

python - OpenAI GPT3 搜索 API 无法在本地运行

转载 作者:行者123 更新时间:2023-12-02 05:46:24 25 4
gpt4 key购买 nike

我在自己的 Jsonlines 文件上使用 python 客户端进行 GPT 3 搜索模型。当我出于测试目的在 Google Colab Notebook 上运行代码时,它工作正常并返回搜索响应。但是,当我在本地计算机 (Mac M1) 上运行代码作为 Web 应用程序(在本地主机上运行)时,使用 flask 实现 Web 服务功能,它会出现以下错误:

openai.error.InvalidRequestError: File is still processing.  Check back later.

即使我实现与 OpenAI 文档中给出的示例完全相同的示例,也会发生此错误。 search example is given here 的链接.

如果我使用 GPT3 playground 使用的完成 API,它在本地机器和 colab notebook 上运行得非常好。 ( code link here )

我的代码如下:

import openai

openai.api_key = API_KEY

file = openai.File.create(file=open(jsonFileName), purpose="search")

response = openai.Engine("davinci").search(
search_model = "davinci",
query = query,
max_rerank = 5,
file = file.id
)
for res in response.data:
print(res.text)

知道为什么会出现这种奇怪的行为吗?我该如何解决?谢谢。

最佳答案

问题出在这一行:

file = openai.File.create(file=open(jsonFileName), purpose="search")

它返回带有文件 ID 和上传状态的调用,这使得上传和文件处理似乎已完成。然后我将该文件 ID 传递给搜索 API,但实际上它还没有完成处理,因此搜索 API 抛出错误 openai.error.InvalidRequestError: File is still processing。稍后再回来查看。

返回的文件对象看起来像这样(误导):

enter image description here

它在 google colab 中工作,因为 openai.File.create 调用和搜索调用在 2 个不同的单元格中,这使它有时间在我一个一个地执行单元格时完成处理。如果我在一个单元格中编写所有相同的代码,就会出现相同的错误。

因此,我不得不引入 4-7 秒的等待时间,具体取决于您的数据大小,time.sleep(5) 在 openai.File.create 调用之后,然后调用 openai。 Engine("davinci").search 调用并解决了问题。 :)

关于python - OpenAI GPT3 搜索 API 无法在本地运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70408322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com