Skip to content

Commit

Permalink
fix: make env a list for passing to gptscript
Browse files Browse the repository at this point in the history
Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Aug 20, 2024
1 parent aac46af commit 6499b85
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion gptscript/gptscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, opts: GlobalOptions = None):
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
env=self.opts.Env,
env={e.split("=", 1)[0]: e.split("=", 1)[1] for e in self.opts.Env},
text=True,
encoding="utf-8",
)
Expand Down
17 changes: 10 additions & 7 deletions gptscript/opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def __init__(
self.BaseURL = baseURL
self.DefaultModel = defaultModel
self.DefaultModelProvider = defaultModelProvider
self.Env = env
if env is None:
env = os.environ
env_list = [f"{k}={v}" for k, v in env.items()]
self.Env = env_list

def merge(self, other: Self) -> Self:
cp = self.__class__()
Expand All @@ -25,21 +28,21 @@ def merge(self, other: Self) -> Self:
cp.BaseURL = other.BaseURL if other.BaseURL != "" else self.BaseURL
cp.DefaultModel = other.DefaultModel if other.DefaultModel != "" else self.DefaultModel
cp.DefaultModelProvider = other.DefaultModelProvider if other.DefaultModelProvider != "" else self.DefaultModelProvider
cp.Env = {**(self.Env or {}), **(other.Env or {})}
cp.Env = (other.Env or []).extend(self.Env or [])
return cp

def toEnv(self):
if self.Env is None:
self.Env = os.environ.copy()
self.Env = [f"{k}={v}" for k, v in os.environ.items()]

if self.APIKey != "":
self.Env["OPENAI_API_KEY"] = self.APIKey
self.Env.append(f"OPENAI_API_KEY={self.APIKey}")
if self.BaseURL != "":
self.Env["OPENAI_BASE_URL"] = self.BaseURL
self.Env.append(f"OPENAI_BASE_URL={self.BaseURL}")
if self.DefaultModel != "":
self.Env["GPTSCRIPT_SDKSERVER_DEFAULT_MODEL"] = self.DefaultModel
self.Env.append(f"GPTSCRIPT_SDKSERVER_DEFAULT_MODEL={self.DefaultModel}")
if self.DefaultModelProvider != "":
self.Env["GPTSCRIPT_SDKSERVER_DEFAULT_MODEL_PROVIDER"] = self.DefaultModelProvider
self.Env.append(f"GPTSCRIPT_SDKSERVER_DEFAULT_MODEL_PROVIDER={self.DefaultModelProvider}")


class Options(GlobalOptions):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_gptscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ async def test_tool_chat(gptscript):
async def test_file_chat(gptscript):
inputs = [
"List the 3 largest of the Great Lakes by volume.",
"What is the volume of the second in the list in cubic miles?",
"What is the total area of the third in the list in square miles?",
"For the second one in the list, what is the volume in cubic miles?",
"For the third one in the list, what is the total area in square miles?",
]
expected_outputs = [
"Lake Superior",
Expand Down

0 comments on commit 6499b85

Please sign in to comment.