-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_all.py
162 lines (139 loc) · 5.13 KB
/
test_all.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
"""
Comprehensive test script for the LLM integration.
Tests all the key functions to ensure they work correctly.
"""
import os
import sys
import logging
from dotenv import load_dotenv
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Add the project root to the Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# Import Django setup to initialize Django environment
from tagwiseapp.reader.django_setup import setup_django
setup_django()
# Import the modules to test
from tagwiseapp.reader.html_utils import clean_html_content
from tagwiseapp.reader.category_matcher import get_existing_categories, get_existing_tags
from tagwiseapp.reader.settings import get_model_config
from tagwiseapp.reader.llm_factory import LLMFactory, LLMChain
from tagwiseapp.reader.content_analyzer import (
categorize_content,
analyze_screenshot,
generate_summary_from_content
)
# Test HTML content
TEST_CONTENT = """
<html>
<head><title>Test Web Page</title></head>
<body>
<h1>Artificial Intelligence in Healthcare</h1>
<p>AI is increasingly being used in healthcare for diagnosis, treatment planning, and patient monitoring.</p>
<p>Machine learning algorithms can analyze medical images, predict disease outcomes, and optimize healthcare operations.</p>
</body>
</html>
"""
def test_settings_and_llm_factory():
"""Test settings and LLM factory"""
logger.info("Testing settings and LLM factory...")
# Test get_model_config
config = get_model_config()
logger.info(f"Default model config: {config}")
# Test with specific provider and model type
vision_config = get_model_config(provider="openai", model_type="vision")
logger.info(f"Vision model config for OpenAI: {vision_config}")
# Test LLMFactory.create_llm
try:
llm = LLMFactory.create_llm(
provider=config.get('provider'),
model_name=config.get('model_name'),
temperature=0.2
)
logger.info(f"Successfully created LLM instance: {llm}")
logger.info("LLM factory test passed")
return True
except Exception as e:
logger.error(f"Error creating LLM instance: {e}")
logger.info("LLM factory test failed")
return False
def test_llm_chain():
"""Test LLM chain"""
logger.info("Testing LLM chain...")
# Create a simple LLM chain
try:
chain = LLMChain(
system_prompt="You are a helpful assistant.",
model_type="text"
)
logger.info(f"Successfully created LLM chain: {chain}")
logger.info("LLM chain test passed")
return True
except Exception as e:
logger.error(f"Error creating LLM chain: {e}")
logger.info("LLM chain test failed")
return False
def test_content_analyzer_basics():
"""Test basic functions in content analyzer"""
logger.info("Testing content analyzer basics...")
# Clean the test content
clean_text = clean_html_content(TEST_CONTENT)
logger.info(f"Cleaned HTML content: {clean_text[:100]}...")
# Get categories and tags
categories = get_existing_categories()
tags = get_existing_tags()
logger.info(f"Found {len(categories)} categories and {len(tags)} tags")
logger.info("Content analyzer basics test passed")
return True
def test_categorize_content():
"""Test categorize_content function"""
logger.info("Testing categorize_content function...")
try:
# This may take some time and requires API keys
logger.info("This test will be skipped in automated testing as it requires API keys")
logger.info("Categorize content test skipped")
return True
# Uncomment to actually run the test
"""
result = categorize_content(
content=TEST_CONTENT,
url="https://example.com/ai-healthcare",
existing_title="AI in Healthcare",
existing_description="Overview of AI applications in healthcare"
)
logger.info(f"Categorization result: {result}")
logger.info("Categorize content test passed")
"""
except Exception as e:
logger.error(f"Error in categorize_content: {e}")
logger.info("Categorize content test failed")
return False
if __name__ == "__main__":
# Load environment variables
load_dotenv()
# Run tests
results = []
results.append(test_settings_and_llm_factory())
results.append(test_llm_chain())
results.append(test_content_analyzer_basics())
results.append(test_categorize_content())
# Print results
logger.info("Test results:")
for i, result in enumerate(results):
test_name = [
"Settings and LLM factory",
"LLM chain",
"Content analyzer basics",
"Categorize content"
][i]
status = "PASSED" if result else "FAILED"
logger.info(f"- {test_name}: {status}")
# Check if all tests passed
if all(results):
logger.info("All tests passed!")
sys.exit(0)
else:
logger.error("Some tests failed!")
sys.exit(1)