package com.ruoyi.web.llm.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.llm.service.ILangChainMilvusService;
import com.ruoyi.common.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;

/**
 * rag增强检索生成对话Controller
 * 
 * @author cmc
 * @date 2025-04-08
 */
@RestController
@RequestMapping("/llm/rag")
public class RagController extends BaseController
{
    @Autowired
    private ILangChainMilvusService langChainMilvusService;

    /**
     * 调用LLM+RAG（知识库）生成回答
     */
    @GetMapping("/answer")
    public SseEmitter answerWithCollection(String collectionName, String topicId, String question) {
        List<JSONObject> contexts = langChainMilvusService.retrieveFromMilvus(collectionName, question, 10);
        return langChainMilvusService.generateAnswerWithCollection(topicId, question, contexts);
    }

    /**
     * RAG上下文列表
     */
    @GetMapping("/context")
    public List<JSONObject> context(String question, String collectionName)
    {
        return langChainMilvusService.similarityFromMilvus(collectionName, question, 10);
    }

}
