agents.patterns.simple_rag_agent_pattern

Simple RAG Agent Pattern - Using SimpleAgentV3 as base for RAG implementation.

This module demonstrates creating a RAG (Retrieval-Augmented Generation) agent using SimpleAgentV3 as the foundation, following the user’s request to use agent.py and SimpleAgentV3 patterns.

The pattern shows: 1. Extending SimpleAgentV3 for specialized functionality 2. Proper state schema composition 3. Tool integration for retrieval 4. Structured output for answers

Classes

AnswerWithSources

Structured answer with source citations.

HybridRAGAgent

Hybrid RAG Agent combining multiple retrieval strategies.

IterativeRAGAgent

Iterative RAG Agent that refines answers through multiple retrievals.

RetrievalResult

Structured retrieval result.

SimpleRAGAgent

Simple RAG Agent built on SimpleAgentV3 foundation.

Functions

create_hybrid_rag_agent([name, retrieval_strategies])

Create a hybrid RAG agent with multiple retrieval strategies.

create_iterative_rag_agent([name, max_iterations])

Create an iterative RAG agent for complex queries.

create_simple_rag_agent([name, temperature, debug])

Create a simple RAG agent with sensible defaults.

example_hybrid_rag()

Example of hybrid retrieval.

example_iterative_rag()

Example of iterative refinement.

example_simple_rag()

Example of using SimpleRAGAgent.

retrieve_documents(query[, top_k])

Retrieve documents based on query.

Module Contents

class agents.patterns.simple_rag_agent_pattern.AnswerWithSources(/, **data)

Bases: pydantic.BaseModel

Structured answer with source citations.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

class agents.patterns.simple_rag_agent_pattern.HybridRAGAgent

Bases: SimpleRAGAgent

Hybrid RAG Agent combining multiple retrieval strategies.

This variant uses multiple retrieval approaches: - Semantic similarity search - Keyword-based retrieval - Knowledge graph traversal - Combines results for comprehensive answers

setup_agent()

Setup hybrid retrieval tools.

Return type:

None

class agents.patterns.simple_rag_agent_pattern.IterativeRAGAgent

Bases: SimpleRAGAgent

Iterative RAG Agent that refines answers through multiple retrievals.

This variant performs iterative retrieval and refinement: 1. Initial retrieval and answer generation 2. Identifies gaps or unclear areas 3. Performs targeted follow-up retrievals 4. Refines the answer with additional context

setup_agent()

Setup iterative RAG configuration.

Return type:

None

class agents.patterns.simple_rag_agent_pattern.RetrievalResult(/, **data)

Bases: pydantic.BaseModel

Structured retrieval result.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

class agents.patterns.simple_rag_agent_pattern.SimpleRAGAgent

Bases: SimpleAgentV3

Simple RAG Agent built on SimpleAgentV3 foundation.

This agent extends SimpleAgentV3 to provide RAG capabilities: - Document retrieval through tools - Context-aware answer generation - Source attribution - Structured output with confidence scores

Examples

>>> rag_agent = SimpleRAGAgent(
...     name="knowledge_assistant",
...     temperature=0.3,
...     debug=True
... )
>>> result = await rag_agent.arun("What is quantum computing?")
setup_agent()

Setup RAG-specific configuration.

Return type:

None

agents.patterns.simple_rag_agent_pattern.create_hybrid_rag_agent(name='hybrid_rag', retrieval_strategies=None, **kwargs)

Create a hybrid RAG agent with multiple retrieval strategies.

Parameters:
  • name (str)

  • retrieval_strategies (list[str] | None)

Return type:

HybridRAGAgent

agents.patterns.simple_rag_agent_pattern.create_iterative_rag_agent(name='iterative_rag', max_iterations=3, **kwargs)

Create an iterative RAG agent for complex queries.

Parameters:
  • name (str)

  • max_iterations (int)

Return type:

IterativeRAGAgent

agents.patterns.simple_rag_agent_pattern.create_simple_rag_agent(name='rag_assistant', temperature=0.3, debug=True, **kwargs)

Create a simple RAG agent with sensible defaults.

Parameters:
Return type:

SimpleRAGAgent

async agents.patterns.simple_rag_agent_pattern.example_hybrid_rag()

Example of hybrid retrieval.

async agents.patterns.simple_rag_agent_pattern.example_iterative_rag()

Example of iterative refinement.

async agents.patterns.simple_rag_agent_pattern.example_simple_rag()

Example of using SimpleRAGAgent.

agents.patterns.simple_rag_agent_pattern.retrieve_documents(query, top_k=5)

Retrieve documents based on query.

Parameters:
Return type:

str