-
Notifications
You must be signed in to change notification settings - Fork 183
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
Add structure run tool/task #767
Merged
Merged
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
31ad883
Add structure run tool/task
collindutter 60acc36
Add workflow recipe
collindutter 6233619
Update changelog
collindutter d6a7d3a
Update changelog
collindutter f9a0700
Update doc wording
collindutter 138962a
Remove prints
collindutter eb9d20c
Mention Griptape Cloud
collindutter 9c75842
Update wording
collindutter 4a0f45a
Rename structure field
collindutter c8c66e4
Refactor Structure running to use Drivers
collindutter 2b01eac
Update docs, many refactors and fixes
collindutter 56fb3b2
More fixes
collindutter fa78376
Update changelog
collindutter 1d87cc2
Format
collindutter 7c32a8f
Better words
collindutter fd6be29
Add multi text input task
collindutter 7734dcd
Update example to use multiple inputs
collindutter abf51e4
Fix doc links
collindutter 4b8d8ee
Fix PR things
collindutter 30894c3
Add async run field
collindutter 51e9189
Remove code execution task as no-op
collindutter e2596b8
Replace no-op with prompt
collindutter d3e2d1b
Change `structure` to `structure_factory`
collindutter 0196143
Rename factory fn
collindutter 01732fa
Fix examples
collindutter 6e3f13c
Add missing env var
collindutter 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 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 |
---|---|---|
|
@@ -23,7 +23,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |
- `AmazonS3FileManagerDriver` for managing files on Amazon S3. | ||
- `MediaArtifact` as a base class for `ImageArtifact` and future media Artifacts. | ||
- Optional `exception` field to `ErrorArtifact`. | ||
- `GriptapeCloudStructureRunClient` tool for invoking Griptape Cloud Structure Run APIs. | ||
- `StructureRunClient` for running other Structures via a Tool. | ||
- `StructureRunTask` for running Structures as a Task from within another Structure. | ||
- `GriptapeCloudStructureRunDriver` for running Structures in Griptape Cloud. | ||
- `LocalStructureRunDriver` for running Structures in the same run-time environment as the code that is running the Structure. | ||
|
||
### Changed | ||
- **BREAKING**: Secret fields (ex: api_key) removed from serialized Drivers. | ||
|
@@ -34,7 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |
- **BREAKING**: `FileManager.default_loader` is now `None` by default. | ||
- **BREAKING** Bumped `pinecone` from `^2` to `^3`. | ||
- **BREAKING**: Removed `workdir`, `loaders`, `default_loader`, and `save_file_encoding` fields from `FileManager` and added `file_manager_driver`. | ||
- **BREADKING**: Removed `mime_type` field from `ImageArtifact`. `mime_type` is now a property constructed using the Artifact type and `format` field. | ||
- **BREAKING**: Removed `mime_type` field from `ImageArtifact`. `mime_type` is now a property constructed using the Artifact type and `format` field. | ||
Comment on lines
-37
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Long live Bread King. |
||
- Improved RAG performance in `VectorQueryEngine`. | ||
- Moved [Griptape Docs](https://github.com/griptape-ai/griptape-docs) to this repository. | ||
- Updated `EventListener.handler`'s behavior so that the return value will be passed to the `EventListenerDriver.try_publish_event_payload`'s `event_payload` parameter. | ||
|
This file contains 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,191 @@ | ||
In this example we implement a multi-agent Workflow. We have a single "Researcher" Agent that conducts research on a topic, and then fans out to multiple "Writer" Agents to write blog posts based on the research. | ||
|
||
By splitting up our workloads across multiple Structures, we can parallelize the work and leverage the strengths of each Agent. The Researcher can focus on gathering data and insights, while the Writers can focus on crafting engaging narratives. | ||
Additionally, this architecture opens us up to using services such as [Griptape Cloud](https://www.griptape.ai/cloud) to have each Agent run on a separate machine, allowing us to scale our Workflow as needed 🤯. | ||
|
||
|
||
```python | ||
import os | ||
|
||
from griptape.drivers import WebhookEventListenerDriver, LocalStructureRunDriver | ||
from griptape.events import EventListener, FinishStructureRunEvent | ||
from griptape.rules import Rule, Ruleset | ||
from griptape.structures import Agent, Workflow | ||
from griptape.tasks import PromptTask, StructureRunTask | ||
from griptape.tools import ( | ||
TaskMemoryClient, | ||
WebScraper, | ||
WebSearch, | ||
) | ||
|
||
WRITERS = [ | ||
{ | ||
"role": "Travel Adventure Blogger", | ||
"goal": "Inspire wanderlust with stories of hidden gems and exotic locales", | ||
"backstory": "With a passport full of stamps, you bring distant cultures and breathtaking scenes to life through vivid storytelling and personal anecdotes.", | ||
}, | ||
{ | ||
"role": "Lifestyle Freelance Writer", | ||
"goal": "Share practical advice on living a balanced and stylish life", | ||
"backstory": "From the latest trends in home decor to tips for wellness, your articles help readers create a life that feels both aspirational and attainable.", | ||
}, | ||
] | ||
|
||
|
||
def build_researcher(): | ||
SavagePencil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Builds a Researcher Structure.""" | ||
researcher = Agent( | ||
id="researcher", | ||
tools=[ | ||
WebSearch( | ||
google_api_key=os.environ["GOOGLE_API_KEY"], | ||
google_api_search_id=os.environ["GOOGLE_API_SEARCH_ID"], | ||
off_prompt=False, | ||
dylanholmes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
WebScraper( | ||
off_prompt=True, | ||
), | ||
TaskMemoryClient(off_prompt=False), | ||
], | ||
rulesets=[ | ||
Ruleset( | ||
name="Position", | ||
rules=[ | ||
Rule( | ||
value="Lead Research Analyst", | ||
) | ||
], | ||
dylanholmes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
Ruleset( | ||
name="Objective", | ||
rules=[ | ||
Rule( | ||
value="Discover innovative advancements in artificial intelligence and data analytics", | ||
) | ||
], | ||
), | ||
Ruleset( | ||
name="Background", | ||
rules=[ | ||
Rule( | ||
value="""You are part of a prominent technology research institute. | ||
Your speciality is spotting new trends. | ||
You excel at analyzing intricate data and delivering practical insights.""" | ||
) | ||
], | ||
), | ||
Ruleset( | ||
name="Desired Outcome", | ||
rules=[ | ||
Rule( | ||
value="Comprehensive analysis report in list format", | ||
) | ||
], | ||
), | ||
], | ||
) | ||
|
||
return researcher | ||
|
||
|
||
def build_writer(role: str, goal: str, backstory: str): | ||
"""Builds a Writer Structure. | ||
|
||
Args: | ||
role: The role of the writer. | ||
goal: The goal of the writer. | ||
backstory: The backstory of the writer. | ||
""" | ||
writer = Agent( | ||
id=role.lower().replace(" ", "_"), | ||
event_listeners=[ | ||
EventListener( | ||
event_types=[FinishStructureRunEvent], | ||
driver=WebhookEventListenerDriver( | ||
webhook_url=os.environ["WEBHOOK_URL"], | ||
), | ||
) | ||
], | ||
rulesets=[ | ||
Ruleset( | ||
name="Position", | ||
rules=[ | ||
Rule( | ||
value=role, | ||
) | ||
], | ||
), | ||
Ruleset( | ||
name="Objective", | ||
rules=[ | ||
Rule( | ||
value=goal, | ||
) | ||
], | ||
), | ||
Ruleset( | ||
name="Backstory", | ||
rules=[Rule(value=backstory)], | ||
), | ||
Ruleset( | ||
name="Desired Outcome", | ||
rules=[ | ||
Rule( | ||
value="Full blog post of at least 4 paragraphs", | ||
) | ||
], | ||
), | ||
], | ||
) | ||
|
||
return writer | ||
|
||
|
||
if __name__ == "__main__": | ||
# Build the team | ||
team = Workflow() | ||
research_task = team.add_task( | ||
StructureRunTask( | ||
( | ||
"""Perform a detailed examination of the newest developments in AI as of 2024. | ||
Pinpoint major trends, breakthroughs, and their implications for various industries.""", | ||
), | ||
id="research", | ||
driver=LocalStructureRunDriver( | ||
structure_factory_fn=build_researcher, | ||
), | ||
), | ||
) | ||
end_task = team.add_task( | ||
PromptTask( | ||
'State "All Done!"', | ||
) | ||
) | ||
team.insert_tasks( | ||
research_task, | ||
[ | ||
StructureRunTask( | ||
( | ||
"""Using insights provided, develop an engaging blog | ||
post that highlights the most significant AI advancements. | ||
Your post should be informative yet accessible, catering to a tech-savvy audience. | ||
Make it sound cool, avoid complex words so it doesn't sound like AI. | ||
|
||
Insights: | ||
{{ parent_outputs["research"] }}""", | ||
), | ||
driver=LocalStructureRunDriver( | ||
structure_factory_fn=lambda: build_writer( | ||
role=writer["role"], | ||
goal=writer["goal"], | ||
backstory=writer["backstory"], | ||
) | ||
), | ||
) | ||
for writer in WRITERS | ||
], | ||
end_task, | ||
) | ||
|
||
team.run() | ||
``` |
This file contains 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,97 @@ | ||
## Overview | ||
Structure Run Drivers can be used to run Griptape Structures in a variety of runtime environments. | ||
When combined with the [Structure Run Task](../../griptape-framework/structures/tasks.md#structure-run-task) or [Structure Run Client](../../griptape-tools/official-tools/structure-run-client.md) you can create complex, multi-agent pipelines that span multiple runtime environments. | ||
|
||
## Local Structure Run Driver | ||
|
||
The [LocalStructureRunDriver](../../reference/griptape/drivers/structure_run/local_structure_run_driver.md) is used to run Griptape Structures in the same runtime environment as the code that is running the Structure. | ||
|
||
```python | ||
from griptape.drivers import LocalStructureRunDriver | ||
from griptape.rules import Rule | ||
from griptape.structures import Agent, Pipeline | ||
from griptape.tasks import StructureRunTask | ||
|
||
joke_teller = Agent( | ||
rules=[ | ||
Rule( | ||
value="You are very funny.", | ||
) | ||
], | ||
) | ||
dylanholmes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
joke_rewriter = Agent( | ||
rules=[ | ||
Rule( | ||
value="You are the editor of a joke book. But you only speak in riddles", | ||
) | ||
], | ||
) | ||
|
||
joke_coordinator = Pipeline( | ||
dylanholmes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tasks=[ | ||
StructureRunTask( | ||
driver=LocalStructureRunDriver( | ||
structure_factory_fn=lambda: joke_teller, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this one also return a new instance, for similar reasons to my other comment? |
||
), | ||
), | ||
StructureRunTask( | ||
("Rewrite this joke: {{ parent_output }}",), | ||
driver=LocalStructureRunDriver( | ||
structure_factory_fn=lambda: joke_rewriter, | ||
), | ||
), | ||
] | ||
) | ||
|
||
joke_coordinator.run("Tell me a joke") | ||
``` | ||
|
||
## Griptape Cloud Structure Run Driver | ||
|
||
The [GriptapeCloudStructureRunDriver](../../reference/griptape/drivers/structure_run/griptape_cloud_structure_run_driver.md) is used to run Griptape Structures in the Griptape Cloud. | ||
|
||
|
||
```python | ||
import os | ||
|
||
from griptape.drivers import GriptapeCloudStructureRunDriver, LocalStructureRunDriver | ||
from griptape.structures import Pipeline, Agent | ||
from griptape.rules import Rule | ||
from griptape.tasks import StructureRunTask | ||
|
||
base_url = os.environ["GRIPTAPE_CLOUD_BASE_URL"] | ||
api_key = os.environ["GRIPTAPE_CLOUD_API_KEY"] | ||
structure_id = os.environ["GRIPTAPE_CLOUD_STRUCTURE_ID"] | ||
|
||
|
||
pipeline = Pipeline( | ||
tasks=[ | ||
StructureRunTask( | ||
("Think of a question related to Retrieval Augmented Generation.",), | ||
driver=LocalStructureRunDriver( | ||
structure_factory_fn=lambda: Agent( | ||
rules=[ | ||
Rule( | ||
value="You are an expert in Retrieval Augmented Generation.", | ||
), | ||
Rule( | ||
value="Only output your answer, no other information.", | ||
), | ||
] | ||
) | ||
), | ||
), | ||
StructureRunTask( | ||
("{{ parent_output }}",), | ||
driver=GriptapeCloudStructureRunDriver( | ||
base_url=base_url, | ||
api_key=api_key, | ||
structure_id=structure_id, | ||
), | ||
), | ||
] | ||
) | ||
|
||
pipeline.run() | ||
``` |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ugh why can't it end with Tool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Named it this way for consistency
TaskMemoryClient
, another Tool that interacts directly with the framework. Agree that it's not ideal but it's at least consistent.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
boo but OK. In the future, we don't let consistency hold us back :)