Skip to content

Commit 7c0b0af

Browse files
authored
Merge pull request #1050 from AranavMahalpure/main
Update run-tests.py
2 parents f7bcce3 + 02d64a7 commit 7c0b0af

File tree

2 files changed

+134
-98
lines changed

2 files changed

+134
-98
lines changed

tests/run-tests.py

Lines changed: 81 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,102 @@
11
#!/usr/bin/env python
22
import os
33
import pytest
4-
import shutil
4+
import shutil
55
import subprocess
66
import sys
77

88
from utils import Logger
99

10-
class RunTests():
1110

12-
# Grab the folders for the llmware and llmware-packaging repos. They are assumed to live in the same parent folder
11+
class RunTests:
12+
# Initialize and grab the folder paths for the llmware and llmware-packaging repos
1313
def __init__(self):
1414
tests_folder = os.path.dirname(os.path.realpath(__file__))
15-
self.repo_root = os.path.join(tests_folder, "..")
16-
17-
# Ensure the latest/greatest llmware module is installed locally
15+
self.repo_root = os.path.abspath(os.path.join(tests_folder, ".."))
16+
self.logger = Logger()
17+
18+
# Ensure the latest llmware module is installed locally
1819
def update_llmware_install(self):
19-
self.run_command("pip uninstall llmware -y", self.repo_root)
20-
self.run_command("pip install .", self.repo_root)
20+
self.logger.log("Updating llmware installation...")
21+
try:
22+
self.run_command("pip uninstall llmware -y", self.repo_root)
23+
self.run_command("pip install .", self.repo_root)
24+
except Exception as e:
25+
self.logger.log(f"Error updating llmware: {e}", level="error")
26+
sys.exit(1)
2127

22-
# Try to start from as clean an environment as possible
28+
# Clean the environment to start from a fresh state
2329
def clean_the_environment(self):
24-
Logger().log("Cleaning the environment...")
30+
self.logger.log("Cleaning the environment...")
31+
32+
# Remove the default llmware data folders
33+
self.remove_folder(os.path.join(os.environ["HOME"], "llmware_data"))
34+
self.remove_folder(os.path.join(os.environ["HOME"], "llmware_data_custom"))
2535

26-
# Remove the default data folder: $HOME/llmware_data
27-
llmware_data_dir = os.path.join(os.environ["HOME"],"llmware_data")
28-
llmware_data_custom_dir = os.path.join(os.environ["HOME"],"llmware_data_custom")
29-
Logger().log(f"Deleting {llmware_data_dir}")
30-
if os.path.exists(llmware_data_dir):
31-
shutil.rmtree(llmware_data_dir)
36+
# Reset MongoDB
37+
try:
38+
self.logger.log("Resetting MongoDB (dropping the 'llmware' database)...")
39+
from llmware.resources import MongoDBManager
40+
MongoDBManager().client.drop_database("llmware")
41+
except Exception as e:
42+
self.logger.log(f"Error resetting MongoDB: {e}", level="error")
43+
sys.exit(1)
3244

33-
if os.path.exists(llmware_data_custom_dir):
34-
shutil.rmtree(llmware_data_custom_dir)
35-
36-
# Reset Mongo
37-
Logger().log("Resetting Mongo (deleting the llmware db)")
38-
from llmware.resources import MongoDBManager
39-
MongoDBManager().client.drop_database("llmware")
45+
# Reset Milvus collections
46+
try:
47+
self.logger.log("Resetting Milvus (dropping all collections)...")
48+
from llmware.configs import LLMWareConfig
49+
from pymilvus import connections, utility
50+
connections.connect("default", host=os.environ.get('MILVUS_HOST', 'localhost'), port=19530)
51+
for collection in utility.list_collections():
52+
utility.drop_collection(collection)
53+
self.logger.log("All Milvus collections dropped successfully.")
54+
except Exception as e:
55+
self.logger.log(f"Error resetting Milvus: {e}", level="error")
56+
sys.exit(1)
4057

41-
# Reset Milvus
42-
Logger().log("Resetting Milvus (deleting all collections)")
43-
from llmware.configs import LLMWareConfig
44-
from pymilvus import connections, utility, FieldSchema, CollectionSchema, DataType, Collection
45-
connections.connect("default", host=os.environ.get('MILVUS_HOST','localhost') , port=19530)
46-
for collection in utility.list_collections():
47-
utility.drop_collection(collection)
58+
# Helper function to remove a folder if it exists
59+
def remove_folder(self, folder_path):
60+
if os.path.exists(folder_path):
61+
try:
62+
self.logger.log(f"Removing folder: {folder_path}")
63+
shutil.rmtree(folder_path)
64+
except Exception as e:
65+
self.logger.log(f"Error removing folder {folder_path}: {e}", level="error")
66+
else:
67+
self.logger.log(f"Folder not found: {folder_path}")
4868

49-
# Run a system command in the given dir, wait for it to complete and print the output
69+
# Run a system command in the given directory, wait for it to complete, and log the output
5070
def run_command(self, command, working_dir):
51-
Logger().log(f"Running command '{command}' in '{working_dir}'...")
71+
self.logger.log(f"Running command '{command}' in '{working_dir}'...")
5272
command_array = command.split(" ")
53-
command_output = ""
54-
p = subprocess.Popen(command_array, cwd=working_dir, stdout=subprocess.PIPE)
55-
for line in p.stdout:
56-
output_line = line.decode('utf8')
57-
command_output += output_line
58-
Logger().log(output_line)
59-
p.wait()
60-
Logger().log("")
61-
return command_output
73+
try:
74+
process = subprocess.Popen(command_array, cwd=working_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
75+
stdout, stderr = process.communicate()
76+
if stdout:
77+
self.logger.log(stdout.decode('utf-8'))
78+
if stderr:
79+
self.logger.log(stderr.decode('utf-8'), level="error")
80+
if process.returncode != 0:
81+
raise subprocess.CalledProcessError(process.returncode, command)
82+
except subprocess.CalledProcessError as e:
83+
self.logger.log(f"Command '{command}' failed with error: {e}", level="error")
84+
sys.exit(1)
85+
except Exception as e:
86+
self.logger.log(f"An unexpected error occurred while running '{command}': {e}", level="error")
87+
sys.exit(1)
88+
89+
90+
if __name__ == "__main__":
91+
test_runner = RunTests()
92+
93+
# Update and clean environment before running tests
94+
test_runner.update_llmware_install()
95+
test_runner.clean_the_environment()
6296

63-
test_runner = RunTests()
64-
test_runner.update_llmware_install()
65-
test_runner.clean_the_environment()
66-
pytest.main(sys.argv[1:])
97+
# Run the tests with pytest
98+
try:
99+
pytest.main(sys.argv[1:])
100+
except Exception as e:
101+
test_runner.logger.log(f"Error running tests: {e}", level="error")
102+
sys.exit(1)

welcome_to_llmware_windows.sh

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
1-
#! /bin/bash
1+
#!/bin/bash
22

3-
# Welcome to LLMWare script - handles some basic setup for first-time cloning of the repo
3+
# Welcome to the LLMWare script - handles basic setup for first-time cloning of the repo
44
# Windows version
55

6+
# Function to handle errors
7+
function error_exit {
8+
echo "Error: $1"
9+
exit 1
10+
}
11+
612
# Install core dependencies
7-
pip3 install -r ./llmware/requirements.txt
13+
echo "Installing core dependencies..."
14+
pip3 install -r ./llmware/requirements.txt || error_exit "Failed to install core dependencies."
15+
16+
# Install optional dependencies (optional step)
17+
echo "Installing optional dependencies..."
18+
pip3 install -r ./llmware/requirements_extras.txt || echo "Optional dependencies installation skipped or failed."
819

9-
# Note: this step is optional but adds many commonly-used optional dependencies (including in several examples)
10-
pip3 install -r ./llmware/requirements_extras.txt
20+
# Move selected examples into root path for easy execution from the command line
21+
echo "Moving selected examples to the root path..."
22+
declare -a examples=(
23+
"examples/Getting_Started/welcome_example.py"
24+
"fast_start/rag/*.py"
25+
"examples/UI/gguf_streaming_chatbot.py"
26+
"examples/SLIM-Agents/agent-llmfx-getting-started.py"
27+
"examples/SLIM-Agents/using_slim_extract_model.py"
28+
"examples/SLIM-Agents/using_slim_summary.py"
29+
"examples/Models/bling_fast_start.py"
30+
"examples/Models/dragon_gguf_fast_start.py"
31+
"examples/Prompts/document_summarizer.py"
32+
"examples/Use_Cases/web_services_slim_fx.py"
33+
"examples/Use_Cases/invoice_processing.py"
34+
"examples/Models/using-whisper-cpp-sample-files.py"
35+
"examples/Parsing/parsing_microsoft_ir_docs.py"
36+
"examples/Models/chat_models_gguf_fast_start.py"
37+
"examples/Models/gguf_streaming.py"
38+
)
1139

12-
# Move selected examples into root path for easy execution from command line
13-
scp ./examples/Getting_Started/welcome_example.py .
14-
scp ./fast_start/rag/*.py .
15-
scp ./examples/UI/gguf_streaming_chatbot.py .
16-
scp ./examples/SLIM-Agents/agent-llmfx-getting-started.py .
17-
scp ./examples/SLIM-Agents/using_slim_extract_model.py .
18-
scp ./examples/SLIM-Agents/using_slim_summary.py .
19-
scp ./examples/Models/bling_fast_start.py .
20-
scp ./examples/Models/dragon_gguf_fast_start.py .
21-
scp ./examples/Prompts/document_summarizer.py .
22-
scp ./examples/Use_Cases/web_services_slim_fx.py .
23-
scp ./examples/Use_Cases/invoice_processing.py .
24-
scp ./examples/Models/using-whisper-cpp-sample-files.py .
25-
scp ./examples/Parsing/parsing_microsoft_ir_docs.py .
26-
scp ./examples/Models/chat_models_gguf_fast_start.py .
27-
scp ./examples/Models/gguf_streaming.py .
40+
for example in "${examples[@]}"; do
41+
cp "$example" . || error_exit "Failed to copy $example."
42+
done
2843

2944
echo "Welcome Steps Completed"
30-
echo "1. Installed Core Dependencies"
31-
echo "2. Installed Several Optional Dependencies Useful for Running Examples"
32-
echo "3. Moved selected Getting Started examples into /root path"
33-
echo " -- welcome_example.py"
34-
echo " -- example-1-create_first_library.py"
35-
echo " -- example-2-build_embeddings.py"
36-
echo " -- example-3-prompts_and_models.py"
37-
echo " -- example-4-rag-text-query.py"
38-
echo " -- example-5-rag-semantic-query.py"
39-
echo " -- example-6-rag-multi-step-query.py"
40-
echo " -- gguf_streaming.py"
41-
echo " -- bling_fast_start.py"
42-
echo " -- using_slim_extract_model.py"
43-
echo " -- using_slim_summary.py"
44-
echo " -- dragon_gguf_fast_start.py"
45-
echo " -- invoice_processing.py"
46-
echo " -- gguf_streaming_chatbot.py"
47-
echo " -- agent-llmfx-getting-started.py"
48-
echo " -- whisper-cpp-sample-files.py"
49-
echo " -- web_services_slim_fx.py"
50-
echo " -- parsing_microsoft_ir_docs.py"
51-
echo " -- document_summarizer.py"
52-
echo " -- chat_models_gguf_fast_start.py ."
45+
echo "1. Installed Core Dependencies"
46+
echo "2. Installed Several Optional Dependencies Useful for Running Examples"
47+
echo "3. Moved selected Getting Started examples into /root path"
48+
49+
echo "Selected examples:"
50+
for example in "${examples[@]}"; do
51+
echo " -- $(basename "$example")"
52+
done
5353

5454
echo ""
55-
echo "To run an example from command-line: py welcome_example.py"
56-
echo "To run gguf_streaming_chatbot.py: streamlit run gguf_streaming_chatbot.py"
57-
echo "Note: check the /examples folder for 100+ additional examples"
58-
echo "Note: on first time use, models will be downloaded and cached locally"
59-
echo "Note: open up the examples and edit for more configuration options"
55+
echo "To run an example from the command line: py welcome_example.py"
56+
echo "To run gguf_streaming_chatbot.py: streamlit run gguf_streaming_chatbot.py"
57+
echo "Note: Check the /examples folder for 100+ additional examples."
58+
echo "Note: On first-time use, models will be downloaded and cached locally."
59+
echo "Note: Open up the examples and edit for more configuration options."
6060
echo ""
6161

62-
echo "Running welcome_example.py"
63-
py welcome_example.py
62+
echo "Running welcome_example.py..."
63+
py welcome_example.py || error_exit "Failed to run welcome_example.py."
6464

65-
# keeps bash console open (may be in separate window)
66-
bash
65+
# Keeps the bash console open (may be in a separate window)
66+
exec bash

0 commit comments

Comments
 (0)