This commit is contained in:
Iyeoluwa Akinrinola
2025-05-12 16:30:35 +01:00
parent 7c61e98340
commit f00941cece
4 changed files with 180 additions and 21 deletions
+28 -14
View File
@@ -286,21 +286,35 @@ class ChatService:
# Get response from model
model_id = chat['model_id']
response_text = model_service.generate_response(
model_id=model_id,
prompt=message,
context=context,
use_rag=use_rag,
model_params=model_params
)
try:
print(f"Requesting response from model {model_id} for chat {chat_id}")
response_text = model_service.generate_response(
model_id=model_id,
prompt=message,
context=context,
use_rag=use_rag,
model_params=model_params
)
# Add bot response to chat
response_message = self.add_message(
chat_id=chat_id,
content=response_text,
user_id=user_id,
is_user_message=False
)
# Add bot response to chat
response_message = self.add_message(
chat_id=chat_id,
content=response_text,
user_id=user_id,
is_user_message=False
)
except Exception as e:
error_message = f"Error generating response: {str(e)}"
print(f"ERROR: {error_message}")
# Add error message to chat
response_message = self.add_message(
chat_id=chat_id,
content=error_message,
user_id=user_id,
is_user_message=False
)
return response_message
+26 -5
View File
@@ -52,6 +52,7 @@ class ModelService:
self.ollama_api_url = config.OLLAMA_API_URL
self.openwebui_url = config.OPENWEBUI_URL
self.openwebui_api_key = config.OPENWEBUI_API_KEY
self.api_timeout = config.API_TIMEOUT
def get_available_models(self) -> List[Dict[str, Any]]:
"""
@@ -193,7 +194,7 @@ class ModelService:
f"{self.openwebui_url}/api/chat/completions",
headers=headers,
json=openwebui_request,
timeout=60 # Longer timeout for RAG
timeout=self.api_timeout
)
response.raise_for_status()
@@ -205,9 +206,19 @@ class ModelService:
else:
return "Error: Unexpected response format from OpenWebUI"
except requests.exceptions.Timeout as e:
error_msg = f"Timeout error connecting to OpenWebUI API: {str(e)}. The request exceeded the {self.api_timeout} second timeout."
print(f"ERROR: {error_msg}")
print("Falling back to direct Ollama call without RAG")
# Continue to the Ollama API call below
except requests.exceptions.ConnectionError as e:
error_msg = f"Connection error to OpenWebUI API: {str(e)}. Please check if OpenWebUI is running at {self.openwebui_url}."
print(f"ERROR: {error_msg}")
print("Falling back to direct Ollama call without RAG")
# Continue to the Ollama API call below
except Exception as e:
print(f"Error calling OpenWebUI API: {str(e)}")
# Fall back to direct Ollama call without RAG
error_msg = f"Error calling OpenWebUI API: {str(e)}"
print(f"ERROR: {error_msg}")
print("Falling back to direct Ollama call without RAG")
# Continue to the Ollama API call below
@@ -250,7 +261,7 @@ class ModelService:
f"{self.ollama_api_url}/api/chat",
headers={"Content-Type": "application/json"},
json=request_json,
timeout=60
timeout=self.api_timeout
)
response.raise_for_status()
@@ -263,8 +274,18 @@ class ModelService:
else:
return "Error: Unexpected response format from Ollama"
except requests.exceptions.Timeout as e:
error_msg = f"Timeout error connecting to Ollama API: {str(e)}. The request exceeded the {self.api_timeout} second timeout."
print(f"ERROR: {error_msg}")
return error_msg
except requests.exceptions.ConnectionError as e:
error_msg = f"Connection error to Ollama API: {str(e)}. Please check if Ollama is running at {self.ollama_api_url}."
print(f"ERROR: {error_msg}")
return error_msg
except Exception as e:
return f"Error generating response: {str(e)}"
error_msg = f"Error generating response: {str(e)}"
print(f"ERROR: {error_msg}")
return error_msg
# Create a singleton instance