利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
Improve Ollama defaults for Docker
harry0703536efa397f1c
diff --git a/app/config/config.py b/app/config/config.pyindex 58545726..7a90a967 100644--- a/app/config/config.py+++ b/app/config/config.py@@ -7,6 +7,121 @@ from loguru import loggerroot_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))config_file = f"{root_dir}/config.toml"+_CONTAINER_CGROUP_MARKERS = ("docker", "containerd", "kubepods", "libpod", "podman")+_DOCKER_HOST_GATEWAY_NAME = "host.docker.internal"+++def is_running_in_container(+ dockerenv_path: str = "/.dockerenv",+ containerenv_path: str = "/run/.containerenv",+ cgroup_path: str = "/proc/1/cgroup",+) -> bool:+ """+ 判断当前进程是否运行在容器内。++ 这个判断主要用于 Ollama 默认地址选择:+ - 普通本机运行时,`localhost` 指向用户机器本身;+ - Docker 容器内,`localhost` 指向容器自己,访问宿主机 Ollama+ 通常需要使用 `host.docker.internal`。++ 不能只判断 `/proc/1/cgroup` 是否存在,因为普通 Linux 也会有这个文件。+ 这里只在检测到明确的容器标记时返回 True,避免误伤非 Docker Linux 用户。+ 参数保留为可注入路径,便于单元测试覆盖不同运行环境。+ """+ if os.path.isfile(dockerenv_path) or os.path.isfile(containerenv_path):+ return True++ try:+ with open(cgroup_path, mode="r", encoding="utf-8") as fp:+ cgroup_content = fp.read().lower()+ except OSError:+ return False++ return any(marker in cgroup_content for marker in _CONTAINER_CGROUP_MARKERS)+++def _can_resolve_hostname(hostname: str) -> bool:+ try:+ socket.gethostbyname(hostname)+ except OSError:+ return False+ return True+++def _decode_linux_route_gateway(hex_gateway: str) -> str:+ # /proc/net/route 里的 Gateway 是 16 进制小端序,例如 010011AC 表示+ # 172.17.0.1。这里单独解析,是为了在原生 Linux Docker 没有+ # host.docker.internal DNS 记录时,还能尝试访问容器默认网关上的宿主机。+ if len(hex_gateway) != 8:+ raise ValueError("invalid gateway length")++ octets = [+ str(int(hex_gateway[index : index + 2], 16))+ for index in range(6, -1, -2)+ ]+ return ".".join(octets)+++def get_container_default_gateway_ip(route_path: str = "/proc/net/route") -> str:+ """+ 读取 Linux 容器里的默认网关 IP。++ Docker Desktop 通常提供 `host.docker.internal`,但原生 Linux Docker+ 默认不一定提供这个 DNS 名称。默认网关通常可以作为访问宿主机服务的+ 兜底地址;如果用户的 Ollama 只监听 127.0.0.1,则仍需要用户让+ Ollama 监听宿主机网卡或手动配置 `ollama_base_url`。+ """+ try:+ with open(route_path, mode="r", encoding="utf-8") as fp:+ route_lines = fp.readlines()+ except OSError:+ return ""++ for line in route_lines[1:]:+ fields = line.strip().split()+ if len(fields) < 3:+ continue++ destination = fields[1]+ gateway = fields[2]+ if destination != "00000000" or gateway == "00000000":+ continue++ try:+ return _decode_linux_route_gateway(gateway)+ except ValueError:+ logger.warning(f"invalid container gateway route entry: {line.strip()}")+ return ""++ return ""+++def get_default_ollama_base_url() -> str:+ """+ 返回 Ollama 的默认 OpenAI-compatible base_url。++ 用户显式配置 `ollama_base_url` 时不会走这里;这里只处理“未配置时的+ 最佳默认值”。容器内默认指向宿主机,普通本机运行默认指向 localhost。+ """+ if not is_running_in_container():+ return "http://localhost:11434/v1"++ if _can_resolve_hostname(_DOCKER_HOST_GATEWAY_NAME):+ return f"http://{_DOCKER_HOST_GATEWAY_NAME}:11434/v1"++ gateway_ip = get_container_default_gateway_ip()+ if gateway_ip:+ logger.info(+ "host.docker.internal is not resolvable, fallback to container "+ f"default gateway for Ollama: {gateway_ip}"+ )+ return f"http://{gateway_ip}:11434/v1"++ logger.warning(+ "failed to resolve host.docker.internal and container default gateway; "+ "fallback to host.docker.internal for Ollama"+ )+ return f"http://{_DOCKER_HOST_GATEWAY_NAME}:11434/v1"def load_config():diff --git a/app/services/llm.py b/app/services/llm.pyindex 8305dff1..243f917f 100644--- a/app/services/llm.py+++ b/app/services/llm.py@@ -99,7 +99,7 @@ def _generate_response(prompt: str) -> str:model_name = config.app.get("ollama_model_name")base_url = config.app.get("ollama_base_url", "")if not base_url:- base_url = "http://localhost:11434/v1"+ base_url = config.get_default_ollama_base_url()elif llm_provider == "openai":api_key = config.app.get("openai_api_key")model_name = config.app.get("openai_model_name")diff --git a/test/services/test_llm.py b/test/services/test_llm.pyindex 72eb3a0b..35b1a15a 100644--- a/test/services/test_llm.py+++ b/test/services/test_llm.py@@ -1,5 +1,6 @@import osimport sys+import tempfileimport typesimport unittestfrom pathlib import Path@@ -117,6 +118,89 @@ class TestLiteLLMProvider(unittest.TestCase):self.assertIn("api_key is not set", result)self.assertNotIn("litellm", result.lower())+ def _use_ollama_provider(self, base_url=""):+ config.app["llm_provider"] = "ollama"+ config.app["ollama_api_key"] = ""+ config.app["ollama_base_url"] = base_url+ config.app["ollama_model_name"] = "llama3"++ def _assert_ollama_base_url(self, expected_base_url: str):+ class FakeCompletions:+ def create(self, **kwargs):+ self.kwargs = kwargs+ message = types.SimpleNamespace(content="hello\nollama")+ choice = types.SimpleNamespace(message=message)+ return types.SimpleNamespace(choices=[choice])++ fake_completions = FakeCompletions()+ fake_client = types.SimpleNamespace(+ chat=types.SimpleNamespace(completions=fake_completions)+ )++ with (+ patch.object(llm, "OpenAI", return_value=fake_client) as openai_client,+ patch.object(llm, "ChatCompletion", types.SimpleNamespace),+ ):+ result = llm._generate_response("Say hello")++ openai_client.assert_called_once_with(+ api_key="ollama",+ base_url=expected_base_url,+ )+ self.assertEqual(+ fake_completions.kwargs,+ {+ "model": "llama3",+ "messages": [{"role": "user", "content": "Say hello"}],+ },+ )+ self.assertEqual(result, "helloollama")++ def test_ollama_default_base_url_uses_localhost_outside_container(self):+ """+ 普通本机运行时,Ollama 默认仍然使用 localhost,避免影响已有用户。+ """+ self._use_ollama_provider()++ with patch.object(config, "is_running_in_container", return_value=False):+ self._assert_ollama_base_url("http://localhost:11434/v1")++ def test_ollama_default_base_url_uses_host_gateway_inside_container(self):+ """+ 容器内运行时,localhost 指向容器自身;默认改为 host.docker.internal,+ 方便 Docker Desktop 用户访问宿主机上的 Ollama。+ """+ self._use_ollama_provider()++ with (+ patch.object(config, "is_running_in_container", return_value=True),+ patch.object(config, "_can_resolve_hostname", return_value=True),+ ):+ self._assert_ollama_base_url("http://host.docker.internal:11434/v1")++ def test_ollama_default_base_url_falls_back_to_container_gateway(self):+ """+ 原生 Linux Docker 里不一定能解析 host.docker.internal。此时使用容器+ 默认网关作为兜底地址,比直接返回不可解析的 hostname 更稳。+ """+ self._use_ollama_provider()++ with (+ patch.object(config, "is_running_in_container", return_value=True),+ patch.object(config, "_can_resolve_hostname", return_value=False),+ patch.object(config, "get_container_default_gateway_ip", return_value="172.17.0.1"),+ ):+ self._assert_ollama_base_url("http://172.17.0.1:11434/v1")++ def test_ollama_explicit_base_url_takes_precedence(self):+ """+ 用户手动配置的 ollama_base_url 优先级最高,不受容器检测影响。+ """+ self._use_ollama_provider(base_url="http://ollama:11434/v1")++ with patch.object(config, "is_running_in_container", return_value=True):+ self._assert_ollama_base_url("http://ollama:11434/v1")+def test_mimo_provider_uses_openai_compatible_client(self):"""MiMo 官方接口兼容 OpenAI Chat Completions 协议。这里用 fake OpenAI@@ -159,7 +243,6 @@ class TestLiteLLMProvider(unittest.TestCase):)self.assertEqual(result, "hellomimo")-def test_azure_provider_uses_azure_client_directly(self):"""Azure OpenAI 的鉴权、endpoint 和 api-version 都由 AzureOpenAI 客户端处理。@@ -246,6 +329,78 @@ class TestLiteLLMProvider(unittest.TestCase):self.assertIn("g4f package is not installed by default", result)+class TestRuntimeEnvironmentDetection(unittest.TestCase):+ def test_container_detection_ignores_plain_linux_cgroup_file(self):+ """+ 普通 Linux 也有 /proc/1/cgroup,不能因为文件存在就判定为容器。+ """+ with tempfile.TemporaryDirectory() as tmp_dir:+ cgroup_path = Path(tmp_dir) / "cgroup"+ cgroup_path.write_text("0::/init.scope\n", encoding="utf-8")++ self.assertFalse(+ config.is_running_in_container(+ dockerenv_path=str(Path(tmp_dir) / "missing-dockerenv"),+ containerenv_path=str(Path(tmp_dir) / "missing-containerenv"),+ cgroup_path=str(cgroup_path),+ )+ )++ def test_container_detection_accepts_dockerenv_marker(self):+ with tempfile.TemporaryDirectory() as tmp_dir:+ dockerenv_path = Path(tmp_dir) / ".dockerenv"+ dockerenv_path.write_text("", encoding="utf-8")++ self.assertTrue(+ config.is_running_in_container(+ dockerenv_path=str(dockerenv_path),+ containerenv_path=str(Path(tmp_dir) / "missing-containerenv"),+ cgroup_path=str(Path(tmp_dir) / "missing-cgroup"),+ )+ )++ def test_container_detection_accepts_cgroup_container_marker(self):+ with tempfile.TemporaryDirectory() as tmp_dir:+ cgroup_path = Path(tmp_dir) / "cgroup"+ cgroup_path.write_text(+ "0::/system.slice/docker-abcdef.scope\n",+ encoding="utf-8",+ )++ self.assertTrue(+ config.is_running_in_container(+ dockerenv_path=str(Path(tmp_dir) / "missing-dockerenv"),+ containerenv_path=str(Path(tmp_dir) / "missing-containerenv"),+ cgroup_path=str(cgroup_path),+ )+ )++ def test_container_gateway_ip_decodes_default_route(self):+ with tempfile.TemporaryDirectory() as tmp_dir:+ route_path = Path(tmp_dir) / "route"+ route_path.write_text(+ "Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\tMTU\tWindow\tIRTT\n"+ "eth0\t00000000\t010011AC\t0003\t0\t0\t0\t00000000\t0\t0\t0\n",+ encoding="utf-8",+ )++ self.assertEqual(+ config.get_container_default_gateway_ip(str(route_path)),+ "172.17.0.1",+ )++ def test_container_gateway_ip_ignores_missing_default_route(self):+ with tempfile.TemporaryDirectory() as tmp_dir:+ route_path = Path(tmp_dir) / "route"+ route_path.write_text(+ "Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\tMTU\tWindow\tIRTT\n"+ "eth0\t0011AC0A\t00000000\t0001\t0\t0\t0\t00FFFFFF\t0\t0\t0\n",+ encoding="utf-8",+ )++ self.assertEqual(config.get_container_default_gateway_ip(str(route_path)), "")++FOUNDRY_KEY = os.environ.get("ANTHROPIC_FOUNDRY_API_KEY", "")FOUNDRY_BASE = "https://amanrai-test-resource.services.ai.azure.com/anthropic"FOUNDRY_MODEL = "azure_ai/claude-sonnet-4-6"diff --git a/webui/Main.py b/webui/Main.pyindex 34188e63..8d5e16f0 100644--- a/webui/Main.py+++ b/webui/Main.py@@ -284,15 +284,18 @@ if not config.app.get("hide_config", False):if not llm_model_name:llm_model_name = "qwen:7b"if not llm_base_url:- llm_base_url = "http://localhost:11434/v1"+ llm_base_url = config.get_default_ollama_base_url()with llm_helper:- tips = """+ docker_hint = ""+ if config.is_running_in_container():+ docker_hint = "\n > 检测到容器环境,未配置 Base Url 时会默认使用 `http://host.docker.internal:11434/v1`\n"+ tips = f"""##### Ollama配置说明- **API Key**: 随便填写,比如 123- **Base Url**: 一般为 http://localhost:11434/v1- 如果 `MoneyPrinterTurbo` 和 `Ollama` **不在同一台机器上**,需要填写 `Ollama` 机器的IP地址- - 如果 `MoneyPrinterTurbo` 是 `Docker` 部署,建议填写 `http://host.docker.internal:11434/v1`+ - 如果 `MoneyPrinterTurbo` 是 `Docker` 部署,建议填写 `http://host.docker.internal:11434/v1`{docker_hint}- **Model Name**: 使用 `ollama list` 查看,比如 `qwen:7b`"""