Skip to content

Commit efe112c

Browse files
authored
Merge pull request #49 from thedadams/ibuildthecloud/main
feat: add load methods
2 parents 404e44f + 92c216b commit efe112c

File tree

5 files changed

+76
-18
lines changed

5 files changed

+76
-18
lines changed

README.md

+2-2
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

+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

+29-5
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
@@ -90,7 +90,7 @@ def evaluate(
9090
opts.merge_global_opts(self.opts),
9191
self._server_url,
9292
event_handlers=event_handlers,
93-
).next_chat("" if opts is None else opts.input)
93+
).next_chat(opts.input)
9494

9595
def run(
9696
self, tool_path: str,
@@ -104,7 +104,31 @@ def run(
104104
opts.merge_global_opts(self.opts),
105105
self._server_url,
106106
event_handlers=event_handlers,
107-
).next_chat("" if opts is None else opts.input)
107+
).next_chat(opts.input)
108+
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": [t.to_json() for t in tool_defs], "disableCache": disable_cache, "subTool": sub_tool},
129+
)
130+
parsed_nodes = json.loads(out)
131+
return Program(**parsed_nodes.get("program", {}))
108132

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})
@@ -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

+38-7
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ def simple_tool():
4848
@pytest.fixture
4949
def complex_tool():
5050
return ToolDef(
51-
tools=["sys.write"],
5251
jsonResponse=True,
53-
instructions="""
54-
Create three short graphic artist descriptions and their muses.
52+
instructions="""Create three short graphic artist descriptions and their muses.
5553
These should be descriptive and explain their point of view.
5654
Also come up with a made up name, they each should be from different
5755
backgrounds and approach art differently.
@@ -272,10 +270,43 @@ async def test_eval_with_context(gptscript):
272270
)
273271

274272
run = gptscript.evaluate(tool)
275-
276273
assert "Acorn Labs" == await run.text(), "Unexpected output from eval using context"
277274

278275

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

296327
@pytest.mark.asyncio
297328
async def test_parse_empty_str(gptscript):
298-
tools = await gptscript.parse_tool("")
329+
tools = await gptscript.parse_content("")
299330
assert len(tools) == 0, "Unexpected number of tools for parsing empty string"
300331

301332

@@ -313,15 +344,15 @@ async def test_parse_tool_with_metadata(gptscript):
313344

314345
@pytest.mark.asyncio
315346
async def test_parse_tool(gptscript):
316-
tools = await gptscript.parse_tool("echo hello")
347+
tools = await gptscript.parse_content("echo hello")
317348
assert len(tools) == 1, "Unexpected number of tools for parsing tool"
318349
assert isinstance(tools[0], Tool), "Unexpected node type from parsing tool"
319350
assert tools[0].instructions == "echo hello", "Unexpected output from parsing tool"
320351

321352

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

0 commit comments

Comments
 (0)