From 386af38a9925d10d49a21431da952db347bcbe47 Mon Sep 17 00:00:00 2001 From: Iyeoluwa Akinrinola Date: Fri, 9 May 2025 20:00:26 +0100 Subject: [PATCH] modified to combine openwebui and ollama --- README.md | 8 +- ai_service/README.md | 6 +- ai_service/config.py | 6 +- ai_service/data/chats.json | 122 +++++++++++++++++++++++++++++ ai_service/deploy.sh | 2 +- ai_service/models/model_service.py | 24 ++---- deploy_ai_service.sh | 10 +-- remote_deploy.sh | 2 +- run_ai_service.py | 4 +- 9 files changed, 149 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index f9c11a3..971b1a5 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,8 @@ To deploy the 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 ## OpenWebUI Configuration @@ -83,8 +83,8 @@ To configure OpenWebUI to use this service as its backend: 1. Set the following environment variables in OpenWebUI: ``` - OLLAMA_API_BASE_URL=http://your-server-ip:5251/ollama - OPENAI_API_BASE_URL=http://your-server-ip:5251/api + OLLAMA_API_BASE_URL=http://your-server-ip:5252/ollama + OPENAI_API_BASE_URL=http://your-server-ip:5252/api ``` 2. Restart OpenWebUI to apply the changes. diff --git a/ai_service/README.md b/ai_service/README.md index 5f634f6..2885713 100644 --- a/ai_service/README.md +++ b/ai_service/README.md @@ -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 diff --git a/ai_service/config.py b/ai_service/config.py index 3d5888d..b8f90cb 100644 --- a/ai_service/config.py +++ b/ai_service/config.py @@ -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') diff --git a/ai_service/data/chats.json b/ai_service/data/chats.json index 8ec329f..9ce5e19 100644 --- a/ai_service/data/chats.json +++ b/ai_service/data/chats.json @@ -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(': 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(': 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(': 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(': 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(': 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": [] } } \ No newline at end of file diff --git a/ai_service/deploy.sh b/ai_service/deploy.sh index 3f61ab9..caa2c47 100755 --- a/ai_service/deploy.sh +++ b/ai_service/deploy.sh @@ -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) diff --git a/ai_service/models/model_service.py b/ai_service/models/model_service.py index 3b185fc..61e108a 100644 --- a/ai_service/models/model_service.py +++ b/ai_service/models/model_service.py @@ -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)}" diff --git a/deploy_ai_service.sh b/deploy_ai_service.sh index e5802e7..135a488 100755 --- a/deploy_ai_service.sh +++ b/deploy_ai_service.sh @@ -12,7 +12,7 @@ touch ai_service/data/document_metadata.json touch ai_service/data/chats.json # Start the AI service -echo "Starting AI Service on port 5251..." +echo "Starting AI Service on port 5252..." VENV_PATH="./venv" PYTHON_PATH="$VENV_PATH/bin/python" @@ -30,13 +30,13 @@ sleep 2 # Check if the service is running 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 "To stop the service, run: pkill -f \"run_ai_service.py\"" # Test the health endpoint echo -e "\nTesting health endpoint..." - curl -s http://localhost:5251/health + curl -s http://localhost:5252/health echo -e "\n" # Test creating a chat and sending a message @@ -49,7 +49,7 @@ import json # Create a chat response = requests.post( - "http://localhost:5251/chats", + "http://localhost:5252/chats", json={ "user_id": "test_user", "title": "Test Chat", @@ -63,7 +63,7 @@ if response.status_code == 200: # Send a message with parameters response = requests.post( - f"http://localhost:5251/chats/{chat_id}/messages", + f"http://localhost:5252/chats/{chat_id}/messages", json={ "message": "Hello, AI!", "user_id": "test_user", diff --git a/remote_deploy.sh b/remote_deploy.sh index 8d0bbfe..8836c9e 100755 --- a/remote_deploy.sh +++ b/remote_deploy.sh @@ -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" if [ $? -eq 0 ]; then 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'" else echo "Error: Service failed to start. Check logs on the server." diff --git a/run_ai_service.py b/run_ai_service.py index 3aaf3cb..876b2ab 100644 --- a/run_ai_service.py +++ b/run_ai_service.py @@ -8,10 +8,10 @@ import sys # Run the service 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( "ai_service.api:app", host="0.0.0.0", - port=5251, + port=5252, reload=True )