On this tutorial, we construct a totally native, API-free agentic storytelling system utilizing Griptape and a light-weight Hugging Face mannequin. We stroll by creating an agent with tool-use talents, producing a fictional world, designing characters, and orchestrating a multi-stage workflow that produces a coherent brief story. By dividing the implementation into modular snippets, we will clearly perceive every element because it comes collectively into an end-to-end inventive pipeline. Take a look at the FULL CODES right here.
!pip set up -q "griptape[drivers-prompt-huggingface-pipeline]" "transformers" "speed up" "sentencepiece"
import textwrap
from griptape.buildings import Workflow, Agent
from griptape.duties import PromptTask
from griptape.instruments import CalculatorTool
from griptape.guidelines import Rule, Ruleset
from griptape.drivers.immediate.huggingface_pipeline import HuggingFacePipelinePromptDriver
local_driver = HuggingFacePipelinePromptDriver(
mannequin="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
max_tokens=256,
)
def present(title, content material):
print(f"n{'='*20} {title} {'='*20}")
print(textwrap.fill(str(content material), width=100))We arrange the environment by putting in Griptape and initializing a neighborhood Hugging Face driver. We configure a helper operate to show outputs cleanly, permitting us to observe every step of the workflow. As we construct the muse, we guarantee every part runs regionally with out counting on exterior APIs. Take a look at the FULL CODES right here.
math_agent = Agent(
prompt_driver=local_driver,
instruments=[CalculatorTool()],
)
math_response = math_agent.run(
"Compute (37*19)/7 and clarify the steps briefly."
)
present("Agent + CalculatorTool", math_response.output.worth)We create an agent outfitted with a calculator software and take a look at it with a easy mathematical immediate. We observe how the agent delegates computation to the software after which formulates a natural-language clarification. By operating this, we validate that our native driver and power integration work accurately. Take a look at the FULL CODES right here.
world_task = PromptTask(
enter="Create a vivid fictional world utilizing these cues: {{ args[0] }}.nDescribe geography, tradition, and conflicts in 3–5 paragraphs.",
id="world",
prompt_driver=local_driver,
)
def character_task(task_id, identify):
return PromptTask(
enter=(
"Based mostly on the world beneath, invent an in depth character named {{ identify }}.n"
"World description:n{{ parent_outputs['world'] }}nn"
"Describe their background, wishes, flaws, and one secret."
),
id=task_id,
parent_ids=["world"],
prompt_driver=local_driver,
context={"identify": identify},
)
scotty_task = character_task("scotty", "Scotty")
annie_task = character_task("annie", "Annie")We construct the world-generation process and dynamically assemble character-generation duties that depend upon the world’s output. We outline a reusable operate to create character duties conditioned on shared context. As we assemble these parts, we see how the workflow begins to take form by hierarchical dependencies. Take a look at the FULL CODES right here.
style_ruleset = Ruleset(
identify="StoryStyle",
guidelines=[
Rule("Write in a cinematic, emotionally engaging style."),
Rule("Avoid explicit gore or graphic violence."),
Rule("Keep the story between 400 and 700 words."),
],
)
story_task = PromptTask(
enter=(
"Write an entire brief story utilizing the next components.nn"
"World:n{{ parent_outputs['world'] }}nn"
"Character 1 (Scotty):n{{ parent_outputs['scotty'] }}nn"
"Character 2 (Annie):n{{ parent_outputs['annie'] }}nn"
"The story will need to have a transparent starting, center, and finish, with a significant character determination close to the climax."
),
id="story",
parent_ids=["world", "scotty", "annie"],
prompt_driver=local_driver,
rulesets=[style_ruleset],
)
story_workflow = Workflow(duties=[world_task, scotty_task, annie_task, story_task])
matter = "tidally locked ocean world with floating cities powered by storms"
story_workflow.run(matter)We introduce stylistic guidelines and create the ultimate storytelling process that merges worldbuilding and characters right into a coherent narrative. We then assemble all duties right into a workflow and run it with a selected matter. Via this, we witness how Griptape chains a number of prompts right into a structured inventive pipeline. Take a look at the FULL CODES right here.
world_text = world_task.output.worth
scotty_text = scotty_task.output.worth
annie_text = annie_task.output.worth
story_text = story_task.output.worth
present("Generated World", world_text)
present("Character: Scotty", scotty_text)
present("Character: Annie", annie_text)
present("Remaining Story", story_text)
def summarize_story(textual content):
paragraphs = [p for p in text.split("n") if p.strip()]
size = len(textual content.cut up())
structure_score = min(len(paragraphs), 10)
return {
"word_count": size,
"paragraphs": len(paragraphs),
"structure_score_0_to_10": structure_score,
}
metrics = summarize_story(story_text)
present("Story Metrics", metrics)We retrieve all generated outputs and show the world, characters, and remaining story. We additionally compute easy metrics to guage construction and size, giving us a fast analytical abstract. As we wrap up, we observe that the total workflow produces measurable, interpretable outcomes.
In conclusion, we reveal how simply we will orchestrate complicated reasoning steps, software interactions, and inventive technology utilizing native fashions inside the Griptape framework. We expertise how modular duties, rulesets, and workflows merge into a robust agentic system able to producing structured narrative outputs. By operating every part with out exterior APIs, we acquire full management, reproducibility, and adaptability, opening the door to extra superior experiments in native agent pipelines, automated writing programs, and multi-task orchestration.
Take a look at the FULL CODES right here. Be at liberty to take a look at our GitHub Web page for Tutorials, Codes and Notebooks. Additionally, be at liberty to observe us on Twitter and don’t neglect to affix our 100k+ ML SubReddit and Subscribe to our E-newsletter. Wait! are you on telegram? now you’ll be able to be part of us on telegram as properly.
Asif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is dedicated to harnessing the potential of Synthetic Intelligence for social good. His most up-to-date endeavor is the launch of an Synthetic Intelligence Media Platform, Marktechpost, which stands out for its in-depth protection of machine studying and deep studying information that’s each technically sound and simply comprehensible by a large viewers. The platform boasts of over 2 million month-to-month views, illustrating its reputation amongst audiences.
