-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDeclarativeAgent.java
More file actions
73 lines (61 loc) · 2.79 KB
/
Copy pathDeclarativeAgent.java
File metadata and controls
73 lines (61 loc) · 2.79 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
/**
* Declarative Agent Example.
*
* Demonstrates building an agent entirely from prompt sections defined
* up front, with simple tool definitions. This pattern keeps prompt
* structure visible and separated from implementation logic.
*/
import com.signalwire.sdk.agent.AgentBase;
import com.signalwire.sdk.swaig.FunctionResult;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
public class DeclarativeAgent {
public static void main(String[] args) throws Exception {
var agent = AgentBase.builder()
.name("declarative")
.route("/declarative")
.port(3000)
.build();
// Personality
agent.promptAddSection("Personality",
"You are a friendly and helpful AI assistant who responds in a casual, conversational tone.");
// Goal
agent.promptAddSection("Goal",
"Help users with their questions about time and weather.");
// Instructions
agent.promptAddSection("Instructions", "", List.of(
"Be concise and direct in your responses.",
"If you don't know something, say so clearly.",
"Use the get_time function when asked about the current time.",
"Use the get_weather function when asked about the weather."
));
// Post-prompt for summary
agent.setPostPrompt("Return a JSON summary: {\"topic\":\"...\",\"satisfied\":true/false}");
// Tools
agent.defineTool("get_time", "Get the current time",
Map.of("type", "object", "properties", Map.of()),
(toolArgs, raw) -> {
String time = LocalDateTime.now()
.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
return new FunctionResult("The current time is " + time);
});
agent.defineTool("get_weather", "Get the current weather for a location",
Map.of("type", "object",
"properties", Map.of(
"location", Map.of("type", "string",
"description", "City or location")
),
"required", List.of("location")),
(toolArgs, raw) -> {
String location = (String) toolArgs.getOrDefault("location", "Unknown");
return new FunctionResult("It's sunny and 72F in " + location + ".");
});
// Summary callback
agent.onSummary((summary, rawPayload) ->
System.out.println("Conversation summary: " + summary));
System.out.println("Starting declarative agent on port 3000...");
agent.run();
}
}