From 05aa5c210415424c264450527dd40e858506661c Mon Sep 17 00:00:00 2001 From: octo-patch Date: Sat, 11 Apr 2026 10:37:17 +0800 Subject: [PATCH] fix: correct LM Studio URL parsing and uvicorn port binding (fixes #394) - lm_studio_fn: use urlparse to extract port from server_address so that addresses with an http:// prefix (e.g. http://127.0.0.1:1234, as documented in README) no longer break the Docker URL construction. Previously split(":")[1] returned "//127.0.0.1" instead of the port number, producing a malformed URL. Also fix the non-Docker path to avoid prepending http:// when the address already includes a scheme. - api.py: pass the computed port variable to uvicorn.run() instead of the hardcoded literal 7777, so that BACKEND_PORT environment variable is actually honoured when starting the server directly. --- api.py | 2 +- sources/llm_provider.py | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/api.py b/api.py index 56d3154..080a485 100755 --- a/api.py +++ b/api.py @@ -299,4 +299,4 @@ if __name__ == "__main__": port = int(envport) else: port = 7777 - uvicorn.run(api, host="0.0.0.0", port=7777) \ No newline at end of file + uvicorn.run(api, host="0.0.0.0", port=port) \ No newline at end of file diff --git a/sources/llm_provider.py b/sources/llm_provider.py index 7a2e7ef..b1a6c8f 100644 --- a/sources/llm_provider.py +++ b/sources/llm_provider.py @@ -344,13 +344,21 @@ class Provider: Use local lm-studio server to generate text. """ if self.in_docker: - # Extract port from server_address if present + # Extract port from server_address, handling both "host:port" and "http://host:port" port = "1234" # default - if ":" in self.server_address: - port = self.server_address.split(":")[1] + addr = self.server_address + if "://" not in addr: + addr = f"http://{addr}" + parsed_addr = urlparse(addr) + if parsed_addr.port: + port = str(parsed_addr.port) url = f"{self.internal_url}:{port}" else: - url = f"http://{self.server_ip}" + # Normalize the address to ensure it has a scheme prefix + addr = self.server_ip + if "://" not in addr: + addr = f"http://{addr}" + url = addr route_start = f"{url}/v1/chat/completions" payload = { "messages": history,