Skip to content

Commit

Permalink
Add result arg to transition_callback, add a dynamic restaurant reser…
Browse files Browse the repository at this point in the history
…vation demo
  • Loading branch information
markbackman committed Feb 4, 2025
1 parent 52a09ac commit ac23408
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 280 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ Example usage:
context = flow_manager.get_current_context()
```

- Added a new dynamic example called `restaurant_reservation.py`.

### Changed

- Transition callbacks now receive function results directly as a second argument:
`async def handle_transition(args: Dict, result: FlowResult, flow_manager: FlowManager)`.
This enables direct access to typed function results for making routing decisions.
For backwards compatibility, the two-argument signature
`(args: Dict, flow_manager: FlowManager)` is still supported.

- Updated dynamic examples to use the new result argument.

### Deprecated

- The `tts` parameter in `FlowManager.__init__()` is now deprecated and will
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,9 @@ In the `examples/static` directory, you'll find these examples:
- `movie_explorer_openai.py` - Movie information bot demonstrating real API integration with TMDB
- `movie_explorer_anthropic.py` - The same movie information demo adapted for Anthropic's format
- `movie_explorer_gemini.py` - The same movie explorer demo adapted for Google Gemini's format
- `patient_intake.py` - A medical intake system showing complex state management
- `restaurant_reservation.py` - A reservation system with availability checking
- `patient_intake_openai.py` - A medical intake system showing complex state management
- `patient_intake_anthropic.py` - The same medical intake demo adapted for Anthropic's format
- `patient_intake_gemini.py` - The same medical intake demo adapted for Gemini's format
- `travel_planner.py` - A vacation planning assistant with parallel paths

### Dynamic
Expand All @@ -415,6 +416,7 @@ In the `examples/dynamic` directory, you'll find these examples:
- `insurance_openai.py` - An insurance quote system using OpenAI's format
- `insurance_anthropic.py` - The same insurance system adapted for Anthropic's format
- `insurance_gemini.py` - The insurance system implemented with Google's format
- `restaurant_reservation.py` - A reservation system with availability checking

Each LLM provider (OpenAI, Anthropic, Google) has slightly different function calling formats, but Pipecat Flows handles these differences internally while maintaining a consistent API for developers.

Expand Down
20 changes: 11 additions & 9 deletions examples/dynamic/insurance_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,15 @@ async def end_quote() -> FlowResult:


# Transition callbacks and handlers
async def handle_age_collection(args: Dict, flow_manager: FlowManager):
flow_manager.state["age"] = args["age"]
async def handle_age_collection(args: Dict, result: AgeCollectionResult, flow_manager: FlowManager):
flow_manager.state["age"] = result["age"]
await flow_manager.set_node("marital_status", create_marital_status_node())


async def handle_marital_status_collection(args: Dict, flow_manager: FlowManager):
flow_manager.state["marital_status"] = args["marital_status"]
async def handle_marital_status_collection(
args: Dict, result: MaritalStatusResult, flow_manager: FlowManager
):
flow_manager.state["marital_status"] = result["marital_status"]
await flow_manager.set_node(
"quote_calculation",
create_quote_calculation_node(
Expand All @@ -164,13 +166,13 @@ async def handle_marital_status_collection(args: Dict, flow_manager: FlowManager
)


async def handle_quote_calculation(args: Dict, flow_manager: FlowManager):
quote = await calculate_quote(args)
flow_manager.state["quote"] = quote
await flow_manager.set_node("quote_results", create_quote_results_node(quote))
async def handle_quote_calculation(
args: Dict, result: QuoteCalculationResult, flow_manager: FlowManager
):
await flow_manager.set_node("quote_results", create_quote_results_node(result))


async def handle_end_quote(_: Dict, flow_manager: FlowManager):
async def handle_end_quote(_: Dict, result: FlowResult, flow_manager: FlowManager):
await flow_manager.set_node("end", create_end_node())


Expand Down
20 changes: 11 additions & 9 deletions examples/dynamic/insurance_gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,15 @@ async def end_quote() -> FlowResult:


# Transition callbacks and handlers
async def handle_age_collection(args: Dict, flow_manager: FlowManager):
flow_manager.state["age"] = args["age"]
async def handle_age_collection(args: Dict, result: AgeCollectionResult, flow_manager: FlowManager):
flow_manager.state["age"] = result["age"]
await flow_manager.set_node("marital_status", create_marital_status_node())


async def handle_marital_status_collection(args: Dict, flow_manager: FlowManager):
flow_manager.state["marital_status"] = args["marital_status"]
async def handle_marital_status_collection(
args: Dict, result: MaritalStatusResult, flow_manager: FlowManager
):
flow_manager.state["marital_status"] = result["marital_status"]
await flow_manager.set_node(
"quote_calculation",
create_quote_calculation_node(
Expand All @@ -164,13 +166,13 @@ async def handle_marital_status_collection(args: Dict, flow_manager: FlowManager
)


async def handle_quote_calculation(args: Dict, flow_manager: FlowManager):
quote = await calculate_quote(args)
flow_manager.state["quote"] = quote
await flow_manager.set_node("quote_results", create_quote_results_node(quote))
async def handle_quote_calculation(
args: Dict, result: QuoteCalculationResult, flow_manager: FlowManager
):
await flow_manager.set_node("quote_results", create_quote_results_node(result))


async def handle_end_quote(_: Dict, flow_manager: FlowManager):
async def handle_end_quote(_: Dict, result: FlowResult, flow_manager: FlowManager):
await flow_manager.set_node("end", create_end_node())


Expand Down
20 changes: 11 additions & 9 deletions examples/dynamic/insurance_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,15 @@ async def end_quote() -> FlowResult:


# Transition callbacks and handlers
async def handle_age_collection(args: Dict, flow_manager: FlowManager):
flow_manager.state["age"] = args["age"]
async def handle_age_collection(args: Dict, result: AgeCollectionResult, flow_manager: FlowManager):
flow_manager.state["age"] = result["age"]
await flow_manager.set_node("marital_status", create_marital_status_node())


async def handle_marital_status_collection(args: Dict, flow_manager: FlowManager):
flow_manager.state["marital_status"] = args["marital_status"]
async def handle_marital_status_collection(
args: Dict, result: MaritalStatusResult, flow_manager: FlowManager
):
flow_manager.state["marital_status"] = result["marital_status"]
await flow_manager.set_node(
"quote_calculation",
create_quote_calculation_node(
Expand All @@ -164,13 +166,13 @@ async def handle_marital_status_collection(args: Dict, flow_manager: FlowManager
)


async def handle_quote_calculation(args: Dict, flow_manager: FlowManager):
quote = await calculate_quote(args)
flow_manager.state["quote"] = quote
await flow_manager.set_node("quote_results", create_quote_results_node(quote))
async def handle_quote_calculation(
args: Dict, result: QuoteCalculationResult, flow_manager: FlowManager
):
await flow_manager.set_node("quote_results", create_quote_results_node(result))


async def handle_end_quote(_: Dict, flow_manager: FlowManager):
async def handle_end_quote(_: Dict, result: FlowResult, flow_manager: FlowManager):
await flow_manager.set_node("end", create_end_node())


Expand Down
Loading

0 comments on commit ac23408

Please sign in to comment.