# Document-Based Question Answering with OpenWebUI This document explains how to use the document-based question answering (RAG) functionality in the AI service. ## Overview The AI service now supports Retrieval Augmented Generation (RAG) by leveraging OpenWebUI's built-in knowledge database. This allows users to: 1. Upload documents to OpenWebUI 2. Ask questions about those documents 3. Receive responses that incorporate information from the documents ## How It Works When RAG is enabled: 1. The AI service forwards the request to OpenWebUI with `use_knowledge=True` 2. OpenWebUI searches its knowledge database for relevant information 3. The retrieved information is used to augment the model's response 4. The response is returned to the user ## Using RAG in API Requests To enable RAG in your API requests, set the `use_rag` parameter to `true`: ```json POST /chats/{chat_id}/messages { "message": "What information do you have about project X?", "user_id": "user123", "use_rag": true } ``` ## Testing RAG Functionality You can test the RAG functionality using the `/test-rag` endpoint: ``` POST /test-rag?query=What information do you have about project X? ``` This will return a response that includes information from documents in OpenWebUI's knowledge database. ## Uploading Documents to OpenWebUI To use RAG effectively, you need to upload documents to OpenWebUI: 1. Log in to OpenWebUI at your configured URL (default: http://104.225.217.215:8080) 2. Navigate to the Knowledge section 3. Upload your documents (PDF, TXT, DOCX, etc.) 4. OpenWebUI will automatically process and index the documents ## Troubleshooting If RAG is not working as expected: 1. Ensure OpenWebUI is running and accessible 2. Check that documents are properly uploaded and indexed in OpenWebUI 3. Verify that the `use_rag` parameter is set to `true` in your requests 4. Check the logs for any errors related to OpenWebUI API calls If there are connection issues with OpenWebUI, the AI service will automatically fall back to using the direct Ollama API without RAG. ## Configuration The following configuration settings affect RAG functionality: - `OPENWEBUI_URL`: URL of your OpenWebUI instance - `OPENWEBUI_API_KEY`: API key for OpenWebUI (if required) - `API_TIMEOUT`: Timeout for API requests (in seconds) These can be set in your environment variables or in the `.env` file.