Learn R Programming

contentanalysis (version 0.2.1)

gemini_content_ai: Process Content with Google Gemini AI

Description

Send images and/or documents to Google Gemini AI for content analysis and generation. The function supports multiple file types including images (PNG, JPG, etc.) and documents (PDF, TXT, HTML, CSV, RTF).

Usage

gemini_content_ai(
  image = NULL,
  docs = NULL,
  prompt = "Explain these images",
  model = "2.5-flash",
  image_type = "png",
  retry_503 = 5,
  api_key = NULL,
  outputSize = "medium"
)

Value

Character vector containing the AI-generated response(s), or an error message string starting with "ERROR:" if the request fails.

Arguments

image

Character vector. Path(s) to image file(s) to be processed. Default is NULL.

docs

Character vector. Path(s) to document file(s) to be processed. Default is NULL.

prompt

Character. The prompt/instruction for the AI model. Default is "Explain these images".

model

Character. The Gemini model version to use. Default is "2.5-flash". Options include "2.5-flash", etc.

image_type

Character. The image MIME type. Default is "png".

retry_503

Integer. Number of retry attempts for HTTP 503 errors. Default is 5.

api_key

Character. Google Gemini API key. If NULL, uses the GEMINI_API_KEY environment variable.

outputSize

Character. Controls the maximum output tokens. Options are:

  • "small": 8,192 tokens

  • "medium": 16,384 tokens (default)

  • "large": 32,768 tokens

  • "huge": 131,072 tokens

Details

The function handles various error scenarios including:

  • Missing or invalid files

  • Invalid API keys (HTTP 400)

  • Service unavailability (HTTP 503/429) with automatic retry

  • File encoding errors

Supported document types: PDF, TXT, HTML, CSV, RTF

Examples

Run this code
if (FALSE) {
# Process an image
result <- gemini_content_ai(
  image = "path/to/image.png",
  prompt = "Describe this image in detail"
)

# Process a PDF document
result <- gemini_content_ai(
  docs = "path/to/document.pdf",
  prompt = "Summarize this document",
  outputSize = "large"
)

# Process multiple images and documents
result <- gemini_content_ai(
  image = c("img1.png", "img2.png"),
  docs = c("doc1.pdf", "doc2.txt"),
  prompt = "Compare these materials"
)
}

Run the code above in your browser using DataLab