Fix model name mismatch and update Ollama connection logic
Browse files- .env +5 -2
- app.py +38 -42
- test_ollama_connection.py +66 -0
.env
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# Hugging Face Settings
|
| 2 |
HF_TOKEN=your_huggingface_token_here
|
| 3 |
HF_API_ENDPOINT_URL=https://api-inference.huggingface.co/v1/
|
|
|
|
| 4 |
|
| 5 |
# API Keys
|
| 6 |
TAVILY_API_KEY=your_tavily_api_key_here
|
|
@@ -12,7 +13,9 @@ REDIS_HOST=localhost
|
|
| 12 |
REDIS_PORT=6379
|
| 13 |
REDIS_USERNAME=
|
| 14 |
REDIS_PASSWORD=
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# Model Configuration
|
| 17 |
-
LOCAL_MODEL_NAME=mistral
|
| 18 |
OLLAMA_HOST=https://ace32bd59aef.ngrok-free.app
|
|
|
|
| 1 |
# Hugging Face Settings
|
| 2 |
HF_TOKEN=your_huggingface_token_here
|
| 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
|
|
|
|
| 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 |
OLLAMA_HOST=https://ace32bd59aef.ngrok-free.app
|
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# Force redeploy trigger - version 1.
|
| 2 |
import streamlit as st
|
| 3 |
from utils.config import config
|
| 4 |
import requests
|
|
@@ -9,7 +9,7 @@ from core.memory import load_user_state, check_redis_health
|
|
| 9 |
# Set page config
|
| 10 |
st.set_page_config(page_title="AI Life Coach", page_icon="🧘", layout="centered")
|
| 11 |
|
| 12 |
-
# Initialize session state
|
| 13 |
if 'ngrok_url' not in st.session_state:
|
| 14 |
st.session_state.ngrok_url = config.ollama_host
|
| 15 |
|
|
@@ -37,36 +37,39 @@ if st.sidebar.button("Update Ngrok URL"):
|
|
| 37 |
st.sidebar.success("Ngrok URL updated!")
|
| 38 |
st.experimental_rerun()
|
| 39 |
|
| 40 |
-
# Model selection
|
| 41 |
-
st.sidebar.markdown("---")
|
| 42 |
-
st.sidebar.subheader("Model Selection")
|
| 43 |
-
|
| 44 |
# Headers to skip ngrok browser warning
|
| 45 |
NGROK_HEADERS = {
|
| 46 |
"ngrok-skip-browser-warning": "true",
|
| 47 |
"User-Agent": "AI-Life-Coach-App"
|
| 48 |
}
|
| 49 |
|
| 50 |
-
# Fetch available models
|
| 51 |
-
|
| 52 |
try:
|
| 53 |
response = requests.get(
|
| 54 |
-
f"{
|
| 55 |
headers=NGROK_HEADERS,
|
| 56 |
timeout=5
|
| 57 |
)
|
| 58 |
if response.status_code == 200:
|
| 59 |
models_data = response.json().get("models", [])
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Model selector dropdown
|
|
|
|
|
|
|
| 70 |
if st.session_state.available_models:
|
| 71 |
selected_model = st.sidebar.selectbox(
|
| 72 |
"Select Model",
|
|
@@ -84,13 +87,12 @@ else:
|
|
| 84 |
st.sidebar.markdown("---")
|
| 85 |
|
| 86 |
# Get environment info
|
| 87 |
-
BASE_URL = os.environ.get("SPACE_ID", "")
|
| 88 |
IS_HF_SPACE = bool(BASE_URL)
|
| 89 |
|
| 90 |
# Fetch Ollama status
|
| 91 |
def get_ollama_status(ngrok_url):
|
| 92 |
try:
|
| 93 |
-
# Try to connect to the remote Ollama service directly
|
| 94 |
response = requests.get(
|
| 95 |
f"{ngrok_url}/api/tags",
|
| 96 |
headers=NGROK_HEADERS,
|
|
@@ -102,7 +104,6 @@ def get_ollama_status(ngrok_url):
|
|
| 102 |
st.session_state.available_models = model_names
|
| 103 |
|
| 104 |
if models:
|
| 105 |
-
# Check if our selected model is available
|
| 106 |
selected_model_available = st.session_state.selected_model in model_names
|
| 107 |
return {
|
| 108 |
"running": True,
|
|
@@ -121,7 +122,6 @@ def get_ollama_status(ngrok_url):
|
|
| 121 |
}
|
| 122 |
except Exception as e:
|
| 123 |
st.session_state.model_status = "unreachable"
|
| 124 |
-
# If direct connection fails, return error info
|
| 125 |
return {
|
| 126 |
"running": False,
|
| 127 |
"model_loaded": None,
|
|
@@ -129,7 +129,7 @@ def get_ollama_status(ngrok_url):
|
|
| 129 |
"remote_host": ngrok_url
|
| 130 |
}
|
| 131 |
|
| 132 |
-
#
|
| 133 |
def get_conversation_history(user_id):
|
| 134 |
try:
|
| 135 |
user_state = load_user_state(user_id)
|
|
@@ -139,7 +139,7 @@ def get_conversation_history(user_id):
|
|
| 139 |
st.warning(f"Could not load conversation history: {e}")
|
| 140 |
return []
|
| 141 |
|
| 142 |
-
# Check Ollama status
|
| 143 |
ollama_status = get_ollama_status(st.session_state.ngrok_url)
|
| 144 |
|
| 145 |
# Update model status
|
|
@@ -166,7 +166,7 @@ else:
|
|
| 166 |
st.sidebar.warning(f"🧠 Ollama Model: {model_status_msg} (selected model not available)")
|
| 167 |
st.sidebar.info(f"Connected to: {ollama_status['remote_host']}")
|
| 168 |
|
| 169 |
-
#
|
| 170 |
model_status_container = st.sidebar.empty()
|
| 171 |
if st.session_state.model_status == "ready":
|
| 172 |
model_status_container.success("✅ Model Ready")
|
|
@@ -174,10 +174,9 @@ elif st.session_state.model_status == "checking":
|
|
| 174 |
model_status_container.info("🔍 Checking model...")
|
| 175 |
elif st.session_state.model_status == "no_models":
|
| 176 |
model_status_container.warning("⚠️ No models found")
|
| 177 |
-
else:
|
| 178 |
model_status_container.error("❌ Ollama unreachable")
|
| 179 |
|
| 180 |
-
# Redis status indicator
|
| 181 |
redis_status_container = st.sidebar.empty()
|
| 182 |
if check_redis_health():
|
| 183 |
redis_status_container.success("✅ Redis Connected")
|
|
@@ -204,10 +203,15 @@ with st.expander("🔍 Connection Status"):
|
|
| 204 |
# Function to send message to Ollama
|
| 205 |
def send_to_ollama(user_input, conversation_history, ngrok_url, model_name):
|
| 206 |
try:
|
|
|
|
| 207 |
payload = {
|
| 208 |
"model": model_name,
|
| 209 |
"messages": conversation_history,
|
| 210 |
-
"stream": False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
}
|
| 212 |
|
| 213 |
response = requests.post(
|
|
@@ -231,23 +235,16 @@ def send_to_ollama(user_input, conversation_history, ngrok_url, model_name):
|
|
| 231 |
# Function to send message to Hugging Face (fallback)
|
| 232 |
def send_to_hf(user_input, conversation_history):
|
| 233 |
try:
|
| 234 |
-
# Import here to avoid issues if not needed
|
| 235 |
from core.llm import LLMClient
|
| 236 |
-
|
| 237 |
-
# Initialize LLM client for Hugging Face
|
| 238 |
llm_client = LLMClient(provider="huggingface")
|
| 239 |
|
| 240 |
-
# Format
|
| 241 |
-
prompt = ""
|
| 242 |
for msg in conversation_history:
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
prompt += f"
|
| 247 |
-
elif role == "user":
|
| 248 |
-
prompt += f"Human: {content}\n"
|
| 249 |
-
elif role == "assistant":
|
| 250 |
-
prompt += f"Assistant: {content}\n"
|
| 251 |
prompt += "Assistant:"
|
| 252 |
|
| 253 |
response = llm_client.generate(prompt, max_tokens=500, stream=False)
|
|
@@ -274,7 +271,7 @@ if st.button("Send"):
|
|
| 274 |
|
| 275 |
# Prepare conversation history
|
| 276 |
conversation_history = [{"role": msg["role"], "content": msg["content"]}
|
| 277 |
-
for msg in conversation[-5:]]
|
| 278 |
conversation_history.append({"role": "user", "content": user_input})
|
| 279 |
|
| 280 |
# Send to appropriate backend
|
|
@@ -293,6 +290,5 @@ if st.button("Send"):
|
|
| 293 |
|
| 294 |
if ai_response:
|
| 295 |
st.markdown(f"**AI Coach ({backend_used}):** {ai_response}")
|
| 296 |
-
# Note: In a production app, we'd save the conversation to Redis here
|
| 297 |
else:
|
| 298 |
st.error(f"Failed to get response from {backend_used}.")
|
|
|
|
| 1 |
+
# Force redeploy trigger - version 1.8
|
| 2 |
import streamlit as st
|
| 3 |
from utils.config import config
|
| 4 |
import requests
|
|
|
|
| 9 |
# Set page config
|
| 10 |
st.set_page_config(page_title="AI Life Coach", page_icon="🧘", layout="centered")
|
| 11 |
|
| 12 |
+
# Initialize session state
|
| 13 |
if 'ngrok_url' not in st.session_state:
|
| 14 |
st.session_state.ngrok_url = config.ollama_host
|
| 15 |
|
|
|
|
| 37 |
st.sidebar.success("Ngrok URL updated!")
|
| 38 |
st.experimental_rerun()
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Headers to skip ngrok browser warning
|
| 41 |
NGROK_HEADERS = {
|
| 42 |
"ngrok-skip-browser-warning": "true",
|
| 43 |
"User-Agent": "AI-Life-Coach-App"
|
| 44 |
}
|
| 45 |
|
| 46 |
+
# Fetch available models
|
| 47 |
+
def fetch_available_models(ngrok_url):
|
| 48 |
try:
|
| 49 |
response = requests.get(
|
| 50 |
+
f"{ngrok_url}/api/tags",
|
| 51 |
headers=NGROK_HEADERS,
|
| 52 |
timeout=5
|
| 53 |
)
|
| 54 |
if response.status_code == 200:
|
| 55 |
models_data = response.json().get("models", [])
|
| 56 |
+
return [m.get("name") for m in models_data]
|
| 57 |
+
except Exception:
|
| 58 |
+
pass
|
| 59 |
+
return []
|
| 60 |
+
|
| 61 |
+
# Update available models
|
| 62 |
+
if st.session_state.ngrok_url and st.session_state.model_status != "unreachable":
|
| 63 |
+
model_names = fetch_available_models(st.session_state.ngrok_url)
|
| 64 |
+
if model_names:
|
| 65 |
+
st.session_state.available_models = model_names
|
| 66 |
+
# If current selected model not in list, select the first one
|
| 67 |
+
if st.session_state.selected_model not in model_names:
|
| 68 |
+
st.session_state.selected_model = model_names[0]
|
| 69 |
|
| 70 |
# Model selector dropdown
|
| 71 |
+
st.sidebar.markdown("---")
|
| 72 |
+
st.sidebar.subheader("Model Selection")
|
| 73 |
if st.session_state.available_models:
|
| 74 |
selected_model = st.sidebar.selectbox(
|
| 75 |
"Select Model",
|
|
|
|
| 87 |
st.sidebar.markdown("---")
|
| 88 |
|
| 89 |
# Get environment info
|
| 90 |
+
BASE_URL = os.environ.get("SPACE_ID", "")
|
| 91 |
IS_HF_SPACE = bool(BASE_URL)
|
| 92 |
|
| 93 |
# Fetch Ollama status
|
| 94 |
def get_ollama_status(ngrok_url):
|
| 95 |
try:
|
|
|
|
| 96 |
response = requests.get(
|
| 97 |
f"{ngrok_url}/api/tags",
|
| 98 |
headers=NGROK_HEADERS,
|
|
|
|
| 104 |
st.session_state.available_models = model_names
|
| 105 |
|
| 106 |
if models:
|
|
|
|
| 107 |
selected_model_available = st.session_state.selected_model in model_names
|
| 108 |
return {
|
| 109 |
"running": True,
|
|
|
|
| 122 |
}
|
| 123 |
except Exception as e:
|
| 124 |
st.session_state.model_status = "unreachable"
|
|
|
|
| 125 |
return {
|
| 126 |
"running": False,
|
| 127 |
"model_loaded": None,
|
|
|
|
| 129 |
"remote_host": ngrok_url
|
| 130 |
}
|
| 131 |
|
| 132 |
+
# Load conversation history
|
| 133 |
def get_conversation_history(user_id):
|
| 134 |
try:
|
| 135 |
user_state = load_user_state(user_id)
|
|
|
|
| 139 |
st.warning(f"Could not load conversation history: {e}")
|
| 140 |
return []
|
| 141 |
|
| 142 |
+
# Check Ollama status
|
| 143 |
ollama_status = get_ollama_status(st.session_state.ngrok_url)
|
| 144 |
|
| 145 |
# Update model status
|
|
|
|
| 166 |
st.sidebar.warning(f"🧠 Ollama Model: {model_status_msg} (selected model not available)")
|
| 167 |
st.sidebar.info(f"Connected to: {ollama_status['remote_host']}")
|
| 168 |
|
| 169 |
+
# Status indicators
|
| 170 |
model_status_container = st.sidebar.empty()
|
| 171 |
if st.session_state.model_status == "ready":
|
| 172 |
model_status_container.success("✅ Model Ready")
|
|
|
|
| 174 |
model_status_container.info("🔍 Checking model...")
|
| 175 |
elif st.session_state.model_status == "no_models":
|
| 176 |
model_status_container.warning("⚠️ No models found")
|
| 177 |
+
else:
|
| 178 |
model_status_container.error("❌ Ollama unreachable")
|
| 179 |
|
|
|
|
| 180 |
redis_status_container = st.sidebar.empty()
|
| 181 |
if check_redis_health():
|
| 182 |
redis_status_container.success("✅ Redis Connected")
|
|
|
|
| 203 |
# Function to send message to Ollama
|
| 204 |
def send_to_ollama(user_input, conversation_history, ngrok_url, model_name):
|
| 205 |
try:
|
| 206 |
+
# Use the correct chat endpoint with proper payload
|
| 207 |
payload = {
|
| 208 |
"model": model_name,
|
| 209 |
"messages": conversation_history,
|
| 210 |
+
"stream": False,
|
| 211 |
+
"options": {
|
| 212 |
+
"temperature": 0.7,
|
| 213 |
+
"top_p": 0.9
|
| 214 |
+
}
|
| 215 |
}
|
| 216 |
|
| 217 |
response = requests.post(
|
|
|
|
| 235 |
# Function to send message to Hugging Face (fallback)
|
| 236 |
def send_to_hf(user_input, conversation_history):
|
| 237 |
try:
|
|
|
|
| 238 |
from core.llm import LLMClient
|
|
|
|
|
|
|
| 239 |
llm_client = LLMClient(provider="huggingface")
|
| 240 |
|
| 241 |
+
# Format for HF
|
| 242 |
+
prompt = "You are a helpful life coach. "
|
| 243 |
for msg in conversation_history:
|
| 244 |
+
if msg["role"] == "user":
|
| 245 |
+
prompt += f"Human: {msg['content']} "
|
| 246 |
+
elif msg["role"] == "assistant":
|
| 247 |
+
prompt += f"Assistant: {msg['content']} "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
prompt += "Assistant:"
|
| 249 |
|
| 250 |
response = llm_client.generate(prompt, max_tokens=500, stream=False)
|
|
|
|
| 271 |
|
| 272 |
# Prepare conversation history
|
| 273 |
conversation_history = [{"role": msg["role"], "content": msg["content"]}
|
| 274 |
+
for msg in conversation[-5:]]
|
| 275 |
conversation_history.append({"role": "user", "content": user_input})
|
| 276 |
|
| 277 |
# Send to appropriate backend
|
|
|
|
| 290 |
|
| 291 |
if ai_response:
|
| 292 |
st.markdown(f"**AI Coach ({backend_used}):** {ai_response}")
|
|
|
|
| 293 |
else:
|
| 294 |
st.error(f"Failed to get response from {backend_used}.")
|
test_ollama_connection.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
# Load environment variables
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
OLLAMA_HOST = os.getenv("OLLAMA_HOST", "https://ace32bd59aef.ngrok-free.app")
|
| 9 |
+
MODEL_NAME = os.getenv("LOCAL_MODEL_NAME", "mistral:latest")
|
| 10 |
+
|
| 11 |
+
print(f"Testing Ollama connection to: {OLLAMA_HOST}")
|
| 12 |
+
print(f"Using model: {MODEL_NAME}")
|
| 13 |
+
print()
|
| 14 |
+
|
| 15 |
+
# Headers to skip ngrok browser warning
|
| 16 |
+
headers = {
|
| 17 |
+
"ngrok-skip-browser-warning": "true",
|
| 18 |
+
"User-Agent": "AI-Life-Coach-Test"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Test 1: List models
|
| 22 |
+
print("Test 1: Listing available models...")
|
| 23 |
+
try:
|
| 24 |
+
response = requests.get(f"{OLLAMA_HOST}/api/tags", headers=headers, timeout=10)
|
| 25 |
+
print(f"Status Code: {response.status_code}")
|
| 26 |
+
|
| 27 |
+
if response.status_code == 200:
|
| 28 |
+
data = response.json()
|
| 29 |
+
models = data.get("models", [])
|
| 30 |
+
print(f"Found {len(models)} models:")
|
| 31 |
+
for model in models:
|
| 32 |
+
print(f" - {model['name']} ({model.get('size', 'Unknown size')})")
|
| 33 |
+
else:
|
| 34 |
+
print(f"Error: {response.text}")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Connection failed: {e}")
|
| 37 |
+
|
| 38 |
+
print()
|
| 39 |
+
|
| 40 |
+
# Test 2: Simple chat test
|
| 41 |
+
print("Test 2: Simple chat test...")
|
| 42 |
+
try:
|
| 43 |
+
payload = {
|
| 44 |
+
"model": MODEL_NAME,
|
| 45 |
+
"messages": [
|
| 46 |
+
{"role": "user", "content": "Hello! Respond with just 'Hi there!'"}
|
| 47 |
+
],
|
| 48 |
+
"stream": False
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
response = requests.post(f"{OLLAMA_HOST}/api/chat", headers=headers, json=payload, timeout=30)
|
| 52 |
+
print(f"Status Code: {response.status_code}")
|
| 53 |
+
|
| 54 |
+
if response.status_code == 200:
|
| 55 |
+
data = response.json()
|
| 56 |
+
message = data.get("message", {})
|
| 57 |
+
content = message.get("content", "")
|
| 58 |
+
print(f"Response: {content}")
|
| 59 |
+
print("✅ Chat test successful!")
|
| 60 |
+
else:
|
| 61 |
+
print(f"Error: {response.text}")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"Chat test failed: {e}")
|
| 64 |
+
|
| 65 |
+
print()
|
| 66 |
+
print("Test completed.")
|