package com.iwe3.langchain4j.controller;

import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.output.TokenUsage;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LowLevelController {

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

    // http://localhost:9003/lc4j/lowapi/api01?prompt=我是谁
    @GetMapping(value = "/lc4j/lowapi/api01")
    public String api01(@RequestParam(value = "prompt", defaultValue = "你是谁") String prompt)
    {
        return chatModel.chat(prompt);
    }

    // http://localhost:9003/lc4j/lowapi/api02?prompt=我是谁
    @GetMapping(value = "/lc4j/lowapi/api02")
    public String api02(@RequestParam(value = "prompt", defaultValue = "你是谁") String prompt)
    {
        ChatResponse chatResponse = chatModel.chat(UserMessage.from(prompt));

        String result = chatResponse.aiMessage().text();
        System.out.println("通过调用大模型返回结果："+result);

        // Token 用量计算的底层api
        TokenUsage tokenUsage = chatResponse.tokenUsage();
        System.out.println("本次调用消耗的token："+tokenUsage);
        result = result +"\t\n"+tokenUsage;

        return result;
    }
}
