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 Agent
2from kern.db.sqlite import SqliteDb
3from kern.models.openai import OpenAIResponses
4from kern.workflow import WorkflowAgent
5from kern.workflow.types import StepInput
6from kern.workflow.workflow import Workflow
7
8story_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)
12
13story_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)
17
18
19def add_references(step_input: StepInput):
20 """Add references to the story"""
21
22 previous_output = step_input.previous_step_content
23
24 if isinstance(previous_output, str):
25 return previous_output + "\n\nReferences: https://www.kern.ndx.rocks"
26
27
28# Create a WorkflowAgent that will decide when to run the workflow
29workflow_agent = WorkflowAgent(model=OpenAIResponses(id="gpt-5.2"), num_history_runs=4)
30
31# Create workflow with the WorkflowAgent
32workflow = 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)
39
40def main():
41 print("\n\n" + "=" * 80)
42 print("STREAMING MODE EXAMPLES")
43 print("=" * 80)
44
45 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 )
52
53 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=True
58 )
59
60 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 )
67
68 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=True
73 )
74
75# ============================================================================
76# STREAMING EXAMPLES
77# ============================================================================
78
79if __name__ == "__main__":
80 main()