Skip to content

Commit bb3cfe8

Browse files
ibuildthecloudthedadams
authored andcommitted
feat: add load methods
Signed-off-by: Donnie Adams <[email protected]>
1 parent 404e44f commit bb3cfe8

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ from gptscript.gptscript import GPTScript
140140

141141
async def parse_tool_example():
142142
gptscript = GPTScript()
143-
tools = await gptscript.parse_tool("Instructions: Say hello!")
143+
tools = await gptscript.parse_content("Instructions: Say hello!")
144144
print(tools)
145145
gptscript.close()
146146
```
@@ -155,7 +155,7 @@ from gptscript.gptscript import GPTScript
155155

156156
async def fmt_example():
157157
gptscript = GPTScript()
158-
tools = await gptscript.parse_tool("Instructions: Say hello!")
158+
tools = await gptscript.parse_content("Instructions: Say hello!")
159159
print(tools)
160160

161161
contents = gptscript.fmt(tools)

gptscript/__init__.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 3 deletions
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

Lines changed: 27 additions & 3 deletions
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
@@ -106,6 +106,30 @@ def run(
106106
event_handlers=event_handlers,
107107
).next_chat("" if opts is None else opts.input)
108108

109+
async def load_file(self, file_path: str, disable_cache: bool = False, sub_tool: str = '') -> Program:
110+
out = await self._run_basic_command(
111+
"load",
112+
{"file": file_path, "disableCache": disable_cache, "subTool": sub_tool},
113+
)
114+
parsed_nodes = json.loads(out)
115+
return Program(**parsed_nodes.get("program", {}))
116+
117+
async def load_content(self, content: str, disable_cache: bool = False, sub_tool: str = '') -> Program:
118+
out = await self._run_basic_command(
119+
"load",
120+
{"content": content, "disableCache": disable_cache, "subTool": sub_tool},
121+
)
122+
parsed_nodes = json.loads(out)
123+
return Program(**parsed_nodes.get("program", {}))
124+
125+
async def load_tools(self, tool_defs: list[ToolDef], disable_cache: bool = False, sub_tool: str = '') -> Program:
126+
out = await self._run_basic_command(
127+
"load",
128+
{"toolDefs": tool_defs, "disableCache": disable_cache, "subTool": sub_tool},
129+
)
130+
parsed_nodes = json.loads(out)
131+
return Program(**parsed_nodes.get("program", {}))
132+
109133
async def parse(self, file_path: str, disable_cache: bool = False) -> list[Text | Tool]:
110134
out = await self._run_basic_command("parse", {"file": file_path, "disableCache": disable_cache})
111135
parsed_nodes = json.loads(out)
@@ -114,8 +138,8 @@ async def parse(self, file_path: str, disable_cache: bool = False) -> list[Text
114138
return [Text(**node["textNode"]) if "textNode" in node else Tool(**node.get("toolNode", {}).get("tool", {})) for
115139
node in parsed_nodes.get("nodes", [])]
116140

117-
async def parse_tool(self, tool_def: str) -> list[Text | Tool]:
118-
out = await self._run_basic_command("parse", {"content": tool_def})
141+
async def parse_content(self, content: str) -> list[Text | Tool]:
142+
out = await self._run_basic_command("parse", {"content": content})
119143
parsed_nodes = json.loads(out)
120144
if parsed_nodes is None or parsed_nodes.get("nodes", None) is None:
121145
return []

tests/test_gptscript.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,42 @@ async def test_eval_with_context(gptscript):
272272
)
273273

274274
run = gptscript.evaluate(tool)
275-
276275
assert "Acorn Labs" == await run.text(), "Unexpected output from eval using context"
277276

278277

278+
@pytest.mark.asyncio
279+
async def test_load_simple_file(gptscript):
280+
wd = os.getcwd()
281+
prg = await gptscript.load_file(wd + "/tests/fixtures/test.gpt")
282+
assert prg.toolSet[prg.entryToolId].instructions == "Who was the president of the United States in 1986?", \
283+
"Unexpected output from parsing simple file"
284+
285+
286+
@pytest.mark.asyncio
287+
async def test_load_remote_tool(gptscript):
288+
prg = await gptscript.load_file("github.com/gptscript-ai/context/workspace")
289+
assert prg.entryToolId != "", "Unexpected entry tool id from remote tool"
290+
assert len(prg.toolSet) > 0, "Unexpected number of tools in remote tool"
291+
assert prg.name != "", "Unexpected name from remote tool"
292+
293+
294+
@pytest.mark.asyncio
295+
async def test_load_simple_content(gptscript):
296+
wd = os.getcwd()
297+
with open(wd + "/tests/fixtures/test.gpt") as f:
298+
prg = await gptscript.load_content(f.read())
299+
assert prg.toolSet[prg.entryToolId].instructions == "Who was the president of the United States in 1986?", \
300+
"Unexpected output from parsing simple file"
301+
302+
303+
@pytest.mark.asyncio
304+
async def test_load_tools(gptscript, tool_list):
305+
prg = await gptscript.load_tools(tool_list)
306+
assert prg.entryToolId != "", "Unexpected entry tool id from remote tool"
307+
assert len(prg.toolSet) > 0, "Unexpected number of tools in remote tool"
308+
assert prg.name != "", "Unexpected name from remote tool"
309+
310+
279311
@pytest.mark.asyncio
280312
async def test_parse_simple_file(gptscript):
281313
wd = os.getcwd()
@@ -295,7 +327,7 @@ async def test_parse_empty_file(gptscript):
295327

296328
@pytest.mark.asyncio
297329
async def test_parse_empty_str(gptscript):
298-
tools = await gptscript.parse_tool("")
330+
tools = await gptscript.parse_content("")
299331
assert len(tools) == 0, "Unexpected number of tools for parsing empty string"
300332

301333

@@ -313,15 +345,15 @@ async def test_parse_tool_with_metadata(gptscript):
313345

314346
@pytest.mark.asyncio
315347
async def test_parse_tool(gptscript):
316-
tools = await gptscript.parse_tool("echo hello")
348+
tools = await gptscript.parse_content("echo hello")
317349
assert len(tools) == 1, "Unexpected number of tools for parsing tool"
318350
assert isinstance(tools[0], Tool), "Unexpected node type from parsing tool"
319351
assert tools[0].instructions == "echo hello", "Unexpected output from parsing tool"
320352

321353

322354
@pytest.mark.asyncio
323355
async def test_parse_tool_with_text_node(gptscript):
324-
tools = await gptscript.parse_tool("echo hello\n---\n!markdown\nhello")
356+
tools = await gptscript.parse_content("echo hello\n---\n!markdown\nhello")
325357
assert len(tools) == 2, "Unexpected number of tools for parsing tool with text node"
326358
assert isinstance(tools[0], Tool), "Unexpected node type for first tool from parsing tool with text node"
327359
assert isinstance(tools[1], Text), "Unexpected node type for second tool from parsing tool with text node"

0 commit comments

Comments
 (0)