Complete API documentation for SA-RAG.
Main entry point for SA-RAG functionality.
Initialize the RAG system.
Parameters:
llm_provider(str): LLM provider ("openai", "deepseek", "local", "mock")embedding_provider(str): Embedding provider ("openai", "deepseek", "local", "mock")**kwargs: Additional configuration parameters
Example:
rag = RAG(
llm_provider="openai",
embedding_provider="openai"
)Index multiple documents.
Parameters:
texts(List[str]): List of document textsgenerate_embeddings(bool): Whether to generate embeddings
Returns:
List[int]: List of document IDs
Example:
doc_ids = rag.index_documents([
"Document 1 text...",
"Document 2 text...",
], generate_embeddings=True)Ask a question and get an answer.
Parameters:
query(str): User querytop_k(int): Number of top results to retrieveuse_graph(bool): Whether to use graph expansionuse_memory(bool): Whether to use long-term memory
Returns:
dict: Dictionary containing:answer(str): Generated answerused_semantic_nodes(List[dict]): Semantic nodes usedused_graph_nodes(List[dict]): Graph nodes usedused_memory_nodes(List[dict]): Memory nodes usedscoring_details(dict): Scoring informationraw_results(List[dict]): Raw retrieval results
Example:
result = rag.ask(
query="What is the main topic?",
top_k=5,
use_graph=True
)
print(result['answer'])Search for relevant documents without generating an answer.
Parameters:
query(str): Query texttop_k(int): Number of results to returnuse_graph(bool): Whether to use graph expansionuse_memory(bool): Whether to use long-term memory
Returns:
List[dict]: List of search results
Add a long-term memory.
Parameters:
text(str): Memory contentimportance(float): Importance score (0.0-1.0)
Example:
rag.add_memory("User prefers detailed explanations", importance=0.8)Update a document using differential indexing.
Parameters:
doc_id(int): Document IDnew_text(str): New document text
Lower-level client API for more control.
Index documents.
Search for documents.
Ask a question.
Get system statistics.
Core RAG pipeline implementation.
index_documents(texts: List[str], generate_embeddings: bool = True, batch_size: int = 32) -> List[int]
Index documents with embedding generation.
search(query: str, top_k: int = 3, use_graph_expansion: bool = False, graph_hops: int = 1, use_memory: bool = False) -> List[dict]
Search for relevant documents.
Complete Q&A workflow.
Generate answer from context.
Rust core engine exposed via PyO3.
Index documents and return document IDs.
search(query: str, top_k: int, query_vector: Optional[List[float]] = None) -> List[Tuple[str, float]]
Search for relevant documents.
Returns:
List[Tuple[str, float]]: List of (text, score) tuples
search_full(query: str, top_k: int, query_vector: Optional[List[float]] = None) -> List[Tuple[int, str, float]]
Full search returning node IDs.
Returns:
List[Tuple[int, str, float]]: List of (node_id, text, score) tuples
Update embeddings for nodes.
Get all node IDs for a document.
Get node information.
Returns:
dict: Node metadata including text, level, node_type, etc.
Get node text content.
Get node metadata.
Expand nodes in the graph.
Parameters:
node_ids(List[int]): Seed node IDshops(int): Number of hops for expansion
Returns:
List[int]: Expanded node IDs
Smart graph expansion with filtering.
Parameters:
node_ids(List[int]): Seed node IDshops(int): Number of hopsmin_weight(float): Minimum edge weight thresholdmax_nodes(int): Maximum number of nodes
Returns:
List[int]: Expanded node IDs
Add a long-term memory.
Search long-term memory.
Update a document using differential indexing.
Get graph statistics.
Returns:
dict: Dictionary withnum_nodesandnum_edges
Embedding vector generation service.
Initialize embedding service.
Get embedding for a single text.
Get embeddings for multiple texts.
Large Language Model service.
Initialize LLM service.
chat_completion(prompt: str, system_prompt: str = "...", context: Optional[List[dict]] = None, max_tokens: Optional[int] = None) -> str
Generate chat completion.
generate_with_rag(query: str, retrieved_context: List[dict], system_prompt: Optional[str] = None) -> str
Generate answer with RAG context.
Query orchestration and result fusion.
Rewrite query for better retrieval.
Plan retrieval strategy.
Fuse results from multiple retrieval methods.
Methods:
"rrf": Reciprocal Rank Fusion"weighted": Weighted fusion"max": Maximum score fusion