Skip to content

Commit

Permalink
Quick fix to workflow streaming docs (#16512)
Browse files Browse the repository at this point in the history
  • Loading branch information
seldo authored Oct 11, 2024
1 parent f6cbb9a commit 58ad854
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions docs/docs/understanding/workflows/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ from llama_index.llms.openai import OpenAI
from llama_index.utils.workflow import draw_all_possible_flows
```

Let's set up some events for a simple three-step workflow:
Let's set up some events for a simple three-step workflow, plus an event to handle streaming our progress as we go:

```python
class FirstEvent(Event):
Expand All @@ -28,6 +28,10 @@ class FirstEvent(Event):
class SecondEvent(Event):
second_output: str
response: str


class ProgressEvent(Event):
msg: str
```

And define a workflow class that sends events:
Expand All @@ -36,7 +40,7 @@ And define a workflow class that sends events:
class MyWorkflow(Workflow):
@step
async def step_one(self, ctx: Context, ev: StartEvent) -> FirstEvent:
ctx.write_event_to_stream(Event(msg="Step one is happening"))
ctx.write_event_to_stream(ProgressEvent(msg="Step one is happening"))
return FirstEvent(first_output="First step complete.")

@step
Expand All @@ -47,15 +51,15 @@ class MyWorkflow(Workflow):
)
async for response in generator:
# Allow the workflow to stream this piece of response
ctx.write_event_to_stream(Event(msg=response.delta))
ctx.write_event_to_stream(ProgressEvent(msg=response.delta))
return SecondEvent(
second_output="Second step complete, full response attached",
response=str(response),
)

@step
async def step_three(self, ctx: Context, ev: SecondEvent) -> StopEvent:
ctx.write_event_to_stream(Event(msg="Step three is happening"))
ctx.write_event_to_stream(ProgressEvent(msg="Step three is happening"))
return StopEvent(result="Workflow complete.")
```

Expand All @@ -72,7 +76,8 @@ async def main():
handler = w.run(first_input="Start the workflow.")

async for ev in handler.stream_events():
print(ev.msg)
if isinstance(ev, ProgressEvent):
print(ev.msg)

final_result = await handler
print("Final result", final_result)
Expand Down

0 comments on commit 58ad854

Please sign in to comment.