|
""" |
|
Knowledge Retriever using LlamaIndex and FAISS for vector search |
|
""" |
|
|
|
import os |
|
import time |
|
import json |
|
from pathlib import Path |
|
from typing import Dict, List, Optional, Any |
|
|
|
try: |
|
import faiss |
|
from llama_index.core import ( |
|
VectorStoreIndex, |
|
SimpleDirectoryReader, |
|
StorageContext, |
|
Settings |
|
) |
|
from llama_index.vector_stores.faiss import FaissVectorStore |
|
from llama_index.embeddings.openai import OpenAIEmbedding |
|
from llama_index.llms.openai import OpenAI |
|
LLAMA_INDEX_AVAILABLE = True |
|
except ImportError: |
|
LLAMA_INDEX_AVAILABLE = False |
|
|
|
|
|
class KnowledgeRetriever: |
|
""" |
|
Knowledge retriever that uses LlamaIndex with FAISS for vector search. |
|
Falls back to simple text search if LlamaIndex is not available. |
|
""" |
|
|
|
def __init__(self, index_path: str = "./data", cache_dir: str = "./cache"): |
|
self.index_path = Path(index_path) |
|
self.cache_dir = Path(cache_dir) |
|
self.cache_dir.mkdir(exist_ok=True) |
|
|
|
self.index = None |
|
self.documents = [] |
|
|
|
|
|
self._init_sample_documents() |
|
|
|
if LLAMA_INDEX_AVAILABLE: |
|
self._init_llama_index() |
|
else: |
|
print("LlamaIndex not available, using fallback text search") |
|
|
|
def _init_sample_documents(self): |
|
"""Initialize with comprehensive AI documents from 2024-2025""" |
|
self.documents = [ |
|
|
|
{ |
|
"id": 1, |
|
"title": "GPT-5 Architecture and Capabilities", |
|
"content": "GPT-5 represents a significant leap in language model capabilities with improved reasoning, multimodal understanding, and reduced hallucinations. The model features enhanced chain-of-thought reasoning, better factual accuracy, and native support for images, audio, and video processing. Key improvements include: 50% reduction in hallucination rates, 3x better mathematical reasoning, native multimodal processing without separate encoders, improved instruction following, and enhanced safety guardrails. The architecture incorporates mixture-of-experts scaling, improved attention mechanisms, and novel alignment techniques.", |
|
"source": "OpenAI Research, 2025 • Technical Report • openai.com/research/gpt-5", |
|
"source_type": "academic", |
|
"url": "https://openai.com/research/gpt-5", |
|
"metadata": { |
|
"authors": ["OpenAI Research Team"], |
|
"year": 2025, |
|
"venue": "OpenAI", |
|
"model_params": "1.8T", |
|
"context_length": "1M tokens" |
|
} |
|
}, |
|
{ |
|
"id": 2, |
|
"title": "Claude 4 Constitutional AI and Safety", |
|
"content": "Claude 4 introduces advanced constitutional AI training with improved helpfulness, harmlessness, and honesty. The model demonstrates superior reasoning capabilities while maintaining strong safety guardrails. Key features include: constitutional training from human feedback, improved factual accuracy through retrieval augmentation, advanced reasoning chains for complex problems, enhanced code generation and debugging, multilingual capabilities across 95 languages, and robust safety measures against misuse. The training process incorporates novel techniques for alignment and reduces harmful outputs by 90% compared to previous versions.", |
|
"source": "Anthropic Research, 2025 • AI Safety Paper • anthropic.com/claude-4", |
|
"source_type": "academic", |
|
"url": "https://anthropic.com/claude-4", |
|
"metadata": { |
|
"authors": ["Anthropic Safety Team"], |
|
"year": 2025, |
|
"venue": "Anthropic", |
|
"safety_rating": "AAA", |
|
"context_length": "2M tokens" |
|
} |
|
}, |
|
{ |
|
"id": 3, |
|
"title": "Gemini Ultra 2.0: Multimodal AI Breakthrough", |
|
"content": "Gemini Ultra 2.0 achieves state-of-the-art performance across text, image, audio, and video understanding tasks. The model demonstrates human-level performance on complex reasoning benchmarks and shows emergent capabilities in scientific discovery. Major breakthroughs include: unified multimodal architecture processing all input types simultaneously, breakthrough performance on MMLU (95.2%), advanced video understanding and generation, real-time multimodal conversation capabilities, integration with robotics applications, and novel attention mechanisms for cross-modal reasoning.", |
|
"source": "Google DeepMind, 2025 • Nature AI • deepmind.google/gemini-ultra-2", |
|
"source_type": "academic", |
|
"url": "https://deepmind.google/gemini-ultra-2", |
|
"metadata": { |
|
"authors": ["Google DeepMind Team"], |
|
"year": 2025, |
|
"venue": "Nature AI", |
|
"benchmark_scores": {"MMLU": 95.2, "GSM8K": 97.8}, |
|
"modalities": ["text", "image", "audio", "video"] |
|
} |
|
}, |
|
{ |
|
"id": 4, |
|
"title": "LangChain 2.0: Advanced Agent Orchestration", |
|
"content": "LangChain 2.0 introduces revolutionary agent orchestration capabilities with improved reasoning, tool use, and multi-agent collaboration. The framework enables complex AI workflows with autonomous planning and execution. New features include: advanced agent planning with hierarchical task decomposition, multi-agent collaboration frameworks, improved tool integration and API calling, enhanced memory systems for long-term context, robust error handling and recovery mechanisms, and native support for voice and multimodal interactions.", |
|
"source": "LangChain Documentation, 2025 • python.langchain.com/v2.0", |
|
"source_type": "documentation", |
|
"url": "https://python.langchain.com/v2.0/", |
|
"metadata": { |
|
"version": "2.0.0", |
|
"year": 2025, |
|
"type": "framework", |
|
"language": "Python", |
|
"github_stars": 85000 |
|
} |
|
}, |
|
{ |
|
"id": 5, |
|
"title": "Retrieval-Augmented Generation Advances in 2024", |
|
"content": "Recent advances in RAG systems focus on improving retrieval accuracy, reducing hallucinations, and enhancing context integration. Key developments include: hybrid dense-sparse retrieval combining semantic and lexical matching, multi-hop reasoning for complex queries, improved chunking strategies with semantic boundaries, real-time knowledge updating and fact verification, advanced reranking using cross-encoders, and integration with knowledge graphs for structured reasoning. Performance improvements show 40% better factual accuracy and 60% reduction in hallucinations.", |
|
"source": "AI Research Collective, 2024 • ICML Workshop • arxiv.org/abs/2024.rag.advances", |
|
"source_type": "academic", |
|
"url": "https://arxiv.org/abs/2024.rag.advances", |
|
"metadata": { |
|
"authors": ["Various Research Teams"], |
|
"year": 2024, |
|
"venue": "ICML Workshop", |
|
"improvements": {"accuracy": "40%", "hallucination_reduction": "60%"} |
|
} |
|
}, |
|
{ |
|
"id": 6, |
|
"title": "Vector Database Optimization for Large-Scale AI", |
|
"content": "Modern vector databases have evolved to handle billion-scale embeddings with sub-millisecond retrieval times. Innovations include: approximate nearest neighbor algorithms with 99.9% recall, distributed indexing across multiple nodes, dynamic embedding updates without full reindexing, GPU-accelerated similarity search, compression techniques reducing storage by 80%, and integration with real-time streaming data. Popular solutions include Pinecone, Weaviate, Qdrant, and Chroma, each optimized for different use cases and scale requirements.", |
|
"source": "Vector Database Survey, 2024 • VLDB Conference • vldb.org/vector-db-2024", |
|
"source_type": "academic", |
|
"url": "https://vldb.org/vector-db-2024", |
|
"metadata": { |
|
"year": 2024, |
|
"venue": "VLDB", |
|
"performance": {"recall": "99.9%", "storage_reduction": "80%"}, |
|
"databases": ["Pinecone", "Weaviate", "Qdrant", "Chroma"] |
|
} |
|
}, |
|
{ |
|
"id": 7, |
|
"title": "Fine-tuning Large Language Models: 2024 Best Practices", |
|
"content": "Latest techniques for fine-tuning LLMs focus on parameter efficiency and task specialization. Key methods include: LoRA (Low-Rank Adaptation) for efficient parameter updates, QLoRA for quantized fine-tuning reducing memory by 75%, instruction tuning for better task following, reinforcement learning from human feedback (RLHF), constitutional AI training for safety, and multi-task learning for generalization. New approaches like AdaLoRA and DoRA show improved performance with even fewer parameters.", |
|
"source": "NeurIPS 2024 • Fine-tuning Workshop • neurips.cc/finetuning-2024", |
|
"source_type": "academic", |
|
"url": "https://neurips.cc/finetuning-2024", |
|
"metadata": { |
|
"year": 2024, |
|
"venue": "NeurIPS", |
|
"techniques": ["LoRA", "QLoRA", "RLHF", "Constitutional AI"], |
|
"memory_reduction": "75%" |
|
} |
|
}, |
|
{ |
|
"id": 8, |
|
"title": "Transformer Architecture Evolution: Beyond Attention", |
|
"content": "New transformer variants address computational efficiency and long-context understanding. Innovations include: Mamba state-space models for linear scaling, mixture-of-experts for sparse computation, ring attention for distributed processing, retrieval-augmented architectures, improved positional encodings for long sequences, and hybrid CNN-transformer models. These advances enable processing of million-token contexts while maintaining efficiency and accuracy.", |
|
"source": "Transformer Evolution Survey, 2024 • ICLR • iclr.cc/transformer-evolution", |
|
"source_type": "academic", |
|
"url": "https://iclr.cc/transformer-evolution", |
|
"metadata": { |
|
"year": 2024, |
|
"venue": "ICLR", |
|
"architectures": ["Mamba", "MoE", "Ring Attention"], |
|
"context_length": "1M tokens" |
|
} |
|
}, |
|
{ |
|
"id": 9, |
|
"title": "AI Safety and Alignment Research 2024", |
|
"content": "Significant progress in AI safety focuses on interpretability, robustness, and alignment. Key developments include: mechanistic interpretability revealing model internals, adversarial training for robustness, constitutional AI for value alignment, red teaming methodologies for vulnerability discovery, safety evaluation frameworks, and governance approaches for responsible deployment. Research shows improved safety metrics across multiple dimensions while maintaining model capabilities.", |
|
"source": "AI Safety Research Consortium, 2024 • AI Safety Journal • aisafety.org/2024-report", |
|
"source_type": "academic", |
|
"url": "https://aisafety.org/2024-report", |
|
"metadata": { |
|
"year": 2024, |
|
"focus_areas": ["interpretability", "robustness", "alignment"], |
|
"safety_improvements": "significant" |
|
} |
|
}, |
|
{ |
|
"id": 10, |
|
"title": "Multimodal AI: Vision-Language Integration", |
|
"content": "Advances in multimodal AI enable seamless integration of text, images, audio, and video. Key breakthroughs include: unified embedding spaces for cross-modal retrieval, vision-language models with improved spatial reasoning, audio-visual understanding for comprehensive scene analysis, real-time multimodal interaction capabilities, and applications in robotics and autonomous systems. Models like GPT-4V, Gemini Vision, and Claude 3 demonstrate human-level performance on complex multimodal tasks.", |
|
"source": "Multimodal AI Survey, 2024 • Computer Vision Conference • cvpr.org/multimodal-2024", |
|
"source_type": "academic", |
|
"url": "https://cvpr.org/multimodal-2024", |
|
"metadata": { |
|
"year": 2024, |
|
"venue": "CVPR", |
|
"modalities": ["text", "image", "audio", "video"], |
|
"models": ["GPT-4V", "Gemini Vision", "Claude 3"] |
|
} |
|
}, |
|
{ |
|
"id": 11, |
|
"title": "Edge AI and Model Compression Techniques", |
|
"content": "Deployment of AI models on edge devices requires sophisticated compression and optimization. Latest techniques include: quantization to INT8 and INT4 precision, pruning redundant parameters, knowledge distillation from large to small models, neural architecture search for efficient designs, and specialized hardware optimization. These methods achieve 10x model size reduction while maintaining 95% of original performance.", |
|
"source": "Edge AI Research, 2024 • Mobile Computing Conference • mobicom.org/edge-ai-2024", |
|
"source_type": "academic", |
|
"url": "https://mobicom.org/edge-ai-2024", |
|
"metadata": { |
|
"year": 2024, |
|
"techniques": ["quantization", "pruning", "distillation"], |
|
"size_reduction": "10x", |
|
"performance_retention": "95%" |
|
} |
|
}, |
|
{ |
|
"id": 12, |
|
"title": "AI Code Generation and Programming Assistants", |
|
"content": "Code generation AI has evolved to support full software development workflows. Advanced capabilities include: multi-language code generation with context awareness, automated testing and debugging assistance, code review and optimization suggestions, natural language to code translation, integration with development environments, and support for complex software architectures. Tools like GitHub Copilot X, CodeT5+, and StarCoder show significant productivity improvements.", |
|
"source": "Software Engineering AI, 2024 • ICSE Conference • icse.org/code-ai-2024", |
|
"source_type": "academic", |
|
"url": "https://icse.org/code-ai-2024", |
|
"metadata": { |
|
"year": 2024, |
|
"venue": "ICSE", |
|
"tools": ["GitHub Copilot X", "CodeT5+", "StarCoder"], |
|
"productivity_gain": "significant" |
|
} |
|
} |
|
] |
|
|
|
def _init_llama_index(self): |
|
"""Initialize LlamaIndex with FAISS vector store""" |
|
try: |
|
|
|
Settings.embed_model = OpenAIEmbedding() |
|
Settings.llm = OpenAI(temperature=0.1) |
|
|
|
|
|
index_cache_path = self.cache_dir / "faiss_index" |
|
|
|
if index_cache_path.exists(): |
|
|
|
self._load_cached_index(index_cache_path) |
|
else: |
|
|
|
self._build_index_from_documents() |
|
|
|
except Exception as e: |
|
print(f"Failed to initialize LlamaIndex: {e}") |
|
print("Falling back to text search") |
|
self.index = None |
|
|
|
def _load_cached_index(self, cache_path: Path): |
|
"""Load cached FAISS index""" |
|
try: |
|
|
|
faiss_index = faiss.read_index(str(cache_path / "index.faiss")) |
|
vector_store = FaissVectorStore(faiss_index=faiss_index) |
|
|
|
|
|
storage_context = StorageContext.from_defaults(vector_store=vector_store) |
|
|
|
|
|
self.index = VectorStoreIndex.from_vector_store( |
|
vector_store=vector_store, |
|
storage_context=storage_context |
|
) |
|
|
|
|
|
metadata_path = cache_path / "metadata.json" |
|
if metadata_path.exists(): |
|
with open(metadata_path, 'r') as f: |
|
self.documents = json.load(f) |
|
|
|
print("Loaded cached FAISS index") |
|
|
|
except Exception as e: |
|
print(f"Failed to load cached index: {e}") |
|
self._build_index_from_documents() |
|
|
|
def _build_index_from_documents(self): |
|
"""Build FAISS index from documents""" |
|
try: |
|
|
|
if self.index_path.exists() and any(self.index_path.iterdir()): |
|
|
|
reader = SimpleDirectoryReader(str(self.index_path)) |
|
documents = reader.load_data() |
|
else: |
|
|
|
from llama_index.core import Document |
|
documents = [ |
|
Document( |
|
text=doc["content"], |
|
metadata={ |
|
"title": doc["title"], |
|
"source": doc["source"], |
|
"url": doc.get("url", ""), |
|
**doc.get("metadata", {}) |
|
} |
|
) |
|
for doc in self.documents |
|
] |
|
|
|
|
|
d = 1536 |
|
faiss_index = faiss.IndexFlatL2(d) |
|
vector_store = FaissVectorStore(faiss_index=faiss_index) |
|
|
|
|
|
storage_context = StorageContext.from_defaults(vector_store=vector_store) |
|
|
|
|
|
self.index = VectorStoreIndex.from_documents( |
|
documents, |
|
storage_context=storage_context |
|
) |
|
|
|
|
|
self._cache_index() |
|
|
|
print(f"Built FAISS index with {len(documents)} documents") |
|
|
|
except Exception as e: |
|
print(f"Failed to build index: {e}") |
|
self.index = None |
|
|
|
def _cache_index(self): |
|
"""Cache the FAISS index to disk""" |
|
try: |
|
cache_path = self.cache_dir / "faiss_index" |
|
cache_path.mkdir(exist_ok=True) |
|
|
|
|
|
faiss.write_index( |
|
self.index.vector_store.faiss_index, |
|
str(cache_path / "index.faiss") |
|
) |
|
|
|
|
|
with open(cache_path / "metadata.json", 'w') as f: |
|
json.dump(self.documents, f, indent=2) |
|
|
|
print("Cached FAISS index to disk") |
|
|
|
except Exception as e: |
|
print(f"Failed to cache index: {e}") |
|
|
|
def search(self, query: str, search_type: str = "semantic", k: int = 5) -> Dict[str, Any]: |
|
""" |
|
Search for relevant documents |
|
|
|
Args: |
|
query: Search query string |
|
search_type: Type of search ("semantic", "keyword", "hybrid") |
|
k: Number of results to return |
|
|
|
Returns: |
|
Dictionary with search results and metadata |
|
""" |
|
start_time = time.time() |
|
|
|
if self.index is not None and search_type in ["semantic", "hybrid"]: |
|
|
|
results = self._vector_search(query, k) |
|
else: |
|
|
|
results = self._text_search(query, k) |
|
|
|
search_time = time.time() - start_time |
|
|
|
return { |
|
"documents": results, |
|
"search_time": search_time, |
|
"query": query, |
|
"total_count": len(results) |
|
} |
|
|
|
def _vector_search(self, query: str, k: int) -> List[Dict[str, Any]]: |
|
"""Perform vector-based search using LlamaIndex""" |
|
try: |
|
|
|
query_engine = self.index.as_query_engine(similarity_top_k=k) |
|
|
|
|
|
response = query_engine.query(query) |
|
|
|
|
|
results = [] |
|
for i, node in enumerate(response.source_nodes): |
|
score = node.score if hasattr(node, 'score') else 0.8 |
|
|
|
|
|
metadata = node.node.metadata |
|
title = metadata.get('title', f'Document {i+1}') |
|
source = metadata.get('source', 'Unknown source') |
|
url = metadata.get('url', '') |
|
|
|
|
|
text = node.node.text |
|
snippet = self._extract_snippet(text, query) |
|
|
|
results.append({ |
|
"id": i + 1, |
|
"title": title, |
|
"content": text, |
|
"snippet": snippet, |
|
"source": source, |
|
"source_type": metadata.get('source_type', 'document'), |
|
"url": url, |
|
"relevance_score": min(score, 1.0), |
|
"rank": i + 1, |
|
"metadata": metadata |
|
}) |
|
|
|
return results |
|
|
|
except Exception as e: |
|
print(f"Vector search failed: {e}") |
|
return self._text_search(query, k) |
|
|
|
def _text_search(self, query: str, k: int) -> List[Dict[str, Any]]: |
|
"""Fallback text-based search""" |
|
query_lower = query.lower() |
|
scored_docs = [] |
|
|
|
for doc in self.documents: |
|
score = self._calculate_text_score(doc, query_lower) |
|
if score > 0: |
|
scored_docs.append((doc, score)) |
|
|
|
|
|
scored_docs.sort(key=lambda x: x[1], reverse=True) |
|
results = [] |
|
|
|
for i, (doc, score) in enumerate(scored_docs[:k]): |
|
snippet = self._extract_snippet(doc["content"], query) |
|
|
|
results.append({ |
|
"id": doc["id"], |
|
"title": doc["title"], |
|
"content": doc["content"], |
|
"snippet": snippet, |
|
"source": doc["source"], |
|
"source_type": doc.get("source_type", "document"), |
|
"url": doc.get("url", ""), |
|
"relevance_score": score, |
|
"rank": i + 1, |
|
"metadata": doc.get("metadata", {}) |
|
}) |
|
|
|
return results |
|
|
|
def _calculate_text_score(self, doc: Dict, query_lower: str) -> float: |
|
"""Calculate relevance score for text search""" |
|
content_lower = doc["content"].lower() |
|
title_lower = doc["title"].lower() |
|
|
|
score = 0.0 |
|
query_words = query_lower.split() |
|
|
|
|
|
for word in query_words: |
|
if word in title_lower: |
|
score += 0.3 |
|
if word in content_lower: |
|
score += 0.1 |
|
|
|
|
|
if query_lower in content_lower: |
|
score += 0.5 |
|
if query_lower in title_lower: |
|
score += 0.8 |
|
|
|
return min(score, 1.0) |
|
|
|
def _extract_snippet(self, content: str, query: str, max_length: int = 200) -> str: |
|
"""Extract relevant snippet from content""" |
|
query_lower = query.lower() |
|
content_lower = content.lower() |
|
|
|
|
|
index = content_lower.find(query_lower) |
|
if index == -1: |
|
|
|
return content[:max_length] + ("..." if len(content) > max_length else "") |
|
|
|
|
|
start = max(0, index - 50) |
|
end = min(len(content), index + len(query) + 150) |
|
|
|
snippet = content[start:end] |
|
|
|
|
|
if start > 0: |
|
snippet = "..." + snippet |
|
if end < len(content): |
|
snippet = snippet + "..." |
|
|
|
return snippet |
|
|
|
|
|
|
|
def test_retriever(): |
|
"""Test function to verify retriever functionality""" |
|
retriever = KnowledgeRetriever() |
|
|
|
|
|
results = retriever.search("retrieval augmented generation", k=3) |
|
|
|
print(f"Found {results['total_count']} results in {results['search_time']:.3f}s") |
|
print("-" * 50) |
|
|
|
for doc in results['documents']: |
|
print(f"Title: {doc['title']}") |
|
print(f"Score: {doc['relevance_score']:.2f}") |
|
print(f"Snippet: {doc['snippet'][:100]}...") |
|
print("-" * 50) |
|
|
|
|
|
if __name__ == "__main__": |
|
test_retriever() |