-
Notifications
You must be signed in to change notification settings - Fork 68
feat(sdk): add typed argument contracts #600
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
+139
−9
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Annotated, TypeAlias, TypeVar | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, Field | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
|
|
||
| class DataFileRef(BaseModel): | ||
| model_config = ConfigDict(extra="forbid") | ||
|
|
||
| file_id: str = Field(description="HUD data-file id") | ||
| path: str | None = Field(default=None, description="Environment-owned destination path") | ||
|
|
||
|
|
||
| PromptArg: TypeAlias = Annotated[ | ||
| str, | ||
| Field(json_schema_extra={"x-hud-hint": "prompt"}), | ||
| ] | ||
|
|
||
| DataFileArg: TypeAlias = Annotated[ | ||
| T, | ||
| Field(json_schema_extra={"x-hud-hint": "data-file"}), | ||
| ] | ||
|
|
||
| DataFilesArg: TypeAlias = Annotated[ | ||
| list[T], | ||
| Field(json_schema_extra={"x-hud-hint": "data-files"}), | ||
| ] | ||
|
|
||
| GradingArg: TypeAlias = Annotated[ | ||
| list[T], | ||
| Field(json_schema_extra={"x-hud-hint": "grading"}), | ||
| ] | ||
|
|
||
|
|
||
| __all__ = ["DataFileArg", "DataFileRef", "DataFilesArg", "GradingArg", "PromptArg"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Literal | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
| from hud.environment import ( | ||
| DataFileArg, | ||
| DataFileRef, | ||
| DataFilesArg, | ||
| Environment, | ||
| GradingArg, | ||
| PromptArg, | ||
| ) | ||
| from hud.eval import Run | ||
|
|
||
| from .conftest import served | ||
|
|
||
|
|
||
| class _Attachment(DataFileRef): | ||
| expand: bool = False | ||
|
|
||
|
|
||
| class _Fixture(DataFileRef): | ||
| channel: Literal["mail", "calendar"] | ||
|
|
||
|
|
||
| class _Criterion(BaseModel): | ||
| requirement: str | ||
| weight: float = 1.0 | ||
| guidance: str | None = None | ||
|
|
||
|
|
||
| def test_argument_types_publish_editor_hints_and_model_schemas() -> None: | ||
| env = Environment("typed-args") | ||
|
|
||
| @env.template() | ||
| async def task( | ||
| prompt: PromptArg, | ||
| attachment: DataFileArg[_Attachment], | ||
| fixtures: DataFilesArg[_Fixture], | ||
| criteria: GradingArg[_Criterion], | ||
| ): | ||
| yield prompt | ||
| yield 1.0 | ||
|
|
||
| schema = task.manifest_entry()["args"] | ||
| properties = schema["properties"] | ||
|
|
||
| assert properties["prompt"]["x-hud-hint"] == "prompt" | ||
| assert properties["attachment"]["x-hud-hint"] == "data-file" | ||
| assert properties["fixtures"]["x-hud-hint"] == "data-files" | ||
| assert properties["criteria"]["x-hud-hint"] == "grading" | ||
| assert properties["fixtures"]["items"]["$ref"] == "#/$defs/_Fixture" | ||
| assert properties["criteria"]["items"]["$ref"] == "#/$defs/_Criterion" | ||
| assert "channel" in schema["$defs"]["_Fixture"]["properties"] | ||
| assert "guidance" in schema["$defs"]["_Criterion"]["properties"] | ||
|
|
||
|
|
||
| async def test_typed_argument_values_reach_task_as_models() -> None: | ||
| env = Environment("typed-args") | ||
|
|
||
| @env.template() | ||
| async def task( | ||
| attachments: DataFilesArg[_Attachment], | ||
| criteria: GradingArg[_Criterion], | ||
| ): | ||
| assert isinstance(attachments[0], _Attachment) | ||
| assert isinstance(criteria[0], _Criterion) | ||
| yield f"{attachments[0].path}:{criteria[0].requirement}" | ||
| yield 1.0 | ||
|
|
||
| async with ( | ||
| served(env) as client, | ||
| Run( | ||
| client, | ||
| "task", | ||
| { | ||
| "attachments": [{"file_id": "file-1", "path": "brief.pdf"}], | ||
| "criteria": [{"requirement": "Answer the question", "weight": 2.0}], | ||
| }, | ||
| ) as run, | ||
| ): | ||
| assert run.prompt_text == "brief.pdf:Answer the question" | ||
| run.trace.content = "done" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.