File size: 15,168 Bytes
3bbc58b 227ab6a 3bbc58b 227ab6a 3bbc58b 853145b 9e8ddda |
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 |
import os
import uuid
import shutil
import logging
from typing import List, Optional, Dict, Any
from pathlib import Path
from langchain.schema import Document as LangchainDocument
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
import fitz # PyMuPDF
import markdown
from fastapi import FastAPI, File, UploadFile, HTTPException, Form, Depends, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from dotenv import load_dotenv
from openrouter_llm import OpenRouterFreeAdapter, OpenRouterFreeChain
# Load environment variables
load_dotenv()
# Import LangChain components for embedding
# Import our free-only OpenRouter adapter
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize FastAPI app
app = FastAPI(title="AskMyDocs API - Free LLM Edition")
# Add CORS middleware for frontend integration
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Set to specific domain in production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Configuration
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY", "")
HF_MODEL_NAME = os.getenv(
"HF_MODEL_NAME", "sentence-transformers/all-mpnet-base-v2")
UPLOAD_DIR = os.getenv("UPLOAD_DIR", "./uploads")
DB_DIR = os.getenv("DB_DIR", "./vectordb")
print(HF_MODEL_NAME)
# Ensure directories exist
os.makedirs(UPLOAD_DIR, exist_ok=True)
os.makedirs(DB_DIR, exist_ok=True)
# Initialize OpenRouter adapter (singleton)
openrouter_adapter = None
# Pydantic models
class QueryRequest(BaseModel):
query: str
collection_id: str
class QueryResponse(BaseModel):
answer: str
sources: List[str]
class Document(BaseModel):
id: str
filename: str
content_type: str
class DocumentList(BaseModel):
documents: List[Document]
class LLMInfo(BaseModel):
model: str
is_free: bool = True
provider: str = "openrouter"
class LLMModelsList(BaseModel):
current_model: str
free_models: List[Dict[str, Any]]
# Global variable to store vector databases (in memory for simplicity)
# In production, you would use persistent storage
vector_dbs = {}
# Helper functions
def get_embeddings():
"""Get HuggingFace embedding model."""
return HuggingFaceEmbeddings(model_name=HF_MODEL_NAME)
def get_openrouter_adapter():
"""Get or initialize the OpenRouter adapter for free models."""
global openrouter_adapter
if openrouter_adapter is None:
openrouter_adapter = OpenRouterFreeAdapter(api_key=OPENROUTER_API_KEY)
return openrouter_adapter
def extract_text_from_pdf(file_path):
"""Extract text content from PDF files."""
text = ""
try:
doc = fitz.open(file_path)
for page in doc:
text += page.get_text()
return text
except Exception as e:
logger.error(f"Error extracting text from PDF: {e}")
raise HTTPException(
status_code=500, detail=f"Error processing PDF: {str(e)}")
def extract_text_from_markdown(file_path):
"""Convert Markdown to plain text."""
try:
with open(file_path, 'r', encoding='utf-8') as f:
md_content = f.read()
html = markdown.markdown(md_content)
# Simple HTML to text conversion - in production use a more robust method
text = html.replace('<p>', '\n\n').replace(
'</p>', '').replace('<br>', '\n')
text = text.replace('<h1>', '\n\n# ').replace('</h1>', '\n')
text = text.replace('<h2>', '\n\n## ').replace('</h2>', '\n')
text = text.replace('<h3>', '\n\n### ').replace('</h3>', '\n')
# Remove other HTML tags
import re
text = re.sub('<[^<]+?>', '', text)
return text
except Exception as e:
logger.error(f"Error processing Markdown: {e}")
raise HTTPException(
status_code=500, detail=f"Error processing Markdown: {str(e)}")
def extract_text_from_file(file_path, content_type):
"""Extract text based on file type."""
if content_type == "application/pdf":
return extract_text_from_pdf(file_path)
elif content_type == "text/markdown":
return extract_text_from_markdown(file_path)
elif content_type == "text/plain":
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
else:
raise HTTPException(
status_code=400, detail=f"Unsupported file type: {content_type}")
def process_documents(collection_id: str, file_paths: List[tuple]):
"""Process documents and create vector store."""
try:
# Create text splitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=100,
length_function=len,
)
all_docs = []
for file_path, content_type, filename in file_paths:
text_content = extract_text_from_file(file_path, content_type)
chunks = text_splitter.split_text(text_content)
# Create Document objects with metadata
docs = [
LangchainDocument(
page_content=chunk,
metadata={"source": filename, "chunk": i}
)
for i, chunk in enumerate(chunks)
]
all_docs.extend(docs)
# Create vector store
embeddings = get_embeddings()
vector_db = FAISS.from_documents(all_docs, embeddings)
# Save vector store
collection_path = os.path.join(DB_DIR, collection_id)
os.makedirs(collection_path, exist_ok=True)
vector_db.save_local(collection_path)
# Store in memory (would be replaced by database lookup in production)
vector_dbs[collection_id] = vector_db
logger.info(
f"Successfully processed {len(all_docs)} chunks from {len(file_paths)} documents")
except Exception as e:
logger.error(f"Error processing documents: {e}")
raise HTTPException(
status_code=500, detail=f"Error processing documents: {str(e)}")
@app.get("/")
async def index():
return {"message": "Welcome to ask my doc"}
@app.get("/health")
async def health_check():
return {"status": "healthy"}
@app.post("/upload", response_model=Document)
async def upload_file(
background_tasks: BackgroundTasks,
collection_id: str = Form(...),
file: UploadFile = File(...),
):
"""Upload a document and process it for querying."""
try:
# Generate a unique ID for the document
doc_id = str(uuid.uuid4())
# Create collection directory if it doesn't exist
collection_dir = os.path.join(UPLOAD_DIR, collection_id)
os.makedirs(collection_dir, exist_ok=True)
# Define the file path
file_path = os.path.join(collection_dir, file.filename)
# Determine content type
content_type = file.content_type
if not content_type:
if file.filename.endswith('.pdf'):
content_type = "application/pdf"
elif file.filename.endswith('.md'):
content_type = "text/markdown"
elif file.filename.endswith('.txt'):
content_type = "text/plain"
else:
raise HTTPException(
status_code=400, detail="Unsupported file type")
# Save the file
with open(file_path, "wb") as f:
shutil.copyfileobj(file.file, f)
# Process the document in the background
background_tasks.add_task(
process_documents,
collection_id,
[(file_path, content_type, file.filename)]
)
return Document(
id=doc_id,
filename=file.filename,
content_type=content_type
)
except Exception as e:
logger.error(f"Error uploading file: {e}")
raise HTTPException(
status_code=500, detail=f"Error uploading file: {str(e)}")
@app.get("/collections/{collection_id}/documents", response_model=DocumentList)
async def list_documents(collection_id: str):
"""List all documents in a collection."""
try:
collection_dir = os.path.join(UPLOAD_DIR, collection_id)
if not os.path.exists(collection_dir):
return DocumentList(documents=[])
documents = []
for filename in os.listdir(collection_dir):
file_path = os.path.join(collection_dir, filename)
if os.path.isfile(file_path):
content_type = "application/octet-stream"
if filename.endswith('.pdf'):
content_type = "application/pdf"
elif filename.endswith('.md'):
content_type = "text/markdown"
elif filename.endswith('.txt'):
content_type = "text/plain"
documents.append(Document(
# In production, store and retrieve actual IDs
id=str(uuid.uuid4()),
filename=filename,
content_type=content_type
))
return DocumentList(documents=documents)
except Exception as e:
logger.error(f"Error listing documents: {e}")
raise HTTPException(
status_code=500, detail=f"Error listing documents: {str(e)}")
@app.post("/query", response_model=QueryResponse)
async def query_documents(request: QueryRequest):
"""Query documents using natural language."""
try:
collection_id = request.collection_id
# Check if vector DB exists in memory
if collection_id in vector_dbs:
vector_db = vector_dbs[collection_id]
else:
# Load from disk
collection_path = os.path.join(DB_DIR, collection_id)
if not os.path.exists(collection_path):
raise HTTPException(
status_code=404, detail=f"Collection {collection_id} not found")
embeddings = get_embeddings()
vector_db = FAISS.load_local(collection_path, embeddings)
vector_dbs[collection_id] = vector_db
# Get the retriever
retriever = vector_db.as_retriever(search_kwargs={"k": 3})
# Get relevant documents
docs = retriever.get_relevant_documents(request.query)
# Extract sources
sources = []
for doc in docs:
if doc.metadata.get("source") not in sources:
sources.append(doc.metadata.get("source"))
# Get context from documents
context = [doc.page_content for doc in docs]
# Get OpenRouter adapter for free LLMs
adapter = get_openrouter_adapter()
chain = OpenRouterFreeChain(adapter)
# Generate answer
answer = chain.run(request.query, context)
return QueryResponse(
answer=answer,
sources=sources
)
except Exception as e:
logger.error(f"Error querying documents: {e}")
raise HTTPException(
status_code=500, detail=f"Error querying documents: {str(e)}")
@app.delete("/collections/{collection_id}/documents/{filename}")
async def delete_document(collection_id: str, filename: str):
"""Delete a document from a collection."""
try:
file_path = os.path.join(UPLOAD_DIR, collection_id, filename)
if not os.path.exists(file_path):
raise HTTPException(
status_code=404, detail=f"Document {filename} not found")
os.remove(file_path)
# Rebuild vector store if needed
collection_path = os.path.join(DB_DIR, collection_id)
if os.path.exists(collection_path):
# In production, you would selectively remove documents rather than rebuilding
shutil.rmtree(collection_path)
# If there are still documents, rebuild the vector store
collection_dir = os.path.join(UPLOAD_DIR, collection_id)
if os.path.exists(collection_dir) and os.listdir(collection_dir):
file_paths = []
for fname in os.listdir(collection_dir):
fpath = os.path.join(collection_dir, fname)
if os.path.isfile(fpath):
content_type = "application/octet-stream"
if fname.endswith('.pdf'):
content_type = "application/pdf"
elif fname.endswith('.md'):
content_type = "text/markdown"
elif fname.endswith('.txt'):
content_type = "text/plain"
file_paths.append((fpath, content_type, fname))
if file_paths:
process_documents(collection_id, file_paths)
# Remove from in-memory cache
if collection_id in vector_dbs:
del vector_dbs[collection_id]
return JSONResponse(content={"message": f"Document {filename} deleted"})
except Exception as e:
logger.error(f"Error deleting document: {e}")
raise HTTPException(
status_code=500, detail=f"Error deleting document: {str(e)}")
@app.get("/llm/info", response_model=LLMInfo)
async def get_llm_info():
"""Get the current LLM information."""
adapter = get_openrouter_adapter()
return LLMInfo(
model=adapter.model,
is_free=True,
provider="openrouter"
)
@app.get("/llm/models", response_model=LLMModelsList)
async def list_free_models():
"""List all available free models."""
adapter = get_openrouter_adapter()
free_models = adapter.list_free_models()
# Create a simplified list for the frontend
model_list = []
for model in free_models:
model_info = {
"id": model.get("id"),
"name": model.get("name", model.get("id")),
"context_length": model.get("context_length", 4096),
"provider": model.get("id").split("/")[0] if "/" in model.get("id") else "unknown"
}
model_list.append(model_info)
return LLMModelsList(
current_model=adapter.model,
free_models=model_list
)
@app.post("/llm/change-model")
async def change_model(model_info: LLMInfo):
"""Change the LLM model (only to another free model)."""
adapter = get_openrouter_adapter()
# Make sure the model has the :free suffix if it doesn't already
model_id = model_info.model
if not model_id.endswith(":free") and ":free" not in model_id:
model_id = f"{model_id}:free"
# Set the new model
adapter.model = model_id
return JSONResponse(content={"message": f"Model changed to {model_id}"})
if __name__ == "__main__":
import uvicorn
# Check if we have an OpenRouter adapter and initialize it
adapter = get_openrouter_adapter()
logger.info(f"Starting AskMyDocs with free model: {adapter.model}")
uvicorn.run(app, host="0.0.0.0", port=7860)
|