Skip to content

feat: add metadata and type fields to tools #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion gptscript/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(self,
agents: list[str] = None,
credentials: list[str] = None,
instructions: str = "",
type: str = "",
):
self.name = name
self.description = description
Expand All @@ -83,6 +84,7 @@ def __init__(self,
self.agents = agents
self.credentials = credentials
self.instructions = instructions
self.type = type

def to_json(self) -> dict[str, Any]:
out = self.__dict__
Expand Down Expand Up @@ -161,14 +163,16 @@ def __init__(self,
agents: list[str] = None,
credentials: list[str] = None,
instructions: str = "",
type: str = "",
toolMapping: dict[str, list[ToolReference]] = None,
metaData: dict[str, str] = None,
localTools: dict[str, str] = None,
source: SourceRef = None,
workingDir: str = "",
):
super().__init__(name, description, maxTokens, modelName, modelProvider, jsonResponse, temperature, cache, chat,
internalPrompt, arguments, tools, globalTools, globalModelName, context, exportContext, export,
agents, credentials, instructions)
agents, credentials, instructions, type)

self.id = id
self.toolMapping = toolMapping
Expand All @@ -179,6 +183,7 @@ def __init__(self,
if isinstance(self.toolMapping[tool][i], dict):
self.toolMapping[tool][i] = ToolReference(**self.toolMapping[tool][i])
self.localTools = localTools
self.metaData = metaData
self.source = source
if self.source is not None and isinstance(self.source, dict):
self.source = SourceRef(**self.source)
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/parse-with-metadata.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Name: foo

#!/usr/bin/env python3
import requests


resp = requests.get("https://google.com")
print(resp.status_code, end="")

---
!metadata:foo:requirements.txt
requests
20 changes: 19 additions & 1 deletion tests/test_gptscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

from gptscript.confirm import AuthResponse
from gptscript.exec_utils import get_env
from gptscript.frame import RunEventType, CallFrame, RunFrame, RunState, PromptFrame
from gptscript.gptscript import GPTScript
from gptscript.install import install, gptscript_binary_name, python_bin_dir
Expand All @@ -16,7 +17,6 @@
from gptscript.run import Run
from gptscript.text import Text
from gptscript.tool import ToolDef, ArgumentSchema, Property, Tool
from gptscript.exec_utils import get_env


# Ensure the OPENAI_API_KEY is set for testing
Expand Down Expand Up @@ -226,6 +226,18 @@ async def test_parse_simple_file(gptscript):
"Unexpected output from parsing simple file"


@pytest.mark.asyncio
async def test_parse_tool_with_metadata(gptscript):
wd = os.getcwd()
tools = await gptscript.parse(wd + "/tests/fixtures/parse-with-metadata.gpt")
assert len(tools) == 2, "Unexpected number of tools for parsing simple file"
assert isinstance(tools[0], Tool), "Unexpected node type from parsing file with metadata"
assert "requests.get(" in tools[0].instructions, "Unexpected output from parsing file with metadata"
assert isinstance(tools[1], Text), "Unexpected node type from parsing file with metadata"
assert tools[1].text == "requests", "Unexpected output from parsing file with metadata"
assert tools[1].format == "metadata:foo:requirements.txt", "Unexpected output from parsing file with metadata"


@pytest.mark.asyncio
async def test_parse_tool(gptscript):
tools = await gptscript.parse_tool("echo hello")
Expand Down Expand Up @@ -525,3 +537,9 @@ def test_get_env():
'_gz': base64.b64encode(gzip.compress(b'test value')).decode('utf-8'),
}).replace(' ', '')
assert 'test value' == get_env('TEST_ENV')


@pytest.mark.asyncio
async def test_stream_run_file(gptscript):
run = gptscript.run("./tests/fixtures/parse-with-metadata.gpt")
assert "200" == await run.text(), "Expect file to have correct output"