Search and RAG

Search and retrieve information using vectors, text, and RAG

R2R provides powerful search and retrieval capabilities through vector search, full-text search, and Retrieval-Augmented Generation (RAG). The system supports multiple search modes and extensive runtime configuration to help you find and contextualize information effectively.

Refer to the retrieval API and SDK reference for detailed retrieval examples.

Search Capabilities

R2R offers three search modes:

  1. Basic Mode: Simple semantic search using vector embeddings. Ideal for finding contextually similar content.

  2. Advanced Mode: Combines semantic and full-text search for comprehensive results. Automatically balances between exact matches and semantic similarity.

  3. Custom Mode: Complete control over search configuration including weights, limits, and search types.

Basic Search Example

1from r2r import R2RClient
2
3client = R2RClient()
4
5# Simple semantic search
6results = client.retrieval.search(
7 query="What are the effects of climate change?",
8 search_mode="basic"
9)

Advanced Search Example

1# Hybrid search with filters
2results = client.retrieval.search(
3 query="What are the effects of climate change?",
4 search_mode="advanced",
5 search_settings={
6 "filters": {
7 "document_type": {"$eq": "research_paper"},
8 "year": {"$gt": 2020}
9 },
10 "limit": 10
11 }
12)

RAG Integration

R2R’s RAG system enhances search results by using them as context for AI-generated responses. You can configure both the search behavior and generation parameters at runtime.

Basic RAG Example

1# Simple RAG query
2response = client.retrieval.rag(
3 query="Summarize recent climate change effects",
4 search_settings={
5 "limit": 5,
6 "filters": {"year": {"$gt": 2020}}
7 }
8)

Advanced RAG Configuration

1# RAG with custom settings
2response = client.retrieval.rag(
3 query="Summarize recent climate change effects",
4 search_settings={
5 "limit": 5,
6 "use_hybrid_search": True,
7 "filters": {"year": {"$gt": 2020}},
8 "chunk_settings": {"limit": 10},
9 "graph_settings": {"enabled": True}
10 },
11 rag_generation_config={
12 "model": "anthropic/claude-3-opus-20240229",
13 "temperature": 0.7,
14 "max_tokens": 500
15 }
16)

Custom Prompting

You can override default prompts to customize how RAG generates responses:

1response = client.retrieval.rag(
2 query="Summarize recent climate change effects",
3 search_settings={"limit": 5},
4 task_prompt_override=(
5 "Based on the provided documents, create a detailed summary that:"
6 "\n1. Highlights key findings"
7 "\n2. Notes research methodologies"
8 "\n3. Identifies gaps in current understanding"
9 "\n\nQuery: {query}"
10 "\n\nDocuments: {documents}"
11 )
12)

Conclusion

R2R’s search and RAG capabilities provide flexible tools for finding and contextualizing information. Whether you need simple semantic search or complex hybrid retrieval with custom RAG generation, the system can be configured to meet your specific needs.

Built with