% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/main_func.R
\name{main_func}
\alias{main_func}
\title{Main Text Processing Function for AI-Based Variable Extraction}
\usage{
main_func(df, to_code_max_id, n_post, provider, worker_env = NULL)
}
\arguments{
\item{df}{Data frame. Input data subset containing text to be processed.}

\item{to_code_max_id}{Integer. Maximum number of posts to process in this batch.}

\item{n_post}{Integer. Number of posts to process per API call (typically 15).}

\item{provider}{Character. AI provider to use, either "OpenAI" or "Groq".}

\item{worker_env}{Environment or NULL. When running in a parallel worker, the worker's
package environment. If NULL (default), \code{.package_env} is used.}
}
\value{
List of data frames. Each element contains successfully processed results
with extracted variables. Returns empty list if no successful processing.
}
\description{
This function processes batches of text data using AI models to extract
machine-readable variables. It implements robust error handling and retry
logic to ensure reliable processing even with API failures.
}
\details{
The function implements a robust processing loop that:
\itemize{
\item Samples posts randomly to avoid processing order bias
\item Makes API calls in manageable batches
\item Validates and cleans AI responses
\item Handles API failures with counter and early stopping
\item Tracks missing IDs for potential reprocessing
\item Collects dataframes into a list for later binding
}

The AI models are instructed to return data in CSV format for efficient parsing.
The function expects responses with exactly N columns as set by set_parameters().
}
\examples{
\dontrun{
# For set_parameters(n_variables = 6):
instruction <- "Extract variables: var1,var2,var3,var4,var5,var6"
result <- main_func(
  df = my_data_subset,
  to_code_max_id = 30,
  n_post = 15,
  provider = "OpenAI"
)
processed_data <- bind_rows(result)  # Combine all dataframes
}

}
\seealso{
\code{\link[=gpt_func]{gpt_func()}}, \code{\link[=groq_func]{groq_func()}}, \code{\link[=make_value_row]{make_value_row()}}
}
