Added Rag Featured

This commit is contained in:
Iyeoluwa Akinrinola
2025-05-16 15:24:01 +01:00
parent e82861a5db
commit 1896298a18
30 changed files with 503 additions and 1580 deletions
+19 -5
View File
@@ -135,9 +135,6 @@ class ModelService:
model_id = self.default_model
print(f" - Model not found, using default: {model_id}")
# Ensure we're using a valid model
# (model_id is already validated above)
# Prepare the messages for the API call
messages = []
@@ -178,6 +175,10 @@ class ModelService:
openwebui_request['max_tokens'] = params['max_tokens']
if 'top_p' in params:
openwebui_request['top_p'] = params['top_p']
if 'top_k' in params:
openwebui_request['top_k'] = params['top_k']
if 'repeat_penalty' in params:
openwebui_request['repeat_penalty'] = params['repeat_penalty']
# Make the API call to OpenWebUI
headers = {"Content-Type": "application/json"}
@@ -201,10 +202,15 @@ class ModelService:
result = response.json()
# Extract the response content
if 'message' in result:
if 'choices' in result and len(result['choices']) > 0 and 'message' in result['choices'][0]:
# OpenAI-compatible format
return result['choices'][0]['message']['content']
elif 'message' in result and 'content' in result['message']:
# OpenWebUI format
return result['message']['content']
else:
return "Error: Unexpected response format from OpenWebUI"
print(f"WARNING: Unexpected response format from OpenWebUI: {json.dumps(result, indent=2)}")
return "Error: Unexpected response format from OpenWebUI. Falling back to direct model call."
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."
@@ -216,6 +222,11 @@ class ModelService:
print(f"ERROR: {error_msg}")
print("Falling back to direct Ollama call without RAG")
# Continue to the Ollama API call below
except requests.exceptions.HTTPError as e:
error_msg = f"HTTP error from 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
except Exception as e:
error_msg = f"Error calling OpenWebUI API: {str(e)}"
print(f"ERROR: {error_msg}")
@@ -247,6 +258,8 @@ class ModelService:
request_json['top_k'] = params['top_k']
if 'max_tokens' in params:
request_json['max_tokens'] = params['max_tokens']
if 'repeat_penalty' in params:
request_json['repeat_penalty'] = params['repeat_penalty']
# Make the API call to Ollama
try:
@@ -272,6 +285,7 @@ class ModelService:
if 'message' in result and 'content' in result['message']:
return result['message']['content']
else:
print(f"WARNING: Unexpected response format from Ollama: {json.dumps(result, indent=2)}")
return "Error: Unexpected response format from Ollama"
except requests.exceptions.Timeout as e: