AgentsDataMap

Helper Functions

View as MarkdownOpen in Claude
1from signalwire import create_simple_api_tool

Create a DataMap for a straightforward single-endpoint API call with minimal configuration.

Parameters

name
strRequired

Function name.

url
strRequired

API endpoint URL.

response_template
strRequired

Template string for formatting the response.

parameters
dict[str, dict]

Parameter definitions. Keys are parameter names, values are dicts with "type", "description", and optional "required" keys.

method
strDefaults to GET

HTTP method.

headers
dict[str, str]

HTTP headers.

body
dict[str, Any]

Request body for POST/PUT.

error_keys
list[str]

Keys indicating an error response.

Returns

DataMap — A fully configured DataMap ready for further chaining or conversion.


1from signalwire import create_expression_tool

Create a DataMap for pattern-matching responses without API calls.

Parameters

name
strRequired

Function name.

patterns
dict[str, tuple[str, FunctionResult]]Required

Dictionary mapping test values to (pattern, [FunctionResult][ref-functionresult]) tuples.

parameters
dict[str, dict]

Parameter definitions (same format as create_simple_api_tool).

Returns

DataMap — A fully configured DataMap.

Example

1from signalwire import AgentBase
2from signalwire import create_simple_api_tool, create_expression_tool
3from signalwire import FunctionResult
4
5# Simple API tool -- one line instead of a full DataMap chain
6weather = create_simple_api_tool(
7 name="get_weather",
8 url="https://api.weatherapi.com/v1/current.json?key=KEY&q=${enc:args.city}",
9 response_template="Weather in ${args.city}: ${response.current.condition.text}",
10 parameters={
11 "city": {"type": "string", "description": "City name", "required": True}
12 },
13 error_keys=["error"]
14)
15
16# Expression tool -- pattern matching without API calls
17greeting = create_expression_tool(
18 name="greet",
19 patterns={
20 "${args.language}": (r"spanish|espanol", FunctionResult("Hola!")),
21 },
22 parameters={
23 "language": {"type": "string", "description": "Language to greet in"}
24 }
25)
26
27agent = AgentBase(name="helper-demo")
28agent.set_prompt_text("You are a helpful assistant.")
29agent.register_swaig_function(weather.to_swaig_function())
30agent.register_swaig_function(greeting.to_swaig_function())
31
32if __name__ == "__main__":
33 agent.run()