Skip to content

Commit d72985d

Browse files
committed
chore: update prompt type for more granular configuration
Signed-off-by: Donnie Adams <[email protected]>
1 parent d9098db commit d72985d

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

gptscript/frame.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,25 @@ def __init__(self,
194194
self.llmResponse = llmResponse
195195

196196

197+
class PromptField:
198+
def __init__(self,
199+
name: str = "",
200+
description: str = "",
201+
sensitive: bool | None = None,
202+
**kwargs,
203+
):
204+
self.name = name
205+
self.description = description
206+
self.sensitive = sensitive
207+
208+
197209
class PromptFrame:
198210
def __init__(self,
199211
id: str = "",
200212
type: RunEventType = RunEventType.prompt,
201213
time: str = "",
202214
message: str = "",
203-
fields: list[str] = None,
215+
fields: list[PromptField] = None,
204216
metadata: dict[str, str] = None,
205217
sensitive: bool = False,
206218
**kwargs,
@@ -209,6 +221,10 @@ def __init__(self,
209221
self.time = time
210222
self.message = message
211223
self.fields = fields
224+
if self.fields is not None:
225+
for i in range(len(self.fields)):
226+
if isinstance(self.fields[i], dict):
227+
self.fields[i] = PromptField(**self.fields[i])
212228
self.metadata = metadata
213229
self.sensitive = sensitive
214230
self.type = type

tests/test_gptscript.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -546,17 +546,21 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame):
546546
event_output += output.content
547547
elif frame.type == RunEventType.callFinish:
548548
call_finish_seen = True
549+
for output in frame.output:
550+
event_output += output.content
549551

550-
run = gptscript.run(os.getcwd() + "/tests/fixtures/global-tools.gpt",
552+
cwd = os.getcwd().removesuffix("/tests")
553+
run = gptscript.run(cwd + "/tests/fixtures/global-tools.gpt",
551554
Options(
552555
disableCache=True,
553556
credentialOverrides=["github.com/gptscript-ai/gateway:OPENAI_API_KEY"],
554557
),
555558
event_handlers=[process_event],
556559
)
557560

558-
assert "Hello!" in await run.text(), "Unexpected output from global tool test"
559-
assert "Hello" in event_output, "Unexpected stream output from global tool test"
561+
output = await run.text()
562+
assert "Hello!" in output, "Unexpected output from global tool test: " + output
563+
assert "Hello" in event_output, "Unexpected stream output from global tool test: " + event_output
560564

561565
assert run_start_seen and call_start_seen and call_progress_seen and call_finish_seen and run_finish_seen, \
562566
f"One of these is False: {run_start_seen}, {call_start_seen}, {call_progress_seen}, {call_finish_seen}, {run_finish_seen}"
@@ -573,7 +577,7 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame):
573577
confirm_event_found = True
574578
assert '"ls' in frame.input or '"dir' in frame.input, "Unexpected confirm input: " + frame.input
575579
await gptscript.confirm(AuthResponse(frame.id, True))
576-
elif frame.type == RunEventType.callProgress:
580+
elif frame.type == RunEventType.callProgress or frame.type == RunEventType.callFinish:
577581
for output in frame.output:
578582
event_content += output.content
579583

@@ -610,7 +614,7 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame):
610614
confirm_event_found = True
611615
assert '"ls"' in frame.input, "Unexpected confirm input: " + frame.input
612616
await gptscript.confirm(AuthResponse(frame.id, False, "I will not allow it!"))
613-
elif frame.type == RunEventType.callProgress:
617+
elif frame.type == RunEventType.callProgress or frame.type == RunEventType.callFinish:
614618
for output in frame.output:
615619
event_content += output.content
616620

@@ -637,9 +641,9 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame):
637641
if frame.type == RunEventType.prompt:
638642
prompt_event_found = True
639643
assert len(frame.fields) == 1, "Unexpected number of fields: " + str(frame.fields)
640-
assert "first name" in frame.fields[0], "Unexpected field: " + frame.fields[0]
641-
await gptscript.prompt(PromptResponse(frame.id, {frame.fields[0]: "Clicky"}))
642-
elif frame.type == RunEventType.callProgress:
644+
assert "first name" in frame.fields[0].name, "Unexpected field: " + frame.fields[0].name
645+
await gptscript.prompt(PromptResponse(frame.id, {frame.fields[0].name: "Clicky"}))
646+
elif frame.type == RunEventType.callProgress or frame.type == RunEventType.callFinish:
643647
for output in frame.output:
644648
event_content += output.content
645649

@@ -667,10 +671,10 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame):
667671
if frame.type == RunEventType.prompt:
668672
prompt_event_found = True
669673
assert len(frame.fields) == 1, "Unexpected number of fields: " + str(frame.fields)
670-
assert "first name" in frame.fields[0], "Unexpected field: " + frame.fields[0]
674+
assert "first name" in frame.fields[0].name, "Unexpected field: " + frame.fields[0].name
671675
assert "first_name" in frame.metadata, "Unexpected metadata: " + str(frame.metadata)
672676
assert frame.metadata["first_name"] == "Clicky", "Unexpected metadata: " + str(frame.metadata)
673-
await gptscript.prompt(PromptResponse(frame.id, {frame.fields[0]: "Clicky"}))
677+
await gptscript.prompt(PromptResponse(frame.id, {frame.fields[0].name: "Clicky"}))
674678

675679
out = await gptscript.run(
676680
"sys.prompt",
@@ -691,8 +695,8 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame):
691695
if frame.type == RunEventType.prompt:
692696
prompt_event_found = True
693697
assert len(frame.fields) == 1, "Unexpected number of fields: " + str(frame.fields)
694-
assert "first name" in frame.fields[0], "Unexpected field: " + frame.fields[0]
695-
await gptscript.prompt(PromptResponse(frame.id, {frame.fields[0]: "Clicky"}))
698+
assert "first name" in frame.fields[0].name, "Unexpected field: " + frame.fields[0].name
699+
await gptscript.prompt(PromptResponse(frame.id, {frame.fields[0].name: "Clicky"}))
696700

697701
tool = ToolDef(
698702
tools=["sys.prompt"],
@@ -727,7 +731,7 @@ async def test_run_file_with_metadata(gptscript):
727731

728732
@pytest.mark.asyncio
729733
async def test_parse_with_metadata_then_run(gptscript):
730-
cwd = os.getcwd().removesuffix("tests")
734+
cwd = os.getcwd().removesuffix("/tests")
731735
tools = await gptscript.parse(cwd + "/tests/fixtures/parse-with-metadata.gpt")
732736
run = gptscript.evaluate(tools[0])
733737
assert "200" == await run.text(), "Expect file to have correct output"

0 commit comments

Comments
 (0)