RAG Agent¶
RAGAgent (Lewis et al., 2020) retrieves relevant chunks before generating an answer, grounding the response in supplied documents rather than parametric memory alone.
Usage¶
from brain.patterns import RAGAgent
# Simple keyword retriever
corpus = ["The API uses OAuth2.", "Rate limit: 100 req/min."]
retriever = lambda q, k=3: corpus[:k]
agent = RAGAgent(retriever=retriever, top_k=5, include_sources=True)
result = agent.run("What auth method does the API use?")
print(result.answer)
print(result.metadata["sources"]) # retrieved chunks
API Reference¶
brain.patterns.rag.RAGAgent
¶
RAGAgent(retriever: Retriever | None = None, provider: LLMProvider | None = None, top_k: int = 5, include_sources: bool = True)
Bases: BasePattern
Retrieval-Augmented Generation agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retriever
|
Retriever | None
|
callable(query, top_k) -> list[str] of text chunks. Defaults to a no-op that returns an empty list (offline-safe). |
None
|
provider
|
LLMProvider | None
|
LLMProvider instance; defaults to LocalEchoProvider. |
None
|
top_k
|
int
|
number of chunks to retrieve. |
5
|
include_sources
|
bool
|
attach retrieved chunks to result metadata. |
True
|
Source code in brain/patterns/rag.py
run
¶
Source code in brain/patterns/rag.py
Retriever Contract¶
The retriever receives the task string and top_k; it must return a list of text strings.
SecondBrain VectorStore¶
Plug in the built-in vector store for vault-grounded answers:
from brain.state import VectorStore
from brain.config import get_settings
settings = get_settings()
vs = VectorStore(settings.paths.chroma_dir, model_name=settings.retrieval.embedding_model)
agent = RAGAgent(
retriever=lambda q, k=5: [c.text for c in vs.search(q, top_k=k)],
)
When to Use¶
| Situation | Recommendation |
|---|---|
| Answer must cite specific documents | RAGAgent |
| Knowledge base is local / private | RAGAgent |
| Open-ended web research | ReAct + search tool |
| Self-improving critique loop | Reflexion |