-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcustom_path_agent.py
More file actions
106 lines (85 loc) · 3.34 KB
/
Copy pathcustom_path_agent.py
File metadata and controls
106 lines (85 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
"""
Copyright (c) 2025 SignalWire
This file is part of the SignalWire SDK.
Licensed under the MIT License.
See LICENSE file in the project root for full license information.
"""
"""
Custom Path Agent Example
This example demonstrates how to create an agent with a custom route/path.
Instead of the default "/" route, this agent will be available at "/chat".
This is useful for:
- Running multiple agents on the same server at different paths
- Creating semantic URLs that describe the agent's purpose
- Organizing agents by department or function
- Hosting agents behind reverse proxies with path-based routing
Usage:
python custom_path_agent.py
The agent will be available at:
http://localhost:3000/chat
Try these requests:
curl "http://localhost:3000/chat"
curl "http://localhost:3000/chat?user_name=Alice&topic=AI"
curl "http://localhost:3000/chat/debug"
"""
from signalwire import AgentBase
class ChatAgent(AgentBase):
def __init__(self, route="/chat"):
super().__init__(
name="Chat Assistant",
route=route, # Custom path for this agent
auto_answer=True,
record_call=True,
)
# Set up dynamic configuration based on query parameters
self.set_dynamic_config_callback(self.configure_chat_agent)
# Base prompt
self.prompt_add_section(
"Role",
"You are a friendly chat assistant ready to help with any questions or conversations.",
)
def configure_chat_agent(self, query_params, body_params, headers, agent):
"""Configure the agent based on request parameters"""
user_name = query_params.get("user_name", "friend")
topic = query_params.get("topic", "general conversation")
mood = query_params.get("mood", "friendly").lower()
# Personalize the greeting
agent.prompt_add_section(
"Personalization",
f"The user's name is {user_name}. They're interested in discussing {topic}.",
)
# Adjust voice and tone based on mood
voice_model = "inworld.Mark"
agent.add_language("English", "en-US", voice_model)
if mood == "professional":
agent.prompt_add_section(
"Communication Style",
"Maintain a professional, business-appropriate tone in all interactions.",
)
elif mood == "casual":
agent.prompt_add_section(
"Communication Style",
"Use a casual, relaxed conversational style. Feel free to use informal language.",
)
else: # friendly (default)
agent.prompt_add_section(
"Communication Style",
"Be warm, friendly, and approachable in your responses.",
)
# Set global data for the conversation
agent.set_global_data(
{
"user_name": user_name,
"topic": topic,
"mood": mood,
"session_type": "chat",
}
)
# Add speech recognition hints for common chat terms
agent.add_hints(["chat", "assistant", "help", "conversation", "question"])
if __name__ == "__main__":
# Create and start the agent
agent = ChatAgent("/chat")
print("Note: Works in any deployment mode (server/CGI/Lambda)")
agent.run()