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
+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