Skip to content

Commit cdc08a9

Browse files
feat: add load method
1 parent 67f6e5c commit cdc08a9

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

gptscript/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from gptscript.gptscript import GPTScript
22
from gptscript.confirm import AuthResponse
3-
from gptscript.frame import RunFrame, CallFrame, PromptFrame
3+
from gptscript.frame import RunFrame, CallFrame, PromptFrame, Program
44
from gptscript.opts import GlobalOptions
55
from gptscript.prompt import PromptResponse
66
from gptscript.run import Run, RunBasicCommand, Options

gptscript/frame.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ def __init__(self,
4747
self.name = name
4848
self.entryToolId = entryToolId
4949
self.toolSet = toolSet
50-
for tool in toolSet:
51-
if isinstance(self.toolSet[tool], dict):
52-
self.toolSet[tool] = Tool(**self.toolSet[tool])
50+
if self.toolSet is None:
51+
self.toolSet = {}
52+
else:
53+
for tool in toolSet:
54+
if isinstance(self.toolSet[tool], dict):
55+
self.toolSet[tool] = Tool(**self.toolSet[tool])
5356

5457

5558
class RunFrame:

gptscript/gptscript.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import requests
1010

1111
from gptscript.confirm import AuthResponse
12-
from gptscript.frame import RunFrame, CallFrame, PromptFrame
12+
from gptscript.frame import RunFrame, CallFrame, PromptFrame, Program
1313
from gptscript.opts import GlobalOptions
1414
from gptscript.prompt import PromptResponse
1515
from gptscript.run import Run, RunBasicCommand, Options
@@ -94,6 +94,11 @@ def run(
9494
"" if opts is None else opts.input
9595
)
9696

97+
async def load(self, file_path: str) -> Program:
98+
out = await self._run_basic_command("load", {"file": file_path})
99+
parsed_nodes = json.loads(out)
100+
return Program(**parsed_nodes.get("program", {}))
101+
97102
async def parse(self, file_path: str, disable_cache: bool = False) -> list[Text | Tool]:
98103
out = await self._run_basic_command("parse", {"file": file_path, "disableCache": disable_cache})
99104
parsed_nodes = json.loads(out)

tests/test_gptscript.py

+7
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,13 @@ async def test_eval_with_context(gptscript):
248248

249249
assert "Acorn Labs" == await run.text(), "Unexpected output from eval using context"
250250

251+
@pytest.mark.asyncio
252+
async def test_load_simple_file(gptscript):
253+
wd = os.getcwd()
254+
prg = await gptscript.load(wd + "/tests/fixtures/test.gpt")
255+
assert prg.toolSet[prg.entryToolId].instructions == "Who was the president of the United States in 1986?", \
256+
"Unexpected output from parsing simple file"
257+
251258

252259
@pytest.mark.asyncio
253260
async def test_parse_simple_file(gptscript):

0 commit comments

Comments
 (0)