Skip to content

Commit f4c6fcc

Browse files
committed
fix: move metadata to tool def
This change allows the following flow to work: parse file with metadata -> run the result tools. Signed-off-by: Donnie Adams <[email protected]>
1 parent 4c9cae7 commit f4c6fcc

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

gptscript/run.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,24 @@ def next_chat(self, input: str = "") -> Self:
8585
run.opts.input = input
8686
if isinstance(run.tools, list):
8787
run._task = asyncio.create_task(
88-
run._request({"toolDefs": [tool.to_json() for tool in run.tools], **vars(run.opts)}))
88+
run._request({
89+
"toolDefs": [
90+
tool.to_json().get("toolNode", {}).get("tool", {}) if isinstance(tool, Tool) else tool.to_json()
91+
for tool in run.tools
92+
], **vars(run.opts)
93+
})
94+
)
8995
elif isinstance(run.tools, str) and run.tools != "":
9096
run._task = asyncio.create_task(run._request({"file": run.tools, **vars(run.opts)}))
9197
elif isinstance(run.tools, ToolDef) or isinstance(run.tools, Tool):
9298
# In this last case, this.tools is a single ToolDef.
93-
run._task = asyncio.create_task(run._request({"toolDefs": [run.tools.to_json()], **vars(run.opts)}))
99+
run._task = asyncio.create_task(run._request({
100+
"toolDefs": [
101+
run.tools.to_json().get("toolNode", {}).get("tool", {})
102+
if isinstance(run.tools, Tool) else run.tools.to_json()
103+
],
104+
**vars(run.opts)
105+
}))
94106
else:
95107
run._task = asyncio.create_task(run._request({**vars(run.opts)}))
96108

gptscript/tool.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def __init__(self,
6060
credentials: list[str] = None,
6161
instructions: str = "",
6262
type: str = "",
63+
metaData: dict[str, str] = None,
6364
):
6465
self.name = name
6566
self.description = description
@@ -85,6 +86,7 @@ def __init__(self,
8586
self.credentials = credentials
8687
self.instructions = instructions
8788
self.type = type
89+
self.metaData = metaData
8890

8991
def to_json(self) -> dict[str, Any]:
9092
out = self.__dict__
@@ -164,15 +166,15 @@ def __init__(self,
164166
credentials: list[str] = None,
165167
instructions: str = "",
166168
type: str = "",
167-
toolMapping: dict[str, list[ToolReference]] = None,
168169
metaData: dict[str, str] = None,
170+
toolMapping: dict[str, list[ToolReference]] = None,
169171
localTools: dict[str, str] = None,
170172
source: SourceRef = None,
171173
workingDir: str = "",
172174
):
173175
super().__init__(name, description, maxTokens, modelName, modelProvider, jsonResponse, temperature, cache, chat,
174176
internalPrompt, arguments, tools, globalTools, globalModelName, context, exportContext, export,
175-
agents, credentials, instructions, type)
177+
agents, credentials, instructions, type, metaData)
176178

177179
self.id = id
178180
self.toolMapping = toolMapping
@@ -183,7 +185,6 @@ def __init__(self,
183185
if isinstance(self.toolMapping[tool][i], dict):
184186
self.toolMapping[tool][i] = ToolReference(**self.toolMapping[tool][i])
185187
self.localTools = localTools
186-
self.metaData = metaData
187188
self.source = source
188189
if self.source is not None and isinstance(self.source, dict):
189190
self.source = SourceRef(**self.source)

tests/test_gptscript.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ async def test_tool_chat(gptscript):
351351
async def test_file_chat(gptscript):
352352
inputs = [
353353
"List the 3 largest of the Great Lakes by volume.",
354-
"What is the volume of the second one in cubic miles?",
355-
"What is the total area of the third one in square miles?",
354+
"What is the volume of the second in the list in cubic miles?",
355+
"What is the total area of the third in the list in square miles?",
356356
]
357357
expected_outputs = [
358358
"Lake Superior",
@@ -540,6 +540,14 @@ def test_get_env():
540540

541541

542542
@pytest.mark.asyncio
543-
async def test_stream_run_file(gptscript):
543+
async def test_run_file_with_metadata(gptscript):
544544
run = gptscript.run("./tests/fixtures/parse-with-metadata.gpt")
545545
assert "200" == await run.text(), "Expect file to have correct output"
546+
547+
548+
@pytest.mark.asyncio
549+
async def test_parse_with_metadata_then_run(gptscript):
550+
cwd = os.getcwd().removesuffix("tests")
551+
tools = await gptscript.parse(cwd + "/tests/fixtures/parse-with-metadata.gpt")
552+
run = gptscript.evaluate(tools[0])
553+
assert "200" == await run.text(), "Expect file to have correct output"

0 commit comments

Comments
 (0)