Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /saichowdary007_docu-query /src /docuquery_ai /cli /main.py
| """ | |
| Command-line interface for DocuQuery AI. | |
| """ | |
| import json | |
| import logging | |
| import os | |
| import sys | |
| from pathlib import Path | |
| from typing import Optional | |
| import click | |
| from docuquery_ai import DocumentQueryClient, __version__ | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", | |
| ) | |
| logger = logging.getLogger(__name__) | |
| def cli(ctx, google_api_key, google_project_id, vector_store_path): | |
| """DocuQuery AI - Document querying with RAG and LLM.""" | |
| # Ensure context object exists | |
| ctx.ensure_object(dict) | |
| # Store configuration in context | |
| ctx.obj["google_api_key"] = google_api_key | |
| ctx.obj["google_project_id"] = google_project_id | |
| ctx.obj["vector_store_path"] = vector_store_path | |
| def upload(ctx, file_path, user_id, output): | |
| """Upload and process a document.""" | |
| try: | |
| client = DocumentQueryClient( | |
| google_api_key=ctx.obj["google_api_key"], | |
| google_project_id=ctx.obj["google_project_id"], | |
| vector_store_path=ctx.obj["vector_store_path"], | |
| ) | |
| result = client.upload_document(file_path, user_id) | |
| if output == "json": | |
| click.echo(json.dumps(result, indent=2)) | |
| else: | |
| if result["success"]: | |
| click.echo(f"✅ Successfully uploaded: {result['filename']}") | |
| click.echo(f" File ID: {result['file_id']}") | |
| click.echo(f" Type: {result['file_type']}") | |
| click.echo(f" Structured: {result['is_structured']}") | |
| if "chunks_count" in result: | |
| click.echo(f" Chunks: {result['chunks_count']}") | |
| else: | |
| click.echo(f"❌ Upload failed: {result['error']}", err=True) | |
| sys.exit(1) | |
| except (ValueError, IOError) as exc: | |
| click.echo(f"❌ Error: {str(exc)}", err=True) | |
| sys.exit(1) | |
| def query(ctx, question, user_id, file_ids, output): | |
| """Query uploaded documents.""" | |
| try: | |
| client = DocumentQueryClient( | |
| google_api_key=ctx.obj["google_api_key"], | |
| google_project_id=ctx.obj["google_project_id"], | |
| vector_store_path=ctx.obj["vector_store_path"], | |
| ) | |
| file_id_list = file_ids.split(",") if file_ids else None | |
| result = client.query(question, user_id, file_id_list) | |
| if output == "json": | |
| # Convert QueryResponse to dict for JSON serialization | |
| result_dict = { | |
| "answer": result.answer, | |
| "sources": result.sources, | |
| "type": result.type, | |
| "download_url": result.download_url, | |
| "metadata": result.metadata, | |
| } | |
| click.echo(json.dumps(result_dict, indent=2)) | |
| else: | |
| click.echo(f"🤖 Answer: {result.answer}") | |
| if result.sources: | |
| click.echo(f"📄 Sources: {', '.join(result.sources)}") | |
| if result.download_url: | |
| click.echo(f"💾 Download: {result.download_url}") | |
| except (ValueError, IOError) as exc: | |
| click.echo(f"❌ Error: {str(exc)}", err=True) | |
| sys.exit(1) | |
| def list(ctx, user_id, output): | |
| """List uploaded documents.""" | |
| try: | |
| client = DocumentQueryClient( | |
| google_api_key=ctx.obj["google_api_key"], | |
| google_project_id=ctx.obj["google_project_id"], | |
| vector_store_path=ctx.obj["vector_store_path"], | |
| ) | |
| documents = client.list_documents(user_id) | |
| if output == "json": | |
| click.echo(json.dumps(documents, indent=2)) | |
| else: | |
| if not documents: | |
| click.echo("No documents found.") | |
| else: | |
| click.echo(f"📄 Found {len(documents)} document(s):") | |
| for doc in documents: | |
| click.echo( | |
| f" • {doc['filename']} ({doc['file_type']}) - ID: {doc['file_id']}" | |
| ) | |
| if doc["created_at"]: | |
| click.echo(f" Created: {doc['created_at']}") | |
| except (ValueError, IOError) as exc: | |
| click.echo(f"❌ Error: {str(exc)}", err=True) | |
| sys.exit(1) | |
| def delete(ctx, file_id, user_id): | |
| """Delete a document.""" | |
| try: | |
| client = DocumentQueryClient( | |
| google_api_key=ctx.obj["google_api_key"], | |
| google_project_id=ctx.obj["google_project_id"], | |
| vector_store_path=ctx.obj["vector_store_path"], | |
| ) | |
| success = client.delete_document(file_id, user_id) | |
| if success: | |
| click.echo(f"✅ Successfully deleted document: {file_id}") | |
| else: | |
| click.echo(f"❌ Document not found: {file_id}", err=True) | |
| sys.exit(1) | |
| except (ValueError, IOError) as exc: | |
| click.echo(f"❌ Error: {str(exc)}", err=True) | |
| sys.exit(1) | |
| def init(ctx): | |
| """Initialize DocuQuery AI configuration.""" | |
| click.echo("🚀 Initializing DocuQuery AI...") | |
| # Check for Google credentials | |
| google_api_key = ctx.obj["google_api_key"] or os.getenv("GOOGLE_API_KEY") | |
| google_project_id = ctx.obj["google_project_id"] or os.getenv("GOOGLE_PROJECT_ID") | |
| if not google_api_key: | |
| click.echo( | |
| "⚠️ GOOGLE_API_KEY not found. Please set it as an environment variable." | |
| ) | |
| click.echo(" export GOOGLE_API_KEY='your-api-key'") | |
| if not google_project_id: | |
| click.echo( | |
| "⚠️ GOOGLE_PROJECT_ID not found. Please set it as an environment variable." | |
| ) | |
| click.echo(" export GOOGLE_PROJECT_ID='your-project-id'") | |
| if google_api_key and google_project_id: | |
| try: | |
| client = DocumentQueryClient( | |
| google_api_key=google_api_key, | |
| google_project_id=google_project_id, | |
| vector_store_path=ctx.obj["vector_store_path"], | |
| ) | |
| click.echo("✅ DocuQuery AI initialized successfully!") | |
| click.echo(f" Vector store: {ctx.obj['vector_store_path']}") | |
| except (ValueError, IOError) as exc: | |
| click.echo(f"❌ Initialization failed: {str(exc)}", err=True) | |
| sys.exit(1) | |
| def main(): | |
| """Entry point for the CLI.""" | |
| cli(obj={}) | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 7.85 kB
- Xet hash:
- f2ab0596276e07ddee814e158f1f2fd2866a78de43a8fc9673ecbc22192f739b
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.