% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/semantic_analysis.R
\name{run_rag_search}
\alias{run_rag_search}
\title{RAG Semantic Search}
\usage{
run_rag_search(
  query,
  documents,
  provider = c("openai", "gemini"),
  api_key = NULL,
  embedding_model = NULL,
  chat_model = NULL,
  top_k = 5
)
}
\arguments{
\item{query}{Character string, user question}

\item{documents}{Character vector, corpus to search}

\item{provider}{Character string, provider: "openai" or "gemini"}

\item{api_key}{Character string, API key (or from
OPENAI_API_KEY/GEMINI_API_KEY env).}

\item{embedding_model}{Character string, embedding model. Defaults:
"text-embedding-3-small" (openai), "gemini-embedding-001" (gemini)}

\item{chat_model}{Character string, chat model. Defaults:
"gpt-4.1-mini" (openai), "gemini-2.5-flash-lite" (gemini)}

\item{top_k}{Integer, number of documents to retrieve (default: 5)}
}
\value{
List with:
\itemize{
\item success: Logical
\item answer: Generated answer
\item confidence: Confidence score (0-1)
\item sources: Vector of source document indices
\item retrieved_docs: Retrieved document chunks
\item scores: Similarity scores
}
}
\description{
Simple in-memory RAG (Retrieval Augmented Generation) for question-answering
over document corpus with source attribution. Uses OpenAI or Gemini
embeddings for semantic search and LLM for answer generation.
}
\details{
Simple RAG workflow:
\enumerate{
\item Generate embeddings for documents and query
\item Find top-k similar documents via cosine similarity
\item Generate answer using LLM with retrieved context
}
}
\examples{
if (interactive()) {
documents <- c(
  "Assistive technology helps students with disabilities access curriculum.",
  "Universal Design for Learning provides multiple means of engagement.",
  "Response to Intervention uses tiered support systems."
)

# Using OpenAI (requires API key)
result <- run_rag_search(
  query = "How does assistive technology support learning?",
  documents = documents,
  provider = "openai"
)

if (result$success) {
  cat("Answer:", result$answer, "\n")
  cat("Sources:", paste(result$sources, collapse = ", "), "\n")
}
}
}
\seealso{
\code{\link[=get_best_embeddings]{get_best_embeddings()}} for the retrieval step alone; \code{\link[=call_llm_api]{call_llm_api()}} for the answer-generation step alone; \code{\link[=sanitize_llm_input]{sanitize_llm_input()}} for an input safety check before calling
}
\concept{ai}
