Skip to content

Commit

Permalink
Make DynamicFlowManager function registration dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
markbackman committed Dec 1, 2024
1 parent 2d0647c commit 17375e6
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 231 deletions.
101 changes: 41 additions & 60 deletions examples/dynamic/insurance_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,13 @@ def create_initial_node():
],
"functions": [
{
"type": "function",
"function": {
"name": "collect_age",
"description": "Record customer's age",
"parameters": {
"type": "object",
"properties": {"age": {"type": "integer"}},
"required": ["age"],
},
"name": "collect_age",
"handler": collect_age,
"description": "Record customer's age",
"parameters": {
"type": "object",
"properties": {"age": {"type": "integer"}},
"required": ["age"],
},
}
],
Expand All @@ -177,17 +175,15 @@ def create_marital_status_node():
],
"functions": [
{
"type": "function",
"function": {
"name": "collect_marital_status",
"description": "Record marital status",
"parameters": {
"type": "object",
"properties": {
"marital_status": {"type": "string", "enum": ["single", "married"]}
},
"required": ["marital_status"],
"name": "collect_marital_status",
"handler": collect_marital_status,
"description": "Record marital status",
"parameters": {
"type": "object",
"properties": {
"marital_status": {"type": "string", "enum": ["single", "married"]}
},
"required": ["marital_status"],
},
}
],
Expand All @@ -210,21 +206,19 @@ def create_quote_calculation_node(age: int, marital_status: str):
],
"functions": [
{
"type": "function",
"function": {
"name": "calculate_quote",
"description": "Calculate initial insurance quote",
"parameters": {
"type": "object",
"properties": {
"age": {"type": "integer"},
"marital_status": {
"type": "string",
"enum": ["single", "married"],
},
"name": "calculate_quote",
"handler": calculate_quote,
"description": "Calculate initial insurance quote",
"parameters": {
"type": "object",
"properties": {
"age": {"type": "integer"},
"marital_status": {
"type": "string",
"enum": ["single", "married"],
},
"required": ["age", "marital_status"],
},
"required": ["age", "marital_status"],
},
}
],
Expand All @@ -250,26 +244,25 @@ def create_quote_results_node(quote: Dict[str, Any]):
],
"functions": [
{
"type": "function",
"function": {
"name": "update_coverage",
"description": "Update coverage options",
"parameters": {
"type": "object",
"properties": {
"coverage_amount": {"type": "integer"},
"deductible": {"type": "integer"},
},
"required": ["coverage_amount", "deductible"],
"name": "update_coverage",
"handler": update_coverage,
"description": "Update coverage options",
"parameters": {
"type": "object",
"properties": {
"coverage_amount": {"type": "integer"},
"deductible": {"type": "integer"},
},
"required": ["coverage_amount", "deductible"],
},
},
{
"type": "function",
"function": {
"name": "end_quote",
"description": "Complete the quote process",
"parameters": {"type": "object", "properties": {}},
"name": "end_quote",
"handler": end_quote,
"description": "Complete the quote process",
"parameters": {
"type": "object",
"properties": {},
},
},
],
Expand Down Expand Up @@ -360,15 +353,6 @@ async def main():
tts = DeepgramTTSService(api_key=os.getenv("DEEPGRAM_API_KEY"), voice="aura-helios-en")
llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o")

# Create function handlers dictionary
function_handlers = {
"collect_age": collect_age,
"collect_marital_status": collect_marital_status,
"calculate_quote": calculate_quote,
"update_coverage": update_coverage,
"end_quote": end_quote,
}

# Create initial context
messages = [
{
Expand Down Expand Up @@ -405,9 +389,6 @@ async def main():
)
flow_manager.state = {} # Initialize state storage

# Register all functions
await flow_manager.register_functions(function_handlers)

@transport.event_handler("on_first_participant_joined")
async def on_first_participant_joined(transport, participant):
await transport.capture_participant_transcription(participant["id"])
Expand Down
3 changes: 1 addition & 2 deletions src/pipecat_flows/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Both StaticFlowManager and DynamicFlowManager build upon this base.
"""

from abc import ABC, abstractmethod
from abc import ABC
from typing import Any, Callable, List, Optional

from loguru import logger
Expand Down Expand Up @@ -133,7 +133,6 @@ async def _execute_actions(
if post_actions:
await self.action_manager.execute_actions(post_actions)

@abstractmethod
async def _validate_initialization(self) -> None:
"""Validate that the manager is properly initialized.
Expand Down
Loading

0 comments on commit 17375e6

Please sign in to comment.