A comprehensive Retrieval-Augmented Generation (RAG) system for intelligent patent analysis using multi-agent AI, semantic search, and privacy-preserving local LLMs. This system combines CrewAI agents with OpenSearch and Ollama to provide powerful patent trend analysis, forecasting, and discovery capabilities.
This project implements an intelligent patent analysis and search system that combines cutting-edge technologies:
- Semantic Search: Advanced vector-based patent retrieval using embeddings for meaning-based search
- CrewAI Multi-Agent System: Specialized AI agents working together for comprehensive patent trend analysis and forecasting
- Hybrid Search: Combines keyword and semantic search for optimal retrieval accuracy
- OpenSearch Backend: Distributed vector and full-text search engine with dashboard visualization
- Local LLM Privacy: Uses Ollama for on-premise, privacy-preserving language model inference
- Lithium Battery Focus: Specialized analysis for lithium battery technology patents
- π Multiple Search Strategies: Semantic, keyword, hybrid, and iterative search methods
- π€ AI-Powered Analysis: CrewAI agents for automated patent analysis and trend forecasting
- π Citation Analysis: Extract and analyze patent citations for technology evolution tracking
- π¨ Interactive Dashboard: OpenSearch Dashboards for visualization and monitoring
- π Privacy-First: All processing done locally without external API calls for data
- π Scalable Architecture: Designed to handle large patent datasets efficiently
RAG/
βββ Core Application Files
β βββ agentic_rag.py # Main CLI interface with interactive menu system
β βββ patent_crew.py # CrewAI agent definitions and orchestration
β βββ patent_search_tools.py # Search implementations (semantic, keyword, hybrid, iterative)
β
βββ Data Processing & Integration
β βββ ingestion.py # Patent data loading, chunking, and indexing pipeline
β βββ embedding.py # Vector embedding generation using Ollama
β βββ opensearch_client.py # OpenSearch client, connection, and index management
β βββ information_collector.py # Web data collection and processing utilities
β βββ helper.py # General utility and helper functions
β
βββ Configuration & Deployment
β βββ docker-compose.yml # Docker Compose configuration for OpenSearch services
β βββ .env # Environment variables (API keys, model configs)
β βββ requirements.txt # Python dependencies
β
βββ Development & Analysis
β βββ dev.ipynb # Jupyter notebook for experimentation and development
β βββ patent_analysis_*.txt # Generated analysis reports with timestamps
β
βββ Data Storage
β βββ files/
β β βββ patents.json # Primary patent dataset with metadata
β β βββ patent_details.json # Extended patent information (claims, descriptions)
β β
β βββ results/ # Analysis outputs and extracted data
β βββ patent_data_*.json # Indexed patent data by batch
β βββ citation_*.json # Extracted citation networks
β βββ [Analysis reports]
β
βββ Documentation
βββ README.md # This file
βββ [Additional documentation]
- patents.json: Main patent dataset containing core metadata, abstracts, and search parameters
- patent_details.json: Extended patent information including detailed descriptions, claims, and technical classifications
- results/: Output directory containing:
patent_data_*.json: Chunked and processed patent records ready for indexingcitation_*.json: Extracted patent citation relationships and networks- Analysis reports with timestamps for trend analysis and forecasting results
- Python 3.10+: Core language for the application
- Docker & Docker Compose: For running OpenSearch and dashboards
- Ollama: Local LLM inference engine with models pre-installed
- Download from: https://ollama.ai
- Recommended models:
llama2,nomic-embed-text
- Git: For repository management
- pip/pip-tools: Python package management
# Clone the repository
git clone <repository-url>
cd RAG
# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate# Install required packages
pip install -r requirements.txt
# Optional: Install Jupyter for development notebook
pip install jupyter jupyterlabCreate a .env file in the project root:
SERP_API_KEY=your_serp_api_key_here
OLLAMA_HOST=http://localhost:11434
OPENSEARCH_HOST=localhost
OPENSEARCH_PORT=9200
OPENSEARCH_USER=admin
OPENSEARCH_PASSWORD=admin# Start OpenSearch and OpenSearch Dashboards using Docker Compose
docker-compose up -d
# Verify services are running
docker-compose psService Endpoints:
- OpenSearch: http://localhost:9200
- OpenSearch Dashboards: http://localhost:5601
- Default credentials: admin / admin
In a new terminal, start Ollama and pull required models:
# Start Ollama service
ollama serve
# In another terminal, pull models
ollama pull llama2
ollama pull nomic-embed-text
# Verify models are available
curl http://localhost:11434/api/tags# Load patents into OpenSearch and generate embeddings
python ingestion.pyWhat this does:
- Loads patent data from
files/patents.jsonandfiles/patent_details.json - Generates vector embeddings using Ollama's embedding model
- Creates/updates OpenSearch indexes with proper vector mappings
- Stores metadata alongside embeddings for retrieval
- Generates processed data files in
results/directory
Expected output:
Connected to OpenSearch!
Loading patent data...
Processing 5000 patents...
Creating index 'patents' with vector mappings...
Indexing complete: 5000 documents indexed
The main entry point is agentic_rag.py, which provides an interactive menu-driven interface:
python agentic_rag.pyMain Menu Options:
1. Run complete patent trend analysis and forecasting
- Analyzes patent trends in the specified research area
- Generates insights using CrewAI agents
- Produces forecasting reports
- Outputs results to patent_analysis_[timestamp].txt
2. Search for specific patents
- Query by keywords or concepts
- Returns top matching patents
- Shows title, abstract, publication date, and patent ID
- Displays relevance scores
3. Iterative patent exploration
- Step-by-step patent discovery
- Refine searches based on results
- Explore related patents and citations
- Build comprehensive understanding of topic
4. View system status
- Check OpenSearch connectivity
- Verify Ollama availability and models
- Display system information
- Diagnose connectivity issues
5. Exit
- Gracefully shutdown application
The patent_search_tools.py module provides four search strategies:
from patent_search_tools import semantic_search
results = semantic_search(
query_text="solid-state battery technology advancements",
top_k=10
)
# Returns: Semantically similar patents based on meaning- Uses vector embeddings for meaning-based retrieval
- Best for: Conceptual searches, technology trend analysis
- Performance: Slower but more accurate for complex queries
from patent_search_tools import keyword_search
results = keyword_search(
query_text="lithium ion battery cathode",
top_k=10
)
# Returns: Patents matching keywords in title/abstract- Traditional inverted-index based search
- Best for: Specific terms, known technologies
- Performance: Fast, real-time results
from patent_search_tools import hybrid_search
results = hybrid_search(
query_text="battery thermal management",
top_k=10
)
# Returns: Combination of semantic and keyword results- Merges results from both search methods
- Best for: Balanced retrieval, general exploration
- Performance: Combines benefits of both methods
from patent_search_tools import iterative_search
results = iterative_search(
query_text="electrode material innovation",
top_k=10,
iterations=3
)
# Returns: Refined results through multiple iterations- Progressively refines search results
- Best for: Deep topic exploration, citation networks
- Performance: Slowest but most comprehensive
For experimentation and interactive analysis:
jupyter notebook dev.ipynbThe notebook provides:
- Pre-configured search examples
- Data exploration utilities
- Custom analysis scripts
- Visualization helpers
Access the underlying agent system directly:
from patent_crew import run_patent_analysis
result = run_patent_analysis(
research_area="Solid-State Batteries",
model_name="llama2"
)
print(result)CrewAI Agents Involved:
- Research Analyst: Gathers patent information
- Trend Analyst: Identifies emerging technologies
- Forecaster: Predicts future patent directions
- Report Writer: Synthesizes findings into reports
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β User Interface Layer β
β (agentic_rag.py CLI) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Application Logic Layer β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β’ Patent Crew (Agents) β’ Search Tools β’ Helpers β
β β’ Data Processing β’ Information Collection β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Integration & Embedding Layer β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β’ OpenSearch Client β’ Embedding Generation β
β β’ Index Management β’ Vector Operations β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β External Services Layer β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β’ OpenSearch (Port 9200) β’ Ollama (Port 11434) β
β β’ OpenSearch Dashboards (5601) β’ Docker Infrastructure β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Purpose: Distributed search and analytics engine
- Features:
- Vector similarity search (KNN-based)
- Full-text keyword search
- JSON document storage with metadata
- Index management and mapping
- Address: http://localhost:9200
- Dashboard: http://localhost:5601 (OpenSearch Dashboards)
- Purpose: Privacy-preserving language model inference
- Features:
- Local model execution (no external API calls)
- Embedding generation
- Text generation and analysis
- Address: http://localhost:11434
- Models: llama2 (reasoning), nomic-embed-text (embeddings)
- Purpose: Orchestrate specialized AI agents for complex tasks
- Agent Types:
- Research Analyst: Queries and analyzes patent data
- Trend Analyst: Identifies technology trends
- Forecaster: Makes predictions about future developments
- Report Writer: Synthesizes findings into comprehensive reports
User Query
β
[Query Preprocessing & Normalization]
β
[Search Strategy Selection]
ββ Semantic Path (Vector Embedding)
ββ Keyword Path (Inverted Index)
ββ Hybrid Path (Combined Ranking)
β
[OpenSearch Query Execution]
β
[Result Ranking & Deduplication]
β
[Metadata Enrichment]
β
[Results Returned to User/Agent]
JSON Patent Files
β
[Load & Parse]
β
[Data Validation & Cleaning]
β
[Text Chunking & Preprocessing]
β
[Embedding Generation (Ollama)]
β
[Index Creation/Update (OpenSearch)]
β
[Results Stored in results/ directory]
User Request
β
[Patent Search (Multiple Strategies)]
β
[Results Aggregation]
β
[CrewAI Agent Processing]
β
[LLM-based Analysis]
β
[Report Generation]
β
[File Output + Console Display]
# SERP API Configuration (for web search capabilities)
SERP_API_KEY=your_api_key_here
# Ollama Configuration
OLLAMA_HOST=http://localhost:11434
OLLAMA_MODEL=llama2
EMBEDDING_MODEL=nomic-embed-text
# OpenSearch Configuration
OPENSEARCH_HOST=localhost
OPENSEARCH_PORT=9200
OPENSEARCH_USER=admin
OPENSEARCH_PASSWORD=admin
# Application Settings
PATENT_RESEARCH_AREA=Lithium Battery
DATA_DIR=./files
RESULTS_DIR=./resultsOpenSearch Settings (docker-compose.yml):
- Single-node cluster (development mode)
- Security disabled for local development
- Persistent data volume: opensearch-data
- Memory: 2GB (adjustable)
OpenSearch Dashboards:
- Auto-connects to OpenSearch
- Security plugin disabled
- Port: 5601
Customization:
# Adjust memory limits
environment:
- "ES_JAVA_OPTS=-Xms1g -Xmx2g"
# Enable security (production)
- DISABLE_SECURITY_PLUGIN=falsepatents.json structure:
{
"patent_id": "US123456789",
"title": "Advanced Lithium Battery System",
"abstract": "A novel approach to lithium battery technology...",
"publication_date": "2023-06-15",
"assignee": "Company Name",
"claims": ["Claim 1", "Claim 2"],
"references": ["Citation 1", "Citation 2"]
}- Loading: JSON files parsed and validated
- Normalization: Standardized field formats
- Chunking: Large documents split into processable chunks
- Embedding: Vector representations generated via Ollama
- Indexing: Documents stored in OpenSearch with embeddings
- Export: Processed data saved to results/ directory
from opensearch_client import get_opensearch_client, create_index_if_not_exists
# Connect to OpenSearch
client = get_opensearch_client("localhost", 9200)
# Create or recreate index with proper vector mappings
create_index_if_not_exists(client, "patents")
# Check index status
status = client.cat.indices(format="json")
# Delete index
client.indices.delete(index="patents")Symptoms: Connection refused at 9200
Solutions:
# Check if Docker containers are running
docker-compose ps
# Start services
docker-compose up -d
# View logs
docker-compose logs opensearch
# Verify connectivity
curl http://localhost:9200/_cluster/healthSymptoms: Error connecting to Ollama: Connection refused
Solutions:
# Verify Ollama is running
curl http://localhost:11434/api/tags
# Start Ollama service
ollama serve
# Check available models
ollama list
# Pull required models if missing
ollama pull llama2
ollama pull nomic-embed-textSymptoms: index_not_found_exception
Solutions:
# Re-run data ingestion
python ingestion.py
# Verify index exists
curl http://localhost:9200/_cat/indices
# Check index mapping
curl http://localhost:9200/patents/_mappingSymptoms: Container crashes, Python process killed
Solutions:
# Increase Docker memory allocation
# Edit docker-compose.yml:
mem_limit: 4g
# Or set system limits:
# Windows: Docker Desktop β Settings β Resources
# Linux: docker update -m 4g opensearch
# Restart services
docker-compose restartCauses & Solutions:
- Large dataset: Optimize embeddings, increase batch size
- Undersized cluster: Add more nodes (production)
- Poor network: Use persistent connections, connection pooling
- Index not refreshed: Force refresh:
curl -X POST http://localhost:9200/patents/_refresh
- Batch processing of embeddings (default: 100 documents)
- Parallel index updates with multiple threads
- Incremental indexing for new data
- Query result caching
- Vector similarity optimization using HNSW
- Index refresh scheduling
# Adjust batch sizes in ingestion.py
BATCH_SIZE = 200
# Modify search parameters in patent_search_tools.py
TOP_K = 50 # Return more results
VECTOR_K = 20 # KNN neighborsDevelopment (Current Setup):
- Security plugins disabled
- Admin credentials: admin/admin
- Single-node cluster
- No SSL/TLS encryption
Production Recommendations:
- Enable OpenSearch security plugins
- Use strong passwords
- Configure SSL/TLS
- Multi-node cluster setup
- Authentication & authorization
- Data encryption at rest
- Network isolation
- All processing occurs locally
- No data sent to external APIs (except optional SERP API)
- Embeddings generated on-premise
- Models run locally on Ollama
# Semantic Search
semantic_search(query_text: str, top_k: int = 20) β List[Dict]
# Keyword Search
keyword_search(query_text: str, top_k: int = 20) β List[Dict]
# Hybrid Search
hybrid_search(query_text: str, top_k: int = 20) β List[Dict]
# Iterative Search
iterative_search(query_text: str, top_k: int = 20, iterations: int = 3) β List[Dict]# Get connected client
get_opensearch_client(host: str, port: int) β OpenSearch
# Create index with vector mappings
create_index_if_not_exists(client: OpenSearch, index_name: str) β None
# Index document
index_document(client: OpenSearch, index: str, doc_id: str, body: Dict) β None
# Search
search(client: OpenSearch, index: str, query: Dict) β Dict# Generate embedding for text
get_embedding(text: str, model: str = "nomic-embed-text") β List[float]
# Batch generate embeddings
batch_embeddings(texts: List[str]) β List[List[float]]Create custom agents in patent_crew.py:
from crewai import Agent, Task
class MyCustomAgent(Agent):
role = "Custom Analyst"
goal = "Analyze specific patent aspects"
backstory = "Expert in specific domain..."
# Add to crew
crew = Crew(
agents=[researcher, trend_analyst, custom_agent],
tasks=[...],
process=Process.hierarchical
)Implement in patent_search_tools.py:
def custom_search(query_text, top_k=20, **kwargs):
# Your custom logic here
client = get_opensearch_client("localhost", 9200)
# Build and execute custom query
return results# Example: Connect to external patent database
from information_collector import fetch_external_patents
patents = fetch_external_patents(query="battery technology")
# Process and index- opensearch-py: OpenSearch Python client
- crewai: Multi-agent orchestration framework
- langchain: LLM chain building
- langchain-ollama: Ollama integration
- python-dotenv: Environment variable management
- requests: HTTP client
- tiktoken: Token counting
- jupyter/jupyterlab: Interactive notebook
- pytest: Testing framework
- black: Code formatter
- pylint: Code linter
- RAM: 8GB minimum (16GB+ recommended)
- Storage: 20GB+ for Docker images and data
- CPU: Multi-core recommended for parallel processing
- Network: Docker networking for container communication
This project is provided as-is for research and development purposes. Please include proper attribution when using this code.
Contributions are welcome! To contribute:
- Fork the repository
- Create a feature branch (git checkout -b feature/amazing-feature)
- Commit your changes (git commit -m 'Add amazing feature')
- Push to the branch (git push origin feature/amazing-feature)
- Open a Pull Request
- Write clean, documented code
- Follow PEP 8 style guidelines
- Add tests for new features
- Update documentation
- Use meaningful commit messages
When reporting bugs, please include:
- System information (OS, Python version)
- Error messages and traceback
- Steps to reproduce
- Expected vs. actual behavior
For questions, issues, or suggestions:
- Open an issue on GitHub
- Check existing issues for solutions
- Review troubleshooting section above
- Check OpenSearch Dashboards for status
- OpenSearch Project: Powerful search and analytics engine
- CrewAI: Multi-agent orchestration framework
- Ollama: Local LLM capabilities
- LangChain: LLM integration framework
- Patent Data Sources: USPTO and related patent databases
- Vector Search Fundamentals
- Patent Data Analysis Best Practices
- Multi-Agent System Design Patterns
- Local LLM Deployment Guide
- Patent analysis systems
- RAG implementations
- CrewAI example projects
- OpenSearch use cases
- Advanced citation network analysis
- Technology trend visualization dashboards
- Batch processing optimization
- Multi-language patent support
- Real-time patent feed integration
- Collaborative analysis features
- Export to multiple formats (PDF, Excel, etc.)
- REST API endpoint
- Distributed indexing
- Caching layer optimization
- GPU acceleration for embeddings
- Query optimization
- Index compression
- Elasticsearch compatibility
- Cloud deployment options
- API gateway integration
- Workflow automation tools
Last Updated: January 2026 Version: 1.0.0 Status: Active Development