rag http service

This commit is contained in:
ire 2024-12-29 18:56:53 +08:00
parent 0ad4e6cb60
commit 9d4cb2f019
5 changed files with 144 additions and 0 deletions

View File

@ -143,6 +143,15 @@ public class HttpUtils {
}
}
public static String postForm(String url, Map<String, String> headers, Map<String, Object> form) {
try (HttpResponse response = HttpRequest.post(url)
.addHeaders(headers)
.form(form)
.execute()) {
return response.body();
}
}
/**
* HTTP get 请求基于 {@link cn.hutool.http.HttpUtil} 实现
*
@ -160,4 +169,12 @@ public class HttpUtils {
}
}
public static String del(String url, Map<String, String> headers) {
try (HttpResponse response = HttpRequest.delete(url)
.addHeaders(headers)
.execute()) {
return response.body();
}
}
}

View File

@ -0,0 +1,86 @@
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.RagQueryReqVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Map;
@Service
public class RagHttpService {
@Resource
private LLMBackendProperties llmBackendProperties;
/**
* RAG健康检查API
*/
public String ragHealth(Map<String, String> headers){
String ragHealth = llmBackendProperties.getRagHealth();
String res = HttpUtils.get(ragHealth, headers);
return res;
}
/**
* 上传并向量化
*/
public String 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;
}
/**
* 获取所有向量id
*/
public String ragIds(Map<String, String> headers){
String ragIds = llmBackendProperties.getRagIds();
String res = HttpUtils.get(ragIds, headers);
return res;
}
/**
* 根据id获取文档
*/
public String ragDocuments(Map<String, String> headers){
String ragDocuments = llmBackendProperties.getRagDocuments();
String res = HttpUtils.get(ragDocuments, headers);
return res;
}
/**
* 根据id删除文档
*/
public String ragDocumentsDel(Map<String,String> headers){
String ragDocumentsDel = llmBackendProperties.getRagDocumentsDel();
String res = HttpUtils.del(ragDocumentsDel, headers);
return res;
}
/**
* 根据file_id检索向量
*/
public String ragQuery(Map<String, String> headers, RagQueryReqVo ragQueryReqVo){
String ragQuery = llmBackendProperties.getRagQuery();
String res = HttpUtils.post(ragQuery, headers, JSON.toJSONString(ragQueryReqVo));
return res;
}
/**
* 支持多个文件id查询向量
*/
public String ragQueryMultiple(Map<String, String> headers){
String ragQueryMultiple = llmBackendProperties.getRagQueryMultiple();
String res = HttpUtils.get(ragQueryMultiple, headers);
return res;
}
}

View File

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

View File

@ -0,0 +1,16 @@
package cn.iocoder.yudao.module.llm.service.http.vo;
import lombok.Data;
import lombok.ToString;
@Data
@ToString(callSuper = true)
public class RagQueryReqVo {
private String query;
private String file_id;
private Integer k;
}

View File

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