正如摘要所述,本文将深入探讨在使用 GPT-4 Vision API 处理大量图像时遇到的常见问题,即由于 API 的速率限制导致的错误。我们将分析问题代码,解释速率限制的原因,并提供一系列可行的解决方案,帮助开发者有效地利用 GPT-4 Vision API。
提供的代码旨在利用 GPT-4 Vision API 为 Google Drive 中的大量图片生成 SEO 优化的元描述。代码的功能主要包括:
问题在于,代码在处理大约 100 张图片后开始返回 "Error",这很可能与 OpenAI API 的速率限制有关。
OpenAI 的 GPT-4 Vision API 具有速率限制,旨在防止滥用并确保所有用户都能获得公平的服务。速率限制的具体数值取决于用户的使用层级。这意味着,免费用户可能具有较低的速率限制,而付费用户则可能具有更高的限制。
如何查看速率限制?
可以在 OpenAI 平台的文档中找到关于速率限制的详细信息。访问OpenAI 速率限制文档可以了解不同使用层级的具体限制。
以下是一些解决 GPT-4 Vision API 速率限制问题的方案:
了解并监控速率限制:
优化代码,减少 API 调用频率:
处理速率限制错误:
升级 OpenAI 使用层级:
以下代码示例展示了如何在原始代码中添加重试机制,以处理速率限制错误:
import os import base64 import requests import pandas as pd from google.colab import drive import time drive.mount('/content/drive') image_folder = '/content/drive/MyDrive/Work related/FS/Imagenes/Metadescripciones HC ' api_key = 'my api code goes here XXXXX' def encode_image(image_path): with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode('utf-8') headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" } results_df = pd.DataFrame(columns=['Nombre del Archivo', 'Metadescripcion']) def get_metadescription(base64_image): payload = { "model": "gpt-4-vision-preview", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Write a meta description for the image of this product, optimized for SEO and in less than 150 words" }, { "type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{base64_image}", "detail": "low" } } ] } ], "max_tokens": 400 } max_retries = 5 for attempt in range(max_retries): try: response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload) response_json = response.json() if response.status_code == 200: return response_json['choices'][0]['message']['content'] else: print(f"Attempt {attempt 1} failed with status code: {response.status_code}") if response.status_code == 429: # Rate limit error wait_time = (2 ** attempt) # Exponential backoff print(f"Rate limit exceeded. Waiting {wait_time} seconds before retrying.") time.sleep(wait_time) else: print(f"Unexpected error: {response_json}") return 'Error' # Return error for other status codes except Exception as e: print(f"An exception occurred: {e}") return 'Error' return 'Error' # Return error after all retries failed for filename in os.listdir(image_folder): if filename.endswith((".png", ".jpg", ".jpeg")): image_path = os.path.join(image_folder, filename) base64_image = encode_image(image_path) metadescription = get_metadescription(base64_image) new_row = pd.DataFrame({'Nombre del Archivo': [filename], 'Metadescripcion': [metadescription]}) results_df = pd.concat([results_df, new_row], ignore_index=True) results_df.to_excel('/content/drive/MyDrive/Work related/FS/Imagenes/Metadescripciones.xlsx', index=False)
代码解释:
在使用 GPT-4 Vision API 处理大量图片时,速率限制是一个常见的问题。通过了解速率限制、优化代码、添加重试机制以及考虑升级使用层级,可以有效地解决这个问题,并更高效地利用 GPT-4 Vision API。 记住,良好的 API 使用习惯不仅可以提高效率,还可以避免不必要的费用。
以上就是使用 GPT-4 Vision API 处理大量图片时出现错误:速率限制及解决方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号