Skip to content

Commit

Permalink
Various RestApiTool improvements/fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter committed Oct 15, 2024
1 parent f6b15ab commit cdce164
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 147 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **BREAKING**: Updated `EventListener.handler` return value behavior.
- If `EventListener.handler` returns `None`, the event will not be published to the `event_listener_driver`.
- If `EventListener.handler` is None, the event will be published to the `event_listener_driver` as-is.
- **BREAKING**: `RestApiTool` now returns a `JsonArtifact` instead of a `TextArtifact`.
- **BREAKING**: Removed `RestApiTool.response_body`, `RestApiTool.request_path_params_schema`.
- **BREAKING**: Changed `RestApiTool` fields from `str` to `dict`:
- `RestApiTool.request_query_params_schema` (`dict`)
- `RestApiTool.request_body_schema` (`dict`)
- Updated `EventListener.handler` return type to `Optional[BaseEvent | dict]`.
- `BaseTask.parent_outputs` type has changed from `dict[str, str | None]` to `dict[str, BaseArtifact]`.
- `Workflow.context["parent_outputs"]` type has changed from `dict[str, str | None]` to `dict[str, BaseArtifact]`.
Expand All @@ -43,6 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Structures not flushing events when not listening for `FinishStructureRunEvent`.
- `EventListener.event_types` and the argument to `BaseEventListenerDriver.handler` being out of sync.
- `RestApiTool` failing with native tool calling due to schemas being in schema description.

## \[0.33.1\] - 2024-10-11

Expand Down
111 changes: 111 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,117 @@ EventListener(handler=handler_fn_return_dict, event_listener_driver=driver)
EventListener(handler=handler_fn_return_base_event, event_listener_driver=driver)
```

### Changed `RestApiTool` fields

The following fields are no longer stringified full schemas. They are now just the properties of the schema.

- `RestApiTool.request_path_params_schema` (`list`)
- `RestApiTool.request_query_params_schema` (`dict`)
- `RestApiTool.request_body_schema` (`dict`)

#### Before
```python
posts_client = RestApiTool(
base_url="https://jsonplaceholder.typicode.com",
path="posts",
description="Allows for creating, updating, deleting, patching, and getting posts.",
request_body_schema=dumps(
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "http://example.com/example.json",
"type": "object",
"default": {},
"title": "Root Schema",
"required": ["title", "body", "userId"],
"properties": {
"title": {
"type": "string",
"default": "",
"title": "The title Schema",
},
"body": {
"type": "string",
"default": "",
"title": "The body Schema",
},
"userId": {
"type": "integer",
"default": 0,
"title": "The userId Schema",
},
},
}
),
request_query_params_schema=dumps(
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "http://example.com/example.json",
"type": "object",
"default": {},
"title": "Root Schema",
"required": ["userId"],
"properties": {
"userId": {
"type": "string",
"default": "",
"title": "The userId Schema",
},
},
}
),
response_body_schema=dumps(
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "http://example.com/example.json",
"type": "object",
"default": {},
"title": "Root Schema",
"required": ["id", "title", "body", "userId"],
"properties": {
"id": {
"type": "integer",
"default": 0,
"title": "The id Schema",
},
"title": {
"type": "string",
"default": "",
"title": "The title Schema",
},
"body": {
"type": "string",
"default": "",
"title": "The body Schema",
},
"userId": {
"type": "integer",
"default": 0,
"title": "The userId Schema",
},
},
}
),
)
```

#### After
```python
posts_client = RestApiTool(
base_url="https://jsonplaceholder.typicode.com",
path="posts",
description="Allows for creating, updating, deleting, patching, and getting posts.",
request_body_schema={
"title": str,
"body": str,
"userId": int,
},
request_query_params_schema={
"userId": str,
},
)
```


## 0.32.X to 0.33.X

### Removed `DataframeLoader`
Expand Down
115 changes: 19 additions & 96 deletions docs/griptape-tools/official-tools/src/rest_api_tool_1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from json import dumps

from griptape.configs import Defaults
from griptape.configs.drivers import DriversConfig
from griptape.drivers import OpenAiChatPromptDriver
Expand All @@ -15,100 +13,21 @@
posts_client = RestApiTool(
base_url="https://jsonplaceholder.typicode.com",
path="posts",
description="Allows for creating, updating, deleting, patching, and getting posts.",
request_body_schema=dumps(
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "http://example.com/example.json",
"type": "object",
"default": {},
"title": "Root Schema",
"required": ["title", "body", "userId"],
"properties": {
"title": {
"type": "string",
"default": "",
"title": "The title Schema",
},
"body": {
"type": "string",
"default": "",
"title": "The body Schema",
},
"userId": {
"type": "integer",
"default": 0,
"title": "The userId Schema",
},
},
}
),
request_query_params_schema=dumps(
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "http://example.com/example.json",
"type": "object",
"default": {},
"title": "Root Schema",
"required": ["userId"],
"properties": {
"userId": {
"type": "string",
"default": "",
"title": "The userId Schema",
},
},
}
),
request_path_params_schema=dumps(
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "http://example.com/example.json",
"type": "array",
"default": [],
"title": "Root Schema",
"items": {
"anyOf": [
{
"type": "string",
"title": "Post id",
},
]
},
}
),
response_body_schema=dumps(
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "http://example.com/example.json",
"type": "object",
"default": {},
"title": "Root Schema",
"required": ["id", "title", "body", "userId"],
"properties": {
"id": {
"type": "integer",
"default": 0,
"title": "The id Schema",
},
"title": {
"type": "string",
"default": "",
"title": "The title Schema",
},
"body": {
"type": "string",
"default": "",
"title": "The body Schema",
},
"userId": {
"type": "integer",
"default": 0,
"title": "The userId Schema",
},
},
}
),
description="Allows for creating, updating, deleting, patching, and getting posts. Can also be used to access subresources.",
request_body_schema={
"title": str,
"body": str,
"userId": int,
},
)

comments_client = RestApiTool(
base_url="https://jsonplaceholder.typicode.com",
path="comments",
description="Allows for getting comments for a post.",
request_query_params_schema={
"postId": str,
},
)

pipeline = Pipeline(
Expand Down Expand Up @@ -140,6 +59,10 @@
"Output the body of all the comments for post 1.",
tools=[posts_client],
),
ToolkitTask(
"Get the comments for post 1.",
tools=[comments_client],
),
)

pipeline.run()
Loading

0 comments on commit cdce164

Please sign in to comment.