Simple Greeting Agent
This example shows how to build a basic agent that greets the user at the start of a session and echoes back anything the user says, using audio responses for both. It demonstrates handling StartEvent and TextEvent, and responding with TextToSpeechEvent.
Features
- Demonstrates the basic structure of an agent function
- Handles a simple greeting via StartEvent
- Handles user text input via TextEvent
- Uses TextToSpeechEvent to respond to the user with audio
Code Example
from primfunctions.events import Event, StartEvent, TextEvent, TextToSpeechEvent
from primfunctions.context import Context
async def handler(event: Event, context: Context):
if isinstance(event, StartEvent):
yield TextToSpeechEvent(text="Hello! I will repeat what you say.", voice="nova")
if isinstance(event, TextEvent):
user_message = event.data.get("text", "").lower()
yield TextToSpeechEvent(text=f"You said: {user_message}", voice="nova")How It Works
Agent Initialization
The agent function is structured to handle different types of events. It listens for both session start and user input events.
Greeting the User
When a session begins (StartEvent), the agent responds with a simple spoken greeting using TextToSpeechEvent: 'Hello! I will repeat what you say.'
Echoing User Input
When the agent receives a TextEvent (user transcription and text input), it extracts the user's message and responds by repeating it back in the format: 'You said: ...', again using TextToSpeechEvent.
Audio Responses
All responses from the agent are delivered as audio using the specified voice ('nova'), demonstrating how to use TextToSpeechEvent for output.
This example highlights the essential event handling and audio response patterns for building conversational agents: greeting the user, processing input, and replying with speech.