|
| 1 | +import logging |
| 2 | + |
| 3 | +import typer |
| 4 | +from dotenv import load_dotenv |
| 5 | +from qdrant_client.models import PointStruct |
| 6 | + |
| 7 | +from template_langgraph.llms.azure_openais import AzureOpenAiWrapper |
| 8 | +from template_langgraph.loggers import get_logger |
| 9 | +from template_langgraph.tools.csv_loaders import CsvLoaderWrapper |
| 10 | +from template_langgraph.tools.qdrants import QdrantClientWrapper |
| 11 | + |
| 12 | +# Initialize the Typer application |
| 13 | +app = typer.Typer( |
| 14 | + add_completion=False, |
| 15 | + help="template-langgraph CLI", |
| 16 | +) |
| 17 | + |
| 18 | +# Set up logging |
| 19 | +logger = get_logger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +@app.command() |
| 23 | +def delete_collection( |
| 24 | + collection_name: str = typer.Option( |
| 25 | + "qa_kabuto", |
| 26 | + "--collection-name", |
| 27 | + "-c", |
| 28 | + help="Name of the Qdrant collection to delete", |
| 29 | + ), |
| 30 | + verbose: bool = typer.Option( |
| 31 | + False, |
| 32 | + "--verbose", |
| 33 | + "-v", |
| 34 | + help="Enable verbose output", |
| 35 | + ), |
| 36 | +): |
| 37 | + # Set up logging |
| 38 | + if verbose: |
| 39 | + logger.setLevel(logging.DEBUG) |
| 40 | + |
| 41 | + logger.info(f"Deleting Qdrant collection: {collection_name}") |
| 42 | + result = QdrantClientWrapper().delete_collection( |
| 43 | + collection_name=collection_name, |
| 44 | + ) |
| 45 | + if result: |
| 46 | + logger.info(f"Successfully deleted Qdrant collection: {collection_name}") |
| 47 | + else: |
| 48 | + logger.warning(f"Qdrant collection {collection_name} does not exist or could not be deleted.") |
| 49 | + logger.info("Deletion task completed.") |
| 50 | + |
| 51 | + |
| 52 | +@app.command() |
| 53 | +def add_documents( |
| 54 | + collection_name: str = typer.Option( |
| 55 | + "qa_kabuto", |
| 56 | + "--collection-name", |
| 57 | + "-c", |
| 58 | + help="Name of the Qdrant collection to add documents to", |
| 59 | + ), |
| 60 | + verbose: bool = typer.Option( |
| 61 | + False, |
| 62 | + "--verbose", |
| 63 | + "-v", |
| 64 | + help="Enable verbose output", |
| 65 | + ), |
| 66 | +): |
| 67 | + # Set up logging |
| 68 | + if verbose: |
| 69 | + logger.setLevel(logging.DEBUG) |
| 70 | + |
| 71 | + # Load documents from CSV files |
| 72 | + documents = CsvLoaderWrapper().load_csv_docs() |
| 73 | + logger.info(f"Loaded {len(documents)} documents from CSV.") |
| 74 | + |
| 75 | + points = [] |
| 76 | + embedding_wrapper = AzureOpenAiWrapper() |
| 77 | + for i, doc in enumerate(documents): |
| 78 | + logger.debug(f"Processing document {i}: {doc.metadata.get('source', 'unknown')}") |
| 79 | + content = doc.page_content.replace("\n", " ") |
| 80 | + logger.debug(f"Creating embedding for document {i} with content: {content[:50]}...") |
| 81 | + vector = embedding_wrapper.create_embedding(content) |
| 82 | + points.append( |
| 83 | + PointStruct( |
| 84 | + id=i, |
| 85 | + vector=vector, |
| 86 | + payload={ |
| 87 | + "file_name": doc.metadata.get("source", f"doc_{i}"), |
| 88 | + "content": content, |
| 89 | + }, |
| 90 | + ) |
| 91 | + ) |
| 92 | + |
| 93 | + # Create Qdrant collection and upsert points |
| 94 | + logger.info(f"Creating Qdrant collection: {collection_name}") |
| 95 | + qdrant_client = QdrantClientWrapper() |
| 96 | + qdrant_client.create_collection( |
| 97 | + collection_name=collection_name, |
| 98 | + vector_size=len(points[0].vector) if points else 1536, # default vector size |
| 99 | + ) |
| 100 | + |
| 101 | + # Upsert points into the Qdrant collection |
| 102 | + logger.info(f"Upserting points into Qdrant collection: {collection_name}") |
| 103 | + operation_info = qdrant_client.upsert_points( |
| 104 | + collection_name=collection_name, |
| 105 | + points=points, |
| 106 | + ) |
| 107 | + logger.info(f"Upserted {len(points)} points into Qdrant collection: {collection_name}") |
| 108 | + logger.info(f"Operation info: {operation_info}") |
| 109 | + |
| 110 | + |
| 111 | +@app.command() |
| 112 | +def search_documents( |
| 113 | + collection_name: str = typer.Option( |
| 114 | + "qa_kabuto", |
| 115 | + "--collection-name", |
| 116 | + "-c", |
| 117 | + help="Name of the Qdrant collection to search documents in", |
| 118 | + ), |
| 119 | + question: str = typer.Option( |
| 120 | + "「鬼灯」を実行すると、KABUTOが急に停止します。原因と対策を教えてください。", |
| 121 | + "--question", |
| 122 | + "-q", |
| 123 | + help="Question to search in the Qdrant collection", |
| 124 | + ), |
| 125 | + verbose: bool = typer.Option( |
| 126 | + False, |
| 127 | + "--verbose", |
| 128 | + "-v", |
| 129 | + help="Enable verbose output", |
| 130 | + ), |
| 131 | +): |
| 132 | + # Set up logging |
| 133 | + if verbose: |
| 134 | + logger.setLevel(logging.DEBUG) |
| 135 | + |
| 136 | + qdrant_client = QdrantClientWrapper() |
| 137 | + |
| 138 | + results = qdrant_client.query_points( |
| 139 | + collection_name=collection_name, |
| 140 | + query=AzureOpenAiWrapper().create_embedding(question), |
| 141 | + ) |
| 142 | + logger.info(f"Found {len(results)} results for the question: {question}") |
| 143 | + for result in results: |
| 144 | + logger.info(f"File Name: {result.payload['file_name']}") |
| 145 | + logger.info(f"Content: {result.payload['content']}") |
| 146 | + logger.info("-" * 40) |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + load_dotenv( |
| 151 | + override=True, |
| 152 | + verbose=True, |
| 153 | + ) |
| 154 | + app() |
0 commit comments