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
+3 -3
View File
@@ -52,8 +52,8 @@ ai_service/
## API Documentation
Once the service is running, you can access the API documentation at:
- Swagger UI: http://localhost:5251/docs
- ReDoc: http://localhost:5251/redoc
- Swagger UI: http://localhost:5252/docs
- ReDoc: http://localhost:5252/redoc
## Deployment
@@ -69,7 +69,7 @@ To deploy the service:
./deploy.sh
```
This will start the service on port 5251 using uvicorn with nohup.
This will start the service on port 5252 using uvicorn with nohup.
## API Endpoints
+4 -2
View File
@@ -6,14 +6,16 @@ import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
import os.path
dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
load_dotenv(dotenv_path=dotenv_path)
class Config:
"""Base configuration."""
# API configuration
API_HOST = os.environ.get('API_HOST', '0.0.0.0')
API_PORT = int(os.environ.get('API_PORT', 5251))
API_PORT = int(os.environ.get('API_PORT', 5252))
# OpenWebUI configuration
OPENWEBUI_URL = os.environ.get('OPENWEBUI_URL', 'http://104.225.217.215:8080')
+122
View File
@@ -76,5 +76,127 @@
}
],
"team_members": []
},
"b72d5056-4061-40e5-9d8f-11c479ff95ed": {
"id": "b72d5056-4061-40e5-9d8f-11c479ff95ed",
"title": "Test Chat",
"user_id": "test_user",
"model_id": "llama3.1",
"is_team_chat": false,
"created_at": "2025-05-09T16:52:09.064723",
"updated_at": "2025-05-09T16:52:14.572522",
"messages": [
{
"id": "7a425092-90a1-447f-b424-f92c001dd70a",
"content": "Hello, how are you?",
"user_id": "test_user",
"is_user_message": true,
"timestamp": "2025-05-09T16:52:14.548916"
},
{
"id": "00927dda-01f8-4f1a-808e-1b596dca258e",
"content": "Error generating response: HTTPConnectionPool(host='localhost', port=11434): Max retries exceeded with url: /api/generate (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x11101fed0>: Failed to establish a new connection: [Errno 61] Connection refused'))",
"user_id": null,
"is_user_message": false,
"timestamp": "2025-05-09T16:52:14.572493"
}
],
"team_members": []
},
"36aed910-1c6e-4184-84f2-729227d57915": {
"id": "36aed910-1c6e-4184-84f2-729227d57915",
"title": "Test Chat",
"user_id": "test_user",
"model_id": "llama3.1",
"is_team_chat": false,
"created_at": "2025-05-09T18:32:34.034219",
"updated_at": "2025-05-09T18:40:42.597983",
"messages": [
{
"id": "cbb1e7d2-6b66-4d0a-9907-0b72d344f3f3",
"content": "Hello, how are you?",
"user_id": "test_user",
"is_user_message": true,
"timestamp": "2025-05-09T18:32:39.388163"
},
{
"id": "6b2abf18-f937-4390-bf51-ef46115f2291",
"content": "Error generating response: HTTPConnectionPool(host='localhost', port=11434): Max retries exceeded with url: /api/generate (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x11154f510>: Failed to establish a new connection: [Errno 61] Connection refused'))",
"user_id": null,
"is_user_message": false,
"timestamp": "2025-05-09T18:32:39.408236"
},
{
"id": "71c388c9-1e96-4b18-a6ac-5f69082e3fa0",
"content": "Hello, how are you?",
"user_id": "test_user",
"is_user_message": true,
"timestamp": "2025-05-09T18:33:48.306212"
},
{
"id": "b2cfa76f-c4bc-4d02-89b5-ebcf0a24616a",
"content": "Error generating response: HTTPConnectionPool(host='localhost', port=11434): Max retries exceeded with url: /api/generate (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x1068eb610>: Failed to establish a new connection: [Errno 61] Connection refused'))",
"user_id": null,
"is_user_message": false,
"timestamp": "2025-05-09T18:33:48.321634"
},
{
"id": "4e61326b-d457-4087-a0e9-a8416de42a51",
"content": "Hello, how are you?",
"user_id": "test_user",
"is_user_message": true,
"timestamp": "2025-05-09T18:36:15.153049"
},
{
"id": "f63c7ee3-9ade-4543-88be-129a48213c85",
"content": "Error generating response: HTTPConnectionPool(host='localhost', port=11434): Max retries exceeded with url: /api/generate (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x10ac36ad0>: Failed to establish a new connection: [Errno 61] Connection refused'))",
"user_id": null,
"is_user_message": false,
"timestamp": "2025-05-09T18:36:15.174809"
},
{
"id": "d463b173-8a9f-4972-9293-380f2959f2e3",
"content": "Hello, how are you?",
"user_id": "test_user",
"is_user_message": true,
"timestamp": "2025-05-09T18:38:17.218487"
},
{
"id": "ee7c8a2e-c7fe-4545-b9d0-895f33fa2f62",
"content": "Error generating response: HTTPConnectionPool(host='104.225.217.215', port=11434): Max retries exceeded with url: /api/generate (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x10e343410>: Failed to establish a new connection: [Errno 61] Connection refused'))",
"user_id": null,
"is_user_message": false,
"timestamp": "2025-05-09T18:38:17.493977"
},
{
"id": "3b94da97-e224-41c5-95c1-6d17118e5665",
"content": "Hello, how are you?",
"user_id": "test_user",
"is_user_message": true,
"timestamp": "2025-05-09T18:39:29.063581"
},
{
"id": "2843b17c-35bb-49cd-b306-cc553712341c",
"content": "Error generating response: 401 Client Error: Unauthorized for url: http://104.225.217.215:8080/ollama/api/generate",
"user_id": null,
"is_user_message": false,
"timestamp": "2025-05-09T18:39:29.605996"
},
{
"id": "314a855a-9922-4059-bb02-57f56baa8c62",
"content": "Hello, how are you?",
"user_id": "test_user",
"is_user_message": true,
"timestamp": "2025-05-09T18:40:42.061269"
},
{
"id": "e417708d-04b6-4b50-999d-cdc242a2283b",
"content": "Error generating response: 401 Client Error: Unauthorized for url: http://104.225.217.215:8080/ollama/api/generate",
"user_id": null,
"is_user_message": false,
"timestamp": "2025-05-09T18:40:42.597967"
}
],
"team_members": []
}
}
+1 -1
View File
@@ -8,7 +8,7 @@ source venv/bin/activate
# Export environment variables
export API_HOST=0.0.0.0
export API_PORT=5251
export API_PORT=5252
# Make sure the Python path includes the current directory
export PYTHONPATH=$PYTHONPATH:$(pwd)
+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)}"