Kaa*_*kci 5 python openai-api gpt-3
我想使用 GPT 4 模型将 csv 文件中的文本翻译成英语,但我不断收到以下错误。即使我更新了版本,我仍然收到相同的错误。
\nimport openai\nimport pandas as pd\nimport os\nfrom tqdm import tqdm\n\n\nopenai.api_key = os.getenv("API")\n\ndef translate_text(text):\n response = openai.Completion.create(\n model="text-davinci-003", # GPT-4 modeli\n prompt=f"Translate the following Turkish text to English: '{text}'",\n max_tokens=60\n )\n # Yeni API yap\xc4\xb1s\xc4\xb1na g\xc3\xb6re yan\xc4\xb1t\xc4\xb1n al\xc4\xb1nmas\xc4\xb1\n return response.choices[0].text.strip()\n\ndf = pd.read_excel('/content/3500-turkish-dataset-column-name.xlsx')\n\ncolumn_to_translate = 'review'\n\ndf[column_to_translate + '_en'] = ''\n\nfor index, row in tqdm(df.iterrows(), total=df.shape[0]):\n translated_text = translate_text(row[column_to_translate])\n df.at[index, column_to_translate + '_en'] = translated_text\n\ndf.to_csv('path/to/your/translated_csvfile.csv', index=False)\n\nRun Code Online (Sandbox Code Playgroud)\n 0%| | 0/3500 [00:00<?, ?it/s]\n---------------------------------------------------------------------------\nAPIRemovedInV1 Traceback (most recent call last)\n<ipython-input-27-337b5b6f4d32> in <cell line: 29>()\n 28 # Her sat\xc4\xb1rdaki metni \xc3\xa7evir ve yeni s\xc3\xbctuna kaydet\n 29 for index, row in tqdm(df.iterrows(), total=df.shape[0]):\n---> 30 translated_text = translate_text(row[column_to_translate])\n 31 df.at[index, column_to_translate + '_en'] = translated_text\n 32 \n\n3 frames\n/usr/local/lib/python3.10/dist-packages/openai/lib/_old_api.py in __load__(self)\n\nAPIRemovedInV1: \n\nYou tried to access openai.Completion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.\n\nYou can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface. \n\nAlternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`\n\nA detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742\nRun Code Online (Sandbox Code Playgroud)\n即使我更新了 OpenAI 软件包版本,我也遇到了同样的错误。
\n您尝试使用的方法名称不适用于 OpenAI Python SDK 版本1.0.0或更高版本。
旧的 SDK(即 version 0.28)使用以下方法名称:
client.Completion.create
Run Code Online (Sandbox Code Playgroud)
新的 SDK(即版本1.0.0或更新版本)使用以下方法名称:
client.completions.create
Run Code Online (Sandbox Code Playgroud)
注意:请小心,因为 API 区分大小写(即client.Completions.create不适用于新的 SDK 版本)。
尝试这个:
import os
from openai import OpenAI
client = OpenAI()
OpenAI.api_key = os.getenv('OPENAI_API_KEY')
completion = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt="Say this is a test",
max_tokens=7,
temperature=0
)
print(completion.choices[0].text)
Run Code Online (Sandbox Code Playgroud)