Fix session management serialization and update Ollama URL
Browse files- .env +5 -20
- core/__pycache__/llm.cpython-313.pyc +0 -0
- core/__pycache__/llm_factory.cpython-313.pyc +0 -0
- core/__pycache__/memory.cpython-313.pyc +0 -0
- core/__pycache__/session.cpython-313.pyc +0 -0
- core/providers/__pycache__/base.cpython-313.pyc +0 -0
- core/providers/__pycache__/huggingface.cpython-313.pyc +0 -0
- core/providers/__pycache__/ollama.cpython-313.pyc +0 -0
- core/providers/__pycache__/openai.cpython-313.pyc +0 -0
- core/session.py +24 -11
- quick_system_test.py +88 -0
- test_full_system.py +105 -0
- utils/__pycache__/config.cpython-313.pyc +0 -0
.env
CHANGED
|
@@ -1,21 +1,6 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
-
HF_API_ENDPOINT_URL=https://api-inference.huggingface.co/v1/
|
| 4 |
-
USE_FALLBACK=false
|
| 5 |
-
|
| 6 |
-
# API Keys
|
| 7 |
-
TAVILY_API_KEY=your_tavily_api_key_here
|
| 8 |
-
OPENWEATHER_API_KEY=your_openweather_api_key_here
|
| 9 |
-
NASA_API_KEY=your_nasa_api_key_here
|
| 10 |
-
|
| 11 |
-
# Redis Configuration
|
| 12 |
-
REDIS_HOST=localhost
|
| 13 |
-
REDIS_PORT=6379
|
| 14 |
-
REDIS_USERNAME=
|
| 15 |
-
REDIS_PASSWORD=
|
| 16 |
-
REDIS_RETRIES=3
|
| 17 |
-
REDIS_RETRY_DELAY=1
|
| 18 |
-
|
| 19 |
-
# Model Configuration - Use the exact model name from Ollama
|
| 20 |
LOCAL_MODEL_NAME=mistral:latest
|
| 21 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ollama Configuration
|
| 2 |
+
OLLAMA_HOST=https://7bcc180dffd1.ngrok-free.app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
LOCAL_MODEL_NAME=mistral:latest
|
| 4 |
+
|
| 5 |
+
# Fallback Settings
|
| 6 |
+
USE_FALLBACK=false
|
core/__pycache__/llm.cpython-313.pyc
ADDED
|
Binary file (3.26 kB). View file
|
|
|
core/__pycache__/llm_factory.cpython-313.pyc
ADDED
|
Binary file (6.38 kB). View file
|
|
|
core/__pycache__/memory.cpython-313.pyc
ADDED
|
Binary file (2.11 kB). View file
|
|
|
core/__pycache__/session.cpython-313.pyc
ADDED
|
Binary file (4.8 kB). View file
|
|
|
core/providers/__pycache__/base.cpython-313.pyc
ADDED
|
Binary file (5.49 kB). View file
|
|
|
core/providers/__pycache__/huggingface.cpython-313.pyc
ADDED
|
Binary file (7.8 kB). View file
|
|
|
core/providers/__pycache__/ollama.cpython-313.pyc
ADDED
|
Binary file (6.66 kB). View file
|
|
|
core/providers/__pycache__/openai.cpython-313.pyc
ADDED
|
Binary file (4.96 kB). View file
|
|
|
core/session.py
CHANGED
|
@@ -13,7 +13,6 @@ class SessionManager:
|
|
| 13 |
|
| 14 |
def __init__(self, session_timeout: int = 3600):
|
| 15 |
"""Initialize session manager
|
| 16 |
-
|
| 17 |
Args:
|
| 18 |
session_timeout: Session timeout in seconds (default: 1 hour)
|
| 19 |
"""
|
|
@@ -21,10 +20,8 @@ class SessionManager:
|
|
| 21 |
|
| 22 |
def get_session(self, user_id: str) -> Dict[str, Any]:
|
| 23 |
"""Retrieve user session data
|
| 24 |
-
|
| 25 |
Args:
|
| 26 |
user_id: Unique identifier for the user
|
| 27 |
-
|
| 28 |
Returns:
|
| 29 |
Dictionary containing session data
|
| 30 |
"""
|
|
@@ -33,13 +30,24 @@ class SessionManager:
|
|
| 33 |
if not state:
|
| 34 |
logger.info(f"Creating new session for user {user_id}")
|
| 35 |
return self._create_new_session()
|
| 36 |
-
|
| 37 |
# Check if session has expired
|
| 38 |
last_activity = float(state.get('last_activity', 0))
|
| 39 |
if time.time() - last_activity > self.session_timeout:
|
| 40 |
logger.info(f"Session expired for user {user_id}, creating new session")
|
| 41 |
return self._create_new_session()
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return state
|
| 44 |
except Exception as e:
|
| 45 |
logger.error(f"Error retrieving session for user {user_id}: {e}")
|
|
@@ -47,11 +55,9 @@ class SessionManager:
|
|
| 47 |
|
| 48 |
def update_session(self, user_id: str, data: Dict[str, Any]) -> bool:
|
| 49 |
"""Update user session data
|
| 50 |
-
|
| 51 |
Args:
|
| 52 |
user_id: Unique identifier for the user
|
| 53 |
data: Data to update in the session
|
| 54 |
-
|
| 55 |
Returns:
|
| 56 |
Boolean indicating success
|
| 57 |
"""
|
|
@@ -63,8 +69,18 @@ class SessionManager:
|
|
| 63 |
session.update(data)
|
| 64 |
session['last_activity'] = time.time()
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
# Save updated session
|
| 67 |
-
result = save_user_state(user_id,
|
| 68 |
if result:
|
| 69 |
logger.debug(f"Successfully updated session for user {user_id}")
|
| 70 |
else:
|
|
@@ -77,10 +93,8 @@ class SessionManager:
|
|
| 77 |
|
| 78 |
def clear_session(self, user_id: str) -> bool:
|
| 79 |
"""Clear user session data
|
| 80 |
-
|
| 81 |
Args:
|
| 82 |
user_id: Unique identifier for the user
|
| 83 |
-
|
| 84 |
Returns:
|
| 85 |
Boolean indicating success
|
| 86 |
"""
|
|
@@ -95,7 +109,6 @@ class SessionManager:
|
|
| 95 |
|
| 96 |
def _create_new_session(self) -> Dict[str, Any]:
|
| 97 |
"""Create a new session with default values
|
| 98 |
-
|
| 99 |
Returns:
|
| 100 |
Dictionary containing new session data
|
| 101 |
"""
|
|
|
|
| 13 |
|
| 14 |
def __init__(self, session_timeout: int = 3600):
|
| 15 |
"""Initialize session manager
|
|
|
|
| 16 |
Args:
|
| 17 |
session_timeout: Session timeout in seconds (default: 1 hour)
|
| 18 |
"""
|
|
|
|
| 20 |
|
| 21 |
def get_session(self, user_id: str) -> Dict[str, Any]:
|
| 22 |
"""Retrieve user session data
|
|
|
|
| 23 |
Args:
|
| 24 |
user_id: Unique identifier for the user
|
|
|
|
| 25 |
Returns:
|
| 26 |
Dictionary containing session data
|
| 27 |
"""
|
|
|
|
| 30 |
if not state:
|
| 31 |
logger.info(f"Creating new session for user {user_id}")
|
| 32 |
return self._create_new_session()
|
| 33 |
+
|
| 34 |
# Check if session has expired
|
| 35 |
last_activity = float(state.get('last_activity', 0))
|
| 36 |
if time.time() - last_activity > self.session_timeout:
|
| 37 |
logger.info(f"Session expired for user {user_id}, creating new session")
|
| 38 |
return self._create_new_session()
|
| 39 |
+
|
| 40 |
+
# Deserialize complex data types
|
| 41 |
+
for key, value in state.items():
|
| 42 |
+
if isinstance(value, str):
|
| 43 |
+
try:
|
| 44 |
+
# Try to parse as JSON for lists/dicts
|
| 45 |
+
if value.startswith('[') or value.startswith('{'):
|
| 46 |
+
state[key] = json.loads(value)
|
| 47 |
+
except (json.JSONDecodeError, TypeError):
|
| 48 |
+
# Keep as string if not valid JSON
|
| 49 |
+
pass
|
| 50 |
+
|
| 51 |
return state
|
| 52 |
except Exception as e:
|
| 53 |
logger.error(f"Error retrieving session for user {user_id}: {e}")
|
|
|
|
| 55 |
|
| 56 |
def update_session(self, user_id: str, data: Dict[str, Any]) -> bool:
|
| 57 |
"""Update user session data
|
|
|
|
| 58 |
Args:
|
| 59 |
user_id: Unique identifier for the user
|
| 60 |
data: Data to update in the session
|
|
|
|
| 61 |
Returns:
|
| 62 |
Boolean indicating success
|
| 63 |
"""
|
|
|
|
| 69 |
session.update(data)
|
| 70 |
session['last_activity'] = time.time()
|
| 71 |
|
| 72 |
+
# Convert complex data types to strings for Redis
|
| 73 |
+
redis_data = {}
|
| 74 |
+
for key, value in session.items():
|
| 75 |
+
if isinstance(value, (list, dict)):
|
| 76 |
+
redis_data[key] = json.dumps(value)
|
| 77 |
+
elif isinstance(value, (int, float, str, bool)):
|
| 78 |
+
redis_data[key] = value
|
| 79 |
+
else:
|
| 80 |
+
redis_data[key] = str(value)
|
| 81 |
+
|
| 82 |
# Save updated session
|
| 83 |
+
result = save_user_state(user_id, redis_data)
|
| 84 |
if result:
|
| 85 |
logger.debug(f"Successfully updated session for user {user_id}")
|
| 86 |
else:
|
|
|
|
| 93 |
|
| 94 |
def clear_session(self, user_id: str) -> bool:
|
| 95 |
"""Clear user session data
|
|
|
|
| 96 |
Args:
|
| 97 |
user_id: Unique identifier for the user
|
|
|
|
| 98 |
Returns:
|
| 99 |
Boolean indicating success
|
| 100 |
"""
|
|
|
|
| 109 |
|
| 110 |
def _create_new_session(self) -> Dict[str, Any]:
|
| 111 |
"""Create a new session with default values
|
|
|
|
| 112 |
Returns:
|
| 113 |
Dictionary containing new session data
|
| 114 |
"""
|
quick_system_test.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
import requests
|
| 10 |
+
import json
|
| 11 |
+
|
| 12 |
+
def quick_test():
|
| 13 |
+
"""Quick test of core components"""
|
| 14 |
+
print("=== Quick System Test ===")
|
| 15 |
+
print()
|
| 16 |
+
|
| 17 |
+
# Test 1: Redis
|
| 18 |
+
print("1. Testing Redis...")
|
| 19 |
+
try:
|
| 20 |
+
client = redis_client.get_client()
|
| 21 |
+
if client and client.ping():
|
| 22 |
+
print("β
Redis connection successful")
|
| 23 |
+
|
| 24 |
+
# Test data storage
|
| 25 |
+
test_data = {
|
| 26 |
+
"name": "test_user",
|
| 27 |
+
"messages": json.dumps([]), # Convert list to string
|
| 28 |
+
"created_at": "2025-09-08"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
result = client.hset("test:user:123", mapping=test_data)
|
| 32 |
+
retrieved = client.hgetall("test:user:123")
|
| 33 |
+
client.delete("test:user:123")
|
| 34 |
+
|
| 35 |
+
if retrieved:
|
| 36 |
+
print("β
Redis data storage working")
|
| 37 |
+
else:
|
| 38 |
+
print("β Redis data storage failed")
|
| 39 |
+
else:
|
| 40 |
+
print("β Redis connection failed")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"β Redis test failed: {e}")
|
| 43 |
+
|
| 44 |
+
print()
|
| 45 |
+
|
| 46 |
+
# Test 2: Ollama
|
| 47 |
+
print("2. Testing Ollama...")
|
| 48 |
+
try:
|
| 49 |
+
ollama_url = "https://7bcc180dffd1.ngrok-free.app"
|
| 50 |
+
headers = {
|
| 51 |
+
"ngrok-skip-browser-warning": "true",
|
| 52 |
+
"User-Agent": "AI-Life-Coach-Test"
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
# Test model listing
|
| 56 |
+
response = requests.get(f"{ollama_url}/api/tags", headers=headers, timeout=10)
|
| 57 |
+
if response.status_code == 200:
|
| 58 |
+
print("β
Ollama model listing successful")
|
| 59 |
+
|
| 60 |
+
# Test chat
|
| 61 |
+
payload = {
|
| 62 |
+
"model": "mistral:latest",
|
| 63 |
+
"messages": [{"role": "user", "content": "Hello!"}],
|
| 64 |
+
"stream": False
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
chat_response = requests.post(
|
| 68 |
+
f"{ollama_url}/api/chat",
|
| 69 |
+
headers=headers,
|
| 70 |
+
json=payload,
|
| 71 |
+
timeout=30
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
if chat_response.status_code == 200:
|
| 75 |
+
print("β
Ollama chat successful")
|
| 76 |
+
else:
|
| 77 |
+
print(f"β Ollama chat failed: {chat_response.status_code}")
|
| 78 |
+
else:
|
| 79 |
+
print(f"β Ollama connection failed: {response.status_code}")
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f"β Ollama test failed: {e}")
|
| 83 |
+
|
| 84 |
+
print()
|
| 85 |
+
print("π Quick test completed!")
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
quick_test()
|
test_full_system.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 os
|
| 12 |
+
|
| 13 |
+
def test_full_system():
|
| 14 |
+
"""Test the complete AI Life Coach system"""
|
| 15 |
+
print("=== AI Life Coach Full System Test ===")
|
| 16 |
+
print()
|
| 17 |
+
|
| 18 |
+
# Test 1: Redis Connection
|
| 19 |
+
print("Test 1: Redis Connection")
|
| 20 |
+
try:
|
| 21 |
+
client = redis_client.get_client()
|
| 22 |
+
if client:
|
| 23 |
+
result = client.ping()
|
| 24 |
+
print(f"β
Redis ping successful: {result}")
|
| 25 |
+
|
| 26 |
+
# Test set/get
|
| 27 |
+
client.set('system_test_key', 'system_test_value')
|
| 28 |
+
value = client.get('system_test_key')
|
| 29 |
+
client.delete('system_test_key')
|
| 30 |
+
|
| 31 |
+
if value == 'system_test_value':
|
| 32 |
+
print("β
Redis set/get operations working")
|
| 33 |
+
else:
|
| 34 |
+
print("β Redis set/get operations failed")
|
| 35 |
+
else:
|
| 36 |
+
print("β Redis client is None")
|
| 37 |
+
return False
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"β Redis test failed: {e}")
|
| 40 |
+
return False
|
| 41 |
+
|
| 42 |
+
print()
|
| 43 |
+
|
| 44 |
+
# Test 2: Session Management
|
| 45 |
+
print("Test 2: Session Management")
|
| 46 |
+
try:
|
| 47 |
+
session = session_manager.get_session("test_user")
|
| 48 |
+
print("β
Session creation successful")
|
| 49 |
+
|
| 50 |
+
# Test session update
|
| 51 |
+
result = session_manager.update_session("test_user", {"test": "data"})
|
| 52 |
+
if result:
|
| 53 |
+
print("β
Session update successful")
|
| 54 |
+
else:
|
| 55 |
+
print("β Session update failed")
|
| 56 |
+
|
| 57 |
+
# Clean up
|
| 58 |
+
session_manager.clear_session("test_user")
|
| 59 |
+
print("β
Session cleanup successful")
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"β Session management test failed: {e}")
|
| 63 |
+
return False
|
| 64 |
+
|
| 65 |
+
print()
|
| 66 |
+
|
| 67 |
+
# Test 3: Ollama Integration
|
| 68 |
+
print("Test 3: Ollama Integration")
|
| 69 |
+
try:
|
| 70 |
+
conversation_history = [
|
| 71 |
+
{"role": "user", "content": "Hello! Please introduce yourself briefly."}
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
ollama_url = "https://7bcc180dffd1.ngrok-free.app"
|
| 75 |
+
model_name = "mistral:latest"
|
| 76 |
+
|
| 77 |
+
response = send_to_ollama(
|
| 78 |
+
"Hello! Please introduce yourself briefly.",
|
| 79 |
+
conversation_history,
|
| 80 |
+
ollama_url,
|
| 81 |
+
model_name
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if response:
|
| 85 |
+
print("β
Ollama integration successful")
|
| 86 |
+
print(f"Response: {response[:100]}{'...' if len(response) > 100 else ''}")
|
| 87 |
+
else:
|
| 88 |
+
print("β Ollama integration failed - no response")
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
print(f"β Ollama integration test failed: {e}")
|
| 92 |
+
return False
|
| 93 |
+
|
| 94 |
+
print()
|
| 95 |
+
print("π All system tests passed!")
|
| 96 |
+
print("Your AI Life Coach is ready to use!")
|
| 97 |
+
return True
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
success = test_full_system()
|
| 101 |
+
if success:
|
| 102 |
+
print("\nβ
System is fully operational!")
|
| 103 |
+
else:
|
| 104 |
+
print("\nβ System has issues that need attention!")
|
| 105 |
+
sys.exit(1)
|
utils/__pycache__/config.cpython-313.pyc
CHANGED
|
Binary files a/utils/__pycache__/config.cpython-313.pyc and b/utils/__pycache__/config.cpython-313.pyc differ
|
|
|