[Python] Gemini Vertex AI Error upon uploading large file: 400 Precondition check failed

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to upload files to Google's Gemini 1.5 Model via Vertex AI from google cloud. Despite the file limit being 30 mbs, a 11 mbs file raises this following error: 400 Precondition check failed

    This is literally all the information google gives me in return. Could someone please help me troubleshoot and identify what the issue is?

    async def async_get_model_output(prompt, filepaths, content_type):

    vertexai.init(project=os.getenv("PROJECT_ID"), location="us-central1")
    model = GenerativeModel(model_name="gemini-1.5-flash-001")
    #Figure
    parts = [Part.from_uri(filepath["gcs_link"], mime_type=filepath["mimetype"]) for filepath in filepaths]
    parts.append(prompt)
    # Create a GenerationConfig object with the specified parameters
    generation_config = GenerationConfig(
    temperature=0.3,
    top_p=0.3,
    max_output_tokens = 5000
    # You can add other parameters here as needed
    )
    #BLOCK_ONLY_HIGH
    safety_settings = [
    SafetySetting(
    category=HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
    threshold=HarmBlockThreshold.BLOCK_ONLY_HIGH,
    ),
    SafetySetting(
    category=HarmCategory.HARM_CATEGORY_HARASSMENT,
    threshold=HarmBlockThreshold.BLOCK_ONLY_HIGH,
    ),
    SafetySetting(
    category=HarmCategory.HARM_CATEGORY_HATE_SPEECH,
    threshold=HarmBlockThreshold.BLOCK_ONLY_HIGH,
    ),
    SafetySetting(
    category=HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
    threshold=HarmBlockThreshold.BLOCK_ONLY_HIGH,
    ),
    # Add other categories as needed
    ]

    for attempt in range(3):
    print(f'Attempt {attempt + 1} to get model output')
    try:
    token_count = model.count_tokens(parts)
    print(f"Token count: {token_count}")
    print(f"##{content_type}## DEBUG: INVOKING VERTEX AI")
    response = await asyncio.to_thread(
    model.generate_content,
    parts,
    generation_config=generation_config,
    safety_settings=safety_settings
    )
    logger.debug(f"Received response: {response}")
    json_match = re.search(r'\{[\s\S]*\}', response.text)

    if json_match:
    json_string = json_match.group(0)
    try:
    study_guide_data = json.loads(json_string)
    print(f"##{content_type}## DEBUG CONTENT SUCCESS: Successfully Generated content and parsed JSON")
    return json_string
    except json.JSONDecodeError as e:
    print(f"##{content_type}##JSON decode error: {e}")
    print(f"##{content_type}##Extracted JSON string:", json_string)
    # Continue to next attempt
    else:
    print(f"##{content_type}## No JSON object found in the response")
    except Exception as e:
    error_message = str(e)
    truncated_message = error_message[:197] + "..." if len(error_message) > 200 else error_message
    print(f"##{content_type}## Unexpected error during attempt {attempt + 1}: {error_message}")

    # If we reach here, the current attempt failed. We'll continue to the next one.
    print(f"##{content_type}## Attempt {attempt + 1} failed. Moving to next attempt.")


    if attempt < 2: # Don't sleep after the last attempt
    print(f"##{content_type}## Waiting 1 seconds before next attempt...")
    await asyncio.sleep(1)
    # If we've exhausted all attempts without success
    raise Exception(f"##{content_type}## Failed to get valid JSON after 3 attempts")


    Above is my code, please let me know if you all need anything else

    Continue reading...

Compartilhe esta Página