Basic Conversational Workflow
This example demonstrates a basic conversational workflow with a WorkflowAgent.
This example shows how to use the WorkflowAgent to create a conversational workflow.
1from kern.agent import Agent2from kern.db.sqlite import SqliteDb3from kern.models.openai import OpenAIResponses4from kern.workflow import WorkflowAgent5from kern.workflow.types import StepInput6from kern.workflow.workflow import Workflow78story_writer = Agent(9 model=OpenAIResponses(id="gpt-5.2"),10 instructions="You are tasked with writing a 100 word story based on a given topic",11)1213story_formatter = Agent(14 model=OpenAIResponses(id="gpt-5.2"),15 instructions="You are tasked with breaking down a short story in prelogues, body and epilogue",16)171819def add_references(step_input: StepInput):20 """Add references to the story"""2122 previous_output = step_input.previous_step_content2324 if isinstance(previous_output, str):25 return previous_output + "\n\nReferences: https://www.kern.ndx.rocks"262728# Create a WorkflowAgent that will decide when to run the workflow29workflow_agent = WorkflowAgent(model=OpenAIResponses(id="gpt-5.2"), num_history_runs=4)3031# Create workflow with the WorkflowAgent32workflow = Workflow(33 name="Story Generation Workflow",34 description="A workflow that generates stories, formats them, and adds references",35 agent=workflow_agent,36 steps=[story_writer, story_formatter, add_references],37 db=SqliteDb(db_file="tmp/workflow.db"),38)3940def main():41 print("\n\n" + "=" * 80)42 print("STREAMING MODE EXAMPLES")43 print("=" * 80)4445 print("\n" + "=" * 80)46 print("FIRST CALL (STREAMING): Tell me a story about a dog named Rocky")47 print("=" * 80)48 workflow.print_response(49 "Tell me a story about a dog named Rocky",50 stream=True,51 )5253 print("\n" + "=" * 80)54 print("SECOND CALL (STREAMING): What was Rocky's personality?")55 print("=" * 80)56 workflow.print_response(57 "What was Rocky's personality?", stream=True58 )5960 print("\n" + "=" * 80)61 print("THIRD CALL (STREAMING): Now tell me a story about a cat named Luna")62 print("=" * 80)63 workflow.print_response(64 "Now tell me a story about a cat named Luna",65 stream=True,66 )6768 print("\n" + "=" * 80)69 print("FOURTH CALL (STREAMING): Compare Rocky and Luna")70 print("=" * 80)71 workflow.print_response(72 "Compare Rocky and Luna", stream=True73 )7475# ============================================================================76# STREAMING EXAMPLES77# ============================================================================7879if __name__ == "__main__":80 main()