RAG Extractor (#3)
Browse files- RAG Extractor Done (913ad2248468bc3e4b0dadba88304fc599f63d66)
Co-authored-by: Omar Kouta <OmarKouta21@users.noreply.huggingface.co>
- app.py +3 -3
- requirements.txt +6 -1
- web2json/ai_extractor.py +156 -1
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import pandas as pd
|
|
| 3 |
import gradio as gr
|
| 4 |
from typing import Dict, Any, Type
|
| 5 |
from web2json.preprocessor import BasicPreprocessor
|
| 6 |
-
from web2json.ai_extractor import AIExtractor, GeminiLLMClient
|
| 7 |
from web2json.postprocessor import PostProcessor
|
| 8 |
from web2json.pipeline import Pipeline
|
| 9 |
from pydantic import BaseModel, Field, create_model
|
|
@@ -172,13 +172,13 @@ def webpage_to_json(content: str, is_url: bool, schema: BaseModel) -> Dict[str,
|
|
| 172 |
|
| 173 |
# Initialize pipeline components
|
| 174 |
# TODO: improve the RAG system and optimize (don't instantiate every time)
|
| 175 |
-
preprocessor = BasicPreprocessor(config={'keep_tags':
|
| 176 |
try:
|
| 177 |
llm = GeminiLLMClient(config={'api_key': os.getenv('GEMINI_API_KEY')})
|
| 178 |
except Exception as e:
|
| 179 |
return {"error": f"Failed to initialize LLM client: {str(e)}"}
|
| 180 |
|
| 181 |
-
ai_extractor =
|
| 182 |
postprocessor = PostProcessor()
|
| 183 |
pipeline = Pipeline(preprocessor, ai_extractor, postprocessor)
|
| 184 |
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from typing import Dict, Any, Type
|
| 5 |
from web2json.preprocessor import BasicPreprocessor
|
| 6 |
+
from web2json.ai_extractor import AIExtractor, RAGExtractor, GeminiLLMClient
|
| 7 |
from web2json.postprocessor import PostProcessor
|
| 8 |
from web2json.pipeline import Pipeline
|
| 9 |
from pydantic import BaseModel, Field, create_model
|
|
|
|
| 172 |
|
| 173 |
# Initialize pipeline components
|
| 174 |
# TODO: improve the RAG system and optimize (don't instantiate every time)
|
| 175 |
+
preprocessor = BasicPreprocessor(config={'keep_tags': True})
|
| 176 |
try:
|
| 177 |
llm = GeminiLLMClient(config={'api_key': os.getenv('GEMINI_API_KEY')})
|
| 178 |
except Exception as e:
|
| 179 |
return {"error": f"Failed to initialize LLM client: {str(e)}"}
|
| 180 |
|
| 181 |
+
ai_extractor = RAGExtractor(llm_client=llm, prompt_template=prompt_template)
|
| 182 |
postprocessor = PostProcessor()
|
| 183 |
pipeline = Pipeline(preprocessor, ai_extractor, postprocessor)
|
| 184 |
|
requirements.txt
CHANGED
|
@@ -1,8 +1,13 @@
|
|
| 1 |
pandas
|
| 2 |
gradio
|
|
|
|
| 3 |
pydantic
|
| 4 |
python-dotenv
|
| 5 |
beautifulsoup4
|
| 6 |
requests
|
| 7 |
google-genai
|
| 8 |
-
json_repair
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
pandas
|
| 2 |
gradio
|
| 3 |
+
gradio[mcp]
|
| 4 |
pydantic
|
| 5 |
python-dotenv
|
| 6 |
beautifulsoup4
|
| 7 |
requests
|
| 8 |
google-genai
|
| 9 |
+
json_repair
|
| 10 |
+
numpy
|
| 11 |
+
langchain
|
| 12 |
+
langchain-text-splitters
|
| 13 |
+
sentence-transformers
|
web2json/ai_extractor.py
CHANGED
|
@@ -3,6 +3,11 @@ from abc import ABC, abstractmethod
|
|
| 3 |
from google import genai
|
| 4 |
from google.genai import types
|
| 5 |
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
class LLMClient(ABC):
|
| 8 |
"""
|
|
@@ -125,4 +130,154 @@ class AIExtractor:
|
|
| 125 |
|
| 126 |
# TODO: RAGExtractor class
|
| 127 |
class RAGExtractor(AIExtractor):
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from google import genai
|
| 4 |
from google.genai import types
|
| 5 |
from pydantic import BaseModel
|
| 6 |
+
import numpy as np
|
| 7 |
+
from typing import List, Any, Dict, Tuple
|
| 8 |
+
import time
|
| 9 |
+
from langchain_text_splitters import HTMLHeaderTextSplitter
|
| 10 |
+
from sentence_transformers import SentenceTransformer
|
| 11 |
|
| 12 |
class LLMClient(ABC):
|
| 13 |
"""
|
|
|
|
| 130 |
|
| 131 |
# TODO: RAGExtractor class
|
| 132 |
class RAGExtractor(AIExtractor):
|
| 133 |
+
"""
|
| 134 |
+
RAG-enhanced extractor that uses similarity search to find relevant chunks
|
| 135 |
+
before performing extraction, utilizing HTML header-based chunking and SentenceTransformer embeddings.
|
| 136 |
+
"""
|
| 137 |
+
|
| 138 |
+
def __init__(self,
|
| 139 |
+
llm_client: LLMClient,
|
| 140 |
+
prompt_template: str,
|
| 141 |
+
embedding_model_path: str = "sentence-transformers/all-mpnet-base-v2",
|
| 142 |
+
top_k: int = 3):
|
| 143 |
+
"""
|
| 144 |
+
Initialize RAG extractor with embedding and chunking capabilities.
|
| 145 |
+
|
| 146 |
+
Args:
|
| 147 |
+
llm_client: LLM client for generation.
|
| 148 |
+
prompt_template: Template for prompts.
|
| 149 |
+
embedding_model_path: Path/name for the SentenceTransformer embedding model.
|
| 150 |
+
top_k: Number of top similar chunks to retrieve.
|
| 151 |
+
"""
|
| 152 |
+
super().__init__(llm_client, prompt_template)
|
| 153 |
+
self.embedding_model_path = embedding_model_path
|
| 154 |
+
# Initialize the SentenceTransformer model for embeddings
|
| 155 |
+
self.embedding_model_instance = SentenceTransformer(self.embedding_model_path)
|
| 156 |
+
self.top_k = top_k
|
| 157 |
+
|
| 158 |
+
@staticmethod
|
| 159 |
+
def _langchain_HHTS(text: str) -> List[str]:
|
| 160 |
+
"""
|
| 161 |
+
Chunks HTML text using Langchain's HTMLHeaderTextSplitter based on h1 and h2 headers.
|
| 162 |
+
|
| 163 |
+
Args:
|
| 164 |
+
text (str): The HTML content to chunk.
|
| 165 |
+
|
| 166 |
+
Returns:
|
| 167 |
+
List[str]: A list of chunked text strings (extracted from Document objects' page_content).
|
| 168 |
+
"""
|
| 169 |
+
headers_to_split_on = [
|
| 170 |
+
("h1", "Header 1"),
|
| 171 |
+
("h2", "Header 2"),
|
| 172 |
+
# ("h3", "Header 3"), # This header was explicitly commented out in the request
|
| 173 |
+
]
|
| 174 |
+
html_splitter = HTMLHeaderTextSplitter(headers_to_split_on=headers_to_split_on)
|
| 175 |
+
return [doc.page_content for doc in html_splitter.split_text(text)]
|
| 176 |
+
|
| 177 |
+
def embed_text(self, text: str) -> np.ndarray:
|
| 178 |
+
"""
|
| 179 |
+
Generate embeddings for text using the initialized SentenceTransformer model.
|
| 180 |
+
|
| 181 |
+
Args:
|
| 182 |
+
text: The text string to embed.
|
| 183 |
+
|
| 184 |
+
Returns:
|
| 185 |
+
np.ndarray: The embedding vector for the input text as a NumPy array.
|
| 186 |
+
"""
|
| 187 |
+
try:
|
| 188 |
+
return self.embedding_model_instance.encode(text)
|
| 189 |
+
except Exception as e:
|
| 190 |
+
print(f"Warning: Embedding failed for text: '{text[:50]}...', using random embedding: {e}")
|
| 191 |
+
|
| 192 |
+
return None
|
| 193 |
+
|
| 194 |
+
def search_similar_chunks(self,
|
| 195 |
+
query: str,
|
| 196 |
+
chunks: List[str],
|
| 197 |
+
embeddings: np.ndarray) -> List[str]:
|
| 198 |
+
"""
|
| 199 |
+
Find the most similar chunks to the query within the given list of chunks
|
| 200 |
+
by calculating cosine similarity between their embeddings.
|
| 201 |
+
|
| 202 |
+
Args:
|
| 203 |
+
query (str): The query text whose embedding will be used for similarity comparison.
|
| 204 |
+
chunks (List[str]): A list of text chunks to search within.
|
| 205 |
+
embeddings (np.ndarray): Precomputed embeddings for the chunks, corresponding to the 'chunks' list.
|
| 206 |
+
|
| 207 |
+
Returns:
|
| 208 |
+
List[str]: A list of the 'top_k' most similar chunks to the query.
|
| 209 |
+
"""
|
| 210 |
+
query_embedding = self.embed_text(query)
|
| 211 |
+
|
| 212 |
+
similarities = []
|
| 213 |
+
|
| 214 |
+
if query_embedding.ndim > 1:
|
| 215 |
+
query_embedding = query_embedding.flatten()
|
| 216 |
+
|
| 217 |
+
for i, chunk_embedding in enumerate(embeddings):
|
| 218 |
+
if chunk_embedding.ndim > 1:
|
| 219 |
+
chunk_embedding = chunk_embedding.flatten()
|
| 220 |
+
|
| 221 |
+
norm_query = np.linalg.norm(query_embedding)
|
| 222 |
+
norm_chunk = np.linalg.norm(chunk_embedding)
|
| 223 |
+
|
| 224 |
+
if norm_query == 0 or norm_chunk == 0:
|
| 225 |
+
similarity = 0.0
|
| 226 |
+
else:
|
| 227 |
+
similarity = np.dot(query_embedding, chunk_embedding) / (norm_query * norm_chunk)
|
| 228 |
+
similarities.append((similarity, i))
|
| 229 |
+
|
| 230 |
+
similarities.sort(key=lambda x: x[0], reverse=True)
|
| 231 |
+
top_indices = [idx for _, idx in similarities[:self.top_k]]
|
| 232 |
+
|
| 233 |
+
return [chunks[i] for i in top_indices]
|
| 234 |
+
|
| 235 |
+
def extract(self, content: str, schema: BaseModel, query: str = None) -> str:
|
| 236 |
+
"""
|
| 237 |
+
Overrides the base AIExtractor's method to implement RAG-enhanced extraction.
|
| 238 |
+
This function first chunks the input HTML content, then uses a query to find
|
| 239 |
+
the most relevant chunks via embedding similarity, and finally sends these
|
| 240 |
+
relevant chunks as context to the LLM for structured information extraction.
|
| 241 |
+
|
| 242 |
+
Args:
|
| 243 |
+
content (str): The raw HTML content from which to extract information.
|
| 244 |
+
schema (BaseModel): A Pydantic model defining the desired output structure for the LLM.
|
| 245 |
+
query (str, optional): An optional query string to guide the retrieval of relevant chunks.
|
| 246 |
+
If not provided, a default query based on the schema will be used.
|
| 247 |
+
|
| 248 |
+
Returns:
|
| 249 |
+
str: The structured JSON object as a string, as generated by the LLM.
|
| 250 |
+
"""
|
| 251 |
+
start_time = time.time()
|
| 252 |
+
|
| 253 |
+
if not query:
|
| 254 |
+
query = f"Extract information based on the following JSON schema: {schema.model_json_schema()}"
|
| 255 |
+
print(f"No explicit query provided for retrieval. Using default: '{query[:100]}...'")
|
| 256 |
+
|
| 257 |
+
chunks = self._langchain_HHTS(content)
|
| 258 |
+
print(f"Content successfully chunked into {len(chunks)} pieces.")
|
| 259 |
+
|
| 260 |
+
combined_content_for_llm = ""
|
| 261 |
+
if not chunks:
|
| 262 |
+
print("Warning: No chunks were generated from the provided content. The entire original content will be sent to the LLM.")
|
| 263 |
+
combined_content_for_llm = content
|
| 264 |
+
else:
|
| 265 |
+
chunk_embeddings = np.array([self.embed_text(chunk) for chunk in chunks])
|
| 266 |
+
print(f"Generated embeddings for {len(chunks)} chunks.")
|
| 267 |
+
|
| 268 |
+
similar_chunks = self.search_similar_chunks(query, chunks, chunk_embeddings)
|
| 269 |
+
print(f"Retrieved {len(similar_chunks)} similar chunks based on the query.")
|
| 270 |
+
|
| 271 |
+
combined_content_for_llm = "\n\n".join(similar_chunks)
|
| 272 |
+
print(f"Combined content for LLM (truncated): '{combined_content_for_llm[:200]}...'")
|
| 273 |
+
|
| 274 |
+
prompt = self.prompt_template.format(content=combined_content_for_llm, schema=schema.model_json_schema())
|
| 275 |
+
print(f"Sending prompt to LLM (truncated): '{prompt[:500]}...'")
|
| 276 |
+
llm_response = self.llm_client.call_api(prompt)
|
| 277 |
+
|
| 278 |
+
execution_time = (time.time() - start_time) * 1000
|
| 279 |
+
print(f"Extraction process completed in {execution_time:.2f} milliseconds.")
|
| 280 |
+
print(f"LLM's final response: {llm_response}")
|
| 281 |
+
print("=" * 78)
|
| 282 |
+
|
| 283 |
+
return llm_response
|