ragflow跟代理聊天
This commit is contained in:
@@ -217,5 +217,104 @@ public class RagFlowApiUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//代理列表
|
||||||
|
public List<Map<String,Object>> getChatAgents(String agentId) throws Exception{
|
||||||
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||||
|
HttpGet get = new HttpGet(url+"/api/v1/agents?id="+agentId);
|
||||||
|
get.setHeader("Authorization", authorization);
|
||||||
|
get.setHeader("Content-Type", "application/json;chartset=utf-8");
|
||||||
|
CloseableHttpResponse response = httpClient.execute(get);
|
||||||
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
|
if (statusCode >= 400) {
|
||||||
|
throw new RuntimeException("API调用失败,状态码:" + statusCode);
|
||||||
|
}
|
||||||
|
HttpEntity responseEntity = response.getEntity();
|
||||||
|
String responseString = EntityUtils.toString(responseEntity, Consts.UTF_8);
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(responseString);
|
||||||
|
List<Map<String,Object>> list = new ArrayList();
|
||||||
|
if ("0".equals(jsonObject.get("code").toString())){
|
||||||
|
List l = jsonObject.getJSONArray("data");
|
||||||
|
for (Object o : l) {
|
||||||
|
Map<String,Object> map = new HashMap<>();
|
||||||
|
Map<String,Object> m = (Map<String,Object>)o;
|
||||||
|
map.put("id",m.get("id"));
|
||||||
|
map.put("name",m.get("title"));
|
||||||
|
list.add(map);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
//新建代理对话
|
||||||
|
public String createAgentChat(Map<String,Object> params) throws Exception{
|
||||||
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||||
|
String agentId = params.get("agentId").toString();
|
||||||
|
HttpPost post = new HttpPost(url+"/api/v1/agents/"+agentId+"/sessions?user_id="+ShiroUtils.getUId());
|
||||||
|
post.setHeader("Authorization", authorization);
|
||||||
|
post.setHeader("Content-Type", "application/json;chartset=utf-8");
|
||||||
|
CloseableHttpResponse response = httpClient.execute(post);
|
||||||
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
|
if (statusCode >= 400) {
|
||||||
|
throw new RuntimeException("API调用失败,状态码:" + statusCode);
|
||||||
|
}
|
||||||
|
HttpEntity responseEntity = response.getEntity();
|
||||||
|
String responseString = EntityUtils.toString(responseEntity, Consts.UTF_8);
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(responseString);
|
||||||
|
if ("0".equals(jsonObject.get("code").toString())){
|
||||||
|
return ((JSONObject)jsonObject.get("data")).get("id").toString();
|
||||||
|
}else {
|
||||||
|
return jsonObject.get("message").toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//与代理聊天流式
|
||||||
|
public void chatToAgentStream(String agentId,String sessionId,String question) {
|
||||||
|
try {
|
||||||
|
String userId = ShiroUtils.getUId()+"";
|
||||||
|
Map<String, Object> entity = new HashMap<>();
|
||||||
|
entity.put("question", question);
|
||||||
|
entity.put("stream", true);
|
||||||
|
entity.put("session_id", sessionId);
|
||||||
|
entity.put("user_id", userId);
|
||||||
|
AiChatContent content = new AiChatContent();
|
||||||
|
content.setUserId(Integer.parseInt(userId));
|
||||||
|
content.setChatAssistantId(agentId);
|
||||||
|
content.setChatId(sessionId);
|
||||||
|
content.setType(0);
|
||||||
|
content.setContent(question);
|
||||||
|
aiChatContentService.save(content);
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
WebClient.create().post()
|
||||||
|
.uri(url+"/api/v1/agents/"+agentId+"/completions")
|
||||||
|
.header("Authorization", authorization)
|
||||||
|
.header("Content-Type", "application/json;chartset=utf-8")
|
||||||
|
.bodyValue(JSONObject.toJSONString(entity))
|
||||||
|
.retrieve()
|
||||||
|
.bodyToFlux(String.class)
|
||||||
|
.doOnNext(data -> {
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(data);
|
||||||
|
if ("0".equals(jsonObject.get("code").toString())){
|
||||||
|
if (!"true".equals(jsonObject.get("data").toString())){
|
||||||
|
webSocket.sendMessage(data);
|
||||||
|
list.add(((JSONObject)jsonObject.get("data")).get("answer").toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.doFinally(data -> {
|
||||||
|
webSocket.sendMessage("{\"code\":0,\"data\":true}");
|
||||||
|
AiChatContent answer = new AiChatContent();
|
||||||
|
answer.setUserId(Integer.parseInt(userId));
|
||||||
|
answer.setChatAssistantId(agentId);
|
||||||
|
answer.setChatId(sessionId);
|
||||||
|
answer.setType(1);
|
||||||
|
answer.setContent(list.get(list.size()-1));
|
||||||
|
aiChatContentService.save(answer);
|
||||||
|
}).subscribe();
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -123,6 +123,28 @@ public class RagFlowApiController {
|
|||||||
return R.ok();
|
return R.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//代理列表
|
||||||
|
@RequestMapping("/getChatAgents")
|
||||||
|
public R getChatAgents() throws Exception{
|
||||||
|
List<Map<String,Object>> list = ragFlowApiUtil.getChatAgents("");
|
||||||
|
return R.ok().put("list",list);
|
||||||
|
}
|
||||||
|
|
||||||
|
//创建代理会话
|
||||||
|
@RequestMapping("/createAgentChat")
|
||||||
|
public R createAgentChat(@RequestBody Map<String,Object> params) throws Exception{
|
||||||
|
String agentId = ragFlowApiUtil.createAgentChat(params);
|
||||||
|
return R.ok().put("id",agentId);
|
||||||
|
}
|
||||||
|
|
||||||
|
//与代理聊天流式
|
||||||
|
@RequestMapping(value = "/chatToAgentStream")
|
||||||
|
@Transactional
|
||||||
|
public R chatToAgentStream(String agentId,String sessionId,String question){
|
||||||
|
ragFlowApiUtil.chatToAgentStream(agentId,sessionId,question);
|
||||||
|
return R.ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user