|
| 1 | +import logging |
| 2 | + |
| 3 | +import typer |
| 4 | +from dotenv import load_dotenv |
| 5 | + |
| 6 | +from template_langgraph.loggers import get_logger |
| 7 | +from template_langgraph.tools.elasticsearch_tool import ElasticsearchClientWrapper |
| 8 | +from template_langgraph.tools.pdf_loaders import PdfLoaderWrapper |
| 9 | + |
| 10 | +# Initialize the Typer application |
| 11 | +app = typer.Typer( |
| 12 | + add_completion=False, |
| 13 | + help="Elasticsearch operator CLI", |
| 14 | +) |
| 15 | + |
| 16 | +# Set up logging |
| 17 | +logger = get_logger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +@app.command() |
| 21 | +def search_documents( |
| 22 | + index_name: str = typer.Option( |
| 23 | + "docs_kabuto", |
| 24 | + "--index-name", |
| 25 | + "-i", |
| 26 | + help="Name of the Elasticsearch index to search documents in", |
| 27 | + ), |
| 28 | + query: str = typer.Option( |
| 29 | + "禅モード", |
| 30 | + "--query", |
| 31 | + "-q", |
| 32 | + help="Query to search in the Elasticsearch index", |
| 33 | + ), |
| 34 | + verbose: bool = typer.Option( |
| 35 | + False, |
| 36 | + "--verbose", |
| 37 | + "-v", |
| 38 | + help="Enable verbose output", |
| 39 | + ), |
| 40 | +): |
| 41 | + # Set up logging |
| 42 | + if verbose: |
| 43 | + logger.setLevel(logging.DEBUG) |
| 44 | + |
| 45 | + es = ElasticsearchClientWrapper() |
| 46 | + |
| 47 | + results = es.search( |
| 48 | + index_name=index_name, |
| 49 | + query=query, |
| 50 | + ) |
| 51 | + logger.info(f"Found {len(results)} results for the question: {query}") |
| 52 | + for i, result in enumerate(results, start=1): |
| 53 | + logger.info(f"Result {i}:") |
| 54 | + logger.info(f"File Name: {result.metadata['source']}") |
| 55 | + logger.info(f"Content: {result.page_content}") |
| 56 | + logger.info("-" * 40) |
| 57 | + |
| 58 | + |
| 59 | +@app.command() |
| 60 | +def add_documents( |
| 61 | + index_name: str = typer.Option( |
| 62 | + "docs_kabuto", |
| 63 | + "--index-name", |
| 64 | + "-i", |
| 65 | + help="Name of the Elasticsearch index to add documents to", |
| 66 | + ), |
| 67 | + verbose: bool = typer.Option( |
| 68 | + False, |
| 69 | + "--verbose", |
| 70 | + "-v", |
| 71 | + help="Enable verbose output", |
| 72 | + ), |
| 73 | +): |
| 74 | + # Set up logging |
| 75 | + if verbose: |
| 76 | + logger.setLevel(logging.DEBUG) |
| 77 | + |
| 78 | + # Create Elasticsearch index |
| 79 | + es = ElasticsearchClientWrapper() |
| 80 | + logger.info(f"Creating Elasticsearch index: {index_name}") |
| 81 | + result = es.create_index( |
| 82 | + index_name=index_name, |
| 83 | + ) |
| 84 | + if result: |
| 85 | + logger.info(f"Created Elasticsearch index: {index_name}") |
| 86 | + else: |
| 87 | + logger.warning(f"Index {index_name} already exists.") |
| 88 | + |
| 89 | + # Load documents from PDF files |
| 90 | + documents = PdfLoaderWrapper().load_pdf_docs() |
| 91 | + logger.info(f"Loaded {len(documents)} documents from PDF.") |
| 92 | + |
| 93 | + # Add documents to Elasticsearch index |
| 94 | + result = es.add_documents( |
| 95 | + index_name=index_name, |
| 96 | + documents=documents, |
| 97 | + ) |
| 98 | + if result: |
| 99 | + logger.info(f"Added {len(documents)} documents to Elasticsearch index: {index_name}") |
| 100 | + else: |
| 101 | + logger.error(f"Failed to add documents to Elasticsearch index: {index_name}") |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + load_dotenv( |
| 106 | + override=True, |
| 107 | + verbose=True, |
| 108 | + ) |
| 109 | + app() |
0 commit comments