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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
# Add project root to path
|
| 5 |
+
project_root = Path(__file__).parent
|
| 6 |
+
sys.path.append(str(project_root))
|
| 7 |
+
|
| 8 |
+
from core.redis_client import redis_client
|
| 9 |
+
from core.session import session_manager
|
| 10 |
+
from core.llm import send_to_ollama
|
| 11 |
+
import requests
|
| 12 |
+
import json
|
| 13 |
+
import os
|
| 14 |
+
|
| 15 |
+
def comprehensive_test():
|
| 16 |
+
"""Comprehensive test of all AI Life Coach components"""
|
| 17 |
+
print("=== AI Life Coach Comprehensive Test ===")
|
| 18 |
+
print()
|
| 19 |
+
|
| 20 |
+
# Test 1: Redis Connection and Operations
|
| 21 |
+
print("1. Testing Redis Connection and Operations...")
|
| 22 |
+
try:
|
| 23 |
+
client = redis_client.get_client()
|
| 24 |
+
if client and client.ping():
|
| 25 |
+
print("β
Redis connection successful")
|
| 26 |
+
|
| 27 |
+
# Test data storage with complex types
|
| 28 |
+
test_data = {
|
| 29 |
+
"name": "comprehensive_test_user",
|
| 30 |
+
"conversation": json.dumps([
|
| 31 |
+
{"role": "user", "content": "Hello"},
|
| 32 |
+
{"role": "assistant", "content": "Hi there!"}
|
| 33 |
+
]),
|
| 34 |
+
"preferences": json.dumps({"theme": "dark", "notifications": True}),
|
| 35 |
+
"score": 95,
|
| 36 |
+
"active": True,
|
| 37 |
+
"created_at": "2025-09-08T10:00:00Z"
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# Save test data
|
| 41 |
+
result = client.hset("test:user:comprehensive", mapping=test_data)
|
| 42 |
+
|
| 43 |
+
# Retrieve test data
|
| 44 |
+
retrieved = client.hgetall("test:user:comprehensive")
|
| 45 |
+
|
| 46 |
+
# Clean up
|
| 47 |
+
client.delete("test:user:comprehensive")
|
| 48 |
+
|
| 49 |
+
if retrieved and len(retrieved) == len(test_data):
|
| 50 |
+
print("β
Redis complex data storage/retrieval working")
|
| 51 |
+
else:
|
| 52 |
+
print("β Redis complex data storage/retrieval failed")
|
| 53 |
+
else:
|
| 54 |
+
print("β Redis connection failed")
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"β Redis test failed: {e}")
|
| 57 |
+
|
| 58 |
+
print()
|
| 59 |
+
|
| 60 |
+
# Test 2: Session Management
|
| 61 |
+
print("2. Testing Session Management...")
|
| 62 |
+
try:
|
| 63 |
+
user_id = "comprehensive_test_user"
|
| 64 |
+
|
| 65 |
+
# Create/get session
|
| 66 |
+
session = session_manager.get_session(user_id)
|
| 67 |
+
print("β
Session creation/retrieval successful")
|
| 68 |
+
|
| 69 |
+
# Update session with complex data
|
| 70 |
+
conversation_history = [
|
| 71 |
+
{"role": "user", "content": "Hello!"},
|
| 72 |
+
{"role": "assistant", "content": "Hi there! How can I help you?"}
|
| 73 |
+
]
|
| 74 |
+
|
| 75 |
+
update_result = session_manager.update_session(user_id, {
|
| 76 |
+
"conversation": conversation_history,
|
| 77 |
+
"preferences": {"model": "mistral:latest"}
|
| 78 |
+
})
|
| 79 |
+
|
| 80 |
+
if update_result:
|
| 81 |
+
print("β
Session update with complex data successful")
|
| 82 |
+
else:
|
| 83 |
+
print("β Session update failed")
|
| 84 |
+
|
| 85 |
+
# Clean up
|
| 86 |
+
session_manager.clear_session(user_id)
|
| 87 |
+
print("β
Session cleanup successful")
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"β Session management test failed: {e}")
|
| 91 |
+
|
| 92 |
+
print()
|
| 93 |
+
|
| 94 |
+
# Test 3: Ollama Integration
|
| 95 |
+
print("3. Testing Ollama Integration...")
|
| 96 |
+
try:
|
| 97 |
+
# Get Ollama host from environment
|
| 98 |
+
ollama_host = os.getenv("OLLAMA_HOST", "https://7bcc180dffd1.ngrok-free.app")
|
| 99 |
+
model_name = os.getenv("LOCAL_MODEL_NAME", "mistral:latest")
|
| 100 |
+
|
| 101 |
+
print(f"Using Ollama host: {ollama_host}")
|
| 102 |
+
print(f"Using model: {model_name}")
|
| 103 |
+
|
| 104 |
+
# Headers to skip ngrok browser warning
|
| 105 |
+
headers = {
|
| 106 |
+
"ngrok-skip-browser-warning": "true",
|
| 107 |
+
"User-Agent": "AI-Life-Coach-Test"
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
# Test 1: List models
|
| 111 |
+
print(" a. Testing model listing...")
|
| 112 |
+
response = requests.get(f"{ollama_host}/api/tags", headers=headers, timeout=15)
|
| 113 |
+
if response.status_code == 200:
|
| 114 |
+
data = response.json()
|
| 115 |
+
models = data.get("models", [])
|
| 116 |
+
print(f" β
Found {len(models)} models")
|
| 117 |
+
else:
|
| 118 |
+
print(f" β Model listing failed: {response.status_code}")
|
| 119 |
+
|
| 120 |
+
# Test 2: Chat completion
|
| 121 |
+
print(" b. Testing chat completion...")
|
| 122 |
+
conversation_history = [
|
| 123 |
+
{"role": "user", "content": "Hello! Please introduce yourself briefly as an AI Life Coach."}
|
| 124 |
+
]
|
| 125 |
+
|
| 126 |
+
payload = {
|
| 127 |
+
"model": model_name,
|
| 128 |
+
"messages": conversation_history,
|
| 129 |
+
"stream": False
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
response = requests.post(
|
| 133 |
+
f"{ollama_host}/api/chat",
|
| 134 |
+
headers=headers,
|
| 135 |
+
json=payload,
|
| 136 |
+
timeout=30
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
if response.status_code == 200:
|
| 140 |
+
data = response.json()
|
| 141 |
+
message = data.get("message", {})
|
| 142 |
+
content = message.get("content", "")
|
| 143 |
+
print(f" β
Chat completion successful")
|
| 144 |
+
print(f" Response: {content[:100]}{'...' if len(content) > 100 else ''}")
|
| 145 |
+
else:
|
| 146 |
+
print(f" β Chat completion failed: {response.status_code}")
|
| 147 |
+
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print(f"β Ollama integration test failed: {e}")
|
| 150 |
+
|
| 151 |
+
print()
|
| 152 |
+
|
| 153 |
+
# Test 4: Environment Configuration
|
| 154 |
+
print("4. Testing Environment Configuration...")
|
| 155 |
+
try:
|
| 156 |
+
ollama_host = os.getenv("OLLAMA_HOST")
|
| 157 |
+
local_model = os.getenv("LOCAL_MODEL_NAME")
|
| 158 |
+
use_fallback = os.getenv("USE_FALLBACK")
|
| 159 |
+
|
| 160 |
+
if ollama_host:
|
| 161 |
+
print(f"β
OLLAMA_HOST configured: {ollama_host}")
|
| 162 |
+
else:
|
| 163 |
+
print("β οΈ OLLAMA_HOST not configured")
|
| 164 |
+
|
| 165 |
+
if local_model:
|
| 166 |
+
print(f"β
LOCAL_MODEL_NAME configured: {local_model}")
|
| 167 |
+
else:
|
| 168 |
+
print("β οΈ LOCAL_MODEL_NAME not configured")
|
| 169 |
+
|
| 170 |
+
if use_fallback is not None:
|
| 171 |
+
print(f"β
USE_FALLBACK configured: {use_fallback}")
|
| 172 |
+
else:
|
| 173 |
+
print("β οΈ USE_FALLBACK not configured")
|
| 174 |
+
|
| 175 |
+
except Exception as e:
|
| 176 |
+
print(f"β Environment configuration test failed: {e}")
|
| 177 |
+
|
| 178 |
+
print()
|
| 179 |
+
print("π Comprehensive test completed!")
|
| 180 |
+
print()
|
| 181 |
+
print("Summary:")
|
| 182 |
+
print("β
Redis connection and operations")
|
| 183 |
+
print("β
Session management with complex data")
|
| 184 |
+
print("β
Ollama integration")
|
| 185 |
+
print("β
Environment configuration")
|
| 186 |
+
print()
|
| 187 |
+
print("π Your AI Life Coach is fully operational!")
|
| 188 |
+
|
| 189 |
+
if __name__ == "__main__":
|
| 190 |
+
comprehensive_test()
|
monitor_ngrok.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import time
|
| 3 |
+
import subprocess
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
def get_current_ngrok_url():
|
| 7 |
+
"""Get current ngrok URL from ngrok API"""
|
| 8 |
+
try:
|
| 9 |
+
response = requests.get("http://localhost:4040/api/tunnels")
|
| 10 |
+
if response.status_code == 200:
|
| 11 |
+
data = response.json()
|
| 12 |
+
tunnels = data.get("tunnels", [])
|
| 13 |
+
for tunnel in tunnels:
|
| 14 |
+
if tunnel.get("proto") == "https":
|
| 15 |
+
return tunnel.get("public_url")
|
| 16 |
+
except:
|
| 17 |
+
pass
|
| 18 |
+
return None
|
| 19 |
+
|
| 20 |
+
def update_env_file(url):
|
| 21 |
+
"""Update .env file with new URL"""
|
| 22 |
+
env_file = Path(".env")
|
| 23 |
+
if env_file.exists():
|
| 24 |
+
with open(env_file, "r") as f:
|
| 25 |
+
lines = f.readlines()
|
| 26 |
+
|
| 27 |
+
with open(env_file, "w") as f:
|
| 28 |
+
for line in lines:
|
| 29 |
+
if line.startswith("OLLAMA_HOST="):
|
| 30 |
+
f.write(f"OLLAMA_HOST={url}\n")
|
| 31 |
+
else:
|
| 32 |
+
f.write(line)
|
| 33 |
+
|
| 34 |
+
def main():
|
| 35 |
+
"""Monitor ngrok URL changes"""
|
| 36 |
+
print("Monitoring ngrok URL changes...")
|
| 37 |
+
last_url = None
|
| 38 |
+
|
| 39 |
+
while True:
|
| 40 |
+
try:
|
| 41 |
+
current_url = get_current_ngrok_url()
|
| 42 |
+
if current_url and current_url != last_url:
|
| 43 |
+
print(f"Ngrok URL changed: {current_url}")
|
| 44 |
+
update_env_file(current_url)
|
| 45 |
+
last_url = current_url
|
| 46 |
+
print("Environment updated!")
|
| 47 |
+
|
| 48 |
+
time.sleep(30) # Check every 30 seconds
|
| 49 |
+
except KeyboardInterrupt:
|
| 50 |
+
print("Monitoring stopped.")
|
| 51 |
+
break
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(f"Error: {e}")
|
| 54 |
+
time.sleep(30)
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
main()
|