nvps增强检索工程
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB

  1. package com.bw.search.service.impl;
  2. import java.util.Map;
  3. import org.springframework.stereotype.Service;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.bw.search.cache.ConfigCache;
  6. import com.bw.search.common.Res;
  7. import com.bw.search.entity.Constants;
  8. import com.bw.search.entity.SearchResponse;
  9. import com.bw.search.service.RagSearchService;
  10. import com.bw.search.utils.DateUtil;
  11. import lombok.extern.slf4j.Slf4j;
  12. @Service
  13. @Slf4j
  14. public class RagSearchServiceImpl implements RagSearchService {
  15. @Override
  16. public Res<?> search(String dataJson) {
  17. log.info("向量检索参数:{}",dataJson);
  18. //转换对象
  19. JSONObject parseObject = JSONObject.parseObject(dataJson);
  20. String id = parseObject.getString(Constants.ID);
  21. Map<String, Object> knowResult = getKnowledge(id);
  22. if(knowResult == null) {
  23. log.error("向量检索失败!");
  24. return Res.fail("知识库获取失败!");
  25. }
  26. log.info("知识库结果已获取:{}",JSONObject.toJSONString(knowResult));
  27. //响应体数据
  28. SearchResponse searchResponse = new SearchResponse();
  29. searchResponse.setIds(JSONObject.parseArray((String)knowResult.get(Constants.IDS), String.class));
  30. return Res.ok(searchResponse);
  31. }
  32. /**
  33. * 获取知识库结果
  34. * 同步
  35. * @param chatId
  36. * @return
  37. */
  38. private Map<String, Object> getKnowledge(String id) {
  39. Map<String, Object> knowResult = null;
  40. int retryTime = 30;
  41. while(ConfigCache.isStart) {
  42. if (retryTime > 0) {
  43. if(ConfigCache.searchResult.containsKey(id)) {
  44. knowResult = (Map<String, Object>) ConfigCache.searchResult.get(id);
  45. break;
  46. }else {
  47. log.info("知识获取中请稍后...{}",retryTime);
  48. DateUtil.sleep(1000);
  49. }
  50. retryTime --;
  51. }else {
  52. break;
  53. }
  54. }
  55. //获取之后删除
  56. ConfigCache.searchResult.remove(id);
  57. return knowResult;
  58. }
  59. }