AttributeError:模块“openai”没有属性“APIConnectionError”

问题描述 投票:0回答:1

我尝试使用GPT openai 将韩语翻译成英语。 我的文本数据是一种列表类型,包含3000多个句子,所以我更新了付款方式,并利用“backoff”库来请求错误;但是,最终会出现 APIConnectionError。为了获得结果 ASPS,我想“尝试 & except”,但它仍然给我一个错误消息,“AttributeError: module 'openai' has no attribute 'APIConnectionError”。请帮帮我!

import backoff
import pandas as pd
import openai
import os
import tiktoken
import re
from google.colab import drive
drive.mount('/content/drive')


tag_df = pd.read_csv('/wo_employee_post.csv')
tag_df = pd.DataFrame(tag_df.contents.dropna())#.contents.str.contains('\$')
tag_list = tag_df[tag_df.contents.str.contains('\$')].contents

def text_cleaning(doc):
    doc = re.sub("[^ㄱ-ㅎㅏ-ㅣ가-힣 ]", "", doc)
    doc = re.sub("[\{\}\[\]\/?.,;:|\)*~`!^\-_+<>@\#$%&\\\=\(\'\"]", " ", doc)

    return doc

clean_tag = []
for tag in tag_list:
    clean_tag.append(text_cleaning(tag))


os.environ["OPENAI_API_KEY"] = "hidden"
print(os.getenv("OPENAI_API_KEY"))
openai.api_key = os.getenv("OPENAI_API_KEY")


@backoff.on_exception(backoff.expo, openai.error.RateLimitError)
def translate(text, engine="text-davinci-002"):
    p = f'Transform this sentence from korean to english: {text}'
        
    # generate the response
    response = openai.Completion.create(
        engine=engine,
        prompt=p,
        temperature=.7,
        #max_tokens=5000,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
        stop=["12."]
    )

    return response['choices'][0]['text']


tag_in_eng = []
for tag in clean_tag:
    try: 
        tag_in_eng.append(translate(tag))
    
    except openai.APIConnectionError:
        continue

错误详情


ConnectionResetError Traceback(最近一次调用最后一次) urlopen 中的/usr/local/lib/python3.9/dist-packages/urllib3/connectionpool.py(自身、方法、url、正文、标头、重试、重定向、assert_same_host、超时、pool_timeout、release_conn、分块、body_pos、* *响应_kw) 702 # 在httplib连接对象上发出请求。 --> 703 httplib_response = self._make_request( 第704章 康涅狄格州,

34帧 ConnectionResetError:[Errno 104] 连接被对等方重置

在处理上述异常的过程中,又发生了一个异常:

协议错误回溯(最近一次调用最后一次) ProtocolError: ('连接中止。', ConnectionResetError(104, '连接被对等方重置'))

在处理上述异常的过程中,又发生了一个异常:

ConnectionError Traceback(最近一次调用最后一次) ConnectionError: ('连接中止。', ConnectionResetError(104, '连接被对等方重置'))

上述异常是导致以下异常的直接原因:

APIConnectionError Traceback(最近一次调用最后一次) APIConnectionError:与 OpenAI 通信时出错:(“连接已中止。”,ConnectionResetError(104,“对等方重置连接”))

在处理上述异常的过程中,又发生了一个异常:

AttributeError Traceback(最近一次调用最后一次) 在 4 tag_in_eng.append(翻译(标签)) 5 ----> 6 除了 openai.APIConnectionError: 7 继续

AttributeError:模块“openai”没有属性“APIConnectionError”

attributeerror openai-api
1个回答
0
投票

更改此行:

except openai.APIConnectionError:
except openai.error.APIConnectionError:
那么一切都应该有效。

© www.soinside.com 2019 - 2024. All rights reserved.