diff --git a/main/xiaozhi-server/core/providers/llm/xinference/xinference.py b/main/xiaozhi-server/core/providers/llm/xinference/xinference.py index b44ce0231..cc307cb75 100644 --- a/main/xiaozhi-server/core/providers/llm/xinference/xinference.py +++ b/main/xiaozhi-server/core/providers/llm/xinference/xinference.py @@ -10,7 +10,9 @@ logger = setup_logging() class LLMProvider(LLMProviderBase): def __init__(self, config): self.model_name = config.get("model_name") - self.base_url = config.get("base_url", "http://localhost:9997") + self.base_url = ( + str(config.get("base_url") or "").strip() or "http://localhost:9997" + ).rstrip("/") # Initialize OpenAI client with Xinference base URL # 如果没有v1,增加v1 if not self.base_url.endswith("/v1"): diff --git a/main/xiaozhi-server/tests/core/providers/llm/__init__.py b/main/xiaozhi-server/tests/core/providers/llm/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/main/xiaozhi-server/tests/core/providers/llm/test_xinference_base_url.py b/main/xiaozhi-server/tests/core/providers/llm/test_xinference_base_url.py new file mode 100644 index 000000000..63ebb213c --- /dev/null +++ b/main/xiaozhi-server/tests/core/providers/llm/test_xinference_base_url.py @@ -0,0 +1,24 @@ +import pytest + +from core.providers.llm.xinference.xinference import LLMProvider + + +@pytest.mark.parametrize( + "base_url", + [ + "http://localhost:9997", + "http://localhost:9997/", + "http://localhost:9997/v1", + "http://localhost:9997/v1/", + " http://localhost:9997/v1 ", + ], +) +def test_base_url_points_at_v1(base_url): + provider = LLMProvider({"model_name": "qwen", "base_url": base_url}) + assert str(provider.client.base_url) == "http://localhost:9997/v1/" + + +@pytest.mark.parametrize("base_url", [None, ""]) +def test_missing_base_url_uses_default(base_url): + provider = LLMProvider({"model_name": "qwen", "base_url": base_url}) + assert str(provider.client.base_url) == "http://localhost:9997/v1/"