package com.iwe3.langchain4j.controller;

import com.iwe3.langchain4j.entity.LawPrompt;
import com.iwe3.langchain4j.service.LawExplainAssistant;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.input.Prompt;
import dev.langchain4j.model.input.PromptTemplate;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@Slf4j
public class ChatPromptController {

    @Resource
    private LawExplainAssistant lawExplainAssistant;
    /**
     * http://localhost:9008/lc4j/chatprompt/test2
     * TRIPS协议（与贸易有关的知识产权协议）：
     * 世界贸易组织（WTO）成员间的一个重要协议，
     * 它规定了最低标准的知识产权保护要求，并适用于所有WTO成员。
     * @return
     */
    @GetMapping(value = "/lc4j/chatprompt/test2")
    public String test2()
    {
        var prompt = new LawPrompt();
        prompt.setLegal("知识产权");
        prompt.setQuestion("TRIPS协议?");

        var chat = lawExplainAssistant.chat(prompt);
        System.out.println(chat);

        return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chat;
    }


    /**
     * 高阶API调用
     * http://localhost:9008/lc4j/chatprompt/test1
     * @return
     */
    @GetMapping(value = "/lc4j/chatprompt/test1")
    public String test1()
    {
        String chat = lawExplainAssistant.chat("什么是知识产权？",2000);
        System.out.println(chat);

        String chat2 = lawExplainAssistant.chat("什么是java？",2000);
        System.out.println(chat2);

        return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chat+"<br> \n\n chat2: "+chat2;
    }






    @Resource(name = "qwen")
    private ChatModel chatModel;

    /**
     * 低阶API调用
     * http://localhost:9008/lc4j/chatprompt/test
     * @return
     */
    @GetMapping(value = "/lc4j/chatprompt/test")
    public String test(){
        // 看看源码，默认 PromptTemplate 构造使用 it 属性作为默认占位符

        /*String role = "外科医生";
        String question = "牙疼";*/

        String role = "财务会计";
        String question = "人民币大写";

        //1 构造PromptTemplate模板
        PromptTemplate template = PromptTemplate.from("你是一个{{it}}助手,{{question}}怎么办");
        //2 由PromptTemplate生成Prompt
        Prompt prompt = template.apply(Map.of("it",role,"question",question));
        //3 Prompt提示词变成UserMessage
        UserMessage userMessage = prompt.toUserMessage();
        //4 调用大模型
        ChatResponse chatResponse = chatModel.chat(userMessage);

        //4.1 后台打印
        System.out.println(chatResponse.aiMessage().text());
        //4.2 前台返回
        return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chatResponse.aiMessage().text();
    }




}
