def Analysis_file(file_path):
try:
# Загрузить текст из файла
text = Path(file_path) .read_text(encoding="utf-8").strip()
if len(text) < 500: # Пропускать короткие файлы
print(f"Пропуск недостаточного файла: {file_path}")
возврат Нет
Код: Выделить всё
# Retry logic for rate limits
max_retries = 5
retry_delay = 2 # Start with a 2-second delay
for attempt in range(max_retries):
try:
# Call OpenAI API to process the text
response = openai.ChatCompletion.create(
model="gpt-4o-mini", # Use a valid model ID
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": f"{prompt}\n\n{text}"}
]
)
# Extract the response content
result = response["choices"][0]["message"]["content"]
if not result.strip():
print(f"Empty response for file: {file_path}")
return None
# Log raw response for debugging
print(f"Raw API response for file {file_path}: {result}")
return result
except openai.error.RateLimitError:
print(f"Rate limit reached. Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
retry_delay *= 2 # Exponential backoff
print(f"Failed to process file {file_path} after {max_retries} retries.")
return None
except Exception as e:
print(f"Error analyzing file: {file_path}\n{e}")
return None
Подробнее здесь: https://stackoverflow.com/questions/792 ... ttribute-e
Мобильная версия