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
+4 -4
View File
@@ -74,8 +74,8 @@ To deploy the service:
## API Documentation ## API Documentation
Once the service is running, you can access the API documentation at: Once the service is running, you can access the API documentation at:
- Swagger UI: http://localhost:5251/docs - Swagger UI: http://localhost:5252/docs
- ReDoc: http://localhost:5251/redoc - ReDoc: http://localhost:5252/redoc
## OpenWebUI Configuration ## OpenWebUI Configuration
@@ -83,8 +83,8 @@ To configure OpenWebUI to use this service as its backend:
1. Set the following environment variables in OpenWebUI: 1. Set the following environment variables in OpenWebUI:
``` ```
OLLAMA_API_BASE_URL=http://your-server-ip:5251/ollama OLLAMA_API_BASE_URL=http://your-server-ip:5252/ollama
OPENAI_API_BASE_URL=http://your-server-ip:5251/api OPENAI_API_BASE_URL=http://your-server-ip:5252/api
``` ```
2. Restart OpenWebUI to apply the changes. 2. Restart OpenWebUI to apply the changes.
+3 -3
View File
@@ -52,8 +52,8 @@ ai_service/
## API Documentation ## API Documentation
Once the service is running, you can access the API documentation at: Once the service is running, you can access the API documentation at:
- Swagger UI: http://localhost:5251/docs - Swagger UI: http://localhost:5252/docs
- ReDoc: http://localhost:5251/redoc - ReDoc: http://localhost:5252/redoc
## Deployment ## Deployment
@@ -69,7 +69,7 @@ To deploy the service:
./deploy.sh ./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 ## API Endpoints
+4 -2
View File
@@ -6,14 +6,16 @@ import os
from dotenv import load_dotenv from dotenv import load_dotenv
# Load environment variables from .env file # 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: class Config:
"""Base configuration.""" """Base configuration."""
# API configuration # API configuration
API_HOST = os.environ.get('API_HOST', '0.0.0.0') 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 configuration
OPENWEBUI_URL = os.environ.get('OPENWEBUI_URL', 'http://104.225.217.215:8080') OPENWEBUI_URL = os.environ.get('OPENWEBUI_URL', 'http://104.225.217.215:8080')
+122
View File
@@ -76,5 +76,127 @@
} }
], ],
"team_members": [] "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 environment variables
export API_HOST=0.0.0.0 export API_HOST=0.0.0.0
export API_PORT=5251 export API_PORT=5252
# Make sure the Python path includes the current directory # Make sure the Python path includes the current directory
export PYTHONPATH=$PYTHONPATH:$(pwd) export PYTHONPATH=$PYTHONPATH:$(pwd)
+7 -17
View File
@@ -2,13 +2,10 @@
Service for model management and interaction. Service for model management and interaction.
""" """
import os
import json
import requests import requests
from typing import List, Dict, Any, Optional from typing import List, Dict, Any, Optional
from ai_service.config import config from ai_service.config import config
from ai_service.embeddings.document_service import document_service
from ai_service.models.model_parameters import ModelParameters from ai_service.models.model_parameters import ModelParameters
class ModelService: class ModelService:
@@ -112,8 +109,8 @@ class ModelService:
if model_id not in self.AVAILABLE_MODELS: if model_id not in self.AVAILABLE_MODELS:
model_id = self.default_model model_id = self.default_model
# Get the provider for this model # Ensure we're using a valid model
provider = self.AVAILABLE_MODELS[model_id].get('provider', 'ollama') # (model_id is already validated above)
# Prepare the messages for the API call # Prepare the messages for the API call
messages = [] messages = []
@@ -212,15 +209,13 @@ class ModelService:
# Make the API call to Ollama # Make the API call to Ollama
try: try:
# Prepare headers with API key # Prepare headers
headers = {"Content-Type": "application/json"} 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( response = requests.post(
f"{self.ollama_api_url}/api/generate", f"{self.ollama_api_url}/api/chat",
headers=headers, headers={"Content-Type": "application/json"},
json=request_json, json=request_json,
timeout=60 timeout=60
) )
@@ -229,18 +224,13 @@ class ModelService:
result = response.json() result = response.json()
# Extract the response content from Ollama # 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']: if 'message' in result and 'content' in result['message']:
# Format for /api/chat
return result['message']['content'] return result['message']['content']
elif 'response' in result:
# Format for /api/generate
return result['response']
else: else:
return "Error: Unexpected response format from Ollama" return "Error: Unexpected response format from Ollama"
except Exception as e: except Exception as e:
print(f"Error calling Ollama API: {str(e)}")
return f"Error generating response: {str(e)}" return f"Error generating response: {str(e)}"
+5 -5
View File
@@ -12,7 +12,7 @@ touch ai_service/data/document_metadata.json
touch ai_service/data/chats.json touch ai_service/data/chats.json
# Start the AI service # Start the AI service
echo "Starting AI Service on port 5251..." echo "Starting AI Service on port 5252..."
VENV_PATH="./venv" VENV_PATH="./venv"
PYTHON_PATH="$VENV_PATH/bin/python" PYTHON_PATH="$VENV_PATH/bin/python"
@@ -30,13 +30,13 @@ sleep 2
# Check if the service is running # Check if the service is running
if pgrep -f "run_ai_service.py" > /dev/null; then if pgrep -f "run_ai_service.py" > /dev/null; then
echo "AI Service started successfully on port 5251" echo "AI Service started successfully on port 5252"
echo "Check ai_service.log for output" echo "Check ai_service.log for output"
echo "To stop the service, run: pkill -f \"run_ai_service.py\"" echo "To stop the service, run: pkill -f \"run_ai_service.py\""
# Test the health endpoint # Test the health endpoint
echo -e "\nTesting health endpoint..." echo -e "\nTesting health endpoint..."
curl -s http://localhost:5251/health curl -s http://localhost:5252/health
echo -e "\n" echo -e "\n"
# Test creating a chat and sending a message # Test creating a chat and sending a message
@@ -49,7 +49,7 @@ import json
# Create a chat # Create a chat
response = requests.post( response = requests.post(
"http://localhost:5251/chats", "http://localhost:5252/chats",
json={ json={
"user_id": "test_user", "user_id": "test_user",
"title": "Test Chat", "title": "Test Chat",
@@ -63,7 +63,7 @@ if response.status_code == 200:
# Send a message with parameters # Send a message with parameters
response = requests.post( response = requests.post(
f"http://localhost:5251/chats/{chat_id}/messages", f"http://localhost:5252/chats/{chat_id}/messages",
json={ json={
"message": "Hello, AI!", "message": "Hello, AI!",
"user_id": "test_user", "user_id": "test_user",
+1 -1
View File
@@ -64,7 +64,7 @@ sleep 5
ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP "ps aux | grep 'uvicorn ai_service.api:app' | grep -v grep" ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP "ps aux | grep 'uvicorn ai_service.api:app' | grep -v grep"
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "Service is running!" echo "Service is running!"
echo "You can access the API at: http://$SERVER_IP:5251" echo "You can access the API at: http://$SERVER_IP:5252"
echo "Check logs with: ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP 'tail -f $REMOTE_DIR/ai_service_app/logs/ai_service.log'" echo "Check logs with: ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP 'tail -f $REMOTE_DIR/ai_service_app/logs/ai_service.log'"
else else
echo "Error: Service failed to start. Check logs on the server." echo "Error: Service failed to start. Check logs on the server."
+2 -2
View File
@@ -8,10 +8,10 @@ import sys
# Run the service # Run the service
if __name__ == "__main__": if __name__ == "__main__":
print("Starting AI service on 0.0.0.0:5251") print("Starting AI service on 0.0.0.0:5252")
uvicorn.run( uvicorn.run(
"ai_service.api:app", "ai_service.api:app",
host="0.0.0.0", host="0.0.0.0",
port=5251, port=5252,
reload=True reload=True
) )