RAG接口

This commit is contained in:
zhangtao 2025-01-04 16:26:56 +08:00
parent abd6259f98
commit 3e0211a248
12 changed files with 144 additions and 27 deletions

View File

@ -3,20 +3,23 @@ package cn.iocoder.yudao.module.llm.service.http;
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import cn.iocoder.yudao.module.llm.framework.backend.config.LLMBackendProperties;
import cn.iocoder.yudao.module.llm.service.http.vo.RagEmbedReqVo;
import cn.iocoder.yudao.module.llm.service.http.vo.RagQueryMultipleReqVo;
import cn.iocoder.yudao.module.llm.service.http.vo.RagQueryReqVo;
import cn.iocoder.yudao.module.llm.service.http.vo.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
* rag相关接口
*/
@Service
@Slf4j
public class RagHttpService {
@Resource
@ -25,38 +28,52 @@ public class RagHttpService {
/**
* RAG健康检查API
*/
public String ragHealth(Map<String, String> headers){
public RagHealthRespVO ragHealth(Map<String, String> headers){
String ragHealth = llmBackendProperties.getRagHealth();
String res = HttpUtils.get(ragHealth, headers);
return res;
log.info(" ragHealth:{}", res);
RagHealthRespVO regHealthRespVO = JSON.parseObject(res, RagHealthRespVO.class);
log.info(" ragHealthRespVO:{}", regHealthRespVO);
return regHealthRespVO;
}
/**
* 上传并向量化
*/
public String ragEmbed(Map<String, String> headers, RagEmbedReqVo ragEmbedReqVo){
public RagEmbedRespVO ragEmbed(Map<String, String> headers, RagEmbedReqVo ragEmbedReqVo){
String ragEmbed = llmBackendProperties.getRagEmbed();
Map<String, Object> map = BeanUtils.beanToMap(ragEmbedReqVo);
String res = HttpUtils.postForm(ragEmbed, headers,map);
return res;
log.info(" ragEmbedRespVO:{}", res);
RagEmbedRespVO ragEmbedRespVO = JSON.parseObject(res, RagEmbedRespVO.class);
log.info(" ragEmbedRespVO:{}", ragEmbedRespVO);
return ragEmbedRespVO;
}
/**
* 获取所有向量id
*/
public String ragIds(Map<String, String> headers){
public List<String> ragIds(Map<String, String> headers){
String ragIds = llmBackendProperties.getRagIds();
String res = HttpUtils.get(ragIds, headers);
return res;
log.info(" ragIds:{}", res);
List<String> ids = JSON.parseArray(res, String.class);
log.info(" ragIds:{}", ids);
return ids;
}
/**
* 根据id获取文档
*/
public String ragDocuments(Map<String, String> headers){
public RagDocumentsRespVO ragDocuments(Map<String, String> headers, RagDocumentsReqVO ragDocumentsReqVO){
String ragDocuments = llmBackendProperties.getRagDocuments();
String res = HttpUtils.get(ragDocuments, headers);
return res;
log.info(" ragDocuments:{}", res);
List<DocumentPageContent> pages = JSON.parseArray(res, DocumentPageContent.class);
RagDocumentsRespVO ragDocumentRespVO = new RagDocumentsRespVO();
ragDocumentRespVO.setPages(pages);
log.info(" ragDocumentRespVO:{}", ragDocumentRespVO);
return ragDocumentRespVO;
}
/**
@ -65,16 +82,28 @@ public class RagHttpService {
public String ragDocumentsDel(Map<String,String> headers){
String ragDocumentsDel = llmBackendProperties.getRagDocumentsDel();
String res = HttpUtils.del(ragDocumentsDel, headers);
log.info(" ragDocumentsDel:{}", res);
return res;
}
/**
* 根据file_id检索向量
*/
public String ragQuery(Map<String, String> headers, RagQueryReqVo ragQueryReqVo){
public RagQueryRespVO ragQuery(Map<String, String> headers, RagQueryReqVo ragQueryReqVo){
String ragQuery = llmBackendProperties.getRagQuery();
String res = HttpUtils.post(ragQuery, headers, JSON.toJSONString(ragQueryReqVo));
return res;
log.info(" ragQuery:{}", res);
JSONArray jsonArray = JSON.parseArray(res);
List<DocumentPageContent> pages = JSON.parseArray(jsonArray.getJSONObject(0).getString("page_content"), DocumentPageContent.class);
for (int i = 0; i < jsonArray.size(); i++) {
JSONArray temp = jsonArray.getJSONArray(i);
DocumentPageContent documentPageContent = JSON.toJavaObject(temp.getJSONObject(0), DocumentPageContent.class);
documentPageContent.setScore(String.valueOf(temp.getDouble(1)));
}
RagQueryRespVO ragQueryRespVO = new RagQueryRespVO();
ragQueryRespVO.setPages(pages);
log.info(" ragQueryRespVO:{}", ragQueryRespVO);
return ragQueryRespVO;
}
/**

View File

@ -0,0 +1,11 @@
package cn.iocoder.yudao.module.llm.service.http.vo;
import lombok.Data;
@Data
public class DocumentMetadata {
private String file_id;
private String user_id;
private String digest;
private String source;
}

View File

@ -0,0 +1,11 @@
package cn.iocoder.yudao.module.llm.service.http.vo;
import lombok.Data;
@Data
public class DocumentPageContent {
private String page_content;
private DocumentMetadata metadata;
private String score;
}

View File

@ -0,0 +1,11 @@
package cn.iocoder.yudao.module.llm.service.http.vo;
import lombok.Data;
import java.util.List;
@Data
public class RagDocumentsReqVO {
List<String> ids;
}

View File

@ -0,0 +1,11 @@
package cn.iocoder.yudao.module.llm.service.http.vo;
import lombok.Data;
import java.util.List;
@Data
public class RagDocumentsRespVO {
List<DocumentPageContent> pages;
}

View File

@ -1,6 +1,8 @@
package cn.iocoder.yudao.module.llm.service.http.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.io.File;
@ -9,10 +11,14 @@ import java.io.File;
* 上传并向量化 请求参数
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
public class RagEmbedReqVo {
private String file_id;
private File file;
private String entity_id;
}

View File

@ -0,0 +1,13 @@
package cn.iocoder.yudao.module.llm.service.http.vo;
import lombok.Data;
@Data
public class RagEmbedRespVO {
private boolean status;
private String message;
private String fileId;
private String filename;
private boolean knownType;
}

View File

@ -0,0 +1,13 @@
package cn.iocoder.yudao.module.llm.service.http.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RagHealthRespVO {
private String status;
}

View File

@ -13,4 +13,5 @@ public class RagQueryReqVo {
private Integer k;
private String entity_id;
}

View File

@ -0,0 +1,11 @@
package cn.iocoder.yudao.module.llm.service.http.vo;
import lombok.Data;
import java.util.List;
@Data
public class RagQueryRespVO {
private List<DocumentPageContent> pages;
}

View File

@ -205,19 +205,19 @@ llm:
backend:
# RAG服务
#RAG健康检查 GET
rag_health: http://localhost:8123/health
rag_health: http://rag.xhllm.xinnuojinzhi.com/health
#上传并向量化 POST
rag_embed: http://localhost:8123/embed
rag_embed: http://rag.xhllm.xinnuojinzhi.com/embed
#获取所有向量id GET
rag_ids: http://localhost:8123/ids
rag_ids: http://rag.xhllm.xinnuojinzhi.com/ids
#根据id获取文档 GET
rag_documents: http://localhost:8123/documents
rag_documents: http://rag.xhllm.xinnuojinzhi.com/documents
#根据id删除文档 DEL
rag_documents_del: http://localhost:8123/documents
rag_documents_del: http://rag.xhllm.xinnuojinzhi.com/documents
#根据file_id检索向量 POST
rag_query: http://localhost:8123/query
rag_query: http://rag.xhllm.xinnuojinzhi.com/query
#支持多个文件id查询向量 GET
rag_query_multiple: http://localhost:8123/query_multiple
rag_query_multiple: http://rag.xhllm.xinnuojinzhi.com/query_multiple
# LLM train and service api
# 训练集列表 GET

View File

@ -248,19 +248,19 @@ llm:
backend:
#### RAG服务
#RAG健康检查 GET
rag_health: http://localhost:8123/health
rag_health: http://rag.xhllm.xinnuojinzhi.com/health
#上传并向量化 POST
rag_embed: http://localhost:8123/embed
rag_embed: http://rag.xhllm.xinnuojinzhi.com/embed
#获取所有向量id GET
rag_ids: http://localhost:8123/ids
rag_ids: http://rag.xhllm.xinnuojinzhi.com/ids
#根据id获取文档 GET
rag_documents: http://localhost:8123/documents
rag_documents: http://rag.xhllm.xinnuojinzhi.com/documents
#根据id删除文档 DEL
rag_documents_del: http://localhost:8123/documents
rag_documents_del: http://rag.xhllm.xinnuojinzhi.com/documents
#根据file_id检索向量 POST
rag_query: http://localhost:8123/query
rag_query: http://rag.xhllm.xinnuojinzhi.com/query
#支持多个文件id查询向量 GET
rag_query_multiple: http://localhost:8123/query_multiple
rag_query_multiple: http://rag.xhllm.xinnuojinzhi.com/query_multiple
#### LLM train and service api
# 训练集列表 GET