OpenAI GPT3 Search API not working locally

题意:"OpenAI GPT-3 搜索 API 在本地无法工作"

问题背景:

I am using the python client for GPT 3 search model on my own Jsonlines files. When I run the code on Google Colab Notebook for test purposes, it works fine and returns the search responses. But when I run the code on my local machine (Mac M1) as a web application (running on localhost) using flask for web service functionalities, it gives the following error:

"我正在使用 Python 客户端在我自己的 Jsonlines 文件上运行 GPT-3 搜索模型。当我在 Google Colab Notebook 上运行代码进行测试时,它可以正常工作并返回搜索结果。但是,当我在本地机器(Mac M1)上将代码作为一个使用 Flask 提供网络服务功能的 Web 应用程序(在本地主机上运行)时,却出现了以下错误:"

python 复制代码
openai.error.InvalidRequestError: File is still processing.  Check back later.

This error occurs even if I implement the exact same example as given in OpenAI documentation. The link to the search example is given here.

"即使我按照 OpenAI 文档中提供的完全相同的示例实现代码,也会出现此错误。搜索示例的链接在这里提供。"

It runs perfectly fine on local machine and on colab notebook if I use the completion API that is used by the GPT3 playground. (code link here)

"如果我使用 GPT-3 Playground 使用的 completion API,无论是在本地机器还是在 Colab Notebook 上,代码都能正常运行。(代码链接在此)"

The code that I have is as follows:

"我使用的代码如下:"

python 复制代码
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)

Any idea why this strange behaviour is occurring and how can I solve it? Thanks.

"有什么想法为什么会出现这种奇怪的行为,我该如何解决?谢谢。"

问题解决:

The problem was on this line:

"问题出在这一行:"

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

It returns the call with a file ID and status uploaded which makes it seem like the upload and file processing is complete. I then passed that fileID to the search API, but in reality it had not completed processing and so the search API threw the error openai.error.InvalidRequestError: File is still processing. Check back later.

"它返回了一个包含文件 ID 和状态为 'uploaded' 的调用,这让它看起来像是上传和文件处理已经完成。然后我将该 fileID 传递给搜索 API,但实际上处理尚未完成,因此搜索 API 抛出了错误 `openai.error.InvalidRequestError: File is still processing. Check back later.`"

The returned file object looks like this (misleading):

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

It worked in google colab because the openai.File.create call and the search call were in 2 different cells, which gave it the time to finish processing as I executed the cells one by one. If I write all of the same code in one cell, it gave me the same error there.

"它在 Google Colab 中工作正常,因为 `openai.File.create` 调用和搜索调用在两个不同的单元格中,这让我在逐个执行单元格时给了它完成处理的时间。如果我把所有代码写在一个单元格中,它也会给我同样的错误。"

So, I had to introduce a wait time for 4-7 seconds depending on the size of your data, time.sleep(5) after openai.File.create call before calling the openai.Engine("davinci").search call and that solved the issue. :)

"因此,我必须在调用 `openai.File.create` 后引入一个 4-7 秒的等待时间,根据数据的大小,使用 `time.sleep(5)`,然后再调用 `openai.Engine("davinci").search`,这样就解决了问题。:)"

相关推荐
阡之尘埃1 小时前
Python数据分析案例61——信贷风控评分卡模型(A卡)(scorecardpy 全面解析)
人工智能·python·机器学习·数据分析·智能风控·信贷风控
丕羽5 小时前
【Pytorch】基本语法
人工智能·pytorch·python
bryant_meng5 小时前
【python】Distribution
开发语言·python·分布函数·常用分布
鱼满满记5 小时前
1.6K+ Star!GenAIScript:一个可自动化的GenAI脚本环境
人工智能·ai·github
m0_594526306 小时前
Python批量合并多个PDF
java·python·pdf
工业互联网专业6 小时前
Python毕业设计选题:基于Hadoop的租房数据分析系统的设计与实现
vue.js·hadoop·python·flask·毕业设计·源码·课程设计
钱钱钱端6 小时前
【压力测试】如何确定系统最大并发用户数?
自动化测试·软件测试·python·职场和发展·压力测试·postman
慕卿扬6 小时前
基于python的机器学习(二)—— 使用Scikit-learn库
笔记·python·学习·机器学习·scikit-learn
manfulshark7 小时前
OPENAI官方prompt文档解析
ai·prompt
Json____7 小时前
python的安装环境Miniconda(Conda 命令管理依赖配置)
开发语言·python·conda·miniconda