Repository navigation
Expand file tree
/
Copy pathSmartAgent.java
More file actions
64 lines (56 loc) · 2.91 KB
/
Copy pathSmartAgent.java
File metadata and controls
64 lines (56 loc) · 2.91 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
/*
* Run a custom AI smart-agent column over a list and read the per-lead results.
*
* ⚠️ Spends credits (priced per processed row). Estimates first and prints the cost.
*
* mvn -q exec:java -Dexec.mainClass="examples.SmartAgent" -Dexec.args="<list_id>"
*/
package examples;
import java.util.Set;
import ai.cleanlist.client.ApiException;
import ai.cleanlist.client.Cleanlist;
import ai.cleanlist.client.model.EstimateCostRequest;
import ai.cleanlist.client.model.EstimateCostResponse;
import ai.cleanlist.client.model.RunSmartAgentRequest;
import ai.cleanlist.client.model.RunSmartAgentResponse;
import ai.cleanlist.client.model.SmartAgentResultRow;
import ai.cleanlist.client.model.SmartAgentResultsResponse;
public class SmartAgent {
private static final Set<String> TERMINAL = Set.of("completed", "failed", "cancelled");
private static final int MAX_ROWS = 25;
public static void main(String[] args) throws ApiException, InterruptedException {
if (args.length < 1) {
System.err.println("Usage: SmartAgent <list_id>");
System.exit(1);
}
String listId = args[0];
Cleanlist cl = Cleanlist.fromEnv();
// Price the run for the exact request shape.
EstimateCostResponse quote = cl.workspace.creditsEstimate(new EstimateCostRequest()
.tool("run_smart_agent").listId(listId).agentType("custom_ai").rowCount(MAX_ROWS));
System.out.printf("Cost: %d credits (sufficient=%b)%n", quote.getEstimatedCost(), quote.getSufficient());
if (!Boolean.TRUE.equals(quote.getSufficient())) {
System.err.println("Not enough credits.");
System.exit(1);
}
RunSmartAgentResponse run = cl.smartAgents.runSmartAgent(new RunSmartAgentRequest()
.listId(listId)
.agentType(RunSmartAgentRequest.AgentTypeEnum.CUSTOM_AI)
.columnName("Outreach angle")
.prompt("In one sentence, suggest a cold-outreach angle for this lead.")
.leadScope(RunSmartAgentRequest.LeadScopeEnum.SUBSET)
.maxRows(MAX_ROWS)
.quoteId(quote.getQuoteId()));
System.out.printf("Started smart agent: %s (status=%s)%n", run.getSmartAgentTaskId(), run.getStatus());
// Poll the run's results endpoint until it finishes.
SmartAgentResultsResponse res = cl.smartAgents.getSmartAgentResults(run.getSmartAgentTaskId());
while (!TERMINAL.contains(res.getStatus())) {
System.out.printf(" %s: ok=%s failed=%s/%s%n", res.getStatus(), res.getSucceeded(), res.getFailed(), res.getTotal());
Thread.sleep(5000);
res = cl.smartAgents.getSmartAgentResults(run.getSmartAgentTaskId());
}
for (SmartAgentResultRow row : res.getResults().subList(0, Math.min(10, res.getResults().size()))) {
System.out.printf(" lead=%s -> %s%n", row.getLeadId(), row.getValue());
}
}
}