Spaces:
Running
Running
""" | |
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() |