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 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, RagQueryMultipleReqVo ragQueryReqVo){
        String ragQueryMultiple = llmBackendProperties.getRagQueryMultiple();
        String res = HttpUtils.getBody(ragQueryMultiple, headers, JSON.toJSONString(ragQueryReqVo));
        return res;
    }


}
