使用dify的api连接外部知识库,dify连接ragflow的知识库(附java代码)
dify的知识库一般般,但是ragflow的知识库很强大,今天教大家如何使用dify连接ragflow的知识库
一.ragflow的准备工作
1.在ragflow建立一个知识库,拿到知识库的id,红框圈出来的地方就是这个知识库的id,后面要用到
2.拿到ragflow的api的key
二.写代码,将ragflow的接口返回的内容按照dify的标准格式进行组装
这里我使用的是Java代码,本地直接新建一个空的springboot项目,然后将以下代码复制粘贴,修改下参数即可
实体类(无需修改)
public class RetrievalSetting { @NotNull private int top_k; @NotNull private float score_threshold; // Getters and Setters public int getTop_k() { return top_k; } public void setTop_k(int top_k) { this.top_k = top_k; } public float getScore_threshold() { return score_threshold; } public void setScore_threshold(float score_threshold) { this.score_threshold = score_threshold; } }
public class RetrievalRequest { @NotBlank private String knowledge_id; @NotBlank private String query; @NotNull private RetrievalSetting retrieval_setting; // Getters and Setters public String getKnowledge_id() { return knowledge_id; } public void setKnowledge_id(String knowledge_id) { this.knowledge_id = knowledge_id; } public String getQuery() { return query; } public void setQuery(String query) { this.query = query; } public RetrievalSetting getRetrieval_setting() { return retrieval_setting; } public void setRetrieval_setting(RetrievalSetting retrieval_setting) { this.retrieval_setting = retrieval_setting; } }
@RestController @RequestMapping("/") public class RetrievalController { private static final String ORIGINAL_API_URL = "http://你的ragflowapi的主机和端口/api/v1/retrieval"; @PostMapping("/retrieval") public ResponseEntity retrieveChunks( @RequestBody RetrievalRequest requestData, @RequestHeader("Authorization") String authorization) { // 验证 Authorization 头 if (!authorization.startsWith("Bearer ")) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null); } String apiKey = authorization.split(" ")[1]; // 构建请求体 Map payload = new HashMap(); payload.put("question", requestData.getQuery()); payload.put("dataset_ids", Collections.singletonList(requestData.getKnowledge_id())); payload.put("top_k", requestData.getRetrieval_setting().getTop_k()); payload.put("similarity_threshold", requestData.getRetrieval_setting().getScore_threshold()); // 构建请求头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.set("Authorization", "Bearer " + apiKey); HttpEntity entity = new HttpEntity(payload, headers); // 发送请求 RestTemplate restTemplate = new RestTemplate(); try { ResponseEntity response = restTemplate.exchange( ORIGINAL_API_URL, HttpMethod.POST, entity, Map.class ); if (response.getStatusCode() != HttpStatus.OK) { return ResponseEntity.status(response.getStatusCode()).body(null); } // 处理响应数据 Map originalData = response.getBody(); Map data = (Map) originalData.get("data"); List chunks = (List) data.get("chunks"); List records = new ArrayList(); if (chunks != null) { for (Map chunk : chunks) { Map record = new HashMap(); record.put("content", chunk.get("content")); record.put("score", chunk.get("similarity")); record.put("title", chunk.getOrDefault("document_keyword", "Unknown Document")); Map metadata = new HashMap(); metadata.put("document_id", chunk.get("document_id")); record.put("metadata", metadata); records.add(record); } } Map result = new HashMap(); result.put("records", records); return ResponseEntity.ok(result); } catch (HttpClientErrorException e) { return ResponseEntity.status(e.getStatusCode()).body(null); } } }
完事可以用api工具测试一下这个接口通不通
注意这里请求头要加一个Bearer 然后接上apikey,中间有个空格
三.dify连接ragflow的知识库
1.rag先连接
2.新建知识库
这里是我运行的结果,完美
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。