Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

check ollama url by probing Langflow validate function, store probed in cache

Lucas Oliveira9c10fa62a4e1
diff --git a/src/api/settings.py b/src/api/settings.py
index c89695311..b22bbd58e 100644
--- a/src/api/settings.py
+++ b/src/api/settings.py
@@ -3,7 +3,6 @@ import platform
from fastapi import Depends, Request, HTTPException
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
-from utils.container_utils import transform_localhost_url
from utils.logging_config import get_logger
from utils.telemetry import TelemetryClient, Category, MessageId
from config.settings import (
@@ -811,12 +810,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 changes
if 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)
@@ -1072,11 +1071,12 @@ async def onboarding(
# or if existing config has values (for OpenAI/Anthropic that might already be set)
if (provider_fields_provided or
current_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 provided
if 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):
+async def _update_langflow_global_variables(config, flows_service=None):
"""Update Langflow global variables for all configured providers"""
try:
# WatsonX global variables
@@ -1331,19 +1331,14 @@ async def _update_langflow_global_variables(config):
# Ollama global variables
if 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=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(
@@ -1358,7 +1353,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 credentials
try:
from services.langflow_mcp_service import LangflowMCPService
@@ -1367,7 +1362,7 @@ async def _update_mcp_servers_with_provider_credentials(config, session_manager
mcp_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 details
if is_no_auth_mode() and session_manager:
@@ -1514,13 +1509,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 functions
try:
- 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 fail
diff --git a/src/config/config_manager.py b/src/config/config_manager.py
index 8b3bb38b8..bbf453ee0 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
diff --git a/src/main.py b/src/main.py
index 76fbf0cf8..f6d564de1 100644
--- a/src/main.py
+++ b/src/main.py
@@ -493,7 +493,8 @@ async def _update_mcp_servers_with_provider_credentials(services):
# Build global vars with provider credentials using utility function
from 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 details
if is_no_auth_mode() and session_manager:
@@ -615,11 +616,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 services
langflow_connector_service = LangflowConnectorService(
@@ -669,7 +671,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 authentication
api_key_service = APIKeyService(session_manager)
diff --git a/src/services/chat_service.py b/src/services/chat_service.py
index d2bfa8b8f..5dfedee4f 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 threshold
from auth_context import (
@@ -202,7 +205,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, langflow_request_func=clients.langflow_request)
# Build the complete filter expression like the chat service does
filter_expression = {}
@@ -331,7 +334,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, langflow_request_func=clients.langflow_request)
# Ensure the Langflow client exists; try lazy init if needed
langflow_client = await clients.ensure_langflow_client()
diff --git a/src/services/flows_service.py b/src/services/flows_service.py
index d770b47fc..674b6cd7f 100644
--- a/src/services/flows_service.py
+++ b/src/services/flows_service.py
@@ -21,6 +21,64 @@ 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 scans
self._flow_file_cache = {}
diff --git a/src/services/langflow_file_service.py b/src/services/langflow_file_service.py
index a569b4d9f..00a05227f 100644
--- a/src/services/langflow_file_service.py
+++ b/src/services/langflow_file_service.py
@@ -8,8 +8,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_service
async def upload_user_file(
self, file_tuple, jwt_token: Optional[str] = None
@@ -161,7 +162,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(
diff --git a/src/services/models_service.py b/src/services/models_service.py
index 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 default
ollama_url = transform_localhost_url(endpoint)
# API endpoints
diff --git a/src/utils/container_utils.py b/src/utils/container_utils.py
index f0f14ccbb..ac1d9bd8e 100644
--- a/src/utils/container_utils.py
+++ b/src/utils/container_utils.py
@@ -102,40 +102,24 @@ 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.
-
- 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 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()
+ localhost_patterns = ["localhost", "127.0.0.1"]
+ if not any(pattern in url for pattern in localhost_patterns):
+ 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"]
-
+ # Replace localhost patterns with the container host
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.py
index bc8dac48f..6d3348ff5 100644
--- a/src/utils/langflow_headers.py
+++ b/src/utils/langflow_headers.py
@@ -4,12 +4,13 @@ from typing import Dict
from 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 to
config: OpenRAGConfig object containing provider configurations
+ flows_service: Optional FlowsService instance to resolve Ollama URLs.
"""
# Add OpenAI credentials
if 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