modified to combine openwebui and ollama

This commit is contained in:
Iyeoluwa Akinrinola
2025-05-09 20:00:26 +01:00
parent 4c0cff7cca
commit 386af38a99
9 changed files with 149 additions and 35 deletions
+7 -17
View File
@@ -2,13 +2,10 @@
Service for model management and interaction.
"""
import os
import json
import requests
from typing import List, Dict, Any, Optional
from ai_service.config import config
from ai_service.embeddings.document_service import document_service
from ai_service.models.model_parameters import ModelParameters
class ModelService:
@@ -112,8 +109,8 @@ class ModelService:
if model_id not in self.AVAILABLE_MODELS:
model_id = self.default_model
# Get the provider for this model
provider = self.AVAILABLE_MODELS[model_id].get('provider', 'ollama')
# Ensure we're using a valid model
# (model_id is already validated above)
# Prepare the messages for the API call
messages = []
@@ -212,15 +209,13 @@ class ModelService:
# Make the API call to Ollama
try:
# Prepare headers with API key
# Prepare headers
headers = {"Content-Type": "application/json"}
if self.openwebui_api_key:
headers["Authorization"] = f"Bearer {self.openwebui_api_key}"
# Ollama API endpoint is /api/chat or /api/generate
# Direct Ollama API call
response = requests.post(
f"{self.ollama_api_url}/api/generate",
headers=headers,
f"{self.ollama_api_url}/api/chat",
headers={"Content-Type": "application/json"},
json=request_json,
timeout=60
)
@@ -229,18 +224,13 @@ class ModelService:
result = response.json()
# Extract the response content from Ollama
# The response format depends on whether we're using /api/chat or /api/generate
# The response format for Ollama API
if 'message' in result and 'content' in result['message']:
# Format for /api/chat
return result['message']['content']
elif 'response' in result:
# Format for /api/generate
return result['response']
else:
return "Error: Unexpected response format from Ollama"
except Exception as e:
print(f"Error calling Ollama API: {str(e)}")
return f"Error generating response: {str(e)}"