From 55791e62af43ff4befb77ca25fbe296575253234 Mon Sep 17 00:00:00 2001 From: Donnie Adams Date: Mon, 22 Jul 2024 12:16:28 -0400 Subject: [PATCH] feat: improve SDK server startup --- gptscript/gptscript.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/gptscript/gptscript.py b/gptscript/gptscript.py index 8d5bfcc..1800d3c 100644 --- a/gptscript/gptscript.py +++ b/gptscript/gptscript.py @@ -30,14 +30,9 @@ def __init__(self, opts: GlobalOptions = None): GPTScript.__gptscript_count += 1 if GPTScript.__server_url == "": - GPTScript.__server_url = os.environ.get("GPTSCRIPT_URL", "") + GPTScript.__server_url = os.environ.get("GPTSCRIPT_URL", "127.0.0.1:0") if GPTScript.__gptscript_count == 1 and os.environ.get("GPTSCRIPT_DISABLE_SERVER", "") != "true": - if GPTScript.__server_url == "": - with socket() as s: - s.bind(("", 0)) - GPTScript.__server_url = f"127.0.0.1:{s.getsockname()[1]}" - opts.toEnv() GPTScript.__process = Popen( @@ -50,6 +45,10 @@ def __init__(self, opts: GlobalOptions = None): encoding="utf-8", ) + GPTScript.__server_url = GPTScript.__process.stderr.readline().strip("\n") + if "=" in GPTScript.__server_url: + GPTScript.__server_url = GPTScript.__server_url.split("=")[1] + self._server_url = f"http://{GPTScript.__server_url}" self._wait_for_gptscript()