-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjoke_agent.py
More file actions
126 lines (106 loc) · 4.31 KB
/
Copy pathjoke_agent.py
File metadata and controls
126 lines (106 loc) · 4.31 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/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.
"""
"""
Joke Agent Example
This agent demonstrates using a raw data_map configuration
to integrate with the API Ninjas joke API.
Run with: API_NINJAS_KEY=your_api_key python examples/joke_agent.py
"""
import os
import sys
from signalwire import AgentBase
class JokeAgent(AgentBase):
"""Simple agent that can tell jokes using data_map"""
def __init__(self):
super().__init__(
name="Joke Agent",
route="/joke-agent"
)
# Get API key from environment variable
api_key = os.environ.get("API_NINJAS_KEY")
if not api_key:
print("Error: API_NINJAS_KEY environment variable is required")
print("Get your free API key from https://api.api-ninjas.com/")
print("Then run: API_NINJAS_KEY=your_api_key python examples/joke_agent.py")
sys.exit(1)
# Configure the agent's personality and behavior
self.prompt_add_section("Personality", body="You are a funny assistant who loves to tell jokes.")
self.prompt_add_section("Goal", body="Make people laugh with great jokes.")
self.prompt_add_section("Instructions", bullets=[
"Use the get_joke function to tell jokes when asked",
"You can tell either regular jokes or dad jokes",
"Be enthusiastic about sharing humor"
])
# Register the joke function with raw data_map configuration
self._add_joke_function(api_key)
def _add_joke_function(self, api_key):
"""Add the joke function using raw data_map configuration"""
joke_function = {
"function": "get_joke",
"description": "tell a joke",
"data_map": {
"webhooks": [
{
"url": "https://api.api-ninjas.com/v1/%{args.type}",
"headers": {
"X-Api-Key": api_key
},
"output": {
"response": "Tell the user: %{array[0].joke}",
"action": [
{
"SWML": {
"sections": {
"main": [
{
"set": {
"dad_joke": "%{array[0].joke}"
}
}
]
},
"version": "1.0.0"
}
}
]
},
"error_keys": "error",
"method": "GET"
}
],
"output": {
"response": "Tell the user that the joke service is not working right now and just make up a joke on your own"
}
},
"parameters": {
"properties": {
"type": {
"description": "must either be 'jokes' or 'dadjokes'",
"type": "string"
}
},
"type": "object"
}
}
# Register the function with the agent
self.register_swaig_function(joke_function)
def main():
"""Run the joke agent"""
print("Starting Joke Agent...")
print("\nThis agent can tell jokes using the API Ninjas service!")
print("Just ask for a joke and specify either 'jokes' or 'dadjokes' type.")
print("\nAvailable function:")
print(" get_joke - Tell a joke (jokes or dadjokes)")
print("Note: Works in any deployment mode (server/CGI/Lambda)")
agent = JokeAgent()
try:
agent.run()
except KeyboardInterrupt:
print("\nShutting down joke agent...")
if __name__ == "__main__":
main()