OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
Revert "check ollama url by probing Langflow validate function, store probed in cache"
Lucas Oliveira04d3ba95c469
diff --git a/src/api/settings.py b/src/api/settings.pyindex b22bbd58e..c89695311 100644--- a/src/api/settings.py+++ b/src/api/settings.py@@ -3,6 +3,7 @@ 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 config.settings import (@@ -810,12 +811,12 @@ async def update_settings(flows_service = _get_flows_service()# Update global variables- await _update_langflow_global_variables(current_config, flows_service=flows_service)+ await _update_langflow_global_variables(current_config)# 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, flows_service=flows_service+ current_config, session_manager)# Update model values if provider or model changed (including removals that trigger fallback)@@ -1071,12 +1072,11 @@ 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 != "" or- current_config.providers.any_configured()):- await _update_langflow_global_variables(current_config, flows_service=flows_service)+ current_config.providers.anthropic.api_key != ""):+ await _update_langflow_global_variables(current_config)if body.embedding_provider or body.embedding_model:- await _update_mcp_servers_with_provider_credentials(current_config, session_manager=session_manager, flows_service=flows_service)+ await _update_mcp_servers_with_provider_credentials(current_config, session_manager)# 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:@@ -1293,7 +1293,7 @@ def _get_flows_service():return FlowsService()-async def _update_langflow_global_variables(config, flows_service=None):+async def _update_langflow_global_variables(config):"""Update Langflow global variables for all configured providers"""try:# WatsonX global variables@@ -1331,14 +1331,19 @@ async def _update_langflow_global_variables(config, flows_service=None):# Ollama global variablesif config.providers.ollama.endpoint:- if not flows_service:- flows_service = _get_flows_service()- endpoint = await flows_service.resolve_ollama_url(config.providers.ollama.endpoint, force=True)- await clients._create_langflow_global_variable(- "OLLAMA_BASE_URL", endpoint, modify=True- )- logger.info("Set OLLAMA_BASE_URL global variable in Langflow")+ 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)")if config.knowledge.embedding_model:await clients._create_langflow_global_variable(@@ -1353,7 +1358,7 @@ async def _update_langflow_global_variables(config, flows_service=None):raise-async def _update_mcp_servers_with_provider_credentials(config, session_manager = None, flows_service=None):+async def _update_mcp_servers_with_provider_credentials(config, session_manager = None):# Update MCP servers with provider credentialstry:from services.langflow_mcp_service import LangflowMCPService@@ -1362,7 +1367,7 @@ async def _update_mcp_servers_with_provider_credentials(config, session_managermcp_service = LangflowMCPService()# Build global vars using utility function- mcp_global_vars = await build_mcp_global_vars_from_config(config, flows_service=flows_service)+ mcp_global_vars = build_mcp_global_vars_from_config(config)# In no-auth mode, add the anonymous JWT token and user detailsif is_no_auth_mode() and session_manager:@@ -1509,13 +1514,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, flows_service=flows_service)+ await _update_mcp_servers_with_provider_credentials(config, session_manager)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, flows_service=flows_service)+ await _update_langflow_global_variables(config)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 bbf453ee0..8b3bb38b8 100644--- a/src/config/config_manager.py+++ b/src/config/config_manager.py@@ -37,7 +37,6 @@ class WatsonXConfig:class OllamaConfig:"""Ollama provider configuration."""endpoint: str = ""- resolved_endpoint: str = ""configured: bool = Falsediff --git a/src/main.py b/src/main.pyindex f6d564de1..76fbf0cf8 100644--- a/src/main.py+++ b/src/main.py@@ -493,8 +493,7 @@ 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- flows_service = services.get("flows_service")- global_vars = await build_mcp_global_vars_from_config(config, flows_service=flows_service)+ global_vars = build_mcp_global_vars_from_config(config)# In no-auth mode, add the anonymous JWT token and user detailsif is_no_auth_mode() and session_manager:@@ -616,12 +615,11 @@ 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(@@ -671,7 +669,7 @@ async def initialize_services():await TelemetryClient.send_event(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/chat_service.py b/src/services/chat_service.pyindex 5dfedee4f..d2bfa8b8f 100644--- a/src/services/chat_service.py+++ b/src/services/chat_service.py@@ -8,9 +8,6 @@ logger = get_logger(__name__)class ChatService:- def __init__(self, flows_service=None):- self.flows_service = flows_service-async def chat(self,prompt: str,@@ -81,7 +78,7 @@ class ChatService:extra_headers["X-LANGFLOW-GLOBAL-VAR-SELECTED_EMBEDDING_MODEL"] = embedding_model# Add provider credentials to headers- await add_provider_credentials_to_headers(extra_headers, config, flows_service=self.flows_service)+ add_provider_credentials_to_headers(extra_headers, config)logger.debug(f"[LF] Extra headers {extra_headers}")# Get context variables for filters, limit, and thresholdfrom auth_context import (@@ -205,7 +202,7 @@ class ChatService:extra_headers["X-LANGFLOW-GLOBAL-VAR-SELECTED_EMBEDDING_MODEL"] = embedding_model# Add provider credentials to headers- await add_provider_credentials_to_headers(extra_headers, config, langflow_request_func=clients.langflow_request)+ add_provider_credentials_to_headers(extra_headers, config)# Build the complete filter expression like the chat service doesfilter_expression = {}@@ -334,7 +331,7 @@ class ChatService:extra_headers["X-LANGFLOW-GLOBAL-VAR-SELECTED_EMBEDDING_MODEL"] = embedding_model# Add provider credentials to headers- await add_provider_credentials_to_headers(extra_headers, config, langflow_request_func=clients.langflow_request)+ add_provider_credentials_to_headers(extra_headers, config)# 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 674b6cd7f..d770b47fc 100644--- a/src/services/flows_service.py+++ b/src/services/flows_service.py@@ -21,64 +21,6 @@ logger = get_logger(__name__)class FlowsService:- async def resolve_ollama_url(self, endpoint: str, force: 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()-- # If not forcing, check if we already have a resolved endpoint for this original endpoint- if not force and config.providers.ollama.resolved_endpoint:- # We only use the cached one if the original endpoint is still a localhost one- # and the cached one actually looks like a transformed version of it- localhost_patterns = ["localhost", "127.0.0.1"]- if any(p in endpoint for p in localhost_patterns):- logger.debug(f"Using cached resolved Ollama URL: {config.providers.ollama.resolved_endpoint}")- return config.providers.ollama.resolved_endpoint-- localhost_patterns = ["localhost", "127.0.0.1"]- if not any(p in endpoint for p in localhost_patterns):- return endpoint-- # Candidates to probe- candidates = ["host.containers.internal", "host.docker.internal"]- from utils.container_utils import get_container_host- 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 = endpoint- for p in localhost_patterns:- test_url = test_url.replace(p, 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- from utils.container_utils import transform_localhost_url- 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 00a05227f..a569b4d9f 100644--- a/src/services/langflow_file_service.py+++ b/src/services/langflow_file_service.py@@ -8,9 +8,8 @@ logger = get_logger(__name__)class LangflowFileService:- def __init__(self, flows_service=None):+ def __init__(self):self.flow_id_ingest = LANGFLOW_INGEST_FLOW_ID- self.flows_service = flows_serviceasync def upload_user_file(self, file_tuple, jwt_token: Optional[str] = None@@ -162,7 +161,7 @@ class LangflowFileService:)# Add provider credentials as global variables for ingestion- await add_provider_credentials_to_headers(headers, config, flows_service=self.flows_service)+ add_provider_credentials_to_headers(headers, config)logger.info(f"[LF] Headers {headers}")logger.info(f"[LF] Payload {payload}")resp = await clients.langflow_request(diff --git a/src/services/models_service.py b/src/services/models_service.pyindex 01797791e..1bc77d833 100644--- a/src/services/models_service.py+++ b/src/services/models_service.py@@ -169,6 +169,7 @@ 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 ac1d9bd8e..f0f14ccbb 100644--- a/src/utils/container_utils.py+++ b/src/utils/container_utils.py@@ -102,24 +102,40 @@ def get_container_host() -> str | None:return None-def transform_localhost_url(url: str) -> str:- """Transform localhost URLs to container-accessible hostnames.+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.++ 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)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-host if applicable, otherwise original URL.+ 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"""- localhost_patterns = ["localhost", "127.0.0.1"]- if not any(pattern in url for pattern in localhost_patterns):- return url+ if is_langflow :+ container_host = "host.containers.internal" if is_podman else "host.docker.internal"+ else:+ container_host = get_container_host()- container_host = get_container_host()if not container_host:return url- # Replace localhost patterns with the container host+ # 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)diff --git a/src/utils/langflow_headers.py b/src/utils/langflow_headers.pyindex 6d3348ff5..bc8dac48f 100644--- a/src/utils/langflow_headers.py+++ b/src/utils/langflow_headers.py@@ -4,13 +4,12 @@ from typing import Dictfrom utils.container_utils import transform_localhost_url-async def add_provider_credentials_to_headers(headers: Dict[str, str], config, flows_service=None) -> None:+def add_provider_credentials_to_headers(headers: Dict[str, str], config) -> 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:@@ -29,19 +28,15 @@ async def add_provider_credentials_to_headers(headers: Dict[str, str], config, f# Add Ollama endpoint (with localhost transformation)if config.providers.ollama.endpoint:- 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)+ ollama_endpoint = transform_localhost_url(config.providers.ollama.endpoint, is_langflow=True)headers["X-LANGFLOW-GLOBAL-VAR-OLLAMA_BASE_URL"] = str(ollama_endpoint)-async def build_mcp_global_vars_from_config(config, flows_service=None) -> Dict[str, str]:+def build_mcp_global_vars_from_config(config) -> 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)@@ -65,10 +60,7 @@ async def build_mcp_global_vars_from_config(config, flows_service=None) -> Dict[# Add Ollama endpoint (with localhost transformation)if config.providers.ollama.endpoint:- 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)+ ollama_endpoint = transform_localhost_url(config.providers.ollama.endpoint, is_langflow=True)global_vars["OLLAMA_BASE_URL"] = ollama_endpoint# Add selected embedding model