-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathAIAgent.java
More file actions
executable file
·121 lines (104 loc) · 3.77 KB
/
Copy pathAIAgent.java
File metadata and controls
executable file
·121 lines (104 loc) · 3.77 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
package io.rong.methods.agent;
import io.rong.RongCloud;
import io.rong.methods.BaseMethod;
import io.rong.models.CheckMethod;
import io.rong.models.agent.AgentModel;
import io.rong.models.agent.ChatModel;
import io.rong.models.response.*;
import io.rong.util.GsonUtil;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
import java.util.Map;
/**
* chatbot service
*/
public class AIAgent extends BaseMethod {
private static final String API_JSON_PATH = "agent";
public AIAgent(String appKey, String appSecret, RongCloud rongCloud) {
this.appKey = appKey;
this.appSecret = appSecret;
this.rongCloud = rongCloud;
initPath();
}
@Override
protected void initPath() {
super.path = API_JSON_PATH;
}
/**
* create agent
**/
public ResponseResult create(AgentModel info) throws Exception {
String method = CheckMethod.AGENT_CREATE;
ResponseResult result = checkFiled(info, method, ResponseResult.class);
if (result != null) {
return result;
}
return doRequest("/v3/agent/create.json", info.toString(), method, ResponseResult.class, CONTENT_TYPE_JSON);
}
/**
* get agent
**/
public GetAIAgentResult get(String agentId) throws Exception {
String method = CheckMethod.AGENT_GET;
if (StringUtils.isBlank(agentId)) {
return new GetAIAgentResult(20005, "The parameter 'agentId' is required");
}
Map<String, Object> params = new HashMap<>();
params.put("agentId", agentId);
return doRequest("/v3/agent/get.json", GsonUtil.toJson(params, Map.class), method, GetAIAgentResult.class, CONTENT_TYPE_JSON);
}
/**
* update agent
**/
public ResponseResult update(AgentModel info) throws Exception {
String method = CheckMethod.AGENT_UPDATE;
ResponseResult result = checkFiled(info, method, ResponseResult.class);
if (result != null) {
return result;
}
return doRequest("/v3/agent/update.json", info.toString(), method, ResponseResult.class, CONTENT_TYPE_JSON);
}
/**
* delete agent
**/
public ResponseResult delete(String agentId) throws Exception {
String method = CheckMethod.AGENT_DELETE;
if (StringUtils.isBlank(agentId)) {
return new ResponseResult(20005, "The parameter 'agentId' is required");
}
Map<String, Object> params = new HashMap<>();
params.put("agentId", agentId);
return doRequest("/v3/agent/delete.json", GsonUtil.toJson(params, Map.class), method, ResponseResult.class, CONTENT_TYPE_JSON);
}
/**
* query agent
**/
public PagingQueryAgentsResult query(Integer page, Integer size, String status, String type) throws Exception {
String method = CheckMethod.AGENT_LIST;
Map<String, Object> params = new HashMap<>();
if (page != null) {
params.put("page", page);
}
if (size != null) {
params.put("size", size);
}
if (status != null) {
params.put("status", status);
}
if (StringUtils.isBlank(type)) {
params.put("type", type);
}
return doRequest("/v3/agent/query.json", GsonUtil.toJson(params, Map.class), method, PagingQueryAgentsResult.class, CONTENT_TYPE_JSON);
}
/**
* query agent
**/
public ChatAgentResult chat(ChatModel model) throws Exception {
String method = CheckMethod.AGENT_CHAT;
ChatAgentResult result = checkFiled(model, method, ChatAgentResult.class);
if (result != null) {
return result;
}
return doRequest("/v3/chat/generate.json", model.toString(), method, ChatAgentResult.class, CONTENT_TYPE_JSON);
}
}