File size: 20,490 Bytes
8ba2581 3e772ec 8ba2581 3e772ec 8ba2581 3e772ec 8ba2581 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
import asyncio
import aiohttp
import chromadb
from chromadb.utils import embedding_functions
import json
import logging
from typing import Dict, List, Any, Optional
from datetime import datetime
import hashlib
from pathlib import Path
import requests
# Document processing libraries (all free)
import PyPDF2
import docx
from bs4 import BeautifulSoup
import pandas as pd
import markdown
import xml.etree.ElementTree as ET
from newspaper import Article
import trafilatura
from duckduckgo_search import DDGS
# AI libraries
from config import Config
from mistralai.client import MistralClient
import anthropic
# Set up logging
logger = logging.getLogger(__name__)
# Initialize AI clients
mistral_client = MistralClient(api_key=Config.MISTRAL_API_KEY) if Config.MISTRAL_API_KEY else None
anthropic_client = anthropic.Anthropic(api_key=Config.ANTHROPIC_API_KEY) if Config.ANTHROPIC_API_KEY else None
# Initialize ChromaDB
chroma_client = chromadb.PersistentClient(path=Config.CHROMA_DB_PATH)
embedding_function = embedding_functions.SentenceTransformerEmbeddingFunction(
model_name=Config.EMBEDDING_MODEL
)
# Get or create collection
try:
collection = chroma_client.get_collection(
name=Config.CHROMA_COLLECTION_NAME,
embedding_function=embedding_function
)
except:
collection = chroma_client.create_collection(
name=Config.CHROMA_COLLECTION_NAME,
embedding_function=embedding_function
)
class DocumentProcessor:
"""Free document processing without Unstructured API"""
@staticmethod
def extract_text_from_pdf(file_path: str) -> str:
"""Extract text from PDF files"""
text = ""
try:
with open(file_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
for page_num in range(len(pdf_reader.pages)):
page = pdf_reader.pages[page_num]
text += page.extract_text() + "\n"
except Exception as e:
logger.error(f"Error reading PDF: {e}")
return text
@staticmethod
def extract_text_from_docx(file_path: str) -> str:
"""Extract text from DOCX files"""
try:
doc = docx.Document(file_path)
text = "\n".join([paragraph.text for paragraph in doc.paragraphs])
return text
except Exception as e:
logger.error(f"Error reading DOCX: {e}")
return ""
@staticmethod
def extract_text_from_html(file_path: str) -> str:
"""Extract text from HTML files"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
soup = BeautifulSoup(file.read(), 'html.parser')
# Remove script and style elements
for script in soup(["script", "style"]):
script.extract()
text = soup.get_text()
lines = (line.strip() for line in text.splitlines())
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
text = '\n'.join(chunk for chunk in chunks if chunk)
return text
except Exception as e:
logger.error(f"Error reading HTML: {e}")
return ""
@staticmethod
def extract_text_from_txt(file_path: str) -> str:
"""Extract text from TXT files"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
except Exception as e:
logger.error(f"Error reading TXT: {e}")
return ""
@staticmethod
def extract_text_from_csv(file_path: str) -> str:
"""Extract text from CSV files"""
try:
df = pd.read_csv(file_path)
return df.to_string()
except Exception as e:
logger.error(f"Error reading CSV: {e}")
return ""
@staticmethod
def extract_text_from_json(file_path: str) -> str:
"""Extract text from JSON files"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
return json.dumps(data, indent=2)
except Exception as e:
logger.error(f"Error reading JSON: {e}")
return ""
@staticmethod
def extract_text_from_markdown(file_path: str) -> str:
"""Extract text from Markdown files"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
md_text = file.read()
html = markdown.markdown(md_text)
soup = BeautifulSoup(html, 'html.parser')
return soup.get_text()
except Exception as e:
logger.error(f"Error reading Markdown: {e}")
return ""
@staticmethod
def extract_text_from_xml(file_path: str) -> str:
"""Extract text from XML files"""
try:
tree = ET.parse(file_path)
root = tree.getroot()
def extract_text(element):
text = element.text or ""
for child in element:
text += " " + extract_text(child)
return text.strip()
return extract_text(root)
except Exception as e:
logger.error(f"Error reading XML: {e}")
return ""
@classmethod
def extract_text(cls, file_path: str) -> str:
"""Extract text from any supported file type"""
path = Path(file_path)
extension = path.suffix.lower()
extractors = {
'.pdf': cls.extract_text_from_pdf,
'.docx': cls.extract_text_from_docx,
'.doc': cls.extract_text_from_docx,
'.html': cls.extract_text_from_html,
'.htm': cls.extract_text_from_html,
'.txt': cls.extract_text_from_txt,
'.csv': cls.extract_text_from_csv,
'.json': cls.extract_text_from_json,
'.md': cls.extract_text_from_markdown,
'.xml': cls.extract_text_from_xml,
}
extractor = extractors.get(extension, cls.extract_text_from_txt)
return extractor(file_path)
def chunk_text(text: str, chunk_size: int = 1000, overlap: int = 100) -> List[str]:
"""Split text into chunks with overlap"""
chunks = []
start = 0
text_length = len(text)
while start < text_length:
end = start + chunk_size
chunk = text[start:end]
# Try to find a sentence boundary
if end < text_length:
last_period = chunk.rfind('.')
last_newline = chunk.rfind('\n')
boundary = max(last_period, last_newline)
if boundary > chunk_size // 2:
chunk = text[start:start + boundary + 1]
end = start + boundary + 1
chunks.append(chunk.strip())
start = end - overlap
return chunks
async def fetch_web_content_free(url: str) -> Optional[str]:
"""Fetch content from URL using multiple free methods"""
# Method 1: Try newspaper3k (best for articles)
try:
article = Article(url)
article.download()
article.parse()
content = f"{article.title}\n\n{article.text}"
if len(content) > 100: # Valid content
return content
except Exception as e:
logger.debug(f"Newspaper failed: {e}")
# Method 2: Try trafilatura (great for web scraping)
try:
downloaded = trafilatura.fetch_url(url)
content = trafilatura.extract(downloaded)
if content and len(content) > 100:
return content
except Exception as e:
logger.debug(f"Trafilatura failed: {e}")
# Method 3: Basic BeautifulSoup scraping
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
response = requests.get(url, headers=headers, timeout=10)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Remove unwanted elements
for element in soup(['script', 'style', 'nav', 'footer', 'header']):
element.decompose()
# Try to find main content
main_content = None
# Common content selectors
content_selectors = [
'main', 'article', '[role="main"]',
'.content', '#content', '.post', '.entry-content',
'.article-body', '.story-body'
]
for selector in content_selectors:
main_content = soup.select_one(selector)
if main_content:
break
if not main_content:
main_content = soup.find('body')
if main_content:
text = main_content.get_text(separator='\n', strip=True)
# Get title
title = soup.find('title')
title_text = title.get_text() if title else "No title"
return f"{title_text}\n\n{text}"
except Exception as e:
logger.error(f"BeautifulSoup failed: {e}")
return None
async def search_web_free(query: str, num_results: int = 5) -> List[Dict[str, str]]:
"""Search the web using free methods (DuckDuckGo)"""
try:
results = []
with DDGS() as ddgs:
for r in ddgs.text(query, max_results=num_results):
results.append({
'title': r.get('title', ''),
'url': r.get('link', ''),
'snippet': r.get('body', '')
})
return results
except Exception as e:
logger.error(f"Search failed: {e}")
return []
# In mcp_tools.py
async def generate_tags(content: str) -> List[str]:
"""Generate tags using Mistral AI or fallback to free method"""
try:
if mistral_client: # This is MistralClient from mistralai.client
prompt = f"""Analyze this content and generate 5-7 relevant tags.
Return only the tags as a comma-separated list.
Content: {content[:2000]}...
Tags:"""
# For mistralai==0.4.2, pass messages as a list of dicts
response = mistral_client.chat(
model=Config.MISTRAL_MODEL,
messages=[{"role": "user", "content": prompt}] # <--- CHANGE HERE
)
tags_text = response.choices[0].message.content.strip()
tags = [tag.strip() for tag in tags_text.split(",")]
return tags[:7]
else:
# Free fallback: Extract keywords using frequency analysis
return generate_tags_free(content)
except Exception as e:
logger.error(f"Error generating tags: {str(e)}")
return generate_tags_free(content)
def generate_tags_free(content: str) -> List[str]:
"""Free tag generation using keyword extraction"""
from collections import Counter
import re
# Simple keyword extraction
words = re.findall(r'\b[a-z]{4,}\b', content.lower())
# Common stop words
stop_words = {
'this', 'that', 'these', 'those', 'what', 'which', 'when', 'where',
'who', 'whom', 'whose', 'why', 'how', 'with', 'about', 'against',
'between', 'into', 'through', 'during', 'before', 'after', 'above',
'below', 'from', 'down', 'out', 'off', 'over', 'under', 'again',
'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why',
'how', 'all', 'both', 'each', 'few', 'more', 'most', 'other', 'some',
'such', 'only', 'same', 'than', 'that', 'have', 'has', 'had',
'been', 'being', 'does', 'doing', 'will', 'would', 'could', 'should'
}
# Filter and count words
filtered_words = [w for w in words if w not in stop_words and len(w) > 4]
word_counts = Counter(filtered_words)
# Get top keywords
top_keywords = [word for word, _ in word_counts.most_common(7)]
return top_keywords if top_keywords else ["untagged"]
async def generate_summary(content: str) -> str:
"""Generate summary using Claude or fallback to free method"""
try:
if anthropic_client:
message = anthropic_client.messages.create(
model=Config.CLAUDE_MODEL,
max_tokens=300,
messages=[{
"role": "user",
"content": f"Summarize this content in 2-3 sentences:\n\n{content[:4000]}..."
}]
)
return message.content[0].text.strip()
else:
# Free fallback
return generate_summary_free(content)
except Exception as e:
logger.error(f"Error generating summary: {str(e)}")
return generate_summary_free(content)
def generate_summary_free(content: str) -> str:
"""Free summary generation using simple extraction"""
sentences = content.split('.')
# Take first 3 sentences
summary_sentences = sentences[:3]
summary = '. '.join(s.strip() for s in summary_sentences if s.strip())
if len(summary) > 300:
summary = summary[:297] + "..."
return summary if summary else "Content preview: " + content[:200] + "..."
async def process_local_file(file_path: str) -> Dict[str, Any]:
"""Process a local file and store it in the knowledge base"""
try:
# Validate file
path = Path(file_path)
if not path.exists():
raise FileNotFoundError(f"File not found: {file_path}")
if path.suffix.lower() not in Config.SUPPORTED_FILE_TYPES:
raise ValueError(f"Unsupported file type: {path.suffix}")
# Extract text using free methods
full_text = DocumentProcessor.extract_text(file_path)
if not full_text:
raise ValueError("No text could be extracted from the file")
# Generate document ID
doc_id = hashlib.md5(f"{path.name}_{datetime.now().isoformat()}".encode()).hexdigest()
# Generate tags
tags = await generate_tags(full_text[:3000])
# Generate summary
summary = await generate_summary(full_text[:5000])
# Chunk the text
chunks = chunk_text(full_text, chunk_size=1000, overlap=100)
chunks = chunks[:10] # Limit chunks for demo
# Store in ChromaDB
chunk_ids = [f"{doc_id}_{i}" for i in range(len(chunks))]
metadata = {
"source": str(path),
"file_name": path.name,
"file_type": path.suffix,
"processed_at": datetime.now().isoformat(),
"tags": ", ".join(tags),
"summary": summary,
"doc_id": doc_id
}
collection.add(
documents=chunks,
ids=chunk_ids,
metadatas=[metadata for _ in chunks]
)
return {
"success": True,
"doc_id": doc_id,
"file_name": path.name,
"tags": tags,
"summary": summary,
"chunks_processed": len(chunks),
"metadata": metadata
}
except Exception as e:
logger.error(f"Error processing file: {str(e)}")
return {
"success": False,
"error": str(e)
}
async def process_web_content(url_or_query: str) -> Dict[str, Any]:
"""Process web content from URL or search query"""
try:
# Check if it's a URL or search query
is_url = url_or_query.startswith(('http://', 'https://'))
if is_url:
content = await fetch_web_content_free(url_or_query)
source = url_or_query
else:
# It's a search query
search_results = await search_web_free(url_or_query, num_results=3)
if not search_results:
raise ValueError("No search results found")
# Process the first result
first_result = search_results[0]
content = await fetch_web_content_free(first_result['url'])
source = first_result['url']
# Add search context
content = f"Search Query: {url_or_query}\n\n{first_result['title']}\n\n{content}"
if not content:
raise ValueError("Failed to fetch content")
# Generate document ID
doc_id = hashlib.md5(f"{source}_{datetime.now().isoformat()}".encode()).hexdigest()
# Generate tags
tags = await generate_tags(content[:3000])
# Generate summary
summary = await generate_summary(content[:5000])
# Chunk the content
chunks = chunk_text(content, chunk_size=1000, overlap=100)
chunks = chunks[:10] # Limit for demo
# Store in ChromaDB
chunk_ids = [f"{doc_id}_{i}" for i in range(len(chunks))]
metadata = {
"source": source,
"url": source if is_url else f"Search: {url_or_query}",
"content_type": "web",
"processed_at": datetime.now().isoformat(),
"tags": ", ".join(tags),
"summary": summary,
"doc_id": doc_id
}
collection.add(
documents=chunks,
ids=chunk_ids,
metadatas=[metadata for _ in chunks]
)
return {
"success": True,
"doc_id": doc_id,
"url": source,
"tags": tags,
"summary": summary,
"chunks_processed": len(chunks),
"metadata": metadata,
"search_query": url_or_query if not is_url else None
}
except Exception as e:
logger.error(f"Error processing web content: {str(e)}")
return {
"success": False,
"error": str(e)
}
async def search_knowledge_base(query: str, limit: int = 5) -> List[Dict[str, Any]]:
"""Perform semantic search in the knowledge base"""
try:
results = collection.query(
query_texts=[query],
n_results=limit
)
if not results["ids"][0]:
return []
# Format results
formatted_results = []
seen_docs = set()
for i, doc_id in enumerate(results["ids"][0]):
metadata = results["metadatas"][0][i]
# Deduplicate by document
if metadata["doc_id"] not in seen_docs:
seen_docs.add(metadata["doc_id"])
formatted_results.append({
"doc_id": metadata["doc_id"],
"source": metadata.get("source", "Unknown"),
"tags": metadata.get("tags", "").split(", "),
"summary": metadata.get("summary", ""),
"relevance_score": 1 - results["distances"][0][i],
"processed_at": metadata.get("processed_at", "")
})
return formatted_results
except Exception as e:
logger.error(f"Error searching knowledge base: {str(e)}")
return []
async def get_document_details(doc_id: str) -> Dict[str, Any]:
"""Get detailed information about a document"""
try:
results = collection.get(
where={"doc_id": doc_id},
limit=1
)
if not results["ids"]:
return {"error": "Document not found"}
metadata = results["metadatas"][0]
return {
"doc_id": doc_id,
"source": metadata.get("source", "Unknown"),
"tags": metadata.get("tags", "").split(", "),
"summary": metadata.get("summary", ""),
"processed_at": metadata.get("processed_at", ""),
"file_type": metadata.get("file_type", ""),
"content_preview": results["documents"][0][:500] + "..."
}
except Exception as e:
logger.error(f"Error getting document details: {str(e)}")
return {"error": str(e)} |