OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
fix: check ollama url by probing Langflow validate (#1099)
Lucas Oliveira9ab663e00d4c
diff --git a/Makefile b/Makefileindex be229179a..c4081bc6a 100644--- a/Makefile+++ b/Makefile@@ -702,7 +702,7 @@ test-integration: ## Run integration tests (requires infrastructure)@echo "$(CYAN)Make sure to run 'make dev-local' first!$(NC)"uv run pytest tests/integration/ -v-test-ci: ensure-rsa-keys ## Start infra, run integration + SDK tests, tear down (uses DockerHub images)+test-ci: ensure-rsa-keys ensure-config-dir ## Start infra, run integration + SDK tests, tear down (uses DockerHub images)@set -e; \echo "$(YELLOW)Installing test dependencies...$(NC)"; \uv sync --group dev; \@@ -809,7 +809,7 @@ test-ci: ensure-rsa-keys ## Start infra, run integration + SDK tests, tear down$(COMPOSE_CMD) down -v 2>/dev/null || true; \exit $$TEST_RESULT-test-ci-local: ensure-rsa-keys ## Same as test-ci but builds all images locally+test-ci-local: ensure-rsa-keys ensure-config-dir ## Same as test-ci but builds all images locally@set -e; \echo "$(YELLOW)Installing test dependencies...$(NC)"; \uv sync --group dev; \diff --git a/docker-compose.yml b/docker-compose.ymlindex d9a3218c6..98fafd9dc 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -98,11 +98,11 @@ services:- UPLOAD_BATCH_SIZE=${UPLOAD_BATCH_SIZE:-25}- MAX_WORKERS=${MAX_WORKERS:-4}volumes:- - ${OPENRAG_DOCUMENTS_PATH:-./openrag-documents}:/app/openrag-documents:Z+ - ${OPENRAG_DOCUMENTS_PATH:-./openrag-documents}:/app/openrag-documents:U,z- ${OPENRAG_KEYS_PATH:-./keys}:/app/keys:U,z- ${OPENRAG_FLOWS_PATH:-./flows}:/app/flows:U,z- - ${OPENRAG_CONFIG_PATH:-./config}:/app/config:Z- - ${OPENRAG_DATA_PATH:-./data}:/app/data:Z+ - ${OPENRAG_CONFIG_PATH:-./config}:/app/config:U,z+ - ${OPENRAG_DATA_PATH:-./data}:/app/data:U,zopenrag-frontend:image: langflowai/openrag-frontend:${OPENRAG_VERSION:-latest}diff --git a/frontend/components/task-collapsible-section.tsx b/frontend/components/task-collapsible-section.tsxindex 4f672d0b8..e7c28068c 100644--- a/frontend/components/task-collapsible-section.tsx+++ b/frontend/components/task-collapsible-section.tsx@@ -56,4 +56,3 @@ export function TaskCollapsibleSection<T>({</div>);}-diff --git a/frontend/lib/time-utils.ts b/frontend/lib/time-utils.tsindex d795a6f37..ef30b9d1b 100644--- a/frontend/lib/time-utils.ts+++ b/frontend/lib/time-utils.ts@@ -60,7 +60,7 @@ export function formatTaskTimestamp(if (diffMinutes < 60) {return `${diffMinutes} min ago · ${formatClockTime(date)}`;}- if (diffHours < 24) return `${diffHours} hours ago · ${formatClockTime(date)}`;+ if (diffHours < 24)+ return `${diffHours} hours ago · ${formatClockTime(date)}`;return `${Math.floor(diffHours / 24)} days ago · ${formatClockTime(date)}`;}-diff --git a/src/api/settings.py b/src/api/settings.pyindex 7c828fc45..5291af5d6 100644--- a/src/api/settings.py+++ b/src/api/settings.py@@ -3,7 +3,6 @@ import platformfrom fastapi import Depends, Request, HTTPExceptionfrom fastapi.responses import JSONResponsefrom pydantic import BaseModel, Field-from utils.container_utils import transform_localhost_urlfrom utils.logging_config import get_loggerfrom utils.telemetry import TelemetryClient, Category, MessageIdfrom utils.version_utils import OPENRAG_VERSION@@ -824,12 +823,12 @@ async def update_settings(flows_service = _get_flows_service()# Update global variables- await _update_langflow_global_variables(current_config)+ await _update_langflow_global_variables(current_config, flows_service=flows_service)# Update LLM client credentials when embedding selection changesif body.embedding_provider is not None or body.embedding_model is not None:await _update_mcp_servers_with_provider_credentials(- current_config, session_manager+ current_config, session_manager, flows_service=flows_service)# Update model values if provider or model changed (including removals that trigger fallback)@@ -1085,11 +1084,12 @@ async def onboarding(# or if existing config has values (for OpenAI/Anthropic that might already be set)if (provider_fields_provided orcurrent_config.providers.openai.api_key != "" or- current_config.providers.anthropic.api_key != ""):- await _update_langflow_global_variables(current_config)+ current_config.providers.anthropic.api_key != "" or+ current_config.providers.any_configured()):+ await _update_langflow_global_variables(current_config, flows_service=flows_service)if body.embedding_provider or body.embedding_model:- await _update_mcp_servers_with_provider_credentials(current_config, session_manager)+ await _update_mcp_servers_with_provider_credentials(current_config, session_manager=session_manager, flows_service=flows_service)# Update model values if provider or model fields were providedif body.llm_provider or body.llm_model or body.embedding_provider or body.embedding_model:@@ -1320,7 +1320,7 @@ def _get_flows_service():return FlowsService()-async def _update_langflow_global_variables(config):+async def _update_langflow_global_variables(config, flows_service=None):"""Update Langflow global variables for all configured providers"""try:# WatsonX global variables@@ -1358,19 +1358,14 @@ async def _update_langflow_global_variables(config):# Ollama global variablesif config.providers.ollama.endpoint:+ if not flows_service:+ flows_service = _get_flows_service()- try:- endpoint = transform_localhost_url(config.providers.ollama.endpoint, is_langflow=True, is_podman=True)- await clients._create_langflow_global_variable(- "OLLAMA_BASE_URL", endpoint, modify=True- )- logger.info("Set OLLAMA_BASE_URL global variable in Langflow (Podman)")- except Exception:- endpoint = transform_localhost_url(config.providers.ollama.endpoint, is_langflow=True, is_podman=False)- await clients._create_langflow_global_variable(- "OLLAMA_BASE_URL", endpoint, modify=True- )- logger.info("Set OLLAMA_BASE_URL global variable in Langflow (Docker)")+ endpoint = await flows_service.resolve_ollama_url(config.providers.ollama.endpoint, force_refresh=True)+ await clients._create_langflow_global_variable(+ "OLLAMA_BASE_URL", endpoint, modify=True+ )+ logger.info("Set OLLAMA_BASE_URL global variable in Langflow")if config.knowledge.embedding_model:await clients._create_langflow_global_variable(@@ -1385,7 +1380,7 @@ async def _update_langflow_global_variables(config):raise-async def _update_mcp_servers_with_provider_credentials(config, session_manager = None):+async def _update_mcp_servers_with_provider_credentials(config, session_manager = None, flows_service=None):# Update MCP servers with provider credentialstry:from services.langflow_mcp_service import LangflowMCPService@@ -1394,7 +1389,7 @@ async def _update_mcp_servers_with_provider_credentials(config, session_managermcp_service = LangflowMCPService()# Build global vars using utility function- mcp_global_vars = build_mcp_global_vars_from_config(config)+ mcp_global_vars = await build_mcp_global_vars_from_config(config, flows_service=flows_service)# In no-auth mode, add the anonymous JWT token and user detailsif is_no_auth_mode() and session_manager:@@ -1557,13 +1552,13 @@ async def reapply_all_settings(session_manager = None):logger.info("Reapplying all settings to Langflow flows and global variables")if config.knowledge.embedding_model or config.knowledge.embedding_provider:- await _update_mcp_servers_with_provider_credentials(config, session_manager)+ await _update_mcp_servers_with_provider_credentials(config, session_manager, flows_service=flows_service)else:logger.info("No embedding model or provider configured, skipping MCP server update")# Update all Langflow settings using helper functionstry:- await _update_langflow_global_variables(config)+ await _update_langflow_global_variables(config, flows_service=flows_service)except Exception as e:logger.error(f"Failed to update Langflow global variables: {str(e)}")# Continue with other updates even if global variables faildiff --git a/src/config/config_manager.py b/src/config/config_manager.pyindex 78f39f853..5f7954715 100644--- a/src/config/config_manager.py+++ b/src/config/config_manager.py@@ -37,6 +37,7 @@ class WatsonXConfig:class OllamaConfig:"""Ollama provider configuration."""endpoint: str = ""+ resolved_endpoint: str = ""configured: bool = False@@ -313,7 +314,7 @@ class ConfigManager:return Trueexcept Exception as e:logger.error(f"Failed to save configuration to {self.config_file}: {e}")- return False+ raise edef update_onboarding_state(self, **kwargs) -> bool:"""Update onboarding state fields.diff --git a/src/main.py b/src/main.pyindex ea3a96045..d264d1959 100644--- a/src/main.py+++ b/src/main.py@@ -1071,7 +1071,8 @@ async def _update_mcp_servers_with_provider_credentials(services):# Build global vars with provider credentials using utility functionfrom utils.langflow_headers import build_mcp_global_vars_from_config- global_vars = build_mcp_global_vars_from_config(config)+ flows_service = services.get("flows_service")+ global_vars = await build_mcp_global_vars_from_config(config, flows_service=flows_service)# In no-auth mode, add the anonymous JWT token and user detailsif is_no_auth_mode() and session_manager:@@ -1248,11 +1249,12 @@ async def initialize_services():document_service = DocumentService(session_manager=session_manager)search_service = SearchService(session_manager)task_service = TaskService(document_service, ingestion_timeout=INGESTION_TIMEOUT)- chat_service = ChatService()flows_service = FlowsService()+ chat_service = ChatService(flows_service=flows_service)knowledge_filter_service = KnowledgeFilterService(session_manager)models_service = ModelsService()monitor_service = MonitorService(session_manager)+ langflow_file_service = LangflowFileService(flows_service=flows_service)# Initialize both connector serviceslangflow_connector_service = LangflowConnectorService(@@ -1277,6 +1279,7 @@ async def initialize_services():auth_service = AuthService(session_manager,connector_service,+ flows_service,langflow_mcp_service=LangflowMCPService(),)@@ -1306,7 +1309,7 @@ async def initialize_services():Category.SERVICE_INITIALIZATION, MessageId.ORB_SVC_INIT_SUCCESS)- langflow_file_service = LangflowFileService()+# API Key service for public API authenticationapi_key_service = APIKeyService(session_manager)diff --git a/src/services/auth_service.py b/src/services/auth_service.pyindex d2fe43971..1adc26522 100644--- a/src/services/auth_service.py+++ b/src/services/auth_service.py@@ -22,10 +22,11 @@ from connectors.sharepoint import SharePointConnectorclass AuthService:- def __init__(self, session_manager: SessionManager, connector_service=None, langflow_mcp_service: LangflowMCPService | None = None):+ def __init__(self, session_manager: SessionManager, connector_service=None, flows_service=None, langflow_mcp_service: LangflowMCPService | None = None):self.session_manager = session_managerself.connector_service = connector_serviceself.used_auth_codes = set() # Track used authorization codes+ self.flows_service = flows_serviceself.langflow_mcp_service = langflow_mcp_serviceself._background_tasks = set()@@ -317,7 +318,7 @@ class AuthService:from utils.langflow_headers import build_mcp_global_vars_from_configconfig = get_openrag_config()- provider_vars = build_mcp_global_vars_from_config(config)+ provider_vars = await build_mcp_global_vars_from_config(config, flows_service=self.flows_service)# Merge provider credentials with user infoglobal_vars.update(provider_vars)diff --git a/src/services/chat_service.py b/src/services/chat_service.pyindex f95b772a0..d0451918a 100644--- a/src/services/chat_service.py+++ b/src/services/chat_service.py@@ -8,6 +8,9 @@ logger = get_logger(__name__)class ChatService:+ def __init__(self, flows_service=None):+ self.flows_service = flows_service+async def chat(self,prompt: str,@@ -78,7 +81,7 @@ class ChatService:extra_headers["X-LANGFLOW-GLOBAL-VAR-SELECTED_EMBEDDING_MODEL"] = embedding_model# Add provider credentials to headers- add_provider_credentials_to_headers(extra_headers, config)+ await add_provider_credentials_to_headers(extra_headers, config, flows_service=self.flows_service)logger.debug(f"[LF] Extra headers {extra_headers}")# Get context variables for filters, limit, and thresholdfrom auth_context import (@@ -203,7 +206,7 @@ class ChatService:extra_headers["X-LANGFLOW-GLOBAL-VAR-SELECTED_EMBEDDING_MODEL"] = embedding_model# Add provider credentials to headers- add_provider_credentials_to_headers(extra_headers, config)+ await add_provider_credentials_to_headers(extra_headers, config, flows_service=self.flows_service)# Build the complete filter expression like the chat service doesfilter_expression = {}@@ -333,7 +336,7 @@ class ChatService:extra_headers["X-LANGFLOW-GLOBAL-VAR-SELECTED_EMBEDDING_MODEL"] = embedding_model# Add provider credentials to headers- add_provider_credentials_to_headers(extra_headers, config)+ await add_provider_credentials_to_headers(extra_headers, config, flows_service=self.flows_service)# Ensure the Langflow client exists; try lazy init if neededlangflow_client = await clients.ensure_langflow_client()diff --git a/src/services/flows_service.py b/src/services/flows_service.pyindex 63dbde6e4..00b381fb4 100644--- a/src/services/flows_service.py+++ b/src/services/flows_service.py@@ -21,6 +21,59 @@ logger = get_logger(__name__)class FlowsService:+ async def resolve_ollama_url(self, endpoint: str, force_refresh: bool = False) -> str:+ """Find the correct Ollama URL by probing candidates via Langflow's validate-provider API."""+ from config.config_manager import config_manager+ config = config_manager.get_config()++ from utils.container_utils import is_localhost_url, replace_localhost_patterns, get_container_host, transform_localhost_url++ # If not forcing, check if we already have a resolved endpoint for this original endpoint+ if not force_refresh and config.providers.ollama.resolved_endpoint:+ # We only use the cached one if the original endpoint is still a localhost one+ if is_localhost_url(endpoint):+ logger.debug(f"Using cached resolved Ollama URL: {config.providers.ollama.resolved_endpoint}")+ return config.providers.ollama.resolved_endpoint++ if not is_localhost_url(endpoint):+ return endpoint++ # Candidates to probe+ candidates = ["host.containers.internal", "host.docker.internal"]+ detected_host = get_container_host()+ if detected_host and detected_host not in candidates:+ candidates.insert(0, detected_host)++ resolved_url = None+ for cand in candidates:+ test_url = replace_localhost_patterns(endpoint, cand)++ logger.debug(f"Probing Ollama candidate via Langflow: {test_url}")+ try:+ response = await clients.langflow_request(+ "POST", "/api/v1/models/validate-provider",+ json={"provider": "Ollama", "variables": {"OLLAMA_BASE_URL": test_url}}+ )+ if response.status_code in (200, 201) and response.json().get("valid"):+ logger.info(f"Resolved Ollama URL via Langflow: {test_url}")+ resolved_url = test_url+ break+ except Exception as e:+ logger.debug(f"Probe failed for {test_url}: {e}")+ continue++ if not resolved_url:+ # Fallback to simple transformation if probing fails+ resolved_url = transform_localhost_url(endpoint)++ # Cache the result if it changed+ if resolved_url and resolved_url != config.providers.ollama.resolved_endpoint:+ config.providers.ollama.resolved_endpoint = resolved_url+ config_manager.save_config_file(config)+ logger.debug(f"Saved resolved Ollama URL to config: {resolved_url}")++ return resolved_url+def __init__(self):# Cache for flow file mappings to avoid repeated filesystem scansself._flow_file_cache = {}diff --git a/src/services/langflow_file_service.py b/src/services/langflow_file_service.pyindex d846b9699..4f08968c6 100644--- a/src/services/langflow_file_service.py+++ b/src/services/langflow_file_service.py@@ -11,8 +11,9 @@ logger = get_logger(__name__)class LangflowFileService:- def __init__(self):+ def __init__(self, flows_service=None):self.flow_id_ingest = LANGFLOW_INGEST_FLOW_ID+ self.flows_service = flows_serviceself.flow_id_url_ingest = LANGFLOW_URL_INGEST_FLOW_ID_TRANSIENT_STATUS_CODES = {408, 429, 500, 502, 503, 504}@@ -182,7 +183,7 @@ class LangflowFileService:)# Add provider credentials as global variables for ingestion- add_provider_credentials_to_headers(headers, config)+ await add_provider_credentials_to_headers(headers, config, flows_service=self.flows_service)logger.info(f"[LF] Headers {headers}")logger.info(f"[LF] Payload {payload}")resp = await clients.langflow_request(@@ -295,7 +296,7 @@ class LangflowFileService:"X-Langflow-Global-Var-ALLOWED_USERS": json.dumps( []),"X-Langflow-Global-Var-ALLOWED_GROUPS": json.dumps( []),}- add_provider_credentials_to_headers(headers, config)+ await add_provider_credentials_to_headers(headers, config, flows_service=self.flows_service)logger.info(diff --git a/src/services/models_service.py b/src/services/models_service.pyindex 1bc77d833..01797791e 100644--- a/src/services/models_service.py+++ b/src/services/models_service.py@@ -169,7 +169,6 @@ class ModelsService:) -> Dict[str, List[Dict[str, str]]]:"""Fetch available models from Ollama API with tool calling capabilities for language models"""try:- # Use provided endpoint or defaultollama_url = transform_localhost_url(endpoint)# API endpointsdiff --git a/src/utils/container_utils.py b/src/utils/container_utils.pyindex f0f14ccbb..f54d699cf 100644--- a/src/utils/container_utils.py+++ b/src/utils/container_utils.py@@ -102,45 +102,35 @@ def get_container_host() -> str | None:return None-def transform_localhost_url(url: str, is_langflow: bool = False, is_podman: bool = True) -> str:- """Transform localhost URLs to container-accessible hosts when running in a container.+def is_localhost_url(url: str) -> bool:+ """Check if the URL contains a localhost pattern."""+ localhost_patterns = ["localhost", "127.0.0.1"]+ return any(pattern in url for pattern in localhost_patterns)- Automatically detects if running inside a container and finds the appropriate host- address to replace localhost/127.0.0.1. Tries in order:- - host.docker.internal (if resolvable)- - host.containers.internal (if resolvable)- - Gateway IP from routing table (fallback)+def replace_localhost_patterns(url: str, replacement: str) -> str:+ """Replace localhost patterns in a URL with a given string."""+ localhost_patterns = ["localhost", "127.0.0.1"]+ for pattern in localhost_patterns:+ url = url.replace(pattern, replacement)+ return url++def transform_localhost_url(url: str) -> str:+ """Transform localhost URLs to container-accessible hostnames.Args:url: The original URL- is_langflow: Detect the container host for the langflow container, not the current one, if True.- is_podman: Use host.containers.internal instead of host.docker.internal if True.Returns:- Transformed URL with container-accessible host if applicable, otherwise the original URL.-- Example:- >>> transform_localhost_url("http://localhost:5001")- # Returns "http://host.docker.internal:5001" if running in Docker and hostname resolves- # Returns "http://172.17.0.1:5001" if running in Docker on Linux (gateway IP fallback)- # Returns "http://localhost:5001" if not in a container+ Transformed URL with container-host if applicable, otherwise original URL."""- if is_langflow :- container_host = "host.containers.internal" if is_podman else "host.docker.internal"- else:- container_host = get_container_host()+ if not is_localhost_url(url):+ return url+ container_host = get_container_host()if not container_host:return url- # Replace localhost and 127.0.0.1 with the container host- localhost_patterns = ["localhost", "127.0.0.1"]-- for pattern in localhost_patterns:- if pattern in url:- return url.replace(pattern, container_host)-- return url+ return replace_localhost_patterns(url, container_host)def guess_host_ip_for_containers(logger=None) -> str:diff --git a/src/utils/embedding_fields.py b/src/utils/embedding_fields.pyindex 27469f374..388228e95 100644--- a/src/utils/embedding_fields.py+++ b/src/utils/embedding_fields.py@@ -113,8 +113,11 @@ async def ensure_embedding_field_exists()return {}- properties = mapping.get(index_name, {}).get("mappings", {}).get("properties", {})- return properties.get(field_name, {}) if isinstance(properties, dict) else {}+ for index_info in mapping.values():+ properties = index_info.get("mappings", {}).get("properties", {})+ if isinstance(properties, dict) and field_name in properties:+ return properties[field_name]+ return {}existing_definition = await _get_field_definition()if existing_definition:@@ -160,6 +163,16 @@ async def ensure_embedding_field_exists(model_name=model_name,)except Exception as e:+ # Check if the field was created by a concurrent request+ check_def = await _get_field_definition()+ if check_def.get("type") == "knn_vector":+ logger.info(+ "Embedding field already exists as knn_vector (race condition handled)",+ field_name=field_name,+ model_name=model_name,+ )+ return field_name+logger.error("Failed to add embedding field mapping",field_name=field_name,diff --git a/src/utils/langflow_headers.py b/src/utils/langflow_headers.pyindex dc21d619a..df7ced01f 100644--- a/src/utils/langflow_headers.py+++ b/src/utils/langflow_headers.py@@ -4,12 +4,13 @@ from typing import Dictfrom utils.container_utils import transform_localhost_url-def add_provider_credentials_to_headers(headers: Dict[str, str], config) -> None:+async def add_provider_credentials_to_headers(headers: Dict[str, str], config, flows_service=None) -> None:"""Add provider credentials to headers as Langflow global variables.Args:headers: Dictionary of headers to add credentials toconfig: OpenRAGConfig object containing provider configurations+ flows_service: Optional FlowsService instance to resolve Ollama URLs."""# Add OpenAI credentialsif config.providers.openai.api_key:@@ -28,15 +29,19 @@ def add_provider_credentials_to_headers(headers: Dict[str, str], config) -> None# Add Ollama endpoint (with localhost transformation)if config.providers.ollama.endpoint:- ollama_endpoint = transform_localhost_url(config.providers.ollama.endpoint, is_langflow=True)+ if flows_service:+ ollama_endpoint = await flows_service.resolve_ollama_url(config.providers.ollama.endpoint)+ else:+ ollama_endpoint = transform_localhost_url(config.providers.ollama.endpoint)headers["X-LANGFLOW-GLOBAL-VAR-OLLAMA_BASE_URL"] = str(ollama_endpoint)-def build_mcp_global_vars_from_config(config) -> Dict[str, str]:+async def build_mcp_global_vars_from_config(config, flows_service=None) -> Dict[str, str]:"""Build MCP global variables dictionary from OpenRAG configuration.Args:config: OpenRAGConfig object containing provider configurations+ flows_service: Optional FlowsService instance to resolve Ollama URLs.Returns:Dictionary of global variables for MCP servers (without X-Langflow-Global-Var prefix)@@ -60,7 +65,10 @@ def build_mcp_global_vars_from_config(config) -> Dict[str, str]:# Add Ollama endpoint (with localhost transformation)if config.providers.ollama.endpoint:- ollama_endpoint = transform_localhost_url(config.providers.ollama.endpoint, is_langflow=True)+ if flows_service:+ ollama_endpoint = await flows_service.resolve_ollama_url(config.providers.ollama.endpoint)+ else:+ ollama_endpoint = transform_localhost_url(config.providers.ollama.endpoint)global_vars["OLLAMA_BASE_URL"] = ollama_endpoint# Add selected embedding model