Retrieve relevant document chunks using semantic search with the Graphor Python SDK
The retrieve_chunks method allows you to retrieve relevant document chunks from your ingested documents using semantic search. It returns the most relevant content for your query, making it ideal for building custom RAG (Retrieval-Augmented Generation) applications with your preferred LLM.
Retrieval from Specific Documents (using file_ids)
Copy
from graphor import Graphorclient = Graphor()# Restrict retrieval to specific files using file_ids (preferred)result = client.sources.retrieve_chunks( query="What is the total amount due?", file_ids=["file_abc123", "file_def456"])print(f"Found {result.total} chunks from specified files")for chunk in result.chunks: print(f"[{chunk.file_id}] {chunk.text[:100]}...")
Retrieval from Specific Documents (using file_names - deprecated)
Copy
from graphor import Graphorclient = Graphor()# Restrict retrieval to specific files using file_names (deprecated)result = client.sources.retrieve_chunks( query="What is the total amount due?", file_names=["invoice-2024.pdf", "invoice-2023.pdf"])print(f"Found {result.total} chunks from specified files")for chunk in result.chunks: print(f"[{chunk.file_name}] {chunk.text[:100]}...")
from graphor import Graphorimport google.generativeai as genaigraphor_client = Graphor()genai.configure(api_key="YOUR_GOOGLE_API_KEY")model = genai.GenerativeModel("gemini-pro")def rag_with_gemini(question: str, file_names: list[str] | None = None) -> str: """RAG pipeline using Google Gemini.""" # Retrieve chunks result = graphor_client.sources.retrieve_chunks( query=question, file_names=file_names ) # Build context context = "\n\n".join([ f"[Source: {chunk.file_name}, Page {chunk.page_number}]\n{chunk.text}" for chunk in result.chunks ]) # Generate answer with Gemini prompt = f"""Based on the following context, answer the question.Context:{context}Question: {question}Please provide a clear answer and cite the sources.""" response = model.generate_content(prompt) return response.text# Usageanswer = rag_with_gemini("What products are mentioned in the catalog?")print(answer)
from graphor import Graphorfrom dataclasses import dataclassfrom typing import Any@dataclassclass Citation: file_name: str page_number: int text_snippet: str@dataclassclass RAGResponse: answer: str citations: list[Citation] confidence: floatclass CitedRAG: def __init__(self, llm_client: Any): self.graphor = Graphor() self.llm = llm_client # Your LLM client (OpenAI, Anthropic, etc.) def ask( self, question: str, file_names: list[str] | None = None ) -> RAGResponse: """Ask a question with proper source citations.""" # Retrieve relevant chunks result = self.graphor.sources.retrieve_chunks( query=question, file_names=file_names ) if not result.chunks: return RAGResponse( answer="I couldn't find relevant information to answer this question.", citations=[], confidence=0.0 ) # Build numbered context for citation context_parts = [] citations = [] for i, chunk in enumerate(result.chunks, 1): context_parts.append(f"[{i}] {chunk.text}") citations.append(Citation( file_name=chunk.file_name or "Unknown", page_number=chunk.page_number or 0, text_snippet=chunk.text[:100] + "..." )) context = "\n\n".join(context_parts) # Generate answer with citation instructions prompt = f"""Based on the following numbered sources, answer the question.When citing information, use the source number in brackets like [1], [2], etc.Sources:{context}Question: {question}Answer with citations:""" # Use your LLM to generate the answer # This example assumes an OpenAI-compatible interface answer = self._generate_with_llm(prompt) # Calculate confidence based on top chunk scores avg_score = sum(c.score or 0 for c in result.chunks) / len(result.chunks) return RAGResponse( answer=answer, citations=citations, confidence=avg_score ) def _generate_with_llm(self, prompt: str) -> str: """Generate response with your LLM. Implement based on your LLM client.""" # Example with OpenAI: response = self.llm.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": prompt}] ) return response.choices[0].message.content# Usagefrom openai import OpenAIrag = CitedRAG(llm_client=OpenAI())response = rag.ask("What are the payment terms?")print(f"Answer: {response.answer}")print(f"\nConfidence: {response.confidence:.2%}")print(f"\nCitations:")for i, citation in enumerate(response.citations, 1): print(f" [{i}] {citation.file_name}, Page {citation.page_number}")
Build a system that retrieves, reasons, and retrieves again if needed:
Copy
from graphor import Graphorclient = Graphor()def multi_step_rag(question: str, max_steps: int = 3) -> dict: """Multi-step RAG with iterative retrieval.""" all_chunks = [] queries_used = [question] for step in range(max_steps): # Retrieve chunks for current query current_query = queries_used[-1] result = client.sources.retrieve_chunks(query=current_query) if not result.chunks: break all_chunks.extend(result.chunks) # Analyze if we need more information (simplified logic) # In practice, you'd use an LLM to determine follow-up queries if len(all_chunks) >= 5 or step == max_steps - 1: break # Generate follow-up query based on what we found # This is a simplified example; use your LLM for better follow-ups follow_up = generate_follow_up_query(question, all_chunks) if follow_up and follow_up not in queries_used: queries_used.append(follow_up) else: break return { "chunks": all_chunks, "queries_used": queries_used, "steps": len(queries_used) }def generate_follow_up_query(original_question: str, chunks: list) -> str | None: """Generate a follow-up query. Use your LLM for better results.""" # Simplified example - in practice, use an LLM if "payment" in original_question.lower(): return "invoice due date late fees" return None# Usageresult = multi_step_rag("What are the payment terms and penalties?")print(f"Used {result['steps']} retrieval steps")print(f"Queries: {result['queries_used']}")print(f"Total chunks: {len(result['chunks'])}")
Use file_names — Restrict to specific documents when you know the source
Filter by score — Consider filtering chunks with low relevance scores
Limit results — Process only the top N most relevant chunks
Copy
# Example: Filter low-score chunksresult = client.sources.retrieve_chunks(query="payment terms")# Only use chunks with high relevancehigh_quality_chunks = [ chunk for chunk in result.chunks if chunk.score and chunk.score > 0.7]