Spaces:
Running
Running
File size: 3,034 Bytes
c05de71 |
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 |
"""
Test script to demonstrate the reindexing functionality.
"""
import os
import shutil
import logging
from backend import Chatbot
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Constants
DATA_DIR = "data"
TEST_DATA_DIR = "test_data"
TEST_DOC_NAME = "test_document.txt"
def setup_test_data():
"""Create a test document that will be added after initial indexing."""
# Create test data directory if it doesn't exist
if not os.path.exists(TEST_DATA_DIR):
os.makedirs(TEST_DATA_DIR)
# Create a test document
test_doc_path = os.path.join(TEST_DATA_DIR, TEST_DOC_NAME)
with open(test_doc_path, "w") as f:
f.write("""
# Test Document for Reindexing
This is a test document created to demonstrate the dynamic reindexing
functionality of the Document Chatbot.
It contains some unique information that can be used to verify that
the reindexing worked correctly.
## Key Facts
- The capital of France is Paris
- The speed of light is approximately 299,792,458 meters per second
- Water freezes at 0 degrees Celsius
## Query Test
If you ask the chatbot "What is the speed of light?" after reindexing,
it should be able to provide the answer from this document.
""")
logger.info(f"Created test document at {test_doc_path}")
return test_doc_path
def main():
"""Main test function."""
logger.info("Starting reindexing test")
# Create test document
test_doc_path = setup_test_data()
# Initialize chatbot with original data
logger.info("Initializing chatbot with original data")
chatbot = Chatbot()
documents = chatbot.load_documents()
chatbot.create_index(documents)
chatbot.initialize_query_engine()
# Test query before adding new document
logger.info("Testing query before adding new document")
query = "What is the speed of light?"
response = chatbot.query(query)
logger.info(f"Response before adding new document: {response}")
# Move test document to data directory
logger.info("Adding new document to data directory")
shutil.copy(test_doc_path, os.path.join(DATA_DIR, TEST_DOC_NAME))
# Update index with new document
logger.info("Updating index with new document")
new_documents = chatbot.load_documents()
chatbot.update_index(new_documents)
# Test query after adding new document
logger.info("Testing query after adding new document")
response = chatbot.query(query)
logger.info(f"Response after adding new document: {response}")
# Clean up - remove test document from data directory
logger.info("Cleaning up test data")
os.remove(os.path.join(DATA_DIR, TEST_DOC_NAME))
logger.info("Reindexing test completed")
if __name__ == "__main__":
main() |