package com.qingyun.service.agent;

import com.agentsflex.core.llm.Llm;
import com.agentsflex.core.llm.response.AiMessageResponse;
import com.agentsflex.core.message.SystemMessage;
import com.agentsflex.core.prompt.TextPrompt;
import com.agentsflex.llm.openai.OpenAILlm;
import com.agentsflex.llm.openai.OpenAILlmConfig;
import lombok.Data;

import java.util.Map;

@Data
public class OpenAILlmStrategy implements LlmStrategy {

    public final Llm llm;

    public OpenAILlmStrategy(OpenAILlmConfig config) {
        this.llm = new OpenAILlm(config);
    }

    @Override
    public AiMessageResponse chat(String promptTemplate, Map<String, String> variables) {
        String resolvedPrompt = resolveTemplate(promptTemplate, variables);
        logger.info("resolvedPrompt: {}" + resolvedPrompt);
        TextPrompt prompt = new TextPrompt();
        prompt.setSystemMessage(new SystemMessage(resolvedPrompt));
        prompt.setContent("[]");
        return llm.chat(prompt);
    }

    private String resolveTemplate(String template, Map<String, String> variables) {
        for (Map.Entry<String, String> entry : variables.entrySet()) {
            template = template.replace("${" + entry.getKey() + "}", entry.getValue());
        }
        return template;
    }
}
