package io.github.qifan777.knowledge.ai.agent.documentSearch;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import io.github.qifan777.knowledge.ai.agent.AbstractAgent;
import io.github.qifan777.knowledge.ai.agent.Agent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;

import org.springframework.context.annotation.Description;

import java.util.function.Function;


/**
 * <p>
 *
 * </p>
 *
 * @author taohongrun
 * @since 2025/3/17
 */
@Agent
@Description("回答用户查询文档相关问题")
@Slf4j
@RequiredArgsConstructor
public class DocumentEsSearchAgent extends AbstractAgent implements Function<DocumentEsSearchAgent.Request, String> {

    private final ChatModel chatModel;

    @Override
    public String apply(DocumentEsSearchAgent.Request request) {
        log.info("用户想要查询的文档名称: {}", request.fileName);
        log.info("用户的角色类型: {}", request.roleName);
        return ChatClient.create(chatModel)
                .prompt()
                .system(s -> s.text("用户查询文档相关问题，用户角色权限:{role},文档名称:{fileName},问题：{query}")
                        .param("role", request.roleName)
                        .param("fileName", request.fileName)
                        .param("query", request.query()))
                .functions(getFunctions(SearchDocumentFunction.class))
                .user(userSpec -> userSpec
                        .param("roleName", request.roleName)
                        .param("fileName", request.fileName)// 传递参数
                        .text(request.query()))
                .call()
                .content();
    }

    public record Request(@JsonProperty(required = true)
                          @JsonPropertyDescription("用户想要查询的文档名称")
                          String fileName
            ,
                          @JsonProperty(required = true)
                          @JsonPropertyDescription("用户的角色类型,如老师，学生")
                          String roleName
            ,
                          @JsonProperty(required = true) @JsonPropertyDescription(value = "用户原始的提问") String query) {
    }
}
