package com.ruoyi.web.llm.service;

import com.alibaba.fastjson2.JSONObject;
import dev.langchain4j.model.embedding.EmbeddingModel;
import io.milvus.client.MilvusClient;
import io.milvus.grpc.MutationResult;
import io.milvus.param.R;
import org.noear.solon.ai.chat.message.AssistantMessage;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.io.IOException;
import java.util.List;

public interface ILangChainMilvusService {

    /**
     * 导入知识库文件
     */
    public R<MutationResult> insertLangchainEmbeddingDocument(MilvusClient milvusClient, MultipartFile file, String collectionName, EmbeddingModel embeddingModel) throws IOException;

    /**
     * 从Milvus检索相关文档
     * @return
     */
    public List<JSONObject> retrieveFromMilvus(MilvusClient milvusClient, EmbeddingModel embeddingModel, String collectionName, String query, int topK);

    /**
     * 从Milvus检索相关文档及相关度
     * @return
     */
    public List<JSONObject> similarityFromMilvus(MilvusClient milvusClient, EmbeddingModel embeddingModel, String collectionName, String query, int topK);

    /**
     * 调用LLM生成回答
     * @return
     */
    public Flux<AssistantMessage> generateAnswer(String topicId, String question, String llmServiceUrl);

    /**
     * 调用LLM+RAG（知识库）生成回答
     * @return
     */
    public Flux<AssistantMessage> generateAnswerWithCollection(String topicId, String question, List<JSONObject> contexts, String llmServiceUrl);

    /**
     * 调用LLM+RAG（外部文件）生成回答
     * @return
     */
    public Flux<AssistantMessage> generateAnswerWithDocument(EmbeddingModel embeddingModel, String topicId, String chatId, String question, String llmServiceUrl) throws IOException;

    /**
     * 调用LLM+RAG（外部文件+知识库）生成回答
     * @return
     */
    public Flux<AssistantMessage> generateAnswerWithDocumentAndCollection(EmbeddingModel embeddingModel, String topicId, String question,  List<JSONObject> requests, String llmServiceUrl) throws IOException;

    /**
     * 获取二级标题下三级标题列表
     * @return
     */
    public List<String> extractSubTitles(String filename, String question) throws IOException;
}
